SlsDetectorJungfrau.cpp 29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################

#include "SlsDetectorJungfrau.h"
#include "lima/MiscUtils.h"

26
27
#include "sls/Geometry.h"

28
29
30
31
32
33
34
35
#include <emmintrin.h>
#include <sched.h>

using namespace std;
using namespace lima;
using namespace lima::SlsDetector;
using namespace lima::SlsDetector::Defs;

36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#define applyDetGeom(j, f, raw)						\
	using namespace sls::Geom::Jungfrau;				\
	int ifaces;							\
	j->getNbUDPInterfaces(ifaces);					\
	auto any_nb_ifaces = AnyNbUDPIfacesFromNbUDPIfaces(ifaces);	\
	std::visit([&](auto nb) {					\
	    constexpr int nb_udp_ifaces = nb;				\
	    Defs::xy det_size = j->m_det->getDetectorSize();		\
	    auto any_geom = AnyDetGeomFromDetSize<nb_udp_ifaces>({det_size.x, \
								  det_size.y});\
	    std::visit([&](auto geom) {					\
	        if (raw)						\
			f(geom.raw_geom);				\
		else							\
			f(geom.asm_wg_geom);				\
	      }, any_geom);						\
	  }, any_nb_ifaces);

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

/*
 * Jungfrau::GainPed::Impl class
 */

template <class M>
void Jungfrau::GainPed::GainPed::Impl<M>::updateImageSize(Size size, bool raw)
{
	m_size = size;
	m_raw = raw;
	m_pixels = Point(m_size).getArea();
	std::vector<int> dims = {m_size.getWidth(), m_size.getHeight()};
	if (m_calib.gain_map[0].dimensions != dims)
		setDefaultCalib();
}

template <class M>
void Jungfrau::GainPed::Impl<M>::getDefaultCalib(Calib& calib)
{
	DEB_MEMBER_FUNCT();
	auto init = [&](auto& d) {
		initData(d, m_size, Data::DOUBLE);
		// gap pixels are set to 0
		if (!m_raw)
			memset(d.data(), 0, d.size());
	};
	double *p[3][2];
	for (int g = 0; g < 3; ++g) {
		init(calib.gain_map[g]);
		p[g][0] = (double *) calib.gain_map[g].data();
		init(calib.ped_map[g]);
		p[g][1] = (double *) calib.ped_map[g].data();
	}
	auto f = [&](auto det_geom) {
		using namespace sls::Geom;
90
91
92
93
94
95
96
97
		det_for_each_pixel(det_geom,
				   [&](auto &chip_view, auto &pixel) {
			int i = chip_view.calcMapPixelIndex(pixel);
			for (int g = 0; g < 3; ++g) {
				p[g][0][i] = M::DefaultCoeffs[g][0];
				p[g][1][i] = M::DefaultCoeffs[g][1];
			}
		   });
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
	};
	applyDetGeom(m_jungfrau, f, m_raw);
}

template <class M>
void Jungfrau::GainPed::Impl<M>::processFrame(Data& data, Data& proc)
{
	DEB_MEMBER_FUNCT();

	auto calib_coeff = [&](auto gain, auto type) {
		Data& d = (type == 0) ? m_calib.gain_map[gain] :
					m_calib.ped_map[gain];
		return (double *) d.data();
	};
	double *coeffs[3][2] = {{calib_coeff(0, 0), calib_coeff(0, 1)},
				{calib_coeff(1, 0), calib_coeff(1, 1)},
				{calib_coeff(2, 0), calib_coeff(2, 1)}};
	{
		unsigned short *src;
		src = reinterpret_cast<unsigned short *>(data.data());
		using P = typename M::Pixel;
		P *dst = reinterpret_cast<P *>(proc.data());
		for (int i = 0; i < m_pixels; ++i, ++src, ++dst) {
			int gain = *src >> 14;
			int adc = *src & 0x3fff;
			DEB_TRACE() << DEB_VAR3(i, gain, adc);
			if (gain == 3)
				gain = 2;
			else if (gain == 2) {
				*dst = std::numeric_limits<P>::max() - 0x10;
				continue;
			}
			*dst = coeffs[gain][0][i] * adc + coeffs[gain][1][i];
			DEB_TRACE() << DEB_VAR1(*dst);
		}
	}
}


