SlsDetectorEiger.h 12.4 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_EIGER_H
#define __SLS_DETECTOR_EIGER_H

#include "SlsDetectorCamera.h"
27
#include "SlsDetectorBebTools.h"
28
29
30
31
32
33
34
35
36
37
38
39

#include "processlib/LinkTask.h"


namespace lima 
{

namespace SlsDetector
{

#define EIGER_PACKET_DATA_LEN	(4 * 1024)

40
#define EigerNbRecvPorts	2
41

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

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

51
52
	typedef std::vector<unsigned long> PtrDiffList;

53
54
	typedef Defs::ClockDiv ClockDiv;

55
	enum ParallelMode {
56
		NonParallel, Parallel,
57
58
	};

59
60
61
62
63
	class Geometry
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::Geometry",
				  "SlsDetector");
	public:
64
		typedef std::bitset<EigerNbRecvPorts> Mask;
65
66
67
68
69
70
71
72

		class Recv
		{
			DEB_CLASS_NAMESPC(DebModCamera, "Eiger::Geometry::Recv",
					  "SlsDetector");
		public:
			class Port;

73
			struct FrameData {
74
				char *src[EigerNbRecvPorts];
75
76
				char *dst;
				Mask valid;
77
78

				FrameData();
79
				FrameData(const Receiver::ImagePackets& image, char *d);
80
81
82
83
84
85
86
87
88
89
90
91
92
93
			};

			class Port
			{
				DEB_CLASS_NAMESPC(DebModCamera,
						  "Eiger::Geometry::Recv::Port",
						  "SlsDetector");
			public:
				Port(Recv *recv, int port);

				void prepareAcq();

				FrameDim getSrcFrameDim();

94
				void copy(char *dst, char *src);
95
96
97
98

			private:
				friend class Recv;

99
100
				struct LocationData {
					int len;	// length
101
102
					int cw;		// chip width
					int lw;		// line width
103
					int off;	// offset
104
105
106
107
108
109
110
111
				};

				Recv *m_recv;
				int m_recv_idx;
				int m_port;
				bool m_top_half_recv;
				bool m_raw;
				int m_pchips;
112
113
114
				LocationData m_src;
				LocationData m_dst;
				int m_port_blocks;
115
116
117
118
119
120
121
122
123
124
			};
			typedef std::vector<AutoPtr<Port> > PortList;

			Recv(Geometry *eiger_geom, int idx);

			int getNbPorts();
			Port *getPort(int idx);

			void prepareAcq();

125
			void processFrame(const FrameData& data)
126
127
			{
				if (m_pixel_depth_4)
128
					expandPixelDepth4(data);
129
				else
130
					copy(data);
131
132
			}

133
134
			void expandPixelDepth4(const FrameData& data);
			void copy(const FrameData& data);
135
136
137

			void fillBadFrame(FrameType frame, char *bptr);

138
139
140
141
			int getDstBufferOffset()
			{
				Port *port = m_port_list[0];
				Port::LocationData& dst = port->m_dst;
142
				return dst.off;
143
144
			}

145
146
147
148
149
150
151
		private:
			friend class Port;
			friend class Geometry;

			Geometry *m_eiger_geom;
			int m_idx;
			PortList m_port_list;
152
			bool m_pixel_depth_4;
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
		};
		typedef std::vector<AutoPtr<Recv> > RecvList;

		Geometry();

		void setNbRecvs(int nb_recv);
		int getNbRecvs()
		{ return m_recv_list.size(); }
		Recv *getRecv(int idx)
		{ return m_recv_list[idx]; }

		int getNbEigerModules()
		{ return getNbRecvs() / 2; }

		void setRaw(bool  raw)
		{ m_raw = raw; }
		bool getRaw()
		{ return m_raw; }

		void setImageType(ImageType image_type)
		{ m_image_type = image_type; }
		ImageType getImageType()
		{ return m_image_type; }

