SlsDetectorJungfrau.h 13.5 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
26
//###########################################################################
// 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/>.
//###########################################################################

#ifndef __SLS_DETECTOR_JUNGFRAU_H
#define __SLS_DETECTOR_JUNGFRAU_H

#include "SlsDetectorCamera.h"
27
#include "SlsDetectorDoubleBuffer.h"
28

29
#include "processlib/SinkTask.h"
30

31
32
33
#include <array>
#include <variant>

34
35
36
37
38
39
namespace lima 
{

namespace SlsDetector
{

40
41
typedef std::array<Data, 3> JungfrauGainDataSet;

42
43
44
45
46
47
48
49
50
class Jungfrau : public Model
{
	DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau", "SlsDetector");

 public:
	typedef unsigned char Byte;
	typedef unsigned short Word;
	typedef unsigned int Long;

51
52
53
54
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
	class GainPed
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::GainPed",
				  "SlsDetector");
	public:
		struct Calib {
			JungfrauGainDataSet gain_map;
			JungfrauGainDataSet ped_map;
			void clear() {
				for (auto& d: gain_map) clearData(d);
				for (auto& d: ped_map) clearData(d);
			}
		};

		enum MapType { Map16, Map32 };

		struct Map16Data {
			static constexpr MapType Type = Map16;
			static constexpr double DefaultCoeffs[3][2] = {
				{ 0.011555,   965.5},
				{-0.302124,  5519.0},
				{-4.254403, 64882.8},
			};
			static constexpr Data::TYPE DataType = Data::UINT16;
			using Pixel = unsigned short;
		};

		struct Map32Data {
			static constexpr MapType Type = Map32;
			static constexpr double DefaultCoeffs[3][2] = {
				{   0.999952,       0.4}, // effectively {1, 0}
82
				{ -26.144935,  394047.3}, // G0 x26
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
				{-368.163215, 5531211.9}, // G1 x14
			};
			static constexpr Data::TYPE DataType = Data::UINT32;
			using Pixel = unsigned int;
		};

		GainPed(Jungfrau *jungfrau);

		void setMapType(MapType  map_type);
		void getMapType(MapType& map_type);

		void updateImageSize(Size size, bool raw);

		void getDefaultCalib(Calib& calib);

		Data::TYPE getDataType();

		void processFrame(Data& data, Data& proc);

		void setCalib(const Calib& calib);
		void getCalib(Calib& calib);

	private:
		template <class M> struct Impl {
			DEB_CLASS_NAMESPC(DebModCamera,
					  "Jungfrau::GainPed::Impl",
					  "SlsDetector");
		public:
			using Map = M;
			Impl(Jungfrau *j) : m_jungfrau(j) {}
			void updateImageSize(Size size, bool raw);
			void getDefaultCalib(Calib& calib);
			void setDefaultCalib() { getDefaultCalib(m_calib); }
			void processFrame(Data& data, Data& proc);

			Jungfrau *m_jungfrau;
			Size m_size;
			bool m_raw;
			int m_pixels{0};
			Calib m_calib;
		};
		using AnyImpl = std::variant<Impl<Map16Data>, Impl<Map32Data>>;

		Jungfrau *m_jungfrau;
		AnyImpl m_impl;
		Size m_size;
		bool m_raw;
	};

132
133
134
135
136
137
138
139
140
141
142
143
	class ImgProcTask : public SinkTaskBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ImgProcTask",
				  "SlsDetector");
	public:
		ImgProcTask(Jungfrau *jungfrau);

		virtual void process(Data& data);
	private:
		Jungfrau *m_jungfrau;
	};

144
145
	enum ImgSrc { Raw, GainPedCorr };

146
147
148
149
	Jungfrau(Camera *cam);
	~Jungfrau();

	virtual void getFrameDim(FrameDim& frame_dim, bool raw = false);
150
	virtual void getAcqFrameDim(FrameDim& frame_dim, bool raw = false);
151

152
153
	virtual void getDetMap(Data& det_map);

154
155
156
157
158
159
160
161
162
163
164
	virtual std::string getName();
	virtual void getPixelSize(double& x_size, double& y_size);

	virtual void getDACInfo(NameList& name_list, IntList& idx_list,
				IntList& milli_volt_list);
	virtual void getADCInfo(NameList& name_list, IntList& idx_list,
				FloatList& factor_list, 
				FloatList& min_val_list);

	virtual void getTimeRanges(TimeRanges& time_ranges);