/*
 * Jungfrau::GainPed class
 */

Jungfrau::GainPed::GainPed(Jungfrau *jungfrau)
	: m_jungfrau(jungfrau), m_impl(std::in_place_index_t<1>(), jungfrau)
{
	DEB_MEMBER_FUNCT();
	setMapType(Map16);
}

void Jungfrau::GainPed::setMapType(MapType map_type)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(map_type);
	MapType curr_type;
	getMapType(curr_type);
	if (map_type == curr_type)
		return;
	if (map_type == Map16)
		m_impl = Impl<Map16Data>(m_jungfrau);
	else
		m_impl = Impl<Map32Data>(m_jungfrau);
	if (m_size.isEmpty())
		return;
	updateImageSize(m_size, m_raw);
	std::visit([&](auto& impl) { impl.setDefaultCalib(); }, m_impl);
}

void Jungfrau::GainPed::getMapType(MapType& map_type)
{
	DEB_MEMBER_FUNCT();
	std::visit([&](auto& impl) {
		map_type = std::decay_t<decltype(impl)>::Map::Type;
	    }, m_impl);
	DEB_RETURN() << DEB_VAR1(map_type);
}

void Jungfrau::GainPed::setCalib(const Calib& calib)
{
	DEB_MEMBER_FUNCT();
	std::visit([&](auto& impl) { impl.m_calib = calib; }, m_impl);
}

void Jungfrau::GainPed::getCalib(Calib& calib)
{
	DEB_MEMBER_FUNCT();
	std::visit([&](auto& impl) { calib = impl.m_calib; }, m_impl);
}

void Jungfrau::GainPed::updateImageSize(Size size, bool raw)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR2(size, raw);
	m_size = size;
	m_raw = raw;
	std::visit([&](auto& impl) { impl.updateImageSize(size, raw); }, m_impl);
}

void Jungfrau::GainPed::getDefaultCalib(Calib& calib)
{
	DEB_MEMBER_FUNCT();
	std::visit([&](auto& impl) { impl.getDefaultCalib(calib); }, m_impl);
}

Data::TYPE Jungfrau::GainPed::getDataType()
{
	DEB_MEMBER_FUNCT();
	return std::visit([&](auto& impl) {
		return std::decay_t<decltype(impl)>::Map::DataType;
	    }, m_impl);
}


void Jungfrau::GainPed::processFrame(Data& data, Data& proc)
{
	DEB_MEMBER_FUNCT();
	std::visit([&](auto& impl) { impl.processFrame(data, proc); }, m_impl);
}

std::ostream& lima::SlsDetector::operator <<(std::ostream& os,
					     Jungfrau::GainPed::MapType map_type)
{
	const char *name = "Unknown";
	switch (map_type) {
	case Jungfrau::GainPed::Map16:	name = "Map16";	break;	
	case Jungfrau::GainPed::Map32:	name = "Map32";	break;	
	}
	return os << name;
}


229
230
231
232
/*
 * Jungfrau::Thread class
 */

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
Jungfrau::Thread::Thread(Jungfrau *jungfrau, int idx)
	: m_jungfrau(jungfrau), m_idx(idx)
{
	DEB_MEMBER_FUNCT();

	AutoMutex l = lock();
	m_state = Init;

	start();

	struct sched_param param;
	param.sched_priority = 50;
	int ret = pthread_setschedparam(m_thread, SCHED_RR, &param);
	if (ret != 0)
		DEB_ERROR() << "Could not set real-time priority!!";

	while (m_state == Init)
		wait();
}

Jungfrau::Thread::~Thread()
{
	DEB_DESTRUCTOR();

	AutoMutex l = lock();
	m_state = Quitting;
	broadcast();
	while (m_state != End)
		wait();
}

