SlsDetectorJungfrau.h 13.1 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
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
	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}
				{ -26.144935,  394047.3}, // G0 x16
				{-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
180
	void readGainPedProcMap(Data& proc_map, FrameType& frame);

181
182
183
184
185
186
187
188
189
	void setGainPedMapType(GainPed::MapType  map_type);
	void getGainPedMapType(GainPed::MapType& map_type);

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

190
191
	virtual bool isXferActive();

192
193
194
	virtual Reconstruction *getReconstruction()
	{ return m_reconstruction; }

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
 protected:
	virtual int getNbFrameMapItems();
	virtual void updateFrameMapItems(FrameMap *map);
	virtual void processBadItemFrame(FrameType frame, int item,
					 char *bptr);

	virtual void setThreadCPUAffinity(const CPUAffinityList& aff_list);

	virtual void updateImageSize();

	virtual bool checkSettings(Settings settings);

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

 private:
212
213
	friend class GainPed;

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
	class Recv
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::Recv", "SlsDetector");
	public:
		typedef Receiver::ImageData RecvImageData;

		Recv(Jungfrau *jungfrau, int idx);

		void prepareAcq();

		bool processOneFrame(FrameType frame, char *bptr);
		void processBadFrame(FrameType frame, char *bptr);

	private:
		Jungfrau *m_jungfrau;
		int m_idx;
		bool m_raw;
		Receiver *m_recv;
		FrameDim m_frame_dim;
		int m_data_offset;
	};

	typedef std::vector<AutoPtr<Recv> > RecvList;

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
284
285
286
287
288
	class Thread : public lima::Thread
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::Thread", "SlsDetector");
	public:
		enum State {
			Init, Ready, Running, Stopping, Quitting, End,
		};

		Thread(Jungfrau *jungfrau, int idx);
		virtual ~Thread();

		void setCPUAffinity(CPUAffinity aff);

		void prepareAcq();

		void startAcq()
		{ setState(Running); }
		void stopAcq()
		{
			setState(Stopping);
			AutoMutex l = lock();
			while (m_state != Ready)
				wait();
		}

	protected:
		virtual void threadFunction();

	private:
		friend class Jungfrau;

		AutoMutex lock()
		{ return m_jungfrau->lock(); }
		void wait()
		{ m_jungfrau->wait(); }
		void broadcast()
		{ m_jungfrau->broadcast(); }

		void setState(State state)
		{
			AutoMutex l = lock();
			m_state = state;
			broadcast();
		}

		Jungfrau *m_jungfrau;
		int m_idx;
		State m_state;
	};
	typedef std::vector<AutoPtr<Thread> > ThreadList;

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
	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();
304
		virtual void clear();
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
		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;

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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
	template <class T>
	class ReaderHelper : public Buffer::Callback {
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ReaderHelper", 
				  "SlsDetector");
	public:
		typedef DoubleBuffer<T> DBuffer;

		T& addRead(DBuffer& b, FrameType& frame) {
			DEB_MEMBER_FUNCT();
			if (m_reader)
				THROW_HW_ERROR(Error)
					<< "A reader is already active";
			m_reader = new Reader(b, frame);
			frame = m_reader->getCounter();
			return m_reader->getBuffer();
		}

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

	protected:
		virtual void destroy(void *buffer) {
			DEB_MEMBER_FUNCT();
			DEB_PARAM() << DEB_VAR1(m_buffer_list.size());
			if (m_buffer_list.empty() || !m_reader)
				DEB_ERROR() << "Unexpected "
					    << DEB_VAR1(buffer);
			BufferList::iterator it, end = m_buffer_list.end();
			it = find(m_buffer_list.begin(), end, buffer);
			if (it == end)
				DEB_ERROR() << "Bad " << DEB_VAR1(buffer);
			m_buffer_list.erase(it);
			if (m_buffer_list.empty())
				m_reader = NULL;
		}

	private:
		typedef DoubleBufferReader<T> Reader;
		typedef std::set<void *> BufferList;

		AutoPtr<Reader> m_reader;
		BufferList m_buffer_list;
	};