165
166
167
	// the returned object must be deleted by the caller
	ImgProcTask *createImgProcTask();

168
169
170
171
172
173
	void setHighVoltage(int  hvolt);
	void getHighVoltage(int& hvolt);

	void setThresholdEnergy(int  thres);
	void getThresholdEnergy(int& thres);

174
175
176
	void setImgProcConfig(std::string  config);
	void getImgProcConfig(std::string &config);

177
	void readGainADCMaps(Data& gain_map, Data& adc_map, FrameType& frame);
178

179
	void readGainPedProcMap(Data& proc_map, FrameType& frame);
180
	void readAveMap(Data& ave_map, FrameType& nb_frames, FrameType& frame);
181

182
183
184
	void setGainPedMapType(GainPed::MapType  map_type);
	void getGainPedMapType(GainPed::MapType& map_type);

185
186
	void setGainPedCalib(const GainPed::Calib& calib)
	{ m_gain_ped_img_proc->m_gain_ped.setCalib(calib); }
187
188
189
190
191
192
	void getGainPedCalib(GainPed::Calib& calib)
	{ m_gain_ped_img_proc->m_gain_ped.getCalib(calib); }

	void setImgSrc(ImgSrc  img_src);
	void getImgSrc(ImgSrc& img_src);

193
194
	virtual bool isXferActive();

195
196
197
	virtual Reconstruction *getReconstruction()
	{ return m_reconstruction; }

198
199
200
201
202
203
204
205
206
207
208
209
210
 protected:
	virtual int getNbFrameMapItems();
	virtual void updateFrameMapItems(FrameMap *map);

	virtual void updateImageSize();

	virtual bool checkSettings(Settings settings);

	virtual void prepareAcq();
	virtual void startAcq();
	virtual void stopAcq();

 private:
211
212
	friend class GainPed;

213
214
	typedef std::vector<Receiver *> RecvList;
	
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
	class ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ImgProcBase", 
				  "SlsDetector");
	public:
		typedef std::vector<int> DataDims;

		ImgProcBase(Jungfrau *jungfrau, std::string name);
		virtual ~ImgProcBase();

		int getNbJungfrauModules()
		{ return m_nb_jungfrau_modules; }

		virtual void updateImageSize(Size size, bool raw);
		virtual void prepareAcq();
230
		virtual void clear();
231
		virtual bool consumesRawData() = 0;
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
		virtual void processFrame(Data& data) = 0;

	protected:
		friend class Jungfrau;
		Jungfrau *m_jungfrau;
		std::string m_name;
		int m_nb_jungfrau_modules;
		bool m_raw;
		int m_pixels;
		Size m_frame_size;
		Point m_det_mods;
	};

	typedef std::vector<ImgProcBase *> ImgProcList;

247
	template <class T>
248
249
	class ReadHelper : public Buffer::Callback {
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ReadHelper", 
250
251
252
				  "SlsDetector");
	public:
		typedef DoubleBuffer<T> DBuffer;
253
		typedef DoubleBufferReader<T> DBufferReader;
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
		class Reader : public DBufferReader {
			DEB_CLASS_NAMESPC(DebModCamera,
					  "Jungfrau::ReadHelper::Reader", 
					  "SlsDetector");
		public:
			Reader(DBuffer& db, FrameType f, ReadHelper *h)
				: DBufferReader(db, f),	m_helper(h)
			{ DEB_CONSTRUCTOR(); }

			~Reader()
			{ DEB_DESTRUCTOR(); }

			void addData(Data& src, Data& ref) {
				DEB_MEMBER_FUNCT();
				makeDataRef(src, ref, m_helper);
				m_buffer_list.insert(ref.data());
				DEB_TRACE() << DEB_VAR1(m_buffer_list.size());
			}

		private:
			friend class ReadHelper;
			typedef std::set<void *> BufferList;

			std::pair<bool, bool> destroy(void *buffer) {
				DEB_MEMBER_FUNCT();
				DEB_PARAM() << DEB_VAR1(m_buffer_list.size());
				typedef typename BufferList::iterator BufferIt;
				BufferIt it, end = m_buffer_list.end();
				it = find(m_buffer_list.begin(), end, buffer);
				bool found = (it != end);
				if (found)
					m_buffer_list.erase(it);
				return {found, m_buffer_list.empty()};
			}

			ReadHelper *m_helper;
			BufferList m_buffer_list;
		};

		~ReadHelper() {
			DEB_DESTRUCTOR();
			AutoMutex l(m_mutex);
			while (!m_reader_list.empty())
				deleteReader(m_reader_list.begin(), l);
299
300
		}