void Jungfrau::Thread::threadFunction()
{
	DEB_MEMBER_FUNCT();

	State& s = m_state;

	AutoMutex l = lock();
	s = Ready;
	broadcast();

	while (s != Quitting) {
		while ((s == Ready) || (s == Stopping)
		       || ((s == Running) && m_jungfrau->allFramesAcquired())) {
			if (s == Stopping) {
				s = Ready;
				broadcast();
			}
			wait();
		}
		if (s == Running)
284
			try {
285
286
				AutoMutexUnlock u(l);
				m_jungfrau->processPackets();
287
288
289
290
291
292
			} catch (Exception& e) {
				Camera *cam = m_jungfrau->getCamera();
				string name = ("Jungfrau::Thread::"
					       "threadFunction");
				cam->reportException(e, name);
			}
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
	}

	s = End;
	broadcast();
}

void Jungfrau::Thread::setCPUAffinity(CPUAffinity aff)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(aff);

        aff.applyToTask(getThreadID(), false);
}

void Jungfrau::Thread::prepareAcq()
{
	DEB_MEMBER_FUNCT();
310
311
312
313
314
315
316
317

	FrameType nb_frames;
	Camera *cam = m_jungfrau->getCamera();
	cam->getNbFrames(nb_frames);

	m_jungfrau->m_nb_frames = nb_frames;
	m_jungfrau->m_next_frame = 0;
	m_jungfrau->m_last_frame = -1;
318
319
}

320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

/*
 * Jungfrau::ImgProcTask class
 */

Jungfrau::ImgProcTask::ImgProcTask(Jungfrau *jungfrau)
	: m_jungfrau(jungfrau)
{
	DEB_CONSTRUCTOR();
}

void Jungfrau::ImgProcTask::process(Data& data)
{
	DEB_MEMBER_FUNCT();

	DEB_PARAM() << DEB_VAR2(data.frameNumber, data.data());
336

337
338
339
	ImgProcList& img_proc_list = m_jungfrau->m_img_proc_list;
	ImgProcList::iterator it, end = img_proc_list.end();
	for (it = img_proc_list.begin(); it != end; ++it)
340
		(*it)->processFrame(data);
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
}


/*
 * Jungfrau::ImgProcBase class
 */

Jungfrau::ImgProcBase::ImgProcBase(Jungfrau *jungfrau, std::string name)
	: m_jungfrau(jungfrau), m_name(name)
{
	DEB_CONSTRUCTOR();
	DEB_PARAM() << DEB_VAR1(m_name);
	m_nb_jungfrau_modules = m_jungfrau->getNbJungfrauModules();
	auto f = [&](auto det_geom) {
		m_det_mods = Point(det_geom.det_mods.x, det_geom.det_mods.y);
		DEB_TRACE() << DEB_VAR2(m_det_mods, m_nb_jungfrau_modules);
	};
	applyDetGeom(m_jungfrau, f, false);
}

Jungfrau::ImgProcBase::~ImgProcBase()
{
	DEB_DESTRUCTOR();
	if (m_jungfrau)
		m_jungfrau->removeImgProc(this);
}

void Jungfrau::ImgProcBase::updateImageSize(Size size, bool raw)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR3(m_name, size, raw);
372
	m_pixels = Point(size).getArea();
373
374
375
376
377
378
379
380
381
382
	m_frame_size = size;
	m_raw = raw;
}

void Jungfrau::ImgProcBase::prepareAcq()
{
	DEB_MEMBER_FUNCT();

	if (!m_jungfrau)
		THROW_HW_ERROR(InvalidValue) << "ImgProc already removed";
383
384
385
386
387
388
389

	clear();
}

void Jungfrau::ImgProcBase::clear()
{
	DEB_MEMBER_FUNCT();
390
391
392
}

/*
393
 * Jungfrau::GainADCMapImgProc class
394
395
396
 */

Jungfrau::GainADCMapImgProc::GainADCMapImgProc(Jungfrau *jungfrau)
397
	: ImgProcBase(jungfrau, "gain_adc_map"), m_helper(new Helper)
398
399
400
401
402
403
404
{
	DEB_CONSTRUCTOR();
}

void Jungfrau::GainADCMapImgProc::updateImageSize(Size size, bool raw)
{
	DEB_MEMBER_FUNCT();
405
	DEB_PARAM() << DEB_VAR3(m_name, size, raw);
406
407
	ImgProcBase::updateImageSize(size, raw);

408
409
	for (auto& d : m_buffer)
		d.updateSize(size);
410
411
}