		void setPixelDepth(PixelDepth pixel_depth)
		{ m_pixel_depth = pixel_depth; }
		PixelDepth getPixelDepth()
		{ return m_pixel_depth; }
		bool isPixelDepth4()
		{ return (m_pixel_depth == PixelDepth4); }


		FrameDim getFrameDim(bool raw);
		FrameDim getRecvFrameDim(bool raw);

		int getInterModuleGap(int det);

		void prepareAcq();

	private:
		friend class Recv;
		friend class Recv::Port;
		friend class Eiger;

		bool m_raw;
		ImageType m_image_type;
		PixelDepth m_pixel_depth;
		FrameDim m_recv_frame_dim;
		RecvList m_recv_list;
	};

204
205
	Eiger(Camera *cam);
	~Eiger();
206

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

209
210
	virtual void getDetMap(Data& det_map);

211
212
213
	virtual std::string getName();
	virtual void getPixelSize(double& x_size, double& y_size);

214
215
	virtual void getDACInfo(NameList& name_list, IntList& idx_list,
				IntList& milli_volt_list);
216
217
218
	virtual void getADCInfo(NameList& name_list, IntList& idx_list,
				FloatList& factor_list, 
				FloatList& min_val_list);
219

220
	virtual void getTimeRanges(TimeRanges& time_ranges);
221
222
223
224
	static void calcTimeRanges(PixelDepth pixel_depth,
				   ClockDiv clock_div,
				   ParallelMode parallel_mode, 
				   TimeRanges& time_ranges);
225

226
227
228
	void setParallelMode(ParallelMode  mode);
	void getParallelMode(ParallelMode& mode);

229
230
	void setFixedClockDiv(bool  fixed_clock_div);
	void getFixedClockDiv(bool& fixed_clock_div);
231
232
	void setClockDiv(ClockDiv  clock_div);
	void getClockDiv(ClockDiv& clock_div);
233
234
	void setSubExpTime(double  sub_exp_time);
	void getSubExpTime(double& sub_exp_time);
235

236
237
	void setAllTrimBits(int mod_idx, int  val);
	void getAllTrimBits(int mod_idx, int& val);
238
239
240
241
242
243
244
245
	void getAllTrimBitsList(IntList& val_list);

	void setHighVoltage(int  hvolt);
	void getHighVoltage(int& hvolt);

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

246
247
248
	void setTxFrameDelay(int  tx_frame_delay);
	void getTxFrameDelay(int& tx_frame_delay);

249
250
251
	bool isTenGigabitEthernetEnabled();
	void setFlowControl10G(bool enabled);

252
253
254
	Geometry *getGeometry()
	{ return &m_geom; }

255
256
257
258
	void getFpgaFramePtrDiff(PtrDiffList& ptr_diff);

	virtual bool isXferActive();

259
260
261
262
263
264
265
266
	void setApplyCorrections(bool  active)
	{ m_reconstruction->setActive(active); }
	void getApplyCorrections(bool& active)
	{ m_reconstruction->getActive(active); }

	virtual Reconstruction *getReconstruction()
	{ return m_reconstruction; }

267
 protected:
268
269
270
	virtual int getNbFrameMapItems();
	virtual void updateFrameMapItems(FrameMap *map);

271
272
	virtual void updateImageSize();

273
274
275
	virtual bool checkSettings(Settings settings);

	virtual void prepareAcq();
276
277
	virtual void startAcq();
	virtual void stopAcq();
278
279
280
281

 private:
	friend class CorrBase;

282
283
284
285
286
287
288
	struct Beb {
		BebShell shell;
		BebFpgaMem fpga_mem;
		Beb(const std::string& host_name);
	};
	typedef std::vector<AutoPtr<Beb> > BebList;

289
	typedef std::vector<Receiver *> RecvList;
290

