SlsDetectorEiger.h 13.7 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 setThreadCPUAffinity(const CPUAffinityList& aff_list);

273
274
	virtual void updateImageSize();

275
276
277
	virtual bool checkSettings(Settings settings);

	virtual void prepareAcq();
278
279
	virtual void startAcq();
	virtual void stopAcq();
280
281
282
283

 private:
	friend class CorrBase;

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

291
	typedef std::vector<Receiver *> RecvList;
292

293
294
295
296
297
	class Thread : public lima::Thread
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::Thread", "SlsDetector");
	public:
		enum State {
298
			Init, Ready, Running, Stopping, Quitting, End,
299
		};
300

301
302
		Thread(Eiger *eiger, int idx);
		virtual ~Thread();
303

304
		void setCPUAffinity(CPUAffinity aff);
305

306
		void prepareAcq();
307

308
309
310
		void startAcq()
		{ setState(Running); }
		void stopAcq()
311
312
313
314
315
316
		{
			setState(Stopping);
			AutoMutex l = lock();
			while (m_state != Ready)
				wait();
		}
317

318
319
320
321
322
	protected:
		virtual void threadFunction();

	private:
		friend class Eiger;
323

324
		AutoMutex lock()
325
		{ return m_eiger->lock(); }
326
		void wait()
327
		{ m_eiger->wait(); }
328
		void broadcast()
329
		{ m_eiger->broadcast(); }
330

331
		void setState(State state)
332
		{
333
334
335
			AutoMutex l = lock();
			m_state = state;
			broadcast();
336
337
		}

338
		Eiger *m_eiger;
339
		int m_idx;
340
		State m_state;
341
	};
342
	typedef std::vector<AutoPtr<Thread> > ThreadList;
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
368
369
	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;

370
371
372
373
374
375
376
	class BadRecvFrameCorr : public CorrBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::BadRecvFrameCorr", 
				  "SlsDetector");
	public:
		BadRecvFrameCorr(Eiger *eiger);

377
		virtual void prepareAcq();
378
		virtual void correctFrame(FrameType frame, void *ptr);
379
380
381
382
383
384
385
386
387

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

		Camera *m_cam;
388
		BadFrameData m_bfd;
389
390
	};

391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
	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;
	};

407
408
409
410
411
	template <class T>
	class ChipBorderCorr : public CorrBase
	{
	public:
		ChipBorderCorr(Eiger *eiger)
412
			: CorrBase(eiger)
413
		{}
414

415
416
417
418
		virtual void prepareAcq()
		{
			CorrBase::prepareAcq();

419
			m_f.resize(m_nb_eiger_modules);
420
			std::vector<BorderFactor>::iterator it = m_f.begin();
421
			for (int i = 0; i < m_nb_eiger_modules; ++i, ++it) {
422
423
424
425
426
427
				it->resize(2);
				(*it)[0] = m_eiger->getBorderCorrFactor(i, 0);
				(*it)[1] = m_eiger->getBorderCorrFactor(i, 1);
			}
		}

428
		virtual void correctFrame(FrameType /*frame*/, void *ptr)
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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
		{
			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);
470
			for (int i = 0; i < m_nb_eiger_modules; ++i) {
471
472
473
474
				T *d = p + ChipSize * width;
				correctInterChipLine(d, -width, width, 1);
				d += width;
				correctInterChipLine(d, width, width, 1);
475
				if (i == m_nb_eiger_modules - 1)
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
					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);
496
			for (int i = 0; i < m_nb_eiger_modules; ++i) {
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
				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;
	};

513
514
515
516
	class ModelReconstruction : public SlsDetector::Reconstruction {
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::ModelReconstruction", 
				  "SlsDetector");
	public:
517
518
519
		ModelReconstruction(Eiger *eiger)
			: SlsDetector::Reconstruction(eiger->getCamera()),
			  m_eiger(eiger)
520
521
		{ setActive(true); }

522
		virtual Data processModel(Data& data);
523
524
525
526
527
528

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

529
	int getNbEigerModules()
530
	{ return m_geom.getNbEigerModules(); }
531

532
533
	const FrameDim& getRecvFrameDim()
	{ return m_geom.m_recv_frame_dim; }
534

535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
	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; }

	int getNbRecvs();

	int getNbProcessingThreads();
	void setNbProcessingThreads(int nb_proc_threads);

550
	CorrBase *createBadRecvFrameCorr();
551
552
553
554
555
	CorrBase *createChipBorderCorr(ImageType image_type);
	CorrBase *createInterModGapCorr();

	void addCorr(CorrBase *corr);
	void removeCorr(CorrBase *corr);
556
	void removeAllCorr();
557
558

	double getBorderCorrFactor(int det, int line);
559
560
	int getInterModuleGap(int det)
	{ return m_geom.getInterModuleGap(det); }
561

562
563
	void measureReadoutTime(double& readout_time);

564
565
566
	static const int ChipSize;
	static const int ChipGap;
	static const int HalfModuleChips;
567
	static const int NbRecvPorts;
568

569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
	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;

585
586
587
588
	static const unsigned long BebFpgaWritePtrAddr;
	static const unsigned long BebFpgaReadPtrAddr;
	static const unsigned long BebFpgaPtrRange;

589
	Cond m_cond;
590
	BebList m_beb_list;
591
	Geometry m_geom;
592
	CorrList m_corr_list;
593
	RecvList m_recv_list;
594
	ModelReconstruction *m_reconstruction;
595
596
	FrameType m_nb_frames;
	ThreadList m_thread_list;
597
598
	bool m_fixed_clock_div;
	ClockDiv m_clock_div;
599
600
};

601
std::ostream& operator <<(std::ostream& os, Eiger::ParallelMode mode);
602
603
604
605
606
607
608
609

} // namespace SlsDetector

} // namespace lima



#endif // __SLS_DETECTOR_EIGER_H