412
void Jungfrau::GainADCMapImgProc::clear()
413
414
{
	DEB_MEMBER_FUNCT();
415
	ImgProcBase::clear();
416
417
418
419

	for (auto& d : m_buffer)
		d.clear();
	m_buffer.reset();
420
421
422
423
424
425
}

void Jungfrau::GainADCMapImgProc::processFrame(Data& data)
{
	DEB_MEMBER_FUNCT();
	long frame = data.frameNumber;
426
	DEB_PARAM() << DEB_VAR1(frame);
427

428
429
	Data raw = m_jungfrau->getRawData(data);

430
431
432
	DoubleBufferWriter<MapData> w(m_buffer);
	MapData& m = w.getBuffer();
	DEB_TRACE() << DEB_VAR1(&m);
433
434
	unsigned short *src;
	{
435
		src = (unsigned short *) raw.data();
436
		unsigned char *dst = (unsigned char *) m.gain_map.data();
437
438
		for (int i = 0; i < m_pixels; ++i, ++src, ++dst)
			*dst = *src >> 14;
439
440
441
		m.gain_map.frameNumber = frame;
	}
	{
442
		src = (unsigned short *) raw.data();
443
		unsigned short *dst = (unsigned short *) m.adc_map.data();
444
445
		for (int i = 0; i < m_pixels; ++i, ++src, ++dst)
			*dst = *src & 0x3fff;
446
447
		m.adc_map.frameNumber = frame;
	}
448
	w.setCounter(frame);
449
450
}

451
452
453
454
455
void Jungfrau::GainADCMapImgProc::readGainADCMaps(Data& gain_map, Data& adc_map,
						  FrameType& frame)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(frame);
456
457
458
459
	Helper::Reader *r = m_helper->addRead(m_buffer, frame);
	MapData &m = r->getBuffer();
	r->addData(m.gain_map, gain_map);
	r->addData(m.adc_map, adc_map);
460
461
	DEB_RETURN() << DEB_VAR3(gain_map, adc_map, frame);
}
462

463
464
465
466
467
468
/*
 * Jungfrau::GainPedImgProc class
 */

Jungfrau::GainPedImgProc::GainPedImgProc(Jungfrau *jungfrau)
	: ImgProcBase(jungfrau, "gain_ped"), m_gain_ped(jungfrau),
469
	  m_helper(new Helper)
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
{
	DEB_CONSTRUCTOR();
}

void Jungfrau::GainPedImgProc::updateImageSize(Size size, bool raw)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR3(m_name, size, raw);
	ImgProcBase::updateImageSize(size, raw);

	for (auto& d : m_buffer)
		d.updateSize(size);

	m_gain_ped.updateImageSize(size, raw);
}

486
void Jungfrau::GainPedImgProc::clear()
487
488
{
	DEB_MEMBER_FUNCT();
489
	ImgProcBase::clear();
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509

	Data::TYPE data_type = m_gain_ped.getDataType();
	DEB_TRACE() << DEB_VAR1(data_type);
	for (auto& d : m_buffer)
		if (d.proc_map.type != data_type) {
			DEB_TRACE() << "Reallocating proc_map";
			d.proc_map.releaseBuffer();
			d.proc_map.type = data_type;
			d.updateSize(m_frame_size);
		} else
			d.clear();
	m_buffer.reset();
}

void Jungfrau::GainPedImgProc::processFrame(Data& data)
{
	DEB_MEMBER_FUNCT();
	long frame = data.frameNumber;
	DEB_PARAM() << DEB_VAR1(frame);

510
511
	Data raw = m_jungfrau->getRawData(data);

512
513
514
	DoubleBufferWriter<MapData> w(m_buffer);
	MapData& m = w.getBuffer();
	DEB_TRACE() << DEB_VAR1(&m);
515
	m_gain_ped.processFrame(raw, m.proc_map);
516
517
518
519
520
521
522
523
	m.proc_map.frameNumber = frame;
	w.setCounter(frame);
}

void Jungfrau::GainPedImgProc::readProcMap(Data& proc_map, FrameType& frame)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(frame);
524
525
526
	Helper::Reader *r = m_helper->addRead(m_buffer, frame);
	MapData &m = r->getBuffer();
	r->addData(m.proc_map, proc_map);
