source: trunk/MagicSoft/Mars/mraw/MRawRunHeader.cc@ 7421

Last change on this file since 7421 was 7035, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 11.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-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MRawRunHeader
28//
29// Root storage container for the RUN HEADER information
30//
31//
32// RAW DATA FORMAT VERSION
33// =======================
34//
35// Format Version 5:
36// -----------------
37// - now the sub millisecond information of the time is valid and decoded
38// which enhances the precision from 51.2us to 200ns
39//
40// Format Version 4:
41// -----------------
42// - added support for pixels with negative IDs
43//
44// Format Version 3:
45// -----------------
46// - ???
47//
48// Format Version 2:
49// -----------------
50// - removed mjd from data
51// - added start time
52// - added stop time
53//
54//
55// MRawRunHeader CLASS VERSION
56// ===========================
57//
58// Class Version 3:
59// ----------------
60// - enhanced SourceName and ProjectName by one character, because
61// without telling us the guranteed trailing \0-character has
62// skipped
63//
64// Class Version 2:
65// ----------------
66// - removed fMJD, fYear, fMonth, fDay
67// - added fRunStart
68// - added fRunStop
69//
70// Class Version 1:
71// ----------------
72// - first implementation
73//
74////////////////////////////////////////////////////////////////////////////
75
76#include "MRawRunHeader.h"
77
78#include <fstream>
79#include <iomanip>
80
81#include "MLog.h"
82#include "MLogManip.h"
83
84#include "MArrayS.h"
85
86ClassImp(MRawRunHeader);
87
88using namespace std;
89
90const UShort_t MRawRunHeader::kMagicNumber = 0xc0c0;
91const Byte_t MRawRunHeader::kMaxFormatVersion = 5;
92
93// --------------------------------------------------------------------------
94//
95// Default constructor. Creates array which stores the pixel assignment.
96//
97//
98MRawRunHeader::MRawRunHeader(const char *name, const char *title) : fPixAssignment(NULL)
99{
100 fName = name ? name : "MRawRunHeader";
101 fTitle = title ? title : "Raw Run Header Information";
102
103 fPixAssignment = new MArrayS(0);
104
105 fFormatVersion=0;
106 fSoftVersion=0;
107 fRunType=kRTNone; // use 0xffff for invalidation, 0 means: Data run
108 fRunNumber=0;
109 memset(fProjectName, 0, 23);
110 memset(fSourceName, 0, 13);
111 fSourceEpochChar[0]=0;
112 fSourceEpochDate=0;
113 fNumCrates=0;
114 fNumPixInCrate=0;
115 fNumSamplesLoGain=0;
116 fNumSamplesHiGain=0;
117 fNumEvents=0;
118}
119
120// --------------------------------------------------------------------------
121//
122// Destructor. Deletes the 'pixel-assignment-array'
123//
124MRawRunHeader::~MRawRunHeader()
125{
126 delete fPixAssignment;
127}
128
129// --------------------------------------------------------------------------
130//
131// Read in one run header from the binary file
132//
133Bool_t MRawRunHeader::ReadEvt(istream& fin)
134{
135 //
136 // read one RUN HEADER from the input stream
137 //
138 fMagicNumber = 0;
139
140 fin.read((char*)&fMagicNumber, 2); // Total=2
141
142 //
143 // check whether the the file has the right file type or not
144 //
145 if (fMagicNumber != kMagicNumber && fMagicNumber != kMagicNumber+1)
146 {
147 *fLog << err << "ERROR - Wrong Magic Number (0x" << hex << fMagicNumber << "): Not a Magic File!" << endl;
148 return kFALSE;
149 }
150
151 if (fMagicNumber == kMagicNumber+1)
152 *fLog << warn << "WARNING - This file maybe broken (0xc0c1) - DAQ didn't close it correctly!" << endl;
153
154 Byte_t dummy[16];
155
156 fin.read((char*)&fFormatVersion, 2); // Total=4
157 if (fFormatVersion>kMaxFormatVersion)
158 {
159 *fLog << err << "ERROR - File vormat V" << fFormatVersion << " not implemented!" << endl;
160 return kFALSE;
161 }
162
163 fin.read((char*)&fSoftVersion, 2); // Total=6
164 fin.read((char*)&fRunType, 2); // Total=8
165 fin.read((char*)&fRunNumber, 4); // Total=12
166 fin.read((char*)&fProjectName, 22); // Total=34
167 fin.read((char*)&fSourceName, 12); // Total=46
168 fin.read((char*)dummy, 4); // was RA (moved to tracking system)
169 fin.read((char*)dummy, 4); // was DEC (moved to tracking system)
170 fin.read((char*)&fSourceEpochChar, 2); // Total=56
171 fin.read((char*)&fSourceEpochDate, 2); // Total=58
172 if (fFormatVersion<2) // Total += 10
173 {
174 UShort_t y, m, d;
175 fin.read((char*)dummy, 4); // Former fMJD[4],
176 fin.read((char*)&y, 2); // Former fDateYear[2]
177 fin.read((char*)&m, 2); // Former fDateMonth[2]
178 fin.read((char*)&d, 2); // Former fDateDay[2]
179 fRunStart.Set(y, m, d, 0, 0, 0, 0);
180 }
181 fin.read((char*)&fNumCrates, 2); // Total=60
182 fin.read((char*)&fNumPixInCrate, 2); // Total=62
183 fin.read((char*)&fNumSamplesLoGain, 2); // Total=64
184 fin.read((char*)&fNumSamplesHiGain, 2); // Total=66
185 fin.read((char*)&fNumEvents, 4); // Total=70
186 if (fFormatVersion>1)
187 {
188 fRunStart.ReadBinary(fin); // Total += 7
189 fRunStop.ReadBinary(fin); // Total += 7
190 }
191
192 //
193 // calculate size of array, create it and fill it
194 //
195 Int_t nPixel = fNumCrates*fNumPixInCrate;
196 fPixAssignment->Set(nPixel);
197
198 fin.read((char*)fPixAssignment->GetArray(), nPixel*2);
199 fin.read((char*)&dummy, 16);
200
201 return kTRUE;
202}
203
204// --------------------------------------------------------------------------
205//
206// Return the run type as string ("Data", "Pedestal", ...), for example
207// to print it as readable text.
208//
209const char *MRawRunHeader::GetRunTypeStr() const
210{
211 switch (fRunType)
212 {
213 case kRTData:
214 return "Data";
215 case kRTPedestal:
216 return "Pedestal";
217 case kRTCalibration:
218 return "Calibration";
219 case kRTPointRun:
220 return "Point-Run";
221 case kRTMonteCarlo:
222 return "Monte Carlo";
223 case kRTNone:
224 return "<none>";
225 default:
226 return "<unknown>";
227 }
228}
229
230// --------------------------------------------------------------------------
231//
232// print run header information on *fLog. The option 'header' supresses
233// the pixel index translation table.
234//
235void MRawRunHeader::Print(Option_t *t) const
236{
237 *fLog << all << endl;
238 *fLog << "MagicNumber: 0x" << hex << fMagicNumber << " - ";
239 switch (fMagicNumber)
240 {
241 case kMagicNumber: *fLog << "OK"; break;
242 case kMagicNumber+1: *fLog << "File not closed!"; break;
243 default: *fLog << "Wrong!"; break;
244 }
245 *fLog << endl;
246 *fLog << "Version: " << dec << "Format=" << fFormatVersion << " ";
247 *fLog << "Software=" << fSoftVersion << endl;
248 *fLog << "RunNumber: " << fRunNumber << " (Type=" << GetRunTypeStr() << ")" << endl;
249 *fLog << "ProjectName: '" << fProjectName << "'" << endl;
250 *fLog << "Source: '" << fSourceName << "' " << " ";
251 *fLog << fSourceEpochChar << dec << fSourceEpochDate << endl;
252 *fLog << "Run Start: " << fRunStart << endl;
253 *fLog << "Run Stop: " << fRunStop << endl;
254 *fLog << "Crates: " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
255 *fLog << "Num Pixels: " << GetNumNormalPixels() << " (normal) + " << GetNumSpecialPixels() << " (special) = " << GetNumConnectedPixels() << " (total)" << endl;
256 *fLog << "Samples: " << fNumSamplesHiGain << "/" << fNumSamplesLoGain << " (hi/lo) = " << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumCrates * fNumPixInCrate /1024 << "kB/Evt" << endl;
257 *fLog << "Evt Counter: " << fNumEvents << endl;
258
259 if (TString(t).Contains("header", TString::kIgnoreCase))
260 return;
261
262 *fLog << inf << hex;
263 for (int i=0; i<GetNumPixel(); i++)
264 *fLog << setfill('0') << setw(3) << (*fPixAssignment)[i] << " ";
265
266 *fLog << dec << endl;
267}
268
269// --------------------------------------------------------------------------
270//
271// Return the assigned pixel number for the given FADC channel
272//
273Short_t MRawRunHeader::GetPixAssignment(UShort_t i) const
274{
275 // FIXME: Do we need a range check here?
276 return (Short_t)(*fPixAssignment)[i];
277}
278
279// --------------------------------------------------------------------------
280//
281// Return the number of pixel which are markes as connected in the
282// pix assignment (!=0)
283//
284UShort_t MRawRunHeader::GetNumConnectedPixels() const
285{
286 const Int_t num = fPixAssignment->GetSize();
287
288 UShort_t rc = 0;
289 for (int i=0; i<num; i++)
290 {
291 if (GetPixAssignment(i)!=0)
292 rc++;
293 }
294 return rc;
295}
296
297// --------------------------------------------------------------------------
298//
299// Return the number of pixel which are markes as connected and so-called
300// 'normal' pixels in the pix assignment (>0)
301//
302UShort_t MRawRunHeader::GetNumNormalPixels() const
303{
304 const Int_t num = fPixAssignment->GetSize();
305
306 UShort_t rc = 0;
307 for (int i=0; i<num; i++)
308 {
309 if (GetPixAssignment(i)>0)
310 rc++;
311 }
312 return rc;
313}
314
315// --------------------------------------------------------------------------
316//
317// Return the number of pixel which are markes as connected and so-called
318// 'special' pixels in the pix assignment (<0)
319//
320UShort_t MRawRunHeader::GetNumSpecialPixels() const
321{
322 const Int_t num = fPixAssignment->GetSize();
323
324 UShort_t rc = 0;
325 for (int i=0; i<num; i++)
326 {
327 if (GetPixAssignment(i)<0)
328 rc++;
329 }
330 return rc;
331}
332
333// --------------------------------------------------------------------------
334//
335// Return the maximum id which exists in the pix assignment
336//
337UShort_t MRawRunHeader::GetMaxPixId() const
338{
339 const Int_t num = fPixAssignment->GetSize();
340
341 Short_t rc = 0;
342 for (int i=0; i<num; i++)
343 rc = TMath::Max(GetPixAssignment(i), rc);
344
345 return rc;
346}
347
348// --------------------------------------------------------------------------
349//
350// Return minus th minimum id which exists in the pix assignment
351//
352UShort_t MRawRunHeader::GetMinPixId() const
353{
354 const Int_t num = fPixAssignment->GetSize();
355
356 Short_t rc = 0;
357 for (int i=0; i<num; i++)
358 rc = TMath::Min(GetPixAssignment(i), rc);
359
360 return (UShort_t)-rc;
361}
362
363// --------------------------------------------------------------------------
364//
365// Return the number of pixel in this event.
366//
367// WARNING: This is the number of pixels stored in this file which is
368// a multiple of the number of pixels per crate and in general
369// a number which is larger than the camera size!
370//
371// To know the range of the pixel indices please use the geometry
372// container!
373//
374UShort_t MRawRunHeader::GetNumPixel() const
375{
376 return fPixAssignment->GetSize();
377}
378
379// --------------------------------------------------------------------------
380//
381// Returns absolute size in bytes of the run header as read from a raw file.
382// This must be done _after_ the header is read, because the header doesn't
383// have a fixed size (used in MRawSocketRead)
384//
385Int_t MRawRunHeader::GetNumTotalBytes() const
386{
387 switch (fFormatVersion)
388 {
389 case 1:
390 return 80+fNumCrates*fNumPixInCrate*2+16;
391 case 2:
392 return 84+fNumCrates*fNumPixInCrate*2+16;
393 }
394 return 0;
395}
Note: See TracBrowser for help on using the repository browser.