SlsDetectorEiger.h 14.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
75
76
				typedef Receiver::ImageData RecvImageData;
				
				char *src[EigerNbRecvPorts];
77
78
				char *dst;
				Mask valid;
79
80
81

				FrameData();
				FrameData(const RecvImageData& image, char *d);
82
83
84
85
86
87
88
89
90
91
92
93
94
95
			};

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

				void prepareAcq();

				FrameDim getSrcFrameDim();

96
				void copy(char *dst, char *src);
97
98
99
100

			private:
				friend class Recv;

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

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

			Recv(Geometry *eiger_geom, int idx);

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

			void prepareAcq();

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

135
136
			void expandPixelDepth4(const FrameData& data);
			void copy(const FrameData& data);
137
138
139

			void fillBadFrame(FrameType frame, char *bptr);

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

147
148
149
150
151
152
153
		private:
			friend class Port;
			friend class Geometry;

			Geometry *m_eiger_geom;
			int m_idx;
			PortList m_port_list;
154
			bool m_pixel_depth_4;
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
204
205
		};
		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;
	};

206
207
	Eiger(Camera *cam);
	~Eiger();
208

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

211
212
	virtual void getDetMap(Data& det_map);

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

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

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

228
229
230
	void setParallelMode(ParallelMode  mode);
	void getParallelMode(ParallelMode& mode);

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

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

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

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

248
249
250
	void setTxFrameDelay(int  tx_frame_delay);
	void getTxFrameDelay(int& tx_frame_delay);

251
252
253
	bool isTenGigabitEthernetEnabled();
	void setFlowControl10G(bool enabled);

254
255
256
	Geometry *getGeometry()
	{ return &m_geom; }

257
258
259
260
	void getFpgaFramePtrDiff(PtrDiffList& ptr_diff);

	virtual bool isXferActive();

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

	virtual Reconstruction *getReconstruction()
	{ return m_reconstruction; }

269
 protected:
270
271
272
273
274
	virtual int getNbFrameMapItems();
	virtual void updateFrameMapItems(FrameMap *map);
	virtual void processBadItemFrame(FrameType frame, int item,
					 char *bptr);

275
276
	virtual void setThreadCPUAffinity(const CPUAffinityList& aff_list);

277
278
	virtual void updateImageSize();

279
280
281
	virtual bool checkSettings(Settings settings);

	virtual void prepareAcq();
282
283
	virtual void startAcq();
	virtual void stopAcq();
284
285
286
287

 private:
	friend class CorrBase;

288
289
290
291
292
293
294
	struct Beb {
		BebShell shell;
		BebFpgaMem fpga_mem;
		Beb(const std::string& host_name);
	};
	typedef std::vector<AutoPtr<Beb> > BebList;

295
	class Recv
296
	{
297
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::Recv", "SlsDetector");
298
	public:
299
		typedef Geometry::Recv::FrameData FrameData;
300
		typedef Receiver::ImageData RecvImageData;
301
302

		Recv(Eiger *eiger, int idx);
303
		virtual ~Recv();
304
305

		void prepareAcq();
306

307
		bool processOneFrame(FrameType frame, char *bptr);
308
309
		void processBadFrame(FrameType frame, char *bptr);

310
	private:
311

312
313
314
315
316
317
		Eiger *m_eiger;
		int m_idx;
		Geometry::Recv *m_geom;
		Receiver *m_recv;
		int m_data_offset;
	};
318

319
	typedef std::vector<AutoPtr<Recv> > RecvList;
320

321

322
323
324
325
326
	class Thread : public lima::Thread
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::Thread", "SlsDetector");
	public:
		enum State {
327
			Init, Ready, Running, Stopping, Quitting, End,
328
		};
329

330
331
		Thread(Eiger *eiger, int idx);
		virtual ~Thread();
332

333
		void setCPUAffinity(CPUAffinity aff);
334

335
		void prepareAcq();
336

337
338
339
		void startAcq()
		{ setState(Running); }
		void stopAcq()
340
341
342
343
344
345
		{
			setState(Stopping);
			AutoMutex l = lock();
			while (m_state != Ready)
				wait();
		}
346

347
348
349
350
351
	protected:
		virtual void threadFunction();

	private:
		friend class Eiger;
352

353
		AutoMutex lock()
354
		{ return m_eiger->lock(); }
355
		void wait()
356
		{ m_eiger->wait(); }
357
		void broadcast()