527
528
529
	DEB_RETURN() << DEB_VAR2(proc_map, frame);
}

530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
 * Jungfrau::AveImgProc class
 */

Jungfrau::AveImgProc::AveImgProc(Jungfrau *jungfrau)
	: ImgProcBase(jungfrau, "ave"), m_helper(new Helper), m_nb_frames(0)
{
	DEB_CONSTRUCTOR();
	m_acc.type = Data::UINT32;
}

void Jungfrau::AveImgProc::updateImageSize(Size size, bool raw)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR3(m_name, size, raw);
	ImgProcBase::updateImageSize(size, raw);

	for (auto& d : m_buffer)
		d.updateSize(size);

	updateDataSize(m_acc, size);
}

void Jungfrau::AveImgProc::clear()
{
	DEB_MEMBER_FUNCT();
	ImgProcBase::clear();

	for (auto& d : m_buffer)
		d.clear();
	m_buffer.reset();

	clearData(m_acc);
	m_nb_frames = 0;
}

template <class M>
void Jungfrau::AveImgProc::processFrameFunct(Data& data)
{
	DEB_MEMBER_FUNCT();
	long frame = data.frameNumber;
	DEB_PARAM() << DEB_VAR1(frame);

	DoubleBufferWriter<MapData> w(m_buffer);
	MapData& m = w.getBuffer();
	DEB_TRACE() << DEB_VAR1(&m);
	++m_nb_frames;

	using S = typename M::Pixel;
	S *src = (S *) data.data();
	unsigned int *acc = (unsigned int *) m_acc.data();
	double *ave = (double *) m.ave_map.data();
	for (int i = 0; i < m_pixels; ++i, ++src, ++acc, ++ave) {
		*acc += *src;
		*ave = *acc / m_nb_frames;
	}
	m.ave_map.frameNumber = frame;
	m.nb_frames = m_nb_frames;
	w.setCounter(frame);
}

void Jungfrau::AveImgProc::processFrame(Data& data)
{
	DEB_MEMBER_FUNCT();
	GainPed& gain_ped = m_jungfrau->m_gain_ped_img_proc->m_gain_ped;
	GainPed::MapType map_type;
	gain_ped.getMapType(map_type);
	if (map_type == GainPed::Map16)
		processFrameFunct<GainPed::Map16Data>(data);
	else
		processFrameFunct<GainPed::Map32Data>(data);
}

void Jungfrau::AveImgProc::readAveMap(Data& ave_map, FrameType& nb_frames,
				      FrameType& frame)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(frame);
	Helper::Reader *r = m_helper->addRead(m_buffer, frame);
	MapData &m = r->getBuffer();
	r->addData(m.ave_map, ave_map);
	nb_frames = m.nb_frames;
	DEB_RETURN() << DEB_VAR3(ave_map, nb_frames, frame);
}

615
616
617
618
/*
 * Jungfrau::ModelReconstruction class
 */

619
Data Jungfrau::ModelReconstruction::processModel(Data& data)
620
621
622
623
624
625
626
627
628
629
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR4(m_jungfrau, data.frameNumber,
				_processingInPlaceFlag, data.data());
	if (!m_jungfrau)
		return data;

	Data ret = _processingInPlaceFlag ? data : data.copy();

	FrameType frame = ret.frameNumber;
630
631
632
	BufferMgr *buffer_mgr = m_jungfrau->getBuffer();
	BufferCtrlObj *buffer = buffer_mgr->getBufferCtrlObj(AcqBuffer);
	Data raw = buffer->getFrameData(frame);
633
634
635
636
637
638
639
640
	DEB_TRACE() << DEB_VAR1(raw);
	GainPed& gain_ped = m_jungfrau->m_gain_ped_img_proc->m_gain_ped;
	gain_ped.processFrame(raw, ret);
	DEB_TRACE() << DEB_VAR1(ret);
	return ret;
}


641
642
643
644
/*
 * Jungfrau detector class
 */

645
Jungfrau::Jungfrau(Camera *cam)
646
	: Model(cam, JungfrauDet), m_img_src(Raw)
