SlsDetectorJungfrau.h 14.8 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
211
212
 protected:
	virtual int getNbFrameMapItems();
	virtual void updateFrameMapItems(FrameMap *map);

	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:
213
214
	friend class GainPed;

215
216
	typedef std::vector<Receiver *> RecvList;
	
217
218
219
220
221
222
223
224
225
226
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
	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;

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	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();
283
		virtual void clear();
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
		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;

299
	template <class T>
300
301
	class ReadHelper : public Buffer::Callback {
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ReadHelper", 
302
303
304
				  "SlsDetector");
	public:
		typedef DoubleBuffer<T> DBuffer;
305
		typedef DoubleBufferReader<T> DBufferReader;
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
		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);
351
352
		}

353
		Reader *addRead(DBuffer& b, FrameType& frame) {
354
			DEB_MEMBER_FUNCT();
355
356
357
358
359
			Reader *r = new Reader(b, frame, this);
			frame = r->getCounter();
			AutoMutex l(m_mutex);
			m_reader_list.insert(r);
			return r;
360
361
362
363
364
		}

	protected:
		virtual void destroy(void *buffer) {
			DEB_MEMBER_FUNCT();
365
366
367
368
369
370
371
372
373
374
375
			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);
376
377
378
		}

	private:
379
380
		typedef std::set<Reader *> ReaderList;
		typedef typename ReaderList::iterator ReaderIt;
381

382
383
384
385
386
387
388
389
390
391
		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;
392
393
	};

394
395
396
397
398
	class GainADCMapImgProc : public ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::GainADCMapImgProc", 
				  "SlsDetector");
	public:
399
400
401
		GainADCMapImgProc(Jungfrau *jungfrau);

		virtual void updateImageSize(Size size, bool raw);
402
		virtual void clear();
403
404
405
406
407
408
		virtual void processFrame(Data& data);

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

	private:
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
		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);
			}
		};

427
428
		typedef ReadHelper<MapData> Helper;
		typedef typename Helper::DBuffer DBuffer;
429

430
		DBuffer m_buffer;
431
		AutoPtr<Helper> m_helper;
432
	};
433

434
435
436
437
438
439
440
441
	class GainPedImgProc : public ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::GainPedImgProc", 
				  "SlsDetector");
	public:
		GainPedImgProc(Jungfrau *jungfrau);

		virtual void updateImageSize(Size size, bool raw);
442
		virtual void clear();
443
444
445
446
447
448
449
450
451
		virtual void processFrame(Data& data);

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

		GainPed m_gain_ped;

	private:
		struct MapData {
			Data proc_map;
452

453
			MapData() { proc_map.type = Data::UINT16; }
454

455
456
457
458
			void updateSize(Size size) {
				updateDataSize(proc_map, size);
			};
			void clear() { clearData(proc_map); }
459
460
		};

461
462
		typedef ReadHelper<MapData> Helper;
		typedef typename Helper::DBuffer DBuffer;
463

464
		DBuffer m_buffer;
465
466
467
468
469
470
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
500
501
502
503
504
		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();
		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;
505
506
	};

507
508
509
510
511
	class ModelReconstruction : public SlsDetector::Reconstruction
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ModelReconstruction", 
				  "SlsDetector");
	public:
512
513
514
		ModelReconstruction(Jungfrau *jungfrau)
			: SlsDetector::Reconstruction(jungfrau->getCamera()),
			  m_jungfrau(jungfrau)
515
516
		{}

517
		virtual Data processModel(Data& data);
518
519
520
521
522
523

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

524
525
526
527
528
529
	bool getRawMode() {
		bool raw;
		getCamera()->getRawMode(raw);
		return raw;
	}

530
531
532
533
	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;
534
535
536
537
			Buffer *b = new Buffer(d.size());
			d.setBuffer(b);
			b->unref();
			clearData(d);
538
539
540
541
		}
	}

	static void clearData(Data& d) {
542
543
544
		void *p = d.data();
		if (p)
			memset(p, 0, d.size());
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
	}

	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();
569
			clearData(d);
570
571
572
		}
	}

573
574
575
	void addImgProc(ImgProcBase *img_proc);
	void removeImgProc(ImgProcBase *img_proc);
	void removeAllImgProc();
576
	void doSetImgProcConfig(std::string config, bool force);
577

578
579
580
581
582
583
584
585
586
	Data getRawData(Data& data)
	{
		if (m_img_src == Raw)
			return data;
		BufferMgr *buffer_mgr = getBuffer();
		BufferCtrlObj *buffer = buffer_mgr->getBufferCtrlObj(AcqBuffer);
		return buffer->getFrameData(data.frameNumber);
	}

587
588
589
590
591
592
593
594
595
	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);

596
597
598
599
600
601
602
603
604
605
	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; }

606
607
	int getNbRecvs();

608
609
610
611
	int getNbProcessingThreads();
	void setNbProcessingThreads(int nb_proc_threads);

	Cond m_cond;
612
613
	AutoPtr<GainPedImgProc> m_gain_ped_img_proc;
	AutoPtr<GainADCMapImgProc> m_gain_adc_map_img_proc;
614
	AutoPtr<AveImgProc> m_ave_img_proc;
615
616
	std::string m_img_proc_config;
	ImgProcList m_img_proc_list;
617
618
	ImgSrc m_img_src;
	ModelReconstruction *m_reconstruction;
619
	RecvList m_recv_list;
620
621
622
623
	FrameType m_nb_frames;
	ThreadList m_thread_list;
};

624
std::ostream& operator <<(std::ostream& os, Jungfrau::GainPed::MapType map_type);
625
std::ostream& operator <<(std::ostream& os, Jungfrau::ImgSrc src);
626

627
628
629
630
631
632
633
} // namespace SlsDetector

} // namespace lima



#endif // __SLS_DETECTOR_JUNGFRAU_H