358
		{ m_eiger->broadcast(); }
359

360
		void setState(State state)
361
		{
362
363
364
			AutoMutex l = lock();
			m_state = state;
			broadcast();
365
366
		}

367
		Eiger *m_eiger;
368
		int m_idx;
369
		State m_state;
370
	};
371
	typedef std::vector<AutoPtr<Thread> > ThreadList;
372

373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
	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;

399
400
401
402
403
404
405
	class BadRecvFrameCorr : public CorrBase
	{
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::BadRecvFrameCorr", 
				  "SlsDetector");
	public:
		BadRecvFrameCorr(Eiger *eiger);

406
		virtual void prepareAcq();
407
		virtual void correctFrame(FrameType frame, void *ptr);
408
409
410
411
412
413
414
415
416

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

		Camera *m_cam;
417
		BadFrameData m_bfd;
418
419
	};

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
	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;
	};

436
437
438
439
440
	template <class T>
	class ChipBorderCorr : public CorrBase
	{
	public:
		ChipBorderCorr(Eiger *eiger)
441
			: CorrBase(eiger)
442
		{}
443

444
445
446
447
		virtual void prepareAcq()
		{
			CorrBase::prepareAcq();

448
			m_f.resize(m_nb_eiger_modules);
449
			std::vector<BorderFactor>::iterator it = m_f.begin();
450
			for (int i = 0; i < m_nb_eiger_modules; ++i, ++it) {
451
452
453
454
455
456
				it->resize(2);
				(*it)[0] = m_eiger->getBorderCorrFactor(i, 0);
				(*it)[1] = m_eiger->getBorderCorrFactor(i, 1);
			}
		}

457
		virtual void correctFrame(FrameType /*frame*/, void *ptr)
458
459
460
461
462
463
464
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
		{
			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);
499
			for (int i = 0; i < m_nb_eiger_modules; ++i) {
500
501
502
503
				T *d = p + ChipSize * width;
				correctInterChipLine(d, -width, width, 1);
				d += width;
				correctInterChipLine(d, width, width, 1);
504
				if (i == m_nb_eiger_modules - 1)
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
					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);
525
			for (int i = 0; i < m_nb_eiger_modules; ++i) {
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
				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;
	};

542
543
544
545
546
547
548
549
550
551
552
553
554
555
	class ModelReconstruction : public SlsDetector::Reconstruction {
		DEB_CLASS_NAMESPC(DebModCamera, "Eiger::ModelReconstruction", 
				  "SlsDetector");
	public:
		ModelReconstruction(Eiger *eiger) : m_eiger(eiger)
		{ setActive(true); }

		virtual Data process(Data& data);

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

556
	int getNbEigerModules()
557
	{ return m_geom.getNbEigerModules(); }
558

559
560
	const FrameDim& getRecvFrameDim()
	{ return m_geom.m_recv_frame_dim; }
561

562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
	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);

577
	void processOneFrame(AutoMutex& l);
578

579
	CorrBase *createBadRecvFrameCorr();
580
581
582
583
584
	CorrBase *createChipBorderCorr(ImageType image_type);
	CorrBase *createInterModGapCorr();

	void addCorr(CorrBase *corr);
	void removeCorr(CorrBase *corr);
585
	void removeAllCorr();
586
587

	double getBorderCorrFactor(int det, int line);
588
589
	int getInterModuleGap(int det)
	{ return m_geom.getInterModuleGap(det); }
590

591
592
	void measureReadoutTime(double& readout_time);

593
594
595
	static const int ChipSize;
	static const int ChipGap;
	static const int HalfModuleChips;
596
	static const int NbRecvPorts;
597

598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
	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;

614
615
616
617
	static const unsigned long BebFpgaWritePtrAddr;
	static const unsigned long BebFpgaReadPtrAddr;
	static const unsigned long BebFpgaPtrRange;

618
	Cond m_cond;
619
	BebList m_beb_list;
620
	Geometry m_geom;
621
	CorrList m_corr_list;
622
	RecvList m_recv_list;
623
	ModelReconstruction *m_reconstruction;
624
625
626
627
628
629
	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;
630
631
	bool m_fixed_clock_div;
	ClockDiv m_clock_div;
632
633
};

634
std::ostream& operator <<(std::ostream& os, Eiger::ParallelMode mode);
635
636
637
638
639
640
641
642

} // namespace SlsDetector

} // namespace lima



#endif // __SLS_DETECTOR_EIGER_H