647
648
649
650
651
652
653
{
	DEB_CONSTRUCTOR();

	int nb_det_modules = getNbDetModules();

	DEB_TRACE() << "Using Jungfrau detector, " << DEB_VAR1(nb_det_modules);

654
655
	for (int i = 0; i < nb_det_modules; ++i)
		m_recv_list.push_back(cam->getRecv(i));
656
657
658

	setNbProcessingThreads(1);

659
660
	m_gain_ped_img_proc = new GainPedImgProc(this);
	m_gain_adc_map_img_proc = new GainADCMapImgProc(this);
661
	m_ave_img_proc = new AveImgProc(this);
662

663
664
	m_reconstruction = new ModelReconstruction(this);

665
666
667
668
669
670
	updateCameraModel();
}

Jungfrau::~Jungfrau()
{
	DEB_DESTRUCTOR();
671
	getCamera()->waitAcqState(Idle);
672
673
	m_reconstruction->m_jungfrau = NULL;
	m_reconstruction->unref();
674
	removeAllImgProc();
675
676
}

677
678
679
680
681
682
683
684
685
686
687
688
689
690
void Jungfrau::setImgSrc(ImgSrc img_src)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR2(img_src, m_img_src);
	if (img_src == m_img_src)
		return;

	m_img_src = img_src;
	bool do_proc = (m_img_src == GainPedCorr);

	doSetImgProcConfig(m_img_proc_config, true);

	m_reconstruction->setActive(do_proc);

691
692
693
	BufferMgr *buffer = getCamera()->getBuffer();
	buffer->releaseBuffers();
	buffer->setMode(do_proc ? BufferMgr::Dual : BufferMgr::Single);
694
695
696
697
698
699
700
701
702
}

void Jungfrau::getImgSrc(ImgSrc& img_src)
{
	DEB_MEMBER_FUNCT();
	img_src = m_img_src;
	DEB_RETURN() << DEB_VAR1(img_src);
}

703
void Jungfrau::getFrameDim(FrameDim& frame_dim, bool raw)
704
705
706
707
708
709
710
711
712
713
714
715
716
717
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(raw);
	getAcqFrameDim(frame_dim, raw);
	if (m_img_src == GainPedCorr) {
		GainPed::MapType map_type;
		m_gain_ped_img_proc->m_gain_ped.getMapType(map_type);
		if (map_type == GainPed::Map32)
			frame_dim.setImageType(Bpp32);
	}
	DEB_RETURN() << DEB_VAR1(frame_dim);
}

void Jungfrau::getAcqFrameDim(FrameDim& frame_dim, bool raw)
718
719
720
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(raw);
721
722
723
724
725
	Size size;
	auto f = [&](auto det_geom) {
		size = Size(det_geom.size.x, det_geom.size.y);
		DEB_TRACE() << DEB_VAR1(size);
	};
726
	applyDetGeom(this, f, raw);
727
728
729
730
	frame_dim = FrameDim(size, Bpp16);
	DEB_RETURN() << DEB_VAR1(frame_dim);
}

731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
template <class DG>
Defs::xy Jungfrau::getModulePosition(const DG& det_geom, int idx)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(idx);
	Defs::xy mod_pos;
	auto det_mods = det_geom.det_mods;
	// module ID is column-wise
	mod_pos.x = idx / det_mods.y;
	mod_pos.y = idx % det_mods.y;
	DEB_RETURN() << DEB_VAR2(mod_pos.x, mod_pos.y);
	return mod_pos;
}

FrameDim Jungfrau::getModuleFrameDim(int idx, bool raw)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR2(idx, raw);
	Size size;
	auto f = [&](auto det_geom) {
		auto mod_geom = det_geom.getModGeom({0, 0});
		bool last_x = true;  // only raw mode supported
		bool last_y = (idx == getNbJungfrauModules() - 1);
		size = Size(!last_x ? det_geom.mod_step.x : mod_geom.size.x,
			    !last_y ? det_geom.mod_step.y : mod_geom.size.y);
		DEB_TRACE() << DEB_VAR1(size);
	};
