SlsDetectorJungfrau.cpp 26.6 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
90
91
92
93
94
95
96
97
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
229
230
231
232
233
234
235

/*
 * 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;
		det_for_each_mod(det_geom, mod, mod_geom,
		    mod_for_each_recv(mod_geom, recv, recv_geom,
		        recv_for_each_iface(recv_geom, iface, iface_geom,
		            iface_for_each_chip(iface_geom, chip, chip_view,
			        view_for_each_pixel(chip_view, 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];
				    }
				  );
			      );
		          );
		      );
		  );
	};
	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;
}


236
237
238
239
/*
 * Jungfrau::Recv class
 */

240
241
242
243
244
245
246
247
248
249
250
251
252
Jungfrau::Recv::Recv(Jungfrau *jungfrau, int idx)
	: m_jungfrau(jungfrau), m_idx(idx)
{
	DEB_CONSTRUCTOR();
	DEB_PARAM() << DEB_VAR1(m_idx);

	Camera *cam = m_jungfrau->getCamera(); 
	m_recv = cam->getRecv(m_idx);
}

void Jungfrau::Recv::prepareAcq()
{
	DEB_MEMBER_FUNCT();
253
	m_raw = m_jungfrau->getRawMode();
254
255
256
257
258
259
260
261
262
263
264
	m_frame_dim = m_jungfrau->getModuleFrameDim(m_idx, m_raw);
	m_data_offset = m_jungfrau->getModuleDataOffset(m_idx, m_raw);
	DEB_TRACE() << DEB_VAR3(m_idx, m_frame_dim, m_data_offset);
}

bool Jungfrau::Recv::processOneFrame(FrameType frame, char *bptr)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR2(m_idx, frame);
	RecvImageData data;
	data.frame = frame;
265
	data.buffer = bptr;
266
267
268
269
270
271
272
273
274
275
276
	return m_recv->getImage(data);
}

void Jungfrau::Recv::processBadFrame(FrameType frame, char *bptr)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR2(m_idx, frame);
	if (!m_raw)
		THROW_HW_ERROR(NotSupported) << "Bad Frames in assembled mode";
	memset(bptr + m_data_offset, 0xff, m_frame_dim.getMemSize());
}
277

278
279
280
281
282

/*
 * Jungfrau::Thread class
 */

283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
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)
			m_jungfrau->processOneFrame(l);
	}

	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();
}

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419

/*
 * Jungfrau::ImgProcTask class
 */

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

void Jungfrau::ImgProcTask::setConfig(string config)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(config);
	m_jungfrau->setImgProcConfig(config);
}

void Jungfrau::ImgProcTask::getConfig(string &config)
{
	DEB_MEMBER_FUNCT();
	m_jungfrau->getImgProcConfig(config);
	DEB_RETURN() << DEB_VAR1(config);
}

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

	DEB_PARAM() << DEB_VAR2(data.frameNumber, data.data());
	
	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)
		(*it)->processFrame(data);
}


/*
 * 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);
420
	m_pixels = Point(size).getArea();
421
422
423
424
425
426
427
428
429
430
431
432
433
	m_frame_size = size;
	m_raw = raw;
}

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

	if (!m_jungfrau)
		THROW_HW_ERROR(InvalidValue) << "ImgProc already removed";
}

/*
434
 * Jungfrau::GainADCMapImgProc class
435
436
437
 */

Jungfrau::GainADCMapImgProc::GainADCMapImgProc(Jungfrau *jungfrau)
438
	: ImgProcBase(jungfrau, "gain_adc_map"), m_reader(new Reader)
439
440
441
442
443
444
445
{
	DEB_CONSTRUCTOR();
}

void Jungfrau::GainADCMapImgProc::updateImageSize(Size size, bool raw)
{
	DEB_MEMBER_FUNCT();
446
	DEB_PARAM() << DEB_VAR3(m_name, size, raw);
447
448
	ImgProcBase::updateImageSize(size, raw);

449
450
	for (auto& d : m_buffer)
		d.updateSize(size);
451
452
453
454
455
456
}

void Jungfrau::GainADCMapImgProc::prepareAcq()
{
	DEB_MEMBER_FUNCT();
	ImgProcBase::prepareAcq();
457
458
459
460

	for (auto& d : m_buffer)
		d.clear();
	m_buffer.reset();
461
462
463
464
465
466
}

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

469
470
471
	DoubleBufferWriter<MapData> w(m_buffer);
	MapData& m = w.getBuffer();
	DEB_TRACE() << DEB_VAR1(&m);
