SlsDetectorJungfrau.h 12.2 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
144
145
146
	class ImgProcTask : public SinkTaskBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ImgProcTask",
				  "SlsDetector");
	public:
		ImgProcTask(Jungfrau *jungfrau);

		void setConfig(std::string  config);
		void getConfig(std::string &config);

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

147
148
149
150
151
	Jungfrau(Camera *cam);
	~Jungfrau();

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

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
181
	GainPed& getGainPed() { return m_gain_ped_img_proc->m_gain_ped; }
	void readGainPedProcMap(Data& proc_map, FrameType& frame);

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
	virtual bool isXferActive();

 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:
201
202
	friend class GainPed;

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
	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;

227
228
229
230
231
232
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
	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;

278
279
280
281
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
	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();
		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;

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
354
355
	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;
	};

356
357
358
359
360
	class GainADCMapImgProc : public ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::GainADCMapImgProc", 
				  "SlsDetector");
	public:
361
362
363
364
365
366
367
368
369
370
		GainADCMapImgProc(Jungfrau *jungfrau);

		virtual void updateImageSize(Size size, bool raw);
		virtual void prepareAcq();
		virtual void processFrame(Data& data);

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

	private:
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
		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);
			}
		};

389
390
		typedef ReaderHelper<MapData> Reader;
		typedef typename Reader::DBuffer DBuffer;
391

392
393
394
		DBuffer m_buffer;
		AutoPtr<Reader> m_reader;
	};
395

396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
	class GainPedImgProc : public ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::GainPedImgProc", 
				  "SlsDetector");
	public:
		GainPedImgProc(Jungfrau *jungfrau);

		virtual void updateImageSize(Size size, bool raw);
		virtual void prepareAcq();
		virtual void processFrame(Data& data);

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

		GainPed m_gain_ped;

	private:
		struct MapData {
			Data proc_map;
414

415
			MapData() { proc_map.type = Data::UINT16; }
416

417
418
419
420
			void updateSize(Size size) {
				updateDataSize(proc_map, size);
			};
			void clear() { clearData(proc_map); }
421
422
		};

423
424
425
		typedef ReaderHelper<MapData> Reader;
		typedef typename Reader::DBuffer DBuffer;

426
		DBuffer m_buffer;
427
		AutoPtr<Reader> m_reader;
428
429
	};

430
431
432
433
434
435
	bool getRawMode() {
		bool raw;
		getCamera()->getRawMode(raw);
		return raw;
	}

436
437
438
439
	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;
440
441
442
443
			Buffer *b = new Buffer(d.size());
			d.setBuffer(b);
			b->unref();
			clearData(d);
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
		}
	}

	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();
473
			clearData(d);
474
475
476
		}
	}

477
478
479
480
	void addImgProc(ImgProcBase *img_proc);
	void removeImgProc(ImgProcBase *img_proc);
	void removeAllImgProc();

481
482
483
484
485
486
487
488
489
	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);

490
491
492
493
494
495
496
497
498
499
	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; }

500
501
	int getNbRecvs();

502
503
504
505
506
507
	int getNbProcessingThreads();
	void setNbProcessingThreads(int nb_proc_threads);

	void processOneFrame(AutoMutex& l);

	Cond m_cond;
508
509
	AutoPtr<GainPedImgProc> m_gain_ped_img_proc;
	AutoPtr<GainADCMapImgProc> m_gain_adc_map_img_proc;
510
511
	std::string m_img_proc_config;
	ImgProcList m_img_proc_list;
512
	RecvList m_recv_list;
513
514
515
516
517
518
519
520
	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;
};

521
522
std::ostream& operator <<(std::ostream& os, Jungfrau::GainPed::MapType map_type);

523
524
525
526
527
528
529
} // namespace SlsDetector

} // namespace lima



#endif // __SLS_DETECTOR_JUNGFRAU_H