758
	applyDetGeom(this, f, raw);
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
	FrameDim frame_dim(size, Bpp16);
	DEB_RETURN() << DEB_VAR1(frame_dim);
	return frame_dim;
}

int Jungfrau::getModuleDataOffset(int idx, bool raw)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR2(idx, raw);
	int data_offset;
	auto f = [&](auto det_geom) {
		auto mod_pos = getModulePosition(det_geom, idx);
		auto mod_view = det_geom.getModView({mod_pos.x,
					mod_pos.y});
		auto origin = mod_view.calcViewOrigin();
		int pixel_offset = mod_view.calcMapPixelIndex(origin);
		data_offset = pixel_offset * sls::Geom::Pixel16::depth();
		DEB_TRACE() << DEB_VAR1(data_offset);
	};
778
	applyDetGeom(this, f, raw);
779
780
781
782
	DEB_RETURN() << DEB_VAR1(data_offset);
	return data_offset;
}

783
784
785
void Jungfrau::getDetMap(Data& det_map)
{
	DEB_MEMBER_FUNCT();
786
	bool raw = getRawMode();
787
788
	FrameDim frame_dim;
	getFrameDim(frame_dim, raw);
789
790
791
792
	initData(det_map, frame_dim.getSize(), Data::UINT32);
	// gap pixels are set to -1
	if (!raw)
		memset(det_map.data(), 0xff, det_map.size());
793
794
795
796
797

	auto f = [&](auto det_geom) {
		using namespace sls::Geom;
		unsigned int *p = (unsigned int *) det_map.data();
		int chip_idx = 0;
798
799
800
801
802
803
804
805
806
		det_for_each_chip(det_geom,
				  [&](auto &chip, auto &chip_view) {
		    view_for_each_pixel(chip_view,
					[&](auto &chip_view, auto &pixel) {
			    int i = chip_view.calcMapPixelIndex(pixel);
			    p[i] = chip_idx;
			});
		    ++chip_idx;
		  });
807
808
809
810
	};
	applyDetGeom(this, f, raw);
}

811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
string Jungfrau::getName()
{
	DEB_MEMBER_FUNCT();
	ostringstream os;
	os << "PSI/Jungfrau-";
	int nb_modules = getNbDetModules();
	if (nb_modules == 1) {
		os << "500k";
	} else if (nb_modules % 2 == 0) {
		os << (nb_modules / 2) << "M";
	} else {
		os << nb_modules << "-Modules";
	}
	string name = os.str();
	DEB_RETURN() << DEB_VAR1(name);
	return name;
}

void Jungfrau::getPixelSize(double& x_size, double& y_size)
{
	DEB_MEMBER_FUNCT();
	x_size = y_size = 75e-6;
	DEB_RETURN() << DEB_VAR2(x_size, y_size);
}

void Jungfrau::getDACInfo(NameList& name_list, IntList& idx_list,
		       IntList& milli_volt_list)
{
	DEB_MEMBER_FUNCT();

#define JUNGFRAU_DAC(x)			{x, 0}
842
#define JUNGFRAU_DAC_MV(x)		{x, 1}
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910

	static struct DACData {
		DACIndex idx;
		int milli_volt;
	} JungfrauDACList[] = {
		JUNGFRAU_DAC(Threshold),
	};
	const unsigned int size = C_LIST_SIZE(JungfrauDACList);

	name_list.resize(size);
	idx_list.resize(size);
	milli_volt_list.resize(size);
	struct DACData *data = JungfrauDACList;
	for (unsigned int i = 0; i < size; ++i, ++data) {
		ostringstream os;
		os << data->idx;
		name_list[i] = os.str();
		idx_list[i] = int(data->idx);
		milli_volt_list[i] = data->milli_volt;
		DEB_RETURN() << DEB_VAR2(name_list[i], idx_list[i]);
	}
}

