source: trunk/MagicSoft/Mars/mraw/MRawEvtHeader.cc@ 9223

Last change on this file since 9223 was 8944, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 13.7 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC 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 appear 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 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2008
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MRawEvtHeader
28//
29// One Event is a sample of FADC measurements of different Pixels
30// (Photomultipliers) from the Camera of MAGIC. So all data (FADC) of the
31// interesting pixels are the modules of an event. To describe pixels the
32// class MRawPixel is used and the class MRawCrate to describe Crates.
33// To define a single events some other data members are needed
34// (Time of the events, tirgger pattern of event..)
35//
36// To describe one event on the level of FADC values the Class MRawEvtHeader is
37// created. It has the following data members:
38//
39// UInt_t fDAQEvtNumber
40// -----------------------
41// This it the number of the Event in one
42// data run. The first event in this run get
43// the number zero. The next one is one bigger.
44//
45// Assuming that one run takes 1 hour and a
46// triggerrate of 1kHz the number must be able
47// to reach 3.6e6 Events. To reach this number
48// you need at least 22 bits. This is the reason
49// why we use an integer (of root type UInt_t)
50// with a range to 4.2e9.
51//
52// MTime fRawEvtTime
53// -------------------
54// Time of the event.
55// The start point of the time determination can be
56// the millenium. From that start point the time is
57// measured in 200ns-count. One day for example
58// contains 432.e9 counts. An array of two unsigned Int is able to
59// contain 1.8e19 200ns-counts. This corresponds to 41.e6
60// days. This should be more than the livetime of MAGIC.
61// Private member of MTime.h
62//
63// UInt_t fNumTrigLvl1
64// --------------------
65//
66// Number of first level trigger
67// This member counts the number of First Level Trigger
68// between the last and this event. May be that due to
69// dead time of the DAQ this number is different from 1.
70// If the DAQ is fast enough, this value should be 1.
71// This may be usefull in GammaRayBursts and if we
72// apply a data reduction in the DAQ-chain, which selects
73// only good events.
74//
75// UInt_t fNumTrigLvl2
76// --------------------
77//
78// Number of second level trigger
79// This member counts the number of Second Level Trigger
80// between the last and this event.
81//
82// UInt_t fTrigPattern[0]
83// -----------------------
84// Trigger Pattern used for this event
85// Each event triggers for a particular configuration and each
86// configuration should have an ID (which is not fixed yet).
87//
88// UInt_t fCalibPattern == fTrigPattern[1]
89// -----------------------
90// Calibration Pattern used for this event
91// Each (calibration) event uses a particular LEDs configuration and
92// particular strength and colour of the continunous light.
93// Bits 1-16: Pulser slot pattern: 16 LEDs slots.
94// Bits 17: CT1 Pulser
95// Bits 21-24: Colour of Continous light source:
96// Bits 25-32: Strength of continuous light source: 256 level
97//
98// UShort_t fAllLowGainOn
99// ----------------------
100// Type of Trigger.
101// This is a Byte (8 bit) to indicated if any of the pixels
102// have a non-negligible low gain (1) or not (0)
103//
104// Class Version 2:
105// ---------------
106// - added fCalibPattern
107//
108/////////////////////////////////////////////////////////////////////////////
109#include "MRawEvtHeader.h"
110
111#include <fstream>
112
113#include <TArrayC.h>
114
115#include "MLog.h"
116#include "MLogManip.h"
117
118#include "MTime.h"
119#include "MArrayB.h"
120#include "MRawRunHeader.h"
121
122ClassImp(MRawEvtHeader);
123
124using namespace std;
125
126// --------------------------------------------------------------------------
127//
128// Default constructor. Create the array to store the data.
129//
130MRawEvtHeader::MRawEvtHeader(const char *name, const char *title)
131{
132 fName = name ? name : "MRawEvtHeader";
133 fTitle = title ? title : "Raw Event Header Information";
134
135 //
136 // set all member to zero, init the pointer to ClonesArray,
137 //
138 fPixLoGainOn = new MArrayB;
139
140 Clear();
141}
142
143// --------------------------------------------------------------------------
144//
145// Destructor. Deletes the array to store pixlogainon
146//
147MRawEvtHeader::~MRawEvtHeader()
148{
149 delete fPixLoGainOn;
150}
151
152// --------------------------------------------------------------------------
153//
154// you have to init the conatainer before you can read from
155// a raw binary file
156//
157void MRawEvtHeader::InitRead(MRawRunHeader *rh, MTime *t)
158{
159 //
160 // this is the number of entries in the array like specification
161 //
162 UInt_t fN = (rh->GetNumCrates() * rh->GetNumPixInCrate() + 7) / 8;
163
164 //
165 // initialize the array
166 //
167 fPixLoGainOn->Set(fN);
168
169 //
170 // this is the conatiner where we have to store the time of the event we
171 // read from the input stream
172 //
173 fTime = t;
174}
175
176// --------------------------------------------------------------------------
177//
178// Implementation of the Clear function
179//
180// Resets all members to zero, clear the list of Pixels
181//
182void MRawEvtHeader::Clear(Option_t *)
183{
184 fDAQEvtNumber = 0;
185 fNumTrigLvl1 = 0;
186 fNumTrigLvl2 = 0;
187 fTrigPattern[0] = 0;
188 fTrigPattern[1] = 0;
189 fTrigType = 0;
190 fNumLoGainOn = 0;
191}
192
193// --------------------------------------------------------------------------
194//
195// This member function prints all Data of one Event to *fLog.
196//
197void MRawEvtHeader::Print(Option_t *o) const
198{
199 *fLog << all;
200 *fLog << "DAQEvtNr: " << dec << fDAQEvtNumber << " (";
201 *fLog << "Trigger: ";
202 *fLog << "NumLvl1=" << fNumTrigLvl1 << " ";
203 *fLog << "NumLvl2=" << fNumTrigLvl2 << " ";
204 *fLog << "Pattern=" << hex << setfill('0');
205 *fLog << setw(8) << fTrigPattern[0];
206 *fLog << setw(8) << fTrigPattern[1] << " " << dec;
207
208 *fLog << "Type=";
209 switch (fTrigType)
210 {
211 case 0:
212 *fLog << "Trigger";
213 break;
214 case 1:
215 *fLog << "Pedestal";
216 break;
217 case 2:
218 *fLog << "Calibration";
219 break;
220 case 3:
221 *fLog << "PinDiode";
222 break;
223 }
224 *fLog << ")" << endl;
225 *fLog << "Number of Lo Gains On: " << fNumLoGainOn << endl;
226
227 TString str(o);
228 str.ToLower();
229
230 if (str.Contains("nogains"))
231 return;
232
233 for (unsigned int i=0; i<fPixLoGainOn->GetSize(); i++)
234 {
235 for (int j=0; j<8; j++)
236 {
237 const UInt_t on = (*fPixLoGainOn)[i]&(1<<j) ? 1 : 0;
238 *fLog << on;
239 }
240 }
241 if (fPixLoGainOn->GetSize())
242 *fLog << endl;
243}
244
245// --------------------------------------------------------------------------
246//
247// Return the size in bytes of the event header.
248//
249Int_t MRawEvtHeader::GetNumBytes() const
250{
251 return 36+fPixLoGainOn->GetSize();
252}
253
254// --------------------------------------------------------------------------
255//
256// Used to set the header information. This is for MC only. NEVER, NEVER
257// use this somewhere else!
258//
259void MRawEvtHeader::FillHeader(UInt_t uiN, Float_t ulTP)
260{
261 fDAQEvtNumber = uiN;
262 fTrigPattern[0] = (UInt_t)(ulTP/4294967296.0) ;
263 fTrigPattern[1] = (UInt_t)(ulTP-fTrigPattern[0]*4294967296.0);
264}
265
266// --------------------------------------------------------------------------
267//
268// Decode the binary Time Stamp. For more detailed information see the
269// source code.
270//
271Bool_t MRawEvtHeader::DecodeTime(const UInt_t tm[2], UShort_t ver) const
272{
273 //
274 // SuperSecond (20 bits giving hh:mm:ss)
275 // ------------
276 //
277 // Reading the hours:
278 // Swap bits: 23->16, 22->17, 21->16, 20->19
279 //
280 UInt_t abstime[2] = { tm[0], tm[1] };
281
282 abstime[0] =
283 abstime[0]>>7 & 0x00010000 |
284 abstime[0]>>5 & 0x00020000 |
285 abstime[0]>>3 & 0x00040000 |
286 abstime[0]>>1 & 0x00080000 |
287 abstime[0] & 0xff00ffff;
288
289 //
290 // SubSecond (24 bits giving number of clock ticks of a 5Mhz signal since
291 // the beginning of last second, i.e., number of ns with a precision of to
292 // 200 ns)
293 // ----------
294 //
295 // The last 8 bits must be flipped.
296 //
297 abstime[1] ^= 0x000000ff;
298
299 //
300 // Due to a problem with one Digital Module, three of the less significant
301 // eight bits of the subsecond are corrupted. So, until new DM's arrive to
302 // La Palma, we won't use the eight first bits of the subsecond.
303 // This reduces the precision from 200 ns to of 51.2 us. (ver<5)
304 //
305 if (ver<5)
306 abstime[1] &= 0xffffff00;
307
308 //
309 // Decode Time Stamp
310 //
311 const Byte_t h = (abstime[0]>>18 & 0x3)*10 + (abstime[0]>>14 & 0xf);
312 const Byte_t m = (abstime[0]>>11 & 0x7)*10 + (abstime[0]>> 7 & 0xf);
313 const Byte_t s = (abstime[0]>> 4 & 0x7)*10 + (abstime[0]>> 0 & 0xf);
314 const UInt_t ns = abstime[1]*200;
315
316 //
317 // Update the time stamp with the current event time.
318 // Make sure, that the time stamp was initialized correctly
319 // with the start-date/time of the run (after reading the run header)
320 //
321 if (fTime->UpdMagicTime(h, m, s, ns))
322 return kTRUE;
323
324 *fLog << warn << "WARNING - Time (" << Form("%2d:%02d:%02d,%09d", h, m, s, ns);
325 *fLog << ") in header of event #" << dec << fDAQEvtNumber << " invalid..." << endl;
326
327 return kFALSE;
328}
329
330// --------------------------------------------------------------------------
331//
332// read the EVENT HEADER information from the input stream
333// return FALSE if there is now header anymore, else TRUE
334//
335// For version>2 we expect to have a valid time-stamp in the files.
336//
337// Updates the time stamp with the current event time.
338// Make sure, that the time stamp was initialized correctly
339// with the start-date/time of the run (after reading the run header)
340//
341// Remark: This 'feature' disallows single runs of more than 11h!
342//
343Int_t MRawEvtHeader::ReadEvtOld(istream &fin, UShort_t ver)
344{
345 Int_t rc = kTRUE;
346
347 fin.read((char*)&fDAQEvtNumber, 4); // Total=4
348 if (!fin)
349 return kFALSE;
350
351 UInt_t abstime[2];
352 fin.read((char*)abstime, 8); // Total=12
353
354 if (ver>2)
355 if (!DecodeTime(abstime, ver))
356 rc = kCONTINUE;
357
358 Byte_t dummy[4];
359 fin.read((char*)&fNumTrigLvl1, 4); // Total=16
360 fin.read((char*)&fNumTrigLvl2, 4); // Total=20
361 fin.read((char*)fTrigPattern, 8); // Total=28
362 fin.read((char*)&fTrigType, 2); // Total=30
363 if (ver<=6)
364 {
365 fin.read((char*)dummy, 2); // Total=32, was fAllLoGainOn
366 fin.read((char*)fPixLoGainOn->GetArray(), fPixLoGainOn->GetSize());
367
368 fNumLoGainOn = 0;
369 for (unsigned int i=0; i<fPixLoGainOn->GetSize(); i++)
370 for (int j=0; j<8; j++)
371 if ((*fPixLoGainOn)[i] & (1<<j))
372 fNumLoGainOn++;
373
374 fin.read((char*)&dummy, 4);
375 }
376 else
377 {
378 // No LoGains for version 7 data
379 fPixLoGainOn->Reset();
380 fNumLoGainOn = 0;
381 }
382
383 return fin.eof() ? kFALSE : rc;
384}
385
386// --------------------------------------------------------------------------
387//
388Int_t MRawEvtHeader::ReadEvt(istream &fin, UShort_t ver, UInt_t size)
389{
390 if (ver<11)
391 return ReadEvtOld(fin, ver);
392
393 if (size==0)
394 {
395 *fLog << err << "ERROR - Event header size unknown." << endl;
396 return kFALSE;
397 }
398
399 if (size<32)
400 {
401 *fLog << err << "ERROR - Event header too small (<32b)." << endl;
402 return kFALSE;
403 }
404
405 TArrayC h(size);
406 fin.read(h.GetArray(), h.GetSize());
407 if (!fin)
408 return kFALSE;
409
410 // ----- convert -----
411 //const Byte_t *Char = reinterpret_cast<Byte_t* >(h.GetArray());
412 const UInt_t *Int = reinterpret_cast<UInt_t* >(h.GetArray());
413 //const Float_t *Float = reinterpret_cast<Float_t*>(h.GetArray());
414
415 fDAQEvtNumber = Int[0];
416 // Decode Time
417 fNumTrigLvl1 = Int[3];
418 fNumTrigLvl2 = Int[4];
419 fTrigPattern[0] = Int[5];
420 fTrigPattern[1] = Int[6];
421 fTrigType = Int[7];
422
423 // No LoGains for version 7 data
424 fPixLoGainOn->Reset();
425 fNumLoGainOn = 0;
426
427 return DecodeTime(Int+1, ver) ? kTRUE : kCONTINUE;
428}
429
430void MRawEvtHeader::SkipEvt(istream &fin, UShort_t ver)
431{
432 fin.seekg(GetNumBytes(), ios::cur);
433}
434
435// --------------------------------------------------------------------------
436//
437// Low level decoding of the trigger pattern.
438// The trigger pattern consists of 16 bits (8+8 bits) generated by the
439// trigger system.
440// The first 8 bits correspond to the trigger configuration before the
441// prescaling, the others after prescaling.
442// The meaning of the configuration depends on the chosen trigger table
443// (that is how the trigger has been programmed) and must be interpreted
444// at higher level by the analysis.
445// Bit structure:
446// not prscd | prscaled
447// xxxx xxxx xxxx xxxx <-- pattern (x=0,1)
448// bit 7654 3210 7654 3210
449// H L
450//
451// e.g. 1000 0000 1000 1000 (hex: 8080) is the pattern when no
452// L2 trigger selection and no prescaling is applied.
453//
454// Up to now only fTrigPattern[0] is used.
455//
456UInt_t MRawEvtHeader::GetTriggerID() const
457{
458 return fTrigPattern[0];
459}
460
461UInt_t MRawEvtHeader::GetCalibrationPattern() const
462{
463 return fTrigPattern[1];
464}
465
466UInt_t MRawEvtHeader::GetPulserSlotPattern() const
467{
468 return (fTrigPattern[1] >> 16) & 0x1ffff;
469}
470
Note: See TracBrowser for help on using the repository browser.