SlsDetectorJungfrau.h 8.69 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
34
35
36
37
38
39
40
41
42
43
44
45

namespace lima 
{

namespace SlsDetector
{

class Jungfrau : public Model
{
	DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau", "SlsDetector");

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

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
	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;
	};

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
	Jungfrau(Camera *cam);
	~Jungfrau();

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

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

77
78
79
	// the returned object must be deleted by the caller
	ImgProcTask *createImgProcTask();

80
81
82
83
84
85
	void setHighVoltage(int  hvolt);
	void getHighVoltage(int& hvolt);

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

86
87
88
	void setImgProcConfig(std::string  config);
	void getImgProcConfig(std::string &config);

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

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
	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:
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
	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;

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

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

		static void updateDataSize(Data& d, Size size) {
			DataDims data_dims{size.getWidth(), size.getHeight()};
			if (d.empty() || (d.dimensions != data_dims)) {
				d.dimensions = data_dims;
				d.setBuffer(new Buffer(d.size()));
			}
		}

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

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
		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();
		}

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
	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;

	class GainADCMapImgProc : public ImgProcBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::GainADCMapImgProc", 
				  "SlsDetector");
	public:
247
248
249
250
251
252
253
254
255
256
		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:
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
		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);
			}
		};

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
		typedef DoubleBuffer<MapData> DBuffer;

		class ReaderHelper : public Buffer::Callback {
			DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::"
					  "GainADCMapImgProc::ReaderHelper", 
					  "SlsDetector");
		public:
			void addRead(DBuffer& b, Data& gain_map, Data& adc_map,
				     FrameType frame) {
				DEB_MEMBER_FUNCT();
				if (m_reader)
					THROW_HW_ERROR(Error)
						<< "A reader is already active";
				m_reader = new Reader(b, frame);
				MapData& m = m_reader->getBuffer();
				DEB_TRACE() << DEB_VAR1(&m);

				makeDataRef(m.gain_map, gain_map, this);
				++m_count;
				makeDataRef(m.adc_map, adc_map, this);
				++m_count;
				DEB_TRACE() << DEB_VAR1(m_count);

				frame = m_reader->getCounter();
			}
300

301
302
303
304
305
306
307
308
309
310
311
312
313
314
		protected:
			virtual void destroy(void *buffer) {
				DEB_MEMBER_FUNCT();
				DEB_PARAM() << DEB_VAR1(m_count);
				if ((m_count == 0) || !m_reader)
					DEB_ERROR() << DEB_VAR1(m_count);
				MapData& m = m_reader->getBuffer();
				if ((buffer != m.gain_map.data()) &&
				    (buffer != m.adc_map.data()))
					THROW_HW_ERROR(Error)
						<< "Bad buffer";
				if (--m_count == 0)
					m_reader = NULL;
			}
315

316
317
318
319
320
321
322
323
324
		private:
			typedef DoubleBufferReader<MapData> Reader;

			AutoPtr<Reader> m_reader;
			int m_count{0};
		};

		DBuffer m_buffer;
		AutoPtr<ReaderHelper> m_reader;
325
326
327
328
329
330
331
332
	};

	void addImgProc(ImgProcBase *img_proc);
	void removeImgProc(ImgProcBase *img_proc);
	void removeAllImgProc();

	ImgProcBase *createGainADCMapImgProc();

333
334
335
336
337
338
339
340
341
	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);

342
343
344
345
346
347
348
349
350
351
	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; }

352
353
	int getNbRecvs();

354
355
356
357
358
359
	int getNbProcessingThreads();
	void setNbProcessingThreads(int nb_proc_threads);

	void processOneFrame(AutoMutex& l);

	Cond m_cond;
360
361
	std::string m_img_proc_config;
	ImgProcList m_img_proc_list;
362
	RecvList m_recv_list;
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
	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;
};

} // namespace SlsDetector

} // namespace lima



#endif // __SLS_DETECTOR_JUNGFRAU_H