void Jungfrau::getADCInfo(NameList& name_list, IntList& idx_list,
		       FloatList& factor_list, FloatList& min_val_list)
{
	DEB_MEMBER_FUNCT();

#define JUNGFRAU_TEMP_FACTOR		(1 / 1000.0)

#define JUNGFRAU_TEMP(x)			{x, JUNGFRAU_TEMP_FACTOR, 0}

	static struct ADCData {
		ADCIndex idx;
		double factor, min_val;
	} JungfrauADCList[] = {
		JUNGFRAU_TEMP(TempFPGA),
		JUNGFRAU_TEMP(TempFPGAExt),
		JUNGFRAU_TEMP(Temp10GE),
		JUNGFRAU_TEMP(TempDCDC),
		JUNGFRAU_TEMP(TempSODL),
		JUNGFRAU_TEMP(TempSODR),
		JUNGFRAU_TEMP(TempFPGAFL),
		JUNGFRAU_TEMP(TempFPGAFR),
	};
	const unsigned int size = C_LIST_SIZE(JungfrauADCList);

	name_list.resize(size);
	idx_list.resize(size);
	factor_list.resize(size);
	min_val_list.resize(size);
	struct ADCData *data = JungfrauADCList;
	for (unsigned int i = 0; i < size; ++i, ++data) {
		ostringstream os;
		os << data->idx;
		name_list[i] = os.str();
		idx_list[i] = int(data->idx);
		factor_list[i] = data->factor;
		min_val_list[i] = data->min_val;
		DEB_RETURN() << DEB_VAR4(name_list[i], idx_list[i], 
					 factor_list[i], min_val_list[i]);
	}
}

void Jungfrau::getTimeRanges(TimeRanges& time_ranges)
{
	DEB_MEMBER_FUNCT();

911
912
913
914
915
916
	int nb_udp_ifaces;
	getNbUDPInterfaces(nb_udp_ifaces);

	double min_exp = 0.1;
	double min_period = 1000 / nb_udp_ifaces;
	double min_lat = 0.1;
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942

	time_ranges.min_exp_time = min_exp * 1e-6;
	time_ranges.max_exp_time = 1e3;
	time_ranges.min_lat_time = min_lat * 1e-6;
	time_ranges.max_lat_time = 1e3;
	time_ranges.min_frame_period = min_period * 1e-6;
	time_ranges.max_frame_period = 1e3;
}

int Jungfrau::getNbFrameMapItems()
{
	DEB_MEMBER_FUNCT();
	int nb_items = 1;
	DEB_RETURN() << DEB_VAR1(nb_items);
	return nb_items;
}

void Jungfrau::updateFrameMapItems(FrameMap *map)
{
	DEB_MEMBER_FUNCT();
}

void Jungfrau::updateImageSize()
{
	DEB_MEMBER_FUNCT();

943
	bool raw = getRawMode();
944
	FrameDim frame_dim;
945
	getAcqFrameDim(frame_dim, raw);
946
947
	Size size = frame_dim.getSize();
	DEB_TRACE() << DEB_VAR2(size, raw);
948

949
 	m_gain_ped_img_proc->updateImageSize(size, raw);
950
	m_gain_adc_map_img_proc->updateImageSize(size, raw);
951
	m_ave_img_proc->updateImageSize(size, raw);
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}

bool Jungfrau::checkSettings(Settings settings)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(settings);
	bool ok;
	switch (settings) {
	case Defs::Standard:
		ok = true;
		break;
	default:
		ok = false;
	}

	DEB_RETURN() << DEB_VAR1(ok);
	return ok;
}

void Jungfrau::setHighVoltage(int hvolt)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(hvolt);
	DEB_ALWAYS() << "Setting high voltage (" << DEB_VAR1(hvolt) << ") ...";
	EXC_CHECK(m_det->setHighVoltage(hvolt));
}

void Jungfrau::getHighVoltage(int& hvolt)
{
	DEB_MEMBER_FUNCT();
	Positions pos = Idx2Pos(0);
	EXC_CHECK(hvolt = m_det->getHighVoltage(pos).front());
	DEB_RETURN() << DEB_VAR1(hvolt);
}

void Jungfrau::setThresholdEnergy(int thres)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(thres);
	EXC_CHECK(m_det->setThresholdEnergy(thres));
}

void Jungfrau::getThresholdEnergy(int& thres)
{
	DEB_MEMBER_FUNCT();
	EXC_CHECK(thres = m_det->getThresholdEnergy().squash(-1));
	DEB_RETURN() << DEB_VAR1(thres);
}