291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
	class CorrBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::CorrBase", 
				  "SlsDetector");
	public:
		CorrBase(Eiger *eiger);
		virtual ~CorrBase();

		int getNbEigerModules()
		{ return m_nb_eiger_modules; }

		virtual void prepareAcq();
		virtual void correctFrame(FrameType frame, void *ptr) = 0;

	protected:
		friend class Eiger;
		Eiger *m_eiger;
		int m_nb_eiger_modules;
		FrameDim m_mod_frame_dim;
		Size m_frame_size;
		std::vector<int> m_inter_lines;
		// TODO: add ref count
	};

	typedef std::vector<CorrBase *> CorrList;

317
318
319
320
321
322
323
	class BadRecvFrameCorr : public CorrBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::BadRecvFrameCorr", 
				  "SlsDetector");
	public:
		BadRecvFrameCorr(Eiger *eiger);

324
		virtual void prepareAcq();
325
		virtual void correctFrame(FrameType frame, void *ptr);
326
327
328
329
330
331
332
333
334

	protected:
		struct BadFrameData {
			int last_idx;
			IntList bad_frame_list;
			void reset();
		};

		Camera *m_cam;
335
		BadFrameData m_bfd;
336
337
	};

338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
	class InterModGapCorr : public CorrBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::InterModGapCorr", 
				  "SlsDetector");
	public:
		InterModGapCorr(Eiger *eiger);

		virtual void prepareAcq();
		virtual void correctFrame(FrameType frame, void *ptr);

	protected:
		typedef std::pair<int, int> Block;
		typedef std::vector<Block> BlockList;
		BlockList m_gap_list;
	};

354
355
356
357
358
	template <class T>
	class ChipBorderCorr : public CorrBase
	{
	public:
		ChipBorderCorr(Eiger *eiger)
359
			: CorrBase(eiger)
360
		{}
361

362
363
364
365
		virtual void prepareAcq()
		{
			CorrBase::prepareAcq();

366
			m_f.resize(m_nb_eiger_modules);
367
			std::vector<BorderFactor>::iterator it = m_f.begin();
368
			for (int i = 0; i < m_nb_eiger_modules; ++i, ++it) {
369
370
371
372
373
374
				it->resize(2);
				(*it)[0] = m_eiger->getBorderCorrFactor(i, 0);
				(*it)[1] = m_eiger->getBorderCorrFactor(i, 1);
			}
		}

375
		virtual void correctFrame(FrameType /*frame*/, void *ptr)
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
		{
			correctBorderCols(ptr);
			correctBorderRows(ptr);
			correctInterChipCols(ptr);
			correctInterChipRows(ptr);
		}

	private:
		typedef std::vector<double> BorderFactor;

		static void correctInterChipLine(T *d, int offset, int nb_iter, 
						 int step) 
		{
			for (int i = 0; i < nb_iter; ++i, d += step)
				d[0] = d[offset] /= 2;
		}

		static void correctBorderLine(T *d, int nb_iter, int step,
					      double f) 
		{
			for (int i = 0; i < nb_iter; ++i, d += step)
				d[0] /= f;
		}

		void correctInterChipCols(void *ptr)
		{
			int width = m_frame_size.getWidth();
			int height= m_frame_size.getHeight();
			T *d = static_cast<T *>(ptr);
			for (int i = 0; i < HalfModuleChips - 1; ++i) {
				d += ChipSize;
				correctInterChipLine(d++, -1, height, width);
				correctInterChipLine(d++, 1, height, width);
			}
		}

		void correctInterChipRows(void *ptr)
		{
			int width = m_frame_size.getWidth();
			int mod_height = m_mod_frame_dim.getSize().getHeight();
			T *p = static_cast<T *>(ptr);
417
			for (int i = 0; i < m_nb_eiger_modules; ++i) {
418
419
420
421
				T *d = p + ChipSize * width;
				correctInterChipLine(d, -width, width, 1);
				d += width;
				correctInterChipLine(d, width, width, 1);
422
				if (i == m_nb_eiger_modules - 1)
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
					continue;
				p += (mod_height + m_inter_lines[i]) * width;
			}
		}

		void correctBorderCols(void *ptr)
		{
			int width = m_frame_size.getWidth();
			int height= m_frame_size.getHeight();
			T *d = static_cast<T *>(ptr);
			correctBorderLine(d, height, width, 2);
			d += width - 1;
			correctBorderLine(d, height, width, 2);
		}

		void correctBorderRows(void *ptr)
		{
			int width = m_frame_size.getWidth();
			int mod_height = m_mod_frame_dim.getSize().getHeight();
			T *p = static_cast<T *>(ptr);
443
			for (int i = 0; i < m_nb_eiger_modules; ++i) {
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
				double f0 = m_f[i][0], f1 = m_f[i][1];
				T *d = p;
				correctBorderLine(d, width, 1, f0);
				d += width;
				correctBorderLine(d, width, 1, f1);
				d += (mod_height - 1 - 2) * width;
				correctBorderLine(d, width, 1, f1);
				d += width;
				correctBorderLine(d, width, 1, f0);
				p += (mod_height + m_inter_lines[i]) * width;
			}
		}

		std::vector<BorderFactor> m_f;
	};