368
369
370
371
372
	class GainADCMapImgProc : public ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::GainADCMapImgProc", 
				  "SlsDetector");
	public:
373
374
375
		GainADCMapImgProc(Jungfrau *jungfrau);

		virtual void updateImageSize(Size size, bool raw);
376
		virtual void clear();
377
378
379
380
381
382
		virtual void processFrame(Data& data);

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

	private:
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
		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);
			}
		};

401
402
		typedef ReaderHelper<MapData> Reader;
		typedef typename Reader::DBuffer DBuffer;
403

404
405
406
		DBuffer m_buffer;
		AutoPtr<Reader> m_reader;
	};
407

408
409
410
411
412
413
414
415
	class GainPedImgProc : public ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::GainPedImgProc", 
				  "SlsDetector");
	public:
		GainPedImgProc(Jungfrau *jungfrau);

		virtual void updateImageSize(Size size, bool raw);
416
		virtual void clear();
417
418
419
420
421
422
423
424
425
		virtual void processFrame(Data& data);

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

		GainPed m_gain_ped;

	private:
		struct MapData {
			Data proc_map;
426

427
			MapData() { proc_map.type = Data::UINT16; }
428

429
430
431
432
			void updateSize(Size size) {
				updateDataSize(proc_map, size);
			};
			void clear() { clearData(proc_map); }
433
434
		};

435
436
437
		typedef ReaderHelper<MapData> Reader;
		typedef typename Reader::DBuffer DBuffer;

438
		DBuffer m_buffer;
439
		AutoPtr<Reader> m_reader;
440
441
	};

442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
	class ModelReconstruction : public SlsDetector::Reconstruction
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ModelReconstruction", 
				  "SlsDetector");
	public:
		ModelReconstruction(Jungfrau *jungfrau) : m_jungfrau(jungfrau)
		{}

		virtual Data process(Data& data);

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

457
458
459
460
461
462
	bool getRawMode() {
		bool raw;
		getCamera()->getRawMode(raw);
		return raw;
	}

463
464
465
466
	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;
467
468
469
470
			Buffer *b = new Buffer(d.size());
			d.setBuffer(b);
			b->unref();
			clearData(d);
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
		}
	}

	static void clearData(Data& d) {
		memset(d.data(), 0, d.size());
	}

	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();
500
			clearData(d);
501
502
503
		}
	}

504
505
506
	void addImgProc(ImgProcBase *img_proc);
	void removeImgProc(ImgProcBase *img_proc);
	void removeAllImgProc();
507
	void doSetImgProcConfig(std::string config, bool force);
508

509
510
511
512
513
514
515
516
517
	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);

518
519
520
521
522
523
524
525
526
527
	AutoMutex lock()
	{ return AutoMutex(m_cond.mutex()); }
	void wait()
	{ m_cond.wait(); }
	void broadcast()
	{ m_cond.broadcast(); }

	bool allFramesAcquired()
	{ return m_next_frame == m_nb_frames; }

528
529
	int getNbRecvs();

530
531
532
533
534
535
	int getNbProcessingThreads();
	void setNbProcessingThreads(int nb_proc_threads);

	void processOneFrame(AutoMutex& l);

	Cond m_cond;
536
537
	AutoPtr<GainPedImgProc> m_gain_ped_img_proc;
	AutoPtr<GainADCMapImgProc> m_gain_adc_map_img_proc;
538
539
	std::string m_img_proc_config;
	ImgProcList m_img_proc_list;
540
541
542
	ImgSrc m_img_src;
	BufferCtrlObj m_acq_buffer_ctrl_obj;
	ModelReconstruction *m_reconstruction;
543
	RecvList m_recv_list;
544
545
546
547
548
549
550
551
	FrameType m_nb_frames;
	FrameType m_next_frame;
	FrameType m_last_frame;
	SortedIntList m_in_process;
	FrameMap::Item *m_frame_map_item;
	ThreadList m_thread_list;
};

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

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

} // namespace lima



#endif // __SLS_DETECTOR_JUNGFRAU_H