SlsDetectorJungfrau.cpp 31.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

/*
 * 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::Recv class
 */

233
234
235
236
237
238
239
240
241
242
243
244
245
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();
246
	m_raw = m_jungfrau->getRawMode();
247
248
249
250
251
252
253
254
255
256
	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;
257
	//data.frame = frame;
258
	data.buffer = bptr;
259
260
261
262
263
264
265
	if (!m_recv->getImage(data))
		return false;
	FrameType recv_frame = data.header.detHeader.frameNumber;
	if (recv_frame != frame)
		DEB_ERROR() << "Unexpected frame: " << DEB_VAR2(recv_frame,
								frame);
	return true;
266
267
268
269
270
271
272
273
274
275
}

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

277
278
279
280
281

/*
 * Jungfrau::Thread class
 */

282
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
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)
333
334
335
336
337
338
339
340
			try {
				m_jungfrau->processOneFrame(l);
			} catch (Exception& e) {
				Camera *cam = m_jungfrau->getCamera();
				string name = ("Jungfrau::Thread::"
					       "threadFunction");
				cam->reportException(e, name);
			}
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
	}

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

360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375

/*
 * 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());
376

377
378
379
	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)
380
		(*it)->processFrame(data);
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
}


/*
 * 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);
412
	m_pixels = Point(size).getArea();
413
414
415
416
417
418
419
420
421
422
	m_frame_size = size;
	m_raw = raw;
}

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

	if (!m_jungfrau)
		THROW_HW_ERROR(InvalidValue) << "ImgProc already removed";
423
424
425
426
427
428
429

	clear();
}

void Jungfrau::ImgProcBase::clear()
{
	DEB_MEMBER_FUNCT();
430
431
432
}

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

Jungfrau::GainADCMapImgProc::GainADCMapImgProc(Jungfrau *jungfrau)
437
	: ImgProcBase(jungfrau, "gain_adc_map"), m_helper(new Helper)
438
439
440
441
442
443
444
{
	DEB_CONSTRUCTOR();
}

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

448
449
	for (auto& d : m_buffer)
		d.updateSize(size);
450
451
}

452
void Jungfrau::GainADCMapImgProc::clear()
453
454
{
	DEB_MEMBER_FUNCT();
455
	ImgProcBase::clear();
456
457
458
459

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

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

468
469
	Data raw = m_jungfrau->getRawData(data);

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

491
492
493
494
495
void Jungfrau::GainADCMapImgProc::readGainADCMaps(Data& gain_map, Data& adc_map,
						  FrameType& frame)
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(frame);
496
497
498
499
	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);
500
501
	DEB_RETURN() << DEB_VAR3(gain_map, adc_map, frame);
}
502

503
504
505
506
507
508
/*
 * Jungfrau::GainPedImgProc class
 */

Jungfrau::GainPedImgProc::GainPedImgProc(Jungfrau *jungfrau)
	: ImgProcBase(jungfrau, "gain_ped"), m_gain_ped(jungfrau),
509
	  m_helper(new Helper)
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
{
	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);
}

526
void Jungfrau::GainPedImgProc::clear()
527
528
{
	DEB_MEMBER_FUNCT();
529
	ImgProcBase::clear();
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549

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

550
551
	Data raw = m_jungfrau->getRawData(data);

552
553
554
	DoubleBufferWriter<MapData> w(m_buffer);
	MapData& m = w.getBuffer();
	DEB_TRACE() << DEB_VAR1(&m);
555
	m_gain_ped.processFrame(raw, m.proc_map);
556
557
558
559
560
561
562
563
	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);
564
565
566
	Helper::Reader *r = m_helper->addRead(m_buffer, frame);
	MapData &m = r->getBuffer();
	r->addData(m.proc_map, proc_map);
567
568
569
	DEB_RETURN() << DEB_VAR2(proc_map, frame);
}

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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
 * 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);
}

655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
 * Jungfrau::ModelReconstruction class
 */