301
		Reader *addRead(DBuffer& b, FrameType& frame) {
302
			DEB_MEMBER_FUNCT();
303
304
305
306
307
			Reader *r = new Reader(b, frame, this);
			frame = r->getCounter();
			AutoMutex l(m_mutex);
			m_reader_list.insert(r);
			return r;
308
309
310
311
312
		}

	protected:
		virtual void destroy(void *buffer) {
			DEB_MEMBER_FUNCT();
313
314
315
316
317
318
319
320
321
322
323
			AutoMutex l(m_mutex);
			ReaderIt it, end = m_reader_list.end();
			for (it = m_reader_list.begin(); it != end; ++it) {
				auto [found, empty] = (*it)->destroy(buffer);
				if (!found)
					continue;
				else if (empty)
					deleteReader(it, l);
				return;
			}
			DEB_ERROR() << "Bad " << DEB_VAR1(buffer);
324
325
326
		}

	private:
327
328
		typedef std::set<Reader *> ReaderList;
		typedef typename ReaderList::iterator ReaderIt;
329

330
331
332
333
334
335
336
337
338
339
		void deleteReader(ReaderIt it, AutoMutex& l) {
			DEB_MEMBER_FUNCT();
			Reader *r = *it;
			m_reader_list.erase(it);
			AutoMutexUnlock u(l);
			delete r;
		}

		Mutex m_mutex;
		ReaderList m_reader_list;
340
341
	};

342
343
344
345
346
	class GainADCMapImgProc : public ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::GainADCMapImgProc", 
				  "SlsDetector");
	public:
347
348
349
		GainADCMapImgProc(Jungfrau *jungfrau);

		virtual void updateImageSize(Size size, bool raw);
350
		virtual void clear();
351
		virtual bool consumesRawData() { return true; }
352
353
354
355
356
357
		virtual void processFrame(Data& data);

		void readGainADCMaps(Data& gain_map, Data& adc_map,
				     FrameType& frame);

	private:
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
		struct MapData {
			Data gain_map;
			Data adc_map;

			MapData() {
				gain_map.type = Data::UINT8;
				adc_map.type = Data::UINT16;
			}
			void updateSize(Size size) {
				updateDataSize(gain_map, size);
				updateDataSize(adc_map, size);
			};
			void clear() {
				clearData(gain_map);
				clearData(adc_map);
			}
		};

376
377
		typedef ReadHelper<MapData> Helper;
		typedef typename Helper::DBuffer DBuffer;
378

379
		DBuffer m_buffer;
380
		AutoPtr<Helper> m_helper;
381
	};
382

383
384
385
386
387
388
389
390
	class GainPedImgProc : public ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::GainPedImgProc", 
				  "SlsDetector");
	public:
		GainPedImgProc(Jungfrau *jungfrau);

		virtual void updateImageSize(Size size, bool raw);
391
		virtual void clear();
392
		virtual bool consumesRawData() { return true; }
393
394
395
396
397
398
399
400
401
		virtual void processFrame(Data& data);

		void readProcMap(Data& proc_map, FrameType& frame);

		GainPed m_gain_ped;

	private:
		struct MapData {
			Data proc_map;
402

403
			MapData() { proc_map.type = Data::UINT16; }
404

405
406
407
408
			void updateSize(Size size) {
				updateDataSize(proc_map, size);
			};
			void clear() { clearData(proc_map); }
409
410
		};

411
412
		typedef ReadHelper<MapData> Helper;
		typedef typename Helper::DBuffer DBuffer;
413

414
		DBuffer m_buffer;
415
416
417
418
419
420
421
422
423
424
425
426
		AutoPtr<Helper> m_helper;
	};

	class AveImgProc : public ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::AveImgProc", 
				  "SlsDetector");
	public:
		AveImgProc(Jungfrau *jungfrau);

		virtual void updateImageSize(Size size, bool raw);
		virtual void clear();
