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

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