472
473
474
475
	unsigned short *src;
	{
		src = (unsigned short *) data.data();
		unsigned char *dst = (unsigned char *) m.gain_map.data();
476
477
		for (int i = 0; i < m_pixels; ++i, ++src, ++dst)
			*dst = *src >> 14;
478
479
480
481
482
		m.gain_map.frameNumber = frame;
	}
	{
		src = (unsigned short *) data.data();
		unsigned short *dst = (unsigned short *) m.adc_map.data();
483
484
		for (int i = 0; i < m_pixels; ++i, ++src, ++dst)
			*dst = *src & 0x3fff;
485
486
		m.adc_map.frameNumber = frame;
	}
487
	w.setCounter(frame);
488
489
}

490
491
492
493
494
void Jungfrau::GainADCMapImgProc::readGainADCMaps(Data& gain_map, Data& adc_map,
						  FrameType& frame)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(frame);
495
496
497
	MapData &m = m_reader->addRead(m_buffer, frame);
	m_reader->addData(m.gain_map, gain_map);
	m_reader->addData(m.adc_map, adc_map);
498
499
	DEB_RETURN() << DEB_VAR3(gain_map, adc_map, frame);
}
500

501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
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
/*
 * Jungfrau::GainPedImgProc class
 */

Jungfrau::GainPedImgProc::GainPedImgProc(Jungfrau *jungfrau)
	: ImgProcBase(jungfrau, "gain_ped"), m_gain_ped(jungfrau),
	  m_reader(new Reader)
{
	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);
}

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

	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);

	DoubleBufferWriter<MapData> w(m_buffer);
	MapData& m = w.getBuffer();
	DEB_TRACE() << DEB_VAR1(&m);
	m_gain_ped.processFrame(data, m.proc_map);
	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);
	MapData &m = m_reader->addRead(m_buffer, frame);
	m_reader->addData(m.proc_map, proc_map);
	DEB_RETURN() << DEB_VAR2(proc_map, frame);
}

565
566
567
568
/*
 * Jungfrau detector class
 */

569
570
571
572
573
574
575
576
577
Jungfrau::Jungfrau(Camera *cam)
	: Model(cam, JungfrauDet)
{
	DEB_CONSTRUCTOR();

	int nb_det_modules = getNbDetModules();

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

578
579
580
581
	for (int i = 0; i < nb_det_modules; ++i) {
		Recv *r = new Recv(this, i);
		m_recv_list.push_back(r);
	}
582
583
584

	setNbProcessingThreads(1);

585
586
587
	m_gain_ped_img_proc = new GainPedImgProc(this);
	m_gain_adc_map_img_proc = new GainADCMapImgProc(this);

588
589
590
591
592
593
	updateCameraModel();
}

Jungfrau::~Jungfrau()
{
	DEB_DESTRUCTOR();
594
	getCamera()->waitAcqState(Idle);
595
	removeAllImgProc();
596
597
598
599
600
601
}

void Jungfrau::getFrameDim(FrameDim& frame_dim, bool raw)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(raw);
602
603
604
605
606
	Size size;
	auto f = [&](auto det_geom) {
		size = Size(det_geom.size.x, det_geom.size.y);
		DEB_TRACE() << DEB_VAR1(size);
	};
607
	applyDetGeom(this, f, raw);
608
609
610
611
	frame_dim = FrameDim(size, Bpp16);
	DEB_RETURN() << DEB_VAR1(frame_dim);
}

612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
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);
	};
639
	applyDetGeom(this, f, raw);
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
	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);
	};
659
	applyDetGeom(this, f, raw);
660
661
662
663
	DEB_RETURN() << DEB_VAR1(data_offset);
	return data_offset;
}

664
665
666
void Jungfrau::getDetMap(Data& det_map)
{
	DEB_MEMBER_FUNCT();
667
	bool raw = getRawMode();
668
669
	FrameDim frame_dim;
	getFrameDim(frame_dim, raw);
670
671
672
673
	initData(det_map, frame_dim.getSize(), Data::UINT32);
	// gap pixels are set to -1
	if (!raw)
		memset(det_map.data(), 0xff, det_map.size());
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695

	auto f = [&](auto det_geom) {
		using namespace sls::Geom;
		unsigned int *p = (unsigned int *) det_map.data();
		int chip_idx = 0;
		det_for_each_mod(det_geom, mod, mod_geom,
		    mod_for_each_recv(mod_geom, recv, recv_geom,
		        recv_for_each_iface(recv_geom, iface, iface_geom,
		            iface_for_each_chip(iface_geom, chip, chip_view,
			        view_for_each_pixel(chip_view, pixel,
				    int i = chip_view.calcMapPixelIndex(pixel);
				    p[i] = chip_idx;
				  );
				++chip_idx;
			      );
		          );
		      );
		  );
	};
	applyDetGeom(this, f, raw);
}

