1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of CheObs, the Modular Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appears in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz, 1/2009 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: CheObs Software Development, 2000-2009
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | //////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MSimTrigger
|
---|
28 | //
|
---|
29 | // This task takes the pure analog channels and simulates a trigger
|
---|
30 | // electronics.
|
---|
31 | //
|
---|
32 | // In a first step several channels can be summed together by a look-up table
|
---|
33 | // fRouteAC.
|
---|
34 | //
|
---|
35 | // In a second step from these analog channels the output of a discriminator
|
---|
36 | // is calculated using a threshold and optional a fixed digital signal length.
|
---|
37 | //
|
---|
38 | // The signal length of the digital signal emitted by the discriminator
|
---|
39 | // can either be bound to the time the signal is above the threshold
|
---|
40 | // defined by fDiscriminatorThreshold if fDigitalSignalLength<0 or set to a
|
---|
41 | // fixed length (fDigitalSignalLength>0).
|
---|
42 | //
|
---|
43 | // With a second look-up table fCoincidenceMap the analog channels are
|
---|
44 | // checked for coincidences. The coincidence must at least be of the length
|
---|
45 | // defined by fCoincidenceTime. The earliest coincide is then stored as
|
---|
46 | // trigger position.
|
---|
47 | //
|
---|
48 | // If a minimum multiplicity m is given, m signals above threshold
|
---|
49 | // in the coincidence patterns are enough to emit a trigger signal.
|
---|
50 | //
|
---|
51 | //
|
---|
52 | // For MAGIC1:
|
---|
53 | // - fDigitalSignalLength between 6ns and 12ns
|
---|
54 | // - fCoincidenceTime between 0.25ns to 1ns
|
---|
55 | //
|
---|
56 | //
|
---|
57 | // Input Containers:
|
---|
58 | // IntendedPulsePos [MParameterD]
|
---|
59 | // MAnalogChannels
|
---|
60 | // MRawRunHeader
|
---|
61 | //
|
---|
62 | // Output Containers:
|
---|
63 | // TriggerPos [MParameterD]
|
---|
64 | // MRawEvtHeader
|
---|
65 | //
|
---|
66 | //////////////////////////////////////////////////////////////////////////////
|
---|
67 | #include "MSimTrigger.h"
|
---|
68 |
|
---|
69 | #include "MLog.h"
|
---|
70 | #include "MLogManip.h"
|
---|
71 |
|
---|
72 | #include "MParList.h"
|
---|
73 | #include "MParameters.h"
|
---|
74 |
|
---|
75 | #include "MLut.h"
|
---|
76 | #include "MArrayI.h"
|
---|
77 |
|
---|
78 | #include "MRawEvtHeader.h"
|
---|
79 | #include "MRawRunHeader.h"
|
---|
80 |
|
---|
81 | #include "MAnalogSignal.h"
|
---|
82 | #include "MAnalogChannels.h"
|
---|
83 | #include "MDigitalSignal.h"
|
---|
84 |
|
---|
85 | #include "MTriggerPattern.h"
|
---|
86 |
|
---|
87 | #include "MPedestalCam.h"
|
---|
88 | #include "MPedestalPix.h"
|
---|
89 |
|
---|
90 | ClassImp(MSimTrigger);
|
---|
91 |
|
---|
92 | using namespace std;
|
---|
93 |
|
---|
94 | // --------------------------------------------------------------------------
|
---|
95 | //
|
---|
96 | // Default Constructor.
|
---|
97 | //
|
---|
98 | MSimTrigger::MSimTrigger(const char *name, const char *title)
|
---|
99 | : fCamera(0),
|
---|
100 | fPulsePos(0),
|
---|
101 | fTrigger(0),
|
---|
102 | fRunHeader(0),
|
---|
103 | fEvtHeader(0),
|
---|
104 | fElectronicNoise(0),
|
---|
105 | fGain(0),
|
---|
106 | fDiscriminatorThreshold(-1),
|
---|
107 | fDigitalSignalLength(8),
|
---|
108 | fCoincidenceTime(0.5),
|
---|
109 | fShiftBaseline(kTRUE),
|
---|
110 | fUngainSignal(kTRUE),
|
---|
111 | fSimulateElectronics(kTRUE),
|
---|
112 | fMinMultiplicity(-1),
|
---|
113 | fCableDelay(21),
|
---|
114 | fCableDamping(0.), // default Damping Set to zero, so users, who do not set
|
---|
115 | fDebugTrigger(kFALSE) // the CableDamoing in the ceres.rc do not see a difference.
|
---|
116 |
|
---|
117 | {
|
---|
118 | fName = name ? name : "MSimTrigger";
|
---|
119 | fTitle = title ? title : "Task to simulate trigger electronics";
|
---|
120 | }
|
---|
121 |
|
---|
122 | // --------------------------------------------------------------------------
|
---|
123 | //
|
---|
124 | // Take two TObjArrays with a collection of digital signals.
|
---|
125 | // Every signal from one array is compared with any from the other array.
|
---|
126 | // For all signals which overlap and which have an overlap time >gate
|
---|
127 | // a new digital signal is created storing start time and length of overlap.
|
---|
128 | // They are collected in a newly allocated TObjArray. A pointer to this array
|
---|
129 | // is returned.
|
---|
130 | //
|
---|
131 | // The user gains owenership of the object, i.e., the user is responsible of
|
---|
132 | // deleting the memory.
|
---|
133 | //
|
---|
134 | TObjArray *MSimTrigger::CalcCoincidence(const TObjArray &arr1, const TObjArray &arr2/*, Float_t gate*/) const
|
---|
135 | {
|
---|
136 | TObjArray *res = new TObjArray;
|
---|
137 |
|
---|
138 | if (arr1.GetEntriesFast()==0 || arr2.GetEntriesFast()==0)
|
---|
139 | return res;
|
---|
140 |
|
---|
141 | TIter Next1(&arr1);
|
---|
142 | MDigitalSignal *ttl1 = 0;
|
---|
143 | while ((ttl1=static_cast<MDigitalSignal*>(Next1())))
|
---|
144 | {
|
---|
145 | TIter Next2(&arr2);
|
---|
146 | MDigitalSignal *ttl2 = 0;
|
---|
147 | while ((ttl2=static_cast<MDigitalSignal*>(Next2())))
|
---|
148 | {
|
---|
149 | MDigitalSignal *ttl = new MDigitalSignal(*ttl1, *ttl2);
|
---|
150 | /*
|
---|
151 | if (ttl->GetLength()<=gate)
|
---|
152 | {
|
---|
153 | delete ttl;
|
---|
154 | continue;
|
---|
155 | }
|
---|
156 | */
|
---|
157 | res->Add(ttl);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | res->SetOwner();
|
---|
162 |
|
---|
163 | return res;
|
---|
164 | }
|
---|
165 |
|
---|
166 | class Edge : public TObject
|
---|
167 | {
|
---|
168 | private:
|
---|
169 | Double_t fEdge;
|
---|
170 | Int_t fRising;
|
---|
171 |
|
---|
172 | public:
|
---|
173 | Edge(Double_t t, Int_t rising) : fEdge(t), fRising(rising) { }
|
---|
174 | Bool_t IsSortable() const { return kTRUE; }
|
---|
175 | Int_t Compare(const TObject *o) const { const Edge *e = static_cast<const Edge*>(o); if (e->fEdge<fEdge) return 1; if (e->fEdge>fEdge) return -1; return 0; }
|
---|
176 |
|
---|
177 | Int_t IsRising() const { return fRising; }
|
---|
178 | Double_t GetEdge() const { return fEdge; }
|
---|
179 | };
|
---|
180 |
|
---|
181 | // --------------------------------------------------------------------------
|
---|
182 | //
|
---|
183 | // Calculate a multiplicity trigger on the given array(s). The idx-array
|
---|
184 | // conatins all channels which should be checked for coincidences
|
---|
185 | // and the ttls array conatins the arrays with the digital signals.
|
---|
186 | //
|
---|
187 | // For the windows in which more or euqal than threshold channels have
|
---|
188 | // a high signal a new MDigitalSignal is created. newly allocated
|
---|
189 | // array with a collection of these trigger signals is returned.
|
---|
190 | //
|
---|
191 | TObjArray *MSimTrigger::CalcMinMultiplicity(const MArrayI &idx, const TObjArray &ttls, Int_t threshold) const
|
---|
192 | {
|
---|
193 | // Create a new array for the rising and falling edges of the signals
|
---|
194 | TObjArray times;
|
---|
195 | times.SetOwner();
|
---|
196 |
|
---|
197 | // Fill the array with edges from all digital signals of all our channels
|
---|
198 | for (UInt_t k=0; k<idx.GetSize(); k++)
|
---|
199 | {
|
---|
200 | TObjArray *arr = static_cast<TObjArray*>(ttls[idx[k]]);
|
---|
201 |
|
---|
202 | TIter Next(arr);
|
---|
203 | MDigitalSignal *ttl = 0;
|
---|
204 | while ((ttl=static_cast<MDigitalSignal*>(Next())))
|
---|
205 | {
|
---|
206 | times.Add(new Edge(ttl->GetStart(), 1));
|
---|
207 | times.Add(new Edge(ttl->GetEnd(), -1));
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | // Sort them in time
|
---|
212 | times.Sort();
|
---|
213 |
|
---|
214 | // Start with no channel active
|
---|
215 | Int_t lvl = 0;
|
---|
216 |
|
---|
217 | TObjArray *res = new TObjArray;
|
---|
218 | res->SetOwner();
|
---|
219 |
|
---|
220 | // First remove all edges which do not change the status
|
---|
221 | // "below threshold" or "above threshold"
|
---|
222 | for (int i=0; i<times.GetEntriesFast(); i++)
|
---|
223 | {
|
---|
224 | // Get i-th edge
|
---|
225 | const Edge &e = *static_cast<Edge*>(times.UncheckedAt(i));
|
---|
226 |
|
---|
227 | // Claculate what the number of active channels after the edge is
|
---|
228 | const Int_t lvl1 = lvl + e.IsRising();
|
---|
229 |
|
---|
230 | // Remove edge if number of active channels before and after the
|
---|
231 | // edge lower is lower than the threshold or higher than
|
---|
232 | // the threshold
|
---|
233 | if (lvl+1<threshold || lvl-1>=threshold)
|
---|
234 | delete times.RemoveAt(i);
|
---|
235 |
|
---|
236 | // keep the (now) "previous" level
|
---|
237 | lvl = lvl1<0 ? 0 : lvl1;
|
---|
238 | }
|
---|
239 |
|
---|
240 | // Remove the empty slots from the array
|
---|
241 | times.Compress();
|
---|
242 |
|
---|
243 | //
|
---|
244 | for (int i=0; i<times.GetEntriesFast()-1; i++)
|
---|
245 | {
|
---|
246 | // get the current edge
|
---|
247 | const Edge &e0 = *static_cast<Edge*>(times.UncheckedAt(i));
|
---|
248 |
|
---|
249 | // go ahead if this is a falling edge
|
---|
250 | if (e0.IsRising()!=1)
|
---|
251 | continue;
|
---|
252 |
|
---|
253 | // get the following edge (must be a falling edge now)
|
---|
254 | const Edge &e1 = *static_cast<Edge*>(times.UncheckedAt(i+1));
|
---|
255 |
|
---|
256 | // calculate the length of the digital signal
|
---|
257 | const Double_t len = e1.GetEdge()-e0.GetEdge();
|
---|
258 |
|
---|
259 | // Create a digital trigger signal
|
---|
260 | MDigitalSignal *ds = new MDigitalSignal(e0.GetEdge(), len);
|
---|
261 | //ds->SetIndex(lvl);
|
---|
262 | res->Add(ds);
|
---|
263 | }
|
---|
264 |
|
---|
265 | return res;
|
---|
266 | }
|
---|
267 |
|
---|
268 | // --------------------------------------------------------------------------
|
---|
269 | //
|
---|
270 | // Check for the necessary parameter containers. Read the luts.
|
---|
271 | //
|
---|
272 | Int_t MSimTrigger::PreProcess(MParList *pList)
|
---|
273 | {
|
---|
274 | fTrigger = (MParameterD*)pList->FindCreateObj("MParameterD", "TriggerPos");
|
---|
275 | if (!fTrigger)
|
---|
276 | return kFALSE;
|
---|
277 |
|
---|
278 | fPulsePos = (MParameterD*)pList->FindObject("IntendedPulsePos", "MParameterD");
|
---|
279 | if (!fPulsePos)
|
---|
280 | {
|
---|
281 | *fLog << err << "IntendedPulsePos [MParameterD] not found... aborting." << endl;
|
---|
282 | return kFALSE;
|
---|
283 | }
|
---|
284 |
|
---|
285 | fCamera = (MAnalogChannels*)pList->FindObject("MAnalogChannels");
|
---|
286 | if (!fCamera)
|
---|
287 | {
|
---|
288 | *fLog << err << "MAnalogChannels not found... aborting." << endl;
|
---|
289 | return kFALSE;
|
---|
290 | }
|
---|
291 |
|
---|
292 | fRunHeader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
|
---|
293 | if (!fRunHeader)
|
---|
294 | {
|
---|
295 | *fLog << err << "MRawRunHeader not found... aborting." << endl;
|
---|
296 | return kFALSE;
|
---|
297 | }
|
---|
298 |
|
---|
299 | fEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader");
|
---|
300 | if (!fEvtHeader)
|
---|
301 | return kFALSE;
|
---|
302 |
|
---|
303 | if (!fSimulateElectronics && !fDebugTrigger)
|
---|
304 | {
|
---|
305 | *fLog << inf << "Simulation of electronics switched off... first photon will trigger." << endl;
|
---|
306 | return kTRUE;
|
---|
307 | }
|
---|
308 |
|
---|
309 | fElectronicNoise = 0;
|
---|
310 | if (fShiftBaseline)
|
---|
311 | {
|
---|
312 | fElectronicNoise = (MPedestalCam*)pList->FindObject("ElectronicNoise", "MPedestalCam");
|
---|
313 | if (!fElectronicNoise)
|
---|
314 | {
|
---|
315 | *fLog << err << "ElectronicNoise [MPedestalCam] not found... aborting." << endl;
|
---|
316 | return kFALSE;
|
---|
317 | }
|
---|
318 | *fLog << inf << "Baseline will be shifted back to 0 for discriminator." << endl;
|
---|
319 | }
|
---|
320 |
|
---|
321 | fGain = 0;
|
---|
322 | if (fUngainSignal)
|
---|
323 | {
|
---|
324 | fGain = (MPedestalCam*)pList->FindObject("Gain", "MPedestalCam");
|
---|
325 | if (!fGain)
|
---|
326 | {
|
---|
327 | *fLog << err << "Gain [MPedestalCam] not found... aborting." << endl;
|
---|
328 | return kFALSE;
|
---|
329 | }
|
---|
330 | *fLog << inf << "Discriminator will be multiplied by applied gain." << endl;
|
---|
331 | }
|
---|
332 |
|
---|
333 | fRouteAC.Delete();
|
---|
334 | if (!fNameRouteAC.IsNull() && fRouteAC.ReadFile(fNameRouteAC)<0)
|
---|
335 | return kFALSE;
|
---|
336 |
|
---|
337 | fCoincidenceMap.Delete();
|
---|
338 | if (!fNameCoincidenceMap.IsNull() && fCoincidenceMap.ReadFile(fNameCoincidenceMap)<0)
|
---|
339 | return kFALSE;
|
---|
340 |
|
---|
341 | // ---------------- Consistency checks ----------------------
|
---|
342 |
|
---|
343 | if (!fRouteAC.IsEmpty() && !fCoincidenceMap.IsEmpty() &&
|
---|
344 | fCoincidenceMap.GetMaxIndex()>fRouteAC.GetNumRows()-1)
|
---|
345 | {
|
---|
346 | *fLog << err;
|
---|
347 | *fLog << "ERROR - AC routing produces " << fRouteAC.GetNumRows() << " analog channels," << endl;
|
---|
348 | *fLog << " but the coincidence map expects at least " << fCoincidenceMap.GetMaxIndex()+1 << " channels." << endl;
|
---|
349 | return kERROR;
|
---|
350 | }
|
---|
351 |
|
---|
352 | // if (fDiscriminatorThreshold<=0)
|
---|
353 | // {
|
---|
354 | // *fLog << err << "ERROR - Discriminator threshold " << fDiscriminatorThreshold << " invalid." << endl;
|
---|
355 | // return kFALSE;
|
---|
356 | // }
|
---|
357 |
|
---|
358 | if (fElectronicNoise && !fRouteAC.IsEmpty() && !fRouteAC.IsDefaultCol())
|
---|
359 | {
|
---|
360 | // FIXME: Apply to analog channels when summing
|
---|
361 | *fLog << warn << "WARNING - A baseline shift doesn't make sense for sum-channels... reset." << endl;
|
---|
362 | fElectronicNoise = 0;
|
---|
363 | }
|
---|
364 |
|
---|
365 | if (fGain && !fRouteAC.IsEmpty() && !fRouteAC.IsDefaultCol())
|
---|
366 | {
|
---|
367 | // FIXME: Apply to analog channels when summing
|
---|
368 | *fLog << warn << "WARNING - Ungain doesn't make sense for sum-channels... reset." << endl;
|
---|
369 | fGain = 0;
|
---|
370 | }
|
---|
371 |
|
---|
372 |
|
---|
373 | // ---------------- Information output ----------------------
|
---|
374 |
|
---|
375 | *fLog << inf;
|
---|
376 |
|
---|
377 | if (fRouteAC.IsEmpty())
|
---|
378 | *fLog << "Re-routing/summing of analog channels before discriminator switched off." << endl;
|
---|
379 | else
|
---|
380 | *fLog << "Using " << fNameRouteAC << " for re-routing/summing of analog channels before discriminator." << endl;
|
---|
381 |
|
---|
382 | if (fCoincidenceMap.IsEmpty() && fMinMultiplicity<=0)
|
---|
383 | *fLog << "No coincidences of digital channels will be checked. Signal-above-threshold trigger applied." << endl;
|
---|
384 | else
|
---|
385 | {
|
---|
386 | *fLog << "Using ";
|
---|
387 | if (fCoincidenceMap.IsEmpty())
|
---|
388 | *fLog << "the whole camera";
|
---|
389 | else
|
---|
390 | *fLog << "patterns from " << fNameCoincidenceMap;
|
---|
391 | *fLog << " to check for ";
|
---|
392 | if (fMinMultiplicity>0)
|
---|
393 | *fLog << fMinMultiplicity << " multiplicity." << endl;
|
---|
394 | else
|
---|
395 | *fLog << "coincidences of the digital channels." << endl;
|
---|
396 | }
|
---|
397 |
|
---|
398 | *fLog << "Using discriminator threshold of " << fDiscriminatorThreshold << endl;
|
---|
399 |
|
---|
400 | *fLog << "Using fCableDelay " << fCableDelay << " samples" << endl;
|
---|
401 | *fLog << "Using fCableDamping " << fCableDamping << endl;
|
---|
402 |
|
---|
403 | if (!fSimulateElectronics)
|
---|
404 | *fLog << inf << "Simulation of electronics switched off... first photon will trigger." << endl;
|
---|
405 |
|
---|
406 | ////////////////////////////////
|
---|
407 | // open some output files for debugging
|
---|
408 | // patch_file.open("/home/fact_opr/patch_file.csv", ios_base::out);
|
---|
409 | // clipped_file.open("/home/fact_opr/clipped_file.csv", ios_base::out);
|
---|
410 | // digital_file.open("/home/fact_opr/digital_file.csv", ios_base::out);
|
---|
411 | // ratescan_file.open("/home/fact_opr/ratescan_file.csv", ios_base::out);
|
---|
412 |
|
---|
413 |
|
---|
414 |
|
---|
415 | return kTRUE;
|
---|
416 | }
|
---|
417 |
|
---|
418 | /*
|
---|
419 | class MDigitalChannel : public TObjArray
|
---|
420 | {
|
---|
421 | private:
|
---|
422 | TObjArray fArray;
|
---|
423 |
|
---|
424 | public:
|
---|
425 | MDigitalSignal *GetSignal(UInt_t i) { return static_cast<MDigitalSignal*>(fArray[i]); }
|
---|
426 |
|
---|
427 | };
|
---|
428 | */
|
---|
429 |
|
---|
430 | #include "MCamEvent.h"
|
---|
431 | class MTriggerSignal : public MParContainer, public MCamEvent
|
---|
432 | {
|
---|
433 | private:
|
---|
434 | TObjArray fSignals;
|
---|
435 |
|
---|
436 | public:
|
---|
437 | MTriggerSignal() { fSignals.SetOwner(); }
|
---|
438 |
|
---|
439 | void Add(MDigitalSignal *signal) { fSignals.Add(signal); }
|
---|
440 |
|
---|
441 | MDigitalSignal *GetSignal(UInt_t i) { return static_cast<MDigitalSignal*>(fSignals[i]); }
|
---|
442 |
|
---|
443 | void Sort() { fSignals.Sort(); }
|
---|
444 |
|
---|
445 | Int_t GetNumSignals() const { return fSignals.GetEntriesFast(); }
|
---|
446 |
|
---|
447 | Float_t GetFirstTrigger() const
|
---|
448 | {
|
---|
449 | MDigitalSignal *sig = static_cast<MDigitalSignal*>(fSignals[0]);
|
---|
450 | return sig ? sig->GetStart() : -1;
|
---|
451 | }
|
---|
452 | Int_t GetFirstIndex() const
|
---|
453 | {
|
---|
454 | MDigitalSignal *sig = static_cast<MDigitalSignal*>(fSignals[0]);
|
---|
455 | return sig ? sig->GetIndex() : -1;
|
---|
456 | }
|
---|
457 | Bool_t GetPixelContent(Double_t&, Int_t, const MGeomCam&, Int_t) const
|
---|
458 | {
|
---|
459 | switch (1)
|
---|
460 | {
|
---|
461 | case 1: // yes/no
|
---|
462 | case 2: // first time
|
---|
463 | case 3: // length
|
---|
464 | case 4: // n
|
---|
465 | break;
|
---|
466 | }
|
---|
467 |
|
---|
468 | return kTRUE;
|
---|
469 | }
|
---|
470 | void DrawPixelContent(Int_t) const
|
---|
471 | {
|
---|
472 | }
|
---|
473 | };
|
---|
474 |
|
---|
475 |
|
---|
476 | void MSimTrigger::SetTrigger(Double_t pos, Int_t idx)
|
---|
477 | {
|
---|
478 | // FIXME: Jitter! (Own class?)
|
---|
479 | fTrigger->SetVal(pos);
|
---|
480 | fTrigger->SetReadyToSave();
|
---|
481 |
|
---|
482 | // Flag this event as triggered by the lvl1 trigger (FIXME?)
|
---|
483 | fEvtHeader->SetTriggerPattern(pos<0 ? 0 : 4);
|
---|
484 | fEvtHeader->SetNumTrigLvl1((UInt_t)idx);
|
---|
485 | fEvtHeader->SetReadyToSave();
|
---|
486 | }
|
---|
487 |
|
---|
488 | // --------------------------------------------------------------------------
|
---|
489 | //
|
---|
490 | Int_t MSimTrigger::Process()
|
---|
491 | {
|
---|
492 | // Invalidate trigger
|
---|
493 | //fTrigger->SetVal(-1);
|
---|
494 | // Calculate the minimum and maximum time for a valid trigger
|
---|
495 | const Double_t freq = fRunHeader->GetFreqSampling()/1000.;
|
---|
496 | const Float_t nsamp = fRunHeader->GetNumSamplesHiGain();
|
---|
497 |
|
---|
498 | // The trigger position in the readout window
|
---|
499 | // FIXME: This currently corresponds to the start of the spline!
|
---|
500 | const Float_t pulspos = fPulsePos->GetVal()*freq;
|
---|
501 |
|
---|
502 | // Valid range in units of bins for the trigger
|
---|
503 | //
|
---|
504 | // GetValidRangeMin/Max contains the earliest and latest
|
---|
505 | // reasonable sample. The earliest is determined by the
|
---|
506 | // the fact that at least a full pulse must fit in front.
|
---|
507 | // The latest ends with the full sampling range.
|
---|
508 | //
|
---|
509 | // As the first trigger can only be initiated by the first
|
---|
510 | // photon from the shower, the valid range for triggers
|
---|
511 | // starts the pulsewidth before the trigger position,
|
---|
512 | // i.e. ValidRangeMin+triggerposition
|
---|
513 | //
|
---|
514 | // If the last photon triggers, then there must at least
|
---|
515 | // be sampling window minus trigger position samples left.
|
---|
516 | // Therefore, the last trigger must be at
|
---|
517 | // ValidRangeMax-(window-triggerpositon)
|
---|
518 | const Float_t min = fCamera->GetValidRangeMin()+pulspos;
|
---|
519 | const Float_t max = fCamera->GetValidRangeMax()-(nsamp-pulspos);
|
---|
520 |
|
---|
521 | // Check if routing should be done
|
---|
522 | const Bool_t empty = fRouteAC.IsEmpty();
|
---|
523 |
|
---|
524 | if (!fSimulateElectronics && (empty || (!empty && !fDebugTrigger)))
|
---|
525 | {
|
---|
526 | SetTrigger(min, -1);
|
---|
527 | return kTRUE;
|
---|
528 | }
|
---|
529 |
|
---|
530 | // ================== Simulate channel bundling ====================
|
---|
531 |
|
---|
532 | // FIXME: Before we can bundle the channels we have to make a copy
|
---|
533 | // and simulate clipping
|
---|
534 |
|
---|
535 | // If no channels are summed the number of patches stays the same
|
---|
536 | const UInt_t npatch = empty ? fCamera->GetNumChannels() : fRouteAC.GetEntriesFast();
|
---|
537 |
|
---|
538 | // Use the given analog channels as default out. If channels are
|
---|
539 | // summed overwrite with a newly allocated set of analog channels
|
---|
540 | MAnalogChannels *patches = fCamera;
|
---|
541 | //MAnalogChannels *raw_patches = fCamera;
|
---|
542 | if (!empty)
|
---|
543 | {
|
---|
544 | // FIXME: Can we add gain and offset here into a new container?
|
---|
545 |
|
---|
546 | patches = new MAnalogChannels(npatch, fCamera->GetNumSamples());
|
---|
547 | //raw_patches = new MAnalogChannels(npatch, fCamera->GetNumSamples());
|
---|
548 | for (UInt_t patch_id=0; patch_id<npatch; patch_id++)
|
---|
549 | {
|
---|
550 | const MArrayI &row = fRouteAC.GetRow(patch_id);
|
---|
551 | for (UInt_t pxl_in_patch=0; pxl_in_patch<row.GetSize(); pxl_in_patch++)
|
---|
552 | {
|
---|
553 | const UInt_t pixel_id = row[pxl_in_patch];
|
---|
554 |
|
---|
555 | // FIXME: Shrinking the mapping table earlier (e.g.
|
---|
556 | // ReInit) would avoid a lot of if's
|
---|
557 | if (pixel_id<fCamera->GetNumChannels())
|
---|
558 | {
|
---|
559 | //(*raw_patches)[patch_id].AddSignal((*fCamera)[pixel_id]);
|
---|
560 | (*patches)[patch_id].AddSignal((*fCamera)[pixel_id]);
|
---|
561 | (*patches)[patch_id].AddSignal((*fCamera)[pixel_id], fCableDelay, fCableDamping);
|
---|
562 | }
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | if (fDebugTrigger)
|
---|
567 | {
|
---|
568 | for (UInt_t patch_id=0; patch_id<npatch; patch_id++)
|
---|
569 | {
|
---|
570 | const MArrayI &row = fRouteAC.GetRow(patch_id);
|
---|
571 | for (UInt_t pxl_in_patch=0; pxl_in_patch<row.GetSize(); pxl_in_patch++)
|
---|
572 | {
|
---|
573 | const UInt_t pixel_id = row[pxl_in_patch];
|
---|
574 | if (pixel_id<fCamera->GetNumChannels())
|
---|
575 | (*fCamera)[pixel_id].CopySignal((*patches)[patch_id]);
|
---|
576 | }
|
---|
577 | }
|
---|
578 | }
|
---|
579 | }
|
---|
580 |
|
---|
581 | if (!fSimulateElectronics)
|
---|
582 | {
|
---|
583 | SetTrigger(min, -1);
|
---|
584 | return kTRUE;
|
---|
585 | }
|
---|
586 |
|
---|
587 | // DN: 20140219 Ratescan:
|
---|
588 | //
|
---|
589 | //
|
---|
590 | // for (UInt_t patch_id=0; patch_id<npatch; patch_id++)
|
---|
591 | // {
|
---|
592 | // MAnalogSignal current_patch = (*raw_patches)[patch_id];
|
---|
593 | // float max = current_patch[0];
|
---|
594 | // for (UInt_t i=1; i<current_patch.GetSize(); i++)
|
---|
595 | // {
|
---|
596 | // if (current_patch[i] > max)
|
---|
597 | // {
|
---|
598 | // max = current_patch[i];
|
---|
599 | // }
|
---|
600 | // }
|
---|
601 | // ratescan_file << max << " ";
|
---|
602 | // }
|
---|
603 | // ratescan_file << endl;
|
---|
604 |
|
---|
605 | // // DN 20131108: DEBUGGING:
|
---|
606 | // for (UInt_t patch_id=0; patch_id<npatch; patch_id++)
|
---|
607 | // {
|
---|
608 | // for (UInt_t slice=0; slice<fCamera->GetNumSamples(); slice++)
|
---|
609 | // {
|
---|
610 | // patch_file << (*raw_patches)[patch_id][slice] << "\t";
|
---|
611 | // clipped_file << (*patches)[patch_id][slice] << "\t";
|
---|
612 | // }
|
---|
613 | // patch_file << endl;
|
---|
614 | // clipped_file << endl;
|
---|
615 | // }
|
---|
616 |
|
---|
617 |
|
---|
618 |
|
---|
619 | // FIXME: Write patches
|
---|
620 |
|
---|
621 | // ================== Simulate discriminators ====================
|
---|
622 |
|
---|
623 | TObjArray ttls(npatch);
|
---|
624 | ttls.SetOwner();
|
---|
625 |
|
---|
626 | for (UInt_t i=0; i<npatch; i++)
|
---|
627 | {
|
---|
628 | // FIXME: What if the gain was also allpied to the baseline?
|
---|
629 | const Double_t offset = fElectronicNoise ? (*fElectronicNoise)[i].GetPedestal() : 0;
|
---|
630 | const Double_t gain = fGain ? (*fGain)[i].GetPedestal() : 1;
|
---|
631 | // FIXME: fCableDelay not taken into account when calculating the
|
---|
632 | // valid range, therefore, fCableDelay must be smaller than the pulse width
|
---|
633 | // FIXME: Start seraching at pulsepos is faster, but breaks things like
|
---|
634 | // ratescans triggering on noise events
|
---|
635 | // We start one sample after cable delay as the spline knots up to cable delay
|
---|
636 | // are still affected by the none simulation of the cable delay
|
---|
637 | ttls.AddAt(
|
---|
638 | (*patches)[i].Discriminate(
|
---|
639 | fDiscriminatorThreshold*gain+offset, // treshold
|
---|
640 | Double_t(fCableDelay)+1, // start
|
---|
641 | Double_t(fCamera->GetNumSamples() - fCableDelay), // end
|
---|
642 | //fDigitalSignalLength // time-over-threshold, or fixed-length?
|
---|
643 | -1 // -1 = time-over-threshold
|
---|
644 | ),
|
---|
645 | i
|
---|
646 | );
|
---|
647 | }
|
---|
648 |
|
---|
649 | // FIXME: Write TTLs!
|
---|
650 |
|
---|
651 | // If analog channels had been newly allocated free memmory
|
---|
652 | if (patches!=fCamera)
|
---|
653 | delete patches;
|
---|
654 | //if (raw_patches!=fCamera)
|
---|
655 | // delete raw_patches;
|
---|
656 |
|
---|
657 | // =================== Simulate coincidences ======================
|
---|
658 |
|
---|
659 | // If the map is empty we create a one-pixel-coincidence map
|
---|
660 | // FIMXE: This could maybe be accelerated if the Clone can be
|
---|
661 | // omitted in the loop
|
---|
662 | if (fCoincidenceMap.IsEmpty())
|
---|
663 | {
|
---|
664 | if (fMinMultiplicity>0)
|
---|
665 | fCoincidenceMap.SetDefaultRow(npatch);
|
---|
666 | else
|
---|
667 | fCoincidenceMap.SetDefaultCol(npatch);
|
---|
668 | }
|
---|
669 |
|
---|
670 | // Create an array for the individual triggers
|
---|
671 | MTriggerSignal triggers;
|
---|
672 |
|
---|
673 | Int_t cnt = 0;
|
---|
674 | Int_t rmlo = 0;
|
---|
675 | Int_t rmhi = 0;
|
---|
676 |
|
---|
677 | // cout << "MSimTrigger::fMinMultiplicity = " << fMinMultiplicity << endl;
|
---|
678 | // cout << "MSimTrigger::fCoincidenceTime = " << fCoincidenceTime << endl;
|
---|
679 | // cout << "fCoincidenceMap.GetEntries() = " << fCoincidenceMap.GetEntries() << endl;
|
---|
680 | // cout << "MSimTrigger::fCableDelay = " << fCableDelay << endl;
|
---|
681 | // cout << "MSimTrigger::fCableDamping = " << fCableDamping << endl;
|
---|
682 | // cout << "min:" << min << endl;
|
---|
683 | // cout << "max:" << max << endl;
|
---|
684 |
|
---|
685 | for (int j=0; j<fCoincidenceMap.GetEntries(); j++)
|
---|
686 | {
|
---|
687 | const MArrayI &idx = fCoincidenceMap.GetRow(j);
|
---|
688 |
|
---|
689 | TObjArray *arr = 0;
|
---|
690 |
|
---|
691 | if (fMinMultiplicity>0)
|
---|
692 | {
|
---|
693 | arr = CalcMinMultiplicity(idx, ttls, fMinMultiplicity);
|
---|
694 | }
|
---|
695 | else
|
---|
696 | {
|
---|
697 | arr = CalcMinMultiplicity(idx, ttls, idx.GetSize());
|
---|
698 | /*
|
---|
699 | // Start with a copy of the first coincidence channel
|
---|
700 | arr = static_cast<TObjArray*>(ttls[idx[0]]->Clone());
|
---|
701 | arr->SetOwner();
|
---|
702 |
|
---|
703 | // compare to all other channels in this coincidence patch, one by one
|
---|
704 | for (UInt_t k=1; k<idx.GetSize() && arr->GetEntriesFast()>0; k++)
|
---|
705 | {
|
---|
706 | TObjArray *res = CalcCoincidence(*arr, *static_cast<TObjArray*>(ttls[idx[k]]));//, fCoincidenceTime);
|
---|
707 |
|
---|
708 | // Delete the original array and keep the new one
|
---|
709 | delete arr;
|
---|
710 | arr = res;
|
---|
711 | }*/
|
---|
712 | }
|
---|
713 |
|
---|
714 | // Count the number of totally emitted coincidence signals
|
---|
715 | cnt += arr->GetEntriesFast();
|
---|
716 |
|
---|
717 | // Remove all signals which are not in the valid digitization range
|
---|
718 | // (This is not the digitization window, but the region in which
|
---|
719 | // the analog channels contain usefull data)
|
---|
720 | // and which are shorter than the defined coincidence gate.
|
---|
721 | TIter Next(arr);
|
---|
722 | MDigitalSignal *ttl = 0;
|
---|
723 | while ((ttl=static_cast<MDigitalSignal*>(Next())))
|
---|
724 | {
|
---|
725 | if (ttl->GetLength()<fCoincidenceTime)
|
---|
726 | {
|
---|
727 | delete arr->Remove(ttl);
|
---|
728 | continue;
|
---|
729 | }
|
---|
730 |
|
---|
731 | if (ttl->GetStart()<min)
|
---|
732 | {
|
---|
733 | delete arr->Remove(ttl);
|
---|
734 | rmlo++;
|
---|
735 | continue;
|
---|
736 | }
|
---|
737 | if (ttl->GetStart()>max)
|
---|
738 | {
|
---|
739 | delete arr->Remove(ttl);
|
---|
740 | rmhi++;
|
---|
741 | continue;
|
---|
742 | }
|
---|
743 |
|
---|
744 | // Set trigger-channel index to keep this information
|
---|
745 | //ttl->SetIndex(j);
|
---|
746 | }
|
---|
747 |
|
---|
748 | // Remove the empty slots
|
---|
749 | arr->Compress();
|
---|
750 |
|
---|
751 | // cout << "ttls(j=" << j << "):";
|
---|
752 | // TObjArray *arr2 = static_cast<TObjArray*>(ttls[j]);
|
---|
753 | // TIter Nexty(arr);
|
---|
754 | // MDigitalSignal *ttly = 0;
|
---|
755 | // while ((ttly=static_cast<MDigitalSignal*>(Nexty())))
|
---|
756 | // {
|
---|
757 | // cout << "|"<< ttly->GetStart() << ", " << ttly->GetLength();
|
---|
758 | // }
|
---|
759 | // cout << endl;
|
---|
760 |
|
---|
761 |
|
---|
762 | // If we have at least one trigger keep the earliest one.
|
---|
763 | // FIXME: The triggers might be ordered in time automatically:
|
---|
764 | // To be checked!
|
---|
765 | // FIXME: Simulate trigger dead-time!
|
---|
766 | if (arr->GetEntriesFast()>0)
|
---|
767 | {
|
---|
768 | ttl = static_cast<MDigitalSignal*>(arr->RemoveAt(0));
|
---|
769 | // Set trigger-channel index to keep this information
|
---|
770 | ttl->SetIndex(j);
|
---|
771 | triggers.Add(ttl);
|
---|
772 | }
|
---|
773 |
|
---|
774 | // delete the allocated space
|
---|
775 | delete arr;
|
---|
776 | }
|
---|
777 |
|
---|
778 | // There are usually not enough entries that it is worth to search
|
---|
779 | // for the earliest instead of just sorting and taking the first one
|
---|
780 | // FIXME: This could be improved if checking for IsSortable
|
---|
781 | // is omitted
|
---|
782 | triggers.Sort();
|
---|
783 | // FIXME: Store triggers! (+ Reversed pixels?)
|
---|
784 |
|
---|
785 | // Shift the trigger such that the pulse position X=0 coincides with the
|
---|
786 | // the trigger position in the readout window
|
---|
787 | SetTrigger(triggers.GetFirstTrigger()/freq, triggers.GetFirstIndex());
|
---|
788 |
|
---|
789 | // No trigger issued. Go on.
|
---|
790 | if (triggers.GetNumSignals()==0)
|
---|
791 | {
|
---|
792 | if (rmlo>0 || rmhi>0)
|
---|
793 | *fLog << inf2 << GetNumExecutions() << ": " << rmlo << "/" << rmhi << " trigger out of valid range. No trigger raised." << endl;
|
---|
794 | return kTRUE;
|
---|
795 | }
|
---|
796 |
|
---|
797 | // Number of patches which have triggered out of the total number of
|
---|
798 | // Coincidence signals emitted. (If the total number is higher than
|
---|
799 | // the number of triggers either some triggers had to be removed or
|
---|
800 | // a patch has emitted more than one trigger signal)
|
---|
801 | // FIXME: inf2?
|
---|
802 | *fLog << inf << GetNumExecutions() << ": ";
|
---|
803 | *fLog << setw(3) << triggers.GetNumSignals() << " triggers left out of ";
|
---|
804 | *fLog << setw(3) << cnt << " (" << rmlo << "/" << rmhi << " trigger out of valid range), T=" << fTrigger->GetVal()-fCamera->GetValidRangeMin()/freq << "ns";
|
---|
805 | *fLog << endl;
|
---|
806 |
|
---|
807 | //# Trigger characteristics: gate length (ns), min. overlapping time (ns),
|
---|
808 | //# amplitude and FWHM of (gaussian) single phe response for trigger:
|
---|
809 | //trigger_prop 3.0 0.25 1.0 2.0
|
---|
810 |
|
---|
811 | return kTRUE;
|
---|
812 | }
|
---|
813 |
|
---|
814 | Int_t MSimTrigger::PostProcess()
|
---|
815 | {
|
---|
816 | // patch_file.close();
|
---|
817 | // clipped_file.close();
|
---|
818 | // digital_file.close();
|
---|
819 | // ratescan_file.close();
|
---|
820 | return kTRUE;
|
---|
821 | }
|
---|
822 |
|
---|
823 |
|
---|
824 | // --------------------------------------------------------------------------
|
---|
825 | //
|
---|
826 | // FileNameRouteac: routeac.txt
|
---|
827 | // FileNameCoincidenceMap: coincidence.txt
|
---|
828 | // DiscriminatorTheshold: 3.5
|
---|
829 | // DigitalSignalLength: 8
|
---|
830 | // CoincidenceTime: 0.5
|
---|
831 | // SimulateElectronics: Yes
|
---|
832 | //
|
---|
833 | Int_t MSimTrigger::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
834 | {
|
---|
835 | Bool_t rc = kFALSE;
|
---|
836 | if (IsEnvDefined(env, prefix, "FileNameRouteAC", print))
|
---|
837 | {
|
---|
838 | rc = kTRUE;
|
---|
839 | fNameRouteAC = GetEnvValue(env, prefix, "FileNameRouteAC", fNameRouteAC);
|
---|
840 | }
|
---|
841 |
|
---|
842 | if (IsEnvDefined(env, prefix, "FileNameCoincidenceMap", print))
|
---|
843 | {
|
---|
844 | rc = kTRUE;
|
---|
845 | fNameCoincidenceMap = GetEnvValue(env, prefix, "FileNameCoincidenceMap", fNameCoincidenceMap);
|
---|
846 | }
|
---|
847 |
|
---|
848 | if (IsEnvDefined(env, prefix, "DiscriminatorThreshold", print))
|
---|
849 | {
|
---|
850 | rc = kTRUE;
|
---|
851 | fDiscriminatorThreshold = GetEnvValue(env, prefix, "DiscriminatorThreshold", fDiscriminatorThreshold);
|
---|
852 | }
|
---|
853 |
|
---|
854 | if (IsEnvDefined(env, prefix, "DigitalSignalLength", print))
|
---|
855 | {
|
---|
856 | rc = kTRUE;
|
---|
857 | fDigitalSignalLength = GetEnvValue(env, prefix, "DigitalSignalLength", fDigitalSignalLength);
|
---|
858 | }
|
---|
859 |
|
---|
860 | if (IsEnvDefined(env, prefix, "CoincidenceTime", print))
|
---|
861 | {
|
---|
862 | rc = kTRUE;
|
---|
863 | fCoincidenceTime = GetEnvValue(env, prefix, "CoincidenceTime", fCoincidenceTime);
|
---|
864 | }
|
---|
865 |
|
---|
866 | if (IsEnvDefined(env, prefix, "SimulateElectronics", print))
|
---|
867 | {
|
---|
868 | rc = kTRUE;
|
---|
869 | fSimulateElectronics = GetEnvValue(env, prefix, "SimulateElectronics", fSimulateElectronics);
|
---|
870 | }
|
---|
871 |
|
---|
872 | if (IsEnvDefined(env, prefix, "MinMultiplicity", print))
|
---|
873 | {
|
---|
874 | rc = kTRUE;
|
---|
875 | fMinMultiplicity = GetEnvValue(env, prefix, "MinMultiplicity", fMinMultiplicity);
|
---|
876 | }
|
---|
877 |
|
---|
878 | if (IsEnvDefined(env, prefix, "CableDelay", print))
|
---|
879 | {
|
---|
880 | rc = kTRUE;
|
---|
881 | fCableDelay = GetEnvValue(env, prefix, "CableDelay", fCableDelay);
|
---|
882 | }
|
---|
883 |
|
---|
884 | if (IsEnvDefined(env, prefix, "CableDamping", print))
|
---|
885 | {
|
---|
886 | rc = kTRUE;
|
---|
887 | fCableDamping = GetEnvValue(env, prefix, "CableDamping", fCableDamping);
|
---|
888 | }
|
---|
889 |
|
---|
890 | if (IsEnvDefined(env, prefix, "DebugTrigger", print))
|
---|
891 | {
|
---|
892 | rc = kTRUE;
|
---|
893 | fDebugTrigger = GetEnvValue(env, prefix, "DebugTrigger", fDebugTrigger);
|
---|
894 | }
|
---|
895 |
|
---|
896 | return rc;
|
---|
897 | }
|
---|