460
461
462
463
	class ModelReconstruction : public SlsDetector::Reconstruction {
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::ModelReconstruction", 
				  "SlsDetector");
	public:
464
465
466
		ModelReconstruction(Eiger *eiger)
			: SlsDetector::Reconstruction(eiger->getCamera()),
			  m_eiger(eiger)
467
468
		{ setActive(true); }

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

	private:
		friend class Eiger;
		Eiger *m_eiger;
	};

476
	int getNbEigerModules()
477
	{ return m_geom.getNbEigerModules(); }
478

479
480
	const FrameDim& getRecvFrameDim()
	{ return m_geom.m_recv_frame_dim; }
481

482
483
	int getNbRecvs();

484
	CorrBase *createBadRecvFrameCorr();
485
486
487
488
489
	CorrBase *createChipBorderCorr(ImageType image_type);
	CorrBase *createInterModGapCorr();

	void addCorr(CorrBase *corr);
	void removeCorr(CorrBase *corr);
490
	void removeAllCorr();
491
492

	double getBorderCorrFactor(int det, int line);
493
494
	int getInterModuleGap(int det)
	{ return m_geom.getInterModuleGap(det); }
495

496
497
	void measureReadoutTime(double& readout_time);

498
499
500
	static const int ChipSize;
	static const int ChipGap;
	static const int HalfModuleChips;
501
	static const int NbRecvPorts;
502

503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
	struct LinScale {
		double factor, offset;
		LinScale(double f, double o) : factor(f), offset(o) {}
		double calcY(double x) const
		{ return x * factor + offset; }
		double calcX(double y) const
		{ return (y - offset) / factor; }
	};

	static const int BitsPerXfer;
	static const int SuperColNbCols;
	static const double BaseChipXferFreq;
	static const double MaxFebBebBandwidth;
	static const LinScale ChipXfer2Buff;
	static const LinScale ChipRealReadout;

519
520
521
522
	static const unsigned long BebFpgaWritePtrAddr;
	static const unsigned long BebFpgaReadPtrAddr;
	static const unsigned long BebFpgaPtrRange;

523
	BebList m_beb_list;
524
	Geometry m_geom;
525
	CorrList m_corr_list;
526
	RecvList m_recv_list;
527
	ModelReconstruction *m_reconstruction;
528
529
	bool m_fixed_clock_div;
	ClockDiv m_clock_div;
530
531
};

532
std::ostream& operator <<(std::ostream& os, Eiger::ParallelMode mode);
533
534
535
536
537
538
539
540

} // namespace SlsDetector

} // namespace lima



#endif // __SLS_DETECTOR_EIGER_H