696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
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}
727
#define JUNGFRAU_DAC_MV(x)		{x, 1}
728
729
730
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795

	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();

796
797
798
799
800
801
	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;
802
803
804
805
806
807
808
809
810
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

	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();
	m_frame_map_item = map->getItem(0);
}

void Jungfrau::processBadItemFrame(FrameType frame, int item, char *bptr)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR2(frame, item);
	DEB_ERROR() << "Bad frames not supported yet";
}

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

836
	bool raw = getRawMode();
837
838
	FrameDim frame_dim;
	getFrameDim(frame_dim, raw);
839
840
	Size size = frame_dim.getSize();
	DEB_TRACE() << DEB_VAR2(size, raw);
841

842
843
	m_gain_ped_img_proc->updateImageSize(size, raw);
	m_gain_adc_map_img_proc->updateImageSize(size, raw);
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
}

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);
}

893
894
895
896
897
898
899
900
int Jungfrau::getNbRecvs()
{
	DEB_MEMBER_FUNCT();
	int nb_recvs = m_recv_list.size();
	DEB_RETURN() << DEB_VAR1(nb_recvs);
	return nb_recvs;
}

901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
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
943
944
void Jungfrau::setNbProcessingThreads(int nb_proc_threads)
{
	DEB_MEMBER_FUNCT();
	int curr_nb_proc_threads = m_thread_list.size();
	DEB_PARAM() << DEB_VAR2(curr_nb_proc_threads, nb_proc_threads);

	if (nb_proc_threads < 1)
		THROW_HW_ERROR(InvalidValue) << DEB_VAR1(nb_proc_threads);
	if (nb_proc_threads == curr_nb_proc_threads)
		return;

	if (nb_proc_threads < curr_nb_proc_threads)
		m_thread_list.resize(nb_proc_threads);

	for (int i = curr_nb_proc_threads; i < nb_proc_threads; ++i) {
		Thread *t = new Thread(this, i);
		m_thread_list.push_back(t);
	}
}

int Jungfrau::getNbProcessingThreads()
{
	DEB_MEMBER_FUNCT();
	int nb_proc_threads = m_thread_list.size();
	DEB_RETURN() << DEB_VAR1(nb_proc_threads);
	return nb_proc_threads;
}

void Jungfrau::setThreadCPUAffinity(const CPUAffinityList& aff_list)
{
	DEB_MEMBER_FUNCT();

	setNbProcessingThreads(aff_list.size());

	CPUAffinityList::const_iterator rit = aff_list.begin();
	ThreadList::iterator it, end = m_thread_list.end();
	for (it = m_thread_list.begin(); it != end; ++it, ++rit)
		(*it)->setCPUAffinity(*rit);
}

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

945
	FrameType nb_frames;
946
	Camera *cam = getCamera();
947
948
949
950
951
952
953
954
955
	cam->getNbFrames(nb_frames);

	{
		AutoMutex l = lock();
		m_in_process.clear();
		m_nb_frames = nb_frames;
		m_next_frame = 0;
		m_last_frame = -1;
	}
956

957
958
959
960
	RecvList::iterator rit, rend = m_recv_list.end();
	for (rit = m_recv_list.begin(); rit != rend; ++rit)
		(*rit)->prepareAcq();

961
962
963
	ThreadList::iterator tit, tend = m_thread_list.end();
	for (tit = m_thread_list.begin(); tit != tend; ++tit)
		(*tit)->prepareAcq();
964
965
966
967

	ImgProcList::iterator pit, pend = m_img_proc_list.end();
	for (pit = m_img_proc_list.begin(); pit != pend; ++pit)
		(*pit)->prepareAcq();
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
}

void Jungfrau::startAcq()
{
	DEB_MEMBER_FUNCT();
	ThreadList::iterator it, end = m_thread_list.end();
	for (it = m_thread_list.begin(); it != end; ++it)
		(*it)->startAcq();
}

void Jungfrau::stopAcq()
{
	DEB_MEMBER_FUNCT();
	ThreadList::iterator it, end = m_thread_list.end();
	for (it = m_thread_list.begin(); it != end; ++it)
		(*it)->stopAcq();
}

void Jungfrau::processOneFrame(AutoMutex& l)
{
	DEB_MEMBER_FUNCT();

	FrameType frame = m_next_frame++;

	class Sync
	{
	public:
		Sync(FrameType f, Jungfrau& e)
			: frame(f), jungfrau(e), in_proc(e.m_in_process)
		{
			in_proc.insert(frame);
		}