427
		virtual bool consumesRawData() { return false; }
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
		virtual void processFrame(Data& data);

		void readAveMap(Data& ave_map, FrameType& nb_frames,
				FrameType& frame);

	private:
		template <class M>
		void processFrameFunct(Data& data);

		struct MapData {
			Data ave_map;
			FrameType nb_frames;

			MapData() { ave_map.type = Data::DOUBLE; }

			void updateSize(Size size) {
				updateDataSize(ave_map, size);
			};
			void clear() { clearData(ave_map); }
		};

		typedef ReadHelper<MapData> Helper;
		typedef typename Helper::DBuffer DBuffer;

		DBuffer m_buffer;
		AutoPtr<Helper> m_helper;
		Data m_acc;
		int m_nb_frames;
456
457
	};

458
459
460
461
462
	class ModelReconstruction : public SlsDetector::Reconstruction
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ModelReconstruction", 
				  "SlsDetector");
	public:
463
464
465
		ModelReconstruction(Jungfrau *jungfrau)
			: SlsDetector::Reconstruction(jungfrau->getCamera()),
			  m_jungfrau(jungfrau)
466
467
		{}

468
		virtual Data processModel(Data& data);
469
470
471
472
473
474

	private:
		friend class Jungfrau;
		Jungfrau *m_jungfrau;
	};

475
476
477
478
479
480
	bool getRawMode() {
		bool raw;
		getCamera()->getRawMode(raw);
		return raw;
	}

481
482
483
484
	static void updateDataSize(Data& d, Size size) {
		std::vector<int> data_dims{size.getWidth(), size.getHeight()};
		if (d.empty() || (d.dimensions != data_dims)) {
			d.dimensions = data_dims;
485
486
487
488
			Buffer *b = new Buffer(d.size());
			d.setBuffer(b);
			b->unref();
			clearData(d);
489
490
491
492
		}
	}

	static void clearData(Data& d) {
493
494
495
		void *p = d.data();
		if (p)
			memset(p, 0, d.size());
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
	}

	static void makeDataRef(Data& src, Data& ref, Buffer::Callback *cb) {
		ref.type = src.type;
		ref.dimensions = src.dimensions;
		ref.frameNumber = src.frameNumber;
		ref.timestamp = src.timestamp;
		ref.header = src.header;
		Buffer *b = new Buffer;
		b->owner = Buffer::MAPPED;
		b->callback = cb;
		b->data = src.data();
		ref.setBuffer(b);
		b->unref();
	}

	static void initData(Data& d, Size size, Data::TYPE type) {
		std::vector<int> dims = {size.getWidth(), size.getHeight()};
		if ((d.type != type) || (d.dimensions != dims)) {
			d.type = type;
			d.dimensions = dims;
			Buffer *b = new Buffer(d.size());
			d.setBuffer(b);
			b->unref();
520
			clearData(d);
521
522
523
		}
	}

524
525
526
	void addImgProc(ImgProcBase *img_proc);
	void removeImgProc(ImgProcBase *img_proc);
	void removeAllImgProc();
527
	void doSetImgProcConfig(std::string config, bool force);
528

529
	Data getRawData(Data& data)
530
	{ return m_reconstruction->getRawData(data); }
531

532
533
534
535
536
537
538
539
540
541
542
	int getNbJungfrauModules()
	{ return getNbDetModules(); }

	template <class DG>
	Defs::xy getModulePosition(const DG& det_geom, int idx);

	FrameDim getModuleFrameDim(int idx, bool raw);
	int getModuleDataOffset(int idx, bool raw);

	int getNbRecvs();

543
544
	AutoPtr<GainPedImgProc> m_gain_ped_img_proc;
	AutoPtr<GainADCMapImgProc> m_gain_adc_map_img_proc;
545
	AutoPtr<AveImgProc> m_ave_img_proc;
546
547
	std::string m_img_proc_config;
	ImgProcList m_img_proc_list;
548
549
	ImgSrc m_img_src;
	ModelReconstruction *m_reconstruction;
550
	RecvList m_recv_list;
551
552
};

553
std::ostream& operator <<(std::ostream& os, Jungfrau::GainPed::MapType map_type);
554
std::ostream& operator <<(std::ostream& os, Jungfrau::ImgSrc src);
555

556
557
558
559
560
561
562
} // namespace SlsDetector

} // namespace lima



#endif // __SLS_DETECTOR_JUNGFRAU_H