Data Jungfrau::ModelReconstruction::process(Data& data)
{
	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;
670
671
672
	BufferMgr *buffer_mgr = m_jungfrau->getBuffer();
	BufferCtrlObj *buffer = buffer_mgr->getBufferCtrlObj(AcqBuffer);
	Data raw = buffer->getFrameData(frame);
673
674
675
676
677
678
679
680
	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;
}


681
682
683
684
/*
 * Jungfrau detector class
 */

685
Jungfrau::Jungfrau(Camera *cam)
686
	: Model(cam, JungfrauDet), m_img_src(Raw)
687
688
689
690
691
692
693
{
	DEB_CONSTRUCTOR();

	int nb_det_modules = getNbDetModules();

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

694
695
696
697
	for (int i = 0; i < nb_det_modules; ++i) {
		Recv *r = new Recv(this, i);
		m_recv_list.push_back(r);
	}
698
699
700

	setNbProcessingThreads(1);

701
702
	m_gain_ped_img_proc = new GainPedImgProc(this);
	m_gain_adc_map_img_proc = new GainADCMapImgProc(this);
703
	m_ave_img_proc = new AveImgProc(this);
704

705
706
	m_reconstruction = new ModelReconstruction(this);

707
708
709
710
711
712
	updateCameraModel();
}

Jungfrau::~Jungfrau()
{
	DEB_DESTRUCTOR();
713
	getCamera()->waitAcqState(Idle);
714
715
	m_reconstruction->m_jungfrau = NULL;
	m_reconstruction->unref();
716
	removeAllImgProc();
717
718
}

719
720
721
722
723
724
725
726
727
728
729
730
731
732
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);

733
734
735
	BufferMgr *buffer = getCamera()->getBuffer();
	buffer->releaseBuffers();
	buffer->setMode(do_proc ? BufferMgr::Dual : BufferMgr::Single);
736
737
738
739
740
741
742
743
744
}

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

745
void Jungfrau::getFrameDim(FrameDim& frame_dim, bool raw)
746
747
748
749
750
751
752
753
754
755
756
757
758
759
{
	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)
760
761
762
{
	DEB_MEMBER_FUNCT();
	DEB_PARAM() << DEB_VAR1(raw);
763
764
765
766
767
	Size size;
	auto f = [&](auto det_geom) {
		size = Size(det_geom.size.x, det_geom.size.y);
		DEB_TRACE() << DEB_VAR1(size);
	};
768
	applyDetGeom(this, f, raw);
769
770
771
772
	frame_dim = FrameDim(size, Bpp16);
	DEB_RETURN() << DEB_VAR1(frame_dim);
}

773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
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);
	};
800
	applyDetGeom(this, f, raw);
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
	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);
	};
820
	applyDetGeom(this, f, raw);
821
822
823
824
	DEB_RETURN() << DEB_VAR1(data_offset);
	return data_offset;
}

825
826
827
void Jungfrau::getDetMap(Data& det_map)
{
	DEB_MEMBER_FUNCT();
828
	bool raw = getRawMode();
829
830
	FrameDim frame_dim;
	getFrameDim(frame_dim, raw);
831
832
833
834
	initData(det_map, frame_dim.getSize(), Data::UINT32);
	// gap pixels are set to -1
	if (!raw)
		memset(det_map.data(), 0xff, det_map.size());
835
836
837
838
839

	auto f = [&](auto det_geom) {
		using namespace sls::Geom;
		unsigned int *p = (unsigned int *) det_map.data();
		int chip_idx = 0;
840
841
842
843
844
845
846
847
848
		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;
		  });
849
850
851
852
	};
	applyDetGeom(this, f, raw);
}

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
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}
884
#define JUNGFRAU_DAC_MV(x)		{x, 1}
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
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
945
946
947
948
949
950
951
952

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

953
954
955
956
957
958
	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;
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

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

993
	bool raw = getRawMode();
994
	FrameDim frame_dim;
995
	getAcqFrameDim(frame_dim, raw);
996
997
	Size size = frame_dim.getSize();
	DEB_TRACE() << DEB_VAR2(size, raw);
998

999
 	m_gain_ped_img_proc->updateImageSize(size, raw);
1000
	m_gain_adc_map_img_proc->updateImageSize(size, raw);