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 | // With a second look-up table fCoincidenceMap the analog channels are
|
---|
39 | // checked for coincidences. The earliest coincide is then stored as
|
---|
40 | // trigger position.
|
---|
41 | //
|
---|
42 | //
|
---|
43 | // Input Containers:
|
---|
44 | // IntendedPulsePos [MParameterD]
|
---|
45 | // MAnalogChannels
|
---|
46 | // MRawRunHeader
|
---|
47 | //
|
---|
48 | // Output Containers:
|
---|
49 | // TriggerPos [MParameterD]
|
---|
50 | // MRawEvtHeader
|
---|
51 | //
|
---|
52 | //////////////////////////////////////////////////////////////////////////////
|
---|
53 | #include "MSimTrigger.h"
|
---|
54 |
|
---|
55 | #include "MLog.h"
|
---|
56 | #include "MLogManip.h"
|
---|
57 |
|
---|
58 | #include "MParList.h"
|
---|
59 | #include "MParameters.h"
|
---|
60 |
|
---|
61 | #include "MLut.h"
|
---|
62 | #include "MArrayI.h"
|
---|
63 |
|
---|
64 | #include "MRawEvtHeader.h"
|
---|
65 | #include "MRawRunHeader.h"
|
---|
66 |
|
---|
67 | #include "MAnalogSignal.h"
|
---|
68 | #include "MAnalogChannels.h"
|
---|
69 | #include "MDigitalSignal.h"
|
---|
70 |
|
---|
71 | ClassImp(MSimTrigger);
|
---|
72 |
|
---|
73 | using namespace std;
|
---|
74 |
|
---|
75 | // --------------------------------------------------------------------------
|
---|
76 | //
|
---|
77 | // Default Constructor.
|
---|
78 | //
|
---|
79 | MSimTrigger::MSimTrigger(const char *name, const char *title)
|
---|
80 | : fCamera(0), fPulsePos(0), fTrigger(0), fRunHeader(0), fEvtHeader(0),
|
---|
81 | fDiscriminatorThreshold(-1)
|
---|
82 | {
|
---|
83 | fName = name ? name : "MSimTrigger";
|
---|
84 | fTitle = title ? title : "Task to simulate trigger electronics";
|
---|
85 | }
|
---|
86 |
|
---|
87 | // --------------------------------------------------------------------------
|
---|
88 | //
|
---|
89 | // Take two TObjArrays with a collection of digital signals.
|
---|
90 | // Every signal from one array is compared with any from the other array.
|
---|
91 | // For all signals whihc overlaps and which have an overlap time >gate
|
---|
92 | // a new digital signal is created storing start time and length of overlap.
|
---|
93 | // They are collected in a newly allocated TObjArray. A pointer to this array
|
---|
94 | // is returned.
|
---|
95 | //
|
---|
96 | // Th euser gains owenership of the object, ie.e., the user is responsible of
|
---|
97 | // deleting the memory.
|
---|
98 | //
|
---|
99 | TObjArray *MSimTrigger::CalcCoincidence(const TObjArray &arr1, const TObjArray &arr2, Float_t gate) const
|
---|
100 | {
|
---|
101 | TObjArray *res = new TObjArray;
|
---|
102 |
|
---|
103 | if (arr1.GetEntriesFast()==0 || arr2.GetEntriesFast()==0)
|
---|
104 | return res;
|
---|
105 |
|
---|
106 | TIter Next1(&arr1);
|
---|
107 | MDigitalSignal *ttl1 = 0;
|
---|
108 | while ((ttl1=static_cast<MDigitalSignal*>(Next1())))
|
---|
109 | {
|
---|
110 | TIter Next2(&arr2);
|
---|
111 | MDigitalSignal *ttl2 = 0;
|
---|
112 | while ((ttl2=static_cast<MDigitalSignal*>(Next2())))
|
---|
113 | {
|
---|
114 | MDigitalSignal *ttl = new MDigitalSignal(*ttl1, *ttl2);
|
---|
115 |
|
---|
116 | if (ttl->GetLength()<=gate)
|
---|
117 | {
|
---|
118 | delete ttl;
|
---|
119 | continue;
|
---|
120 | }
|
---|
121 |
|
---|
122 | res->Add(ttl);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | res->SetOwner();
|
---|
127 |
|
---|
128 | return res;
|
---|
129 | }
|
---|
130 |
|
---|
131 | // --------------------------------------------------------------------------
|
---|
132 | //
|
---|
133 | // Check for the necessary parameter containers. Read the luts.
|
---|
134 | //
|
---|
135 | Int_t MSimTrigger::PreProcess(MParList *pList)
|
---|
136 | {
|
---|
137 | fTrigger = (MParameterD*)pList->FindCreateObj("MParameterD", "TriggerPos");
|
---|
138 | if (!fTrigger)
|
---|
139 | return kFALSE;
|
---|
140 |
|
---|
141 | fPulsePos = (MParameterD*)pList->FindObject("IntendedPulsePos", "MParameterD");
|
---|
142 | if (!fPulsePos)
|
---|
143 | {
|
---|
144 | *fLog << err << "IntendedPulsePos [MParameterD] not found... aborting." << endl;
|
---|
145 | return kFALSE;
|
---|
146 | }
|
---|
147 |
|
---|
148 | fCamera = (MAnalogChannels*)pList->FindObject("MAnalogChannels");
|
---|
149 | if (!fCamera)
|
---|
150 | {
|
---|
151 | *fLog << err << "MAnalogChannels not found... aborting." << endl;
|
---|
152 | return kFALSE;
|
---|
153 | }
|
---|
154 |
|
---|
155 | fRunHeader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
|
---|
156 | if (!fRunHeader)
|
---|
157 | {
|
---|
158 | *fLog << err << "MRawRunHeader not found... aborting." << endl;
|
---|
159 | return kFALSE;
|
---|
160 | }
|
---|
161 |
|
---|
162 | fEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader");
|
---|
163 | if (!fEvtHeader)
|
---|
164 | return kFALSE;
|
---|
165 |
|
---|
166 |
|
---|
167 | if (fRouteAC.ReadFile(fNameRouteAC)<0)
|
---|
168 | return kFALSE;
|
---|
169 |
|
---|
170 | if (fCoincidenceMap.ReadFile(fNameCoincidenceMap)<0)
|
---|
171 | return kFALSE;
|
---|
172 |
|
---|
173 | if (fDiscriminatorThreshold<=0)
|
---|
174 | {
|
---|
175 | *fLog << err << "ERROR - Discriminator threshold " << fDiscriminatorThreshold << " invalid." << endl;
|
---|
176 | return kFALSE;
|
---|
177 | }
|
---|
178 |
|
---|
179 | *fLog << inf << "Using discriminator threshold of " << fDiscriminatorThreshold << endl;
|
---|
180 |
|
---|
181 | return kTRUE;
|
---|
182 | }
|
---|
183 |
|
---|
184 | // --------------------------------------------------------------------------
|
---|
185 | //
|
---|
186 | Int_t MSimTrigger::Process()
|
---|
187 | {
|
---|
188 | // Invalidate trigger
|
---|
189 | fTrigger->SetVal(-1);
|
---|
190 |
|
---|
191 | // FIXME: Set from somewhere else? (see also MSimCalibrationSignal)
|
---|
192 | fEvtHeader->SetDAQEvtNumber(GetNumExecutions());
|
---|
193 | fEvtHeader->SetReadyToSave();
|
---|
194 |
|
---|
195 | // ================== Simulate channel bundling ====================
|
---|
196 |
|
---|
197 | const UInt_t npatch = fRouteAC.GetEntriesFast();
|
---|
198 |
|
---|
199 | MAnalogChannels patches(npatch, fCamera->GetNumSamples());
|
---|
200 |
|
---|
201 | for (UInt_t i=0; i<npatch; i++)
|
---|
202 | {
|
---|
203 | const MArrayI &row = fRouteAC.GetRow(i);
|
---|
204 | for (UInt_t j=0; j<row.GetSize(); j++)
|
---|
205 | {
|
---|
206 | const UInt_t idx = row[j];
|
---|
207 | patches[i].AddSignal((*fCamera)[idx]);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | // FIXME: Write patches
|
---|
212 |
|
---|
213 | // ================== Simulate discriminators ====================
|
---|
214 |
|
---|
215 | TObjArray ttls(npatch);
|
---|
216 | ttls.SetOwner();
|
---|
217 |
|
---|
218 | // MAGIC: Constant length of signals between 6ns and 12ns
|
---|
219 | const Double_t siglen = 8; // Signal length (-1: As long as Signal-above-Threshold)
|
---|
220 |
|
---|
221 | for (UInt_t i=0; i<npatch; i++)
|
---|
222 | ttls.AddAt(patches[i].Discriminate(fDiscriminatorThreshold, siglen), i);
|
---|
223 |
|
---|
224 | // FIXME: Write TTLs!
|
---|
225 |
|
---|
226 | // =================== Simulate coincidences ======================
|
---|
227 |
|
---|
228 | // Calculate the minimum and maximum time for a valid trigger
|
---|
229 | const Double_t freq = fRunHeader->GetFreqSampling()/1000.;
|
---|
230 | const Float_t nsamp = fRunHeader->GetNumSamplesHiGain();
|
---|
231 | const Float_t pulspos = fPulsePos->GetVal()/freq;
|
---|
232 |
|
---|
233 | const Float_t min = fCamera->GetValidRangeMin()+pulspos;
|
---|
234 | const Float_t max = fCamera->GetValidRangeMax()+pulspos-nsamp;
|
---|
235 |
|
---|
236 | // Define gate time (minimum coincidence time)
|
---|
237 | const Double_t gate = 1; // MAGIC: minimum coincidence time 0.25ns to 1ns,
|
---|
238 |
|
---|
239 | // Create an array for the individual triggers
|
---|
240 | TObjArray triggers;
|
---|
241 | triggers.SetOwner();
|
---|
242 |
|
---|
243 | for (int j=0; j<fCoincidenceMap.GetEntries(); j++)
|
---|
244 | {
|
---|
245 | const MArrayI &idx = fCoincidenceMap.GetRow(j);
|
---|
246 |
|
---|
247 | // Start with a copy of the first coincidence channel
|
---|
248 | TObjArray *arr = static_cast<TObjArray*>(ttls[idx[0]]->Clone());
|
---|
249 | arr->SetOwner();
|
---|
250 |
|
---|
251 | // compare to all other channels in this coincidence patch, one by one
|
---|
252 | for (UInt_t k=1; k<idx.GetSize() && arr->GetEntriesFast()>0; k++)
|
---|
253 | {
|
---|
254 | TObjArray *res = CalcCoincidence(*arr, *static_cast<TObjArray*>(ttls[idx[k]]),
|
---|
255 | gate);
|
---|
256 |
|
---|
257 | // Delete the original array and keep the new one
|
---|
258 | delete arr;
|
---|
259 | arr = res;
|
---|
260 | }
|
---|
261 |
|
---|
262 | // Remove all signals which are not in the valid digitization range
|
---|
263 | // (This is not the digitization window, but the region in which
|
---|
264 | // the analog channels contain usefull data)
|
---|
265 | TIter Next(arr);
|
---|
266 | MDigitalSignal *ttl = 0;
|
---|
267 | while ((ttl=static_cast<MDigitalSignal*>(Next())))
|
---|
268 | if (ttl->GetStart()<min || ttl->GetStart()>max)
|
---|
269 | delete arr->Remove(ttl);
|
---|
270 |
|
---|
271 | // Remove the empty slots
|
---|
272 | arr->Compress();
|
---|
273 |
|
---|
274 | // If we have at least one trigger keep the earliest one.
|
---|
275 | // FIXME: The triggers should be ordered in time automatically: To be checked!
|
---|
276 | // FIXME: Simulate trigger dead-time!
|
---|
277 | if (arr->GetEntriesFast()>0)
|
---|
278 | triggers.Add(arr->RemoveAt(0));
|
---|
279 |
|
---|
280 | // delete the allocated space
|
---|
281 | delete arr;
|
---|
282 | }
|
---|
283 |
|
---|
284 | // No trigger issued. Go on.
|
---|
285 | if (triggers.GetEntriesFast()==0)
|
---|
286 | return kTRUE;
|
---|
287 |
|
---|
288 | // There are usually not enough entries that it is worth to search
|
---|
289 | // for the earliest instead of just sorting and taking the first one
|
---|
290 | // FIXME: This could be improved if checking for IsSortable
|
---|
291 | // is omitted
|
---|
292 | triggers.Sort();
|
---|
293 |
|
---|
294 | // inf2?
|
---|
295 | *fLog << all;
|
---|
296 | *fLog << triggers.GetEntriesFast() << " triggers in " << triggers.GetEntriesFast() << " patches.";
|
---|
297 | *fLog << endl;
|
---|
298 |
|
---|
299 | // FIXME: Jitter! (Own class?)
|
---|
300 | fTrigger->SetVal(static_cast<MDigitalSignal*>(triggers[0])->GetStart());
|
---|
301 |
|
---|
302 |
|
---|
303 | return kTRUE;
|
---|
304 | }
|
---|
305 |
|
---|
306 | // --------------------------------------------------------------------------
|
---|
307 | //
|
---|
308 | // FileNameRouteac: routeac.txt
|
---|
309 | // FileNameCoincidenceMap: coincidence.txt
|
---|
310 | // DiscriminatorTheshold: 3.5
|
---|
311 | //
|
---|
312 | Int_t MSimTrigger::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
313 | {
|
---|
314 | Bool_t rc = kFALSE;
|
---|
315 | if (IsEnvDefined(env, prefix, "FileNameRouteAC", print))
|
---|
316 | {
|
---|
317 | rc = kTRUE;
|
---|
318 | fNameRouteAC = GetEnvValue(env, prefix, "FileNameRouteAC", fNameRouteAC);
|
---|
319 | }
|
---|
320 |
|
---|
321 | if (IsEnvDefined(env, prefix, "FileNameCoincidenceMap", print))
|
---|
322 | {
|
---|
323 | rc = kTRUE;
|
---|
324 | fNameCoincidenceMap = GetEnvValue(env, prefix, "FileNameCoincidenceMap", fNameCoincidenceMap);
|
---|
325 | }
|
---|
326 |
|
---|
327 | if (IsEnvDefined(env, prefix, "DiscriminatorThreshold", print))
|
---|
328 | {
|
---|
329 | rc = kTRUE;
|
---|
330 | fDiscriminatorThreshold = GetEnvValue(env, prefix, "DiscriminatorThreshold", fDiscriminatorThreshold);
|
---|
331 | }
|
---|
332 |
|
---|
333 | return rc;
|
---|
334 | }
|
---|