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

Last change on this file since 4740 was 4738, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 10.9 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
214//
215void MRawRunHeader::Print(Option_t *t) const
216{
217 *fLog << all << endl;
218 *fLog << "MagicNumber: 0x" << hex << fMagicNumber << " - ";
219 switch (fMagicNumber)
220 {
221 case kMagicNumber: *fLog << "OK"; break;
222 case kMagicNumber+1: *fLog << "File not closed!"; break;
223 default: *fLog << "Wrong!"; break;
224 }
225 *fLog << endl;
226 *fLog << "Version: " << dec << "Format=" << fFormatVersion << " ";
227 *fLog << "Software=" << fSoftVersion << endl;
228 *fLog << "RunNumber: " << fRunNumber << " (Type=" << GetRunTypeStr() << ")" << endl;
229 *fLog << "ProjectName: '" << fProjectName << "'" << endl;
230 *fLog << "Source: '" << fSourceName << "' " << " ";
231 *fLog << fSourceEpochChar << dec << fSourceEpochDate << endl;
232 *fLog << "Run Start: " << fRunStart << endl;
233 *fLog << "Run Stop: " << fRunStop << endl;
234 *fLog << "Crates: " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
235 *fLog << "Samples: " << fNumSamplesLoGain << "/" << fNumSamplesHiGain << " (lo/hi) = " << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumCrates * fNumPixInCrate /1024 << "kB/Evt" << endl;
236 *fLog << "Evt Counter: " << fNumEvents << endl;
237
238 *fLog << inf << hex;
239 for (int i=0; i<GetNumPixel(); i++)
240 *fLog << setfill('0') << setw(3) << (*fPixAssignment)[i] << " ";
241
242 *fLog << endl;
243}
244
245// --------------------------------------------------------------------------
246//
247// Return the assigned pixel number for the given FADC channel
248//
249Short_t MRawRunHeader::GetPixAssignment(UShort_t i) const
250{
251 // FIXME: Do we need a range check here?
252 return (Short_t)(*fPixAssignment)[i];
253}
254
255// --------------------------------------------------------------------------
256//
257// Return the number of pixel which are markes as connected in the
258// pix assignment (!=0)
259//
260UShort_t MRawRunHeader::GetNumConnectedPixels() const
261{
262 const Int_t num = fPixAssignment->GetSize();
263
264 UShort_t rc = 0;
265 for (int i=0; i<num; i++)
266 {
267 if (GetPixAssignment(i)!=0)
268 rc++;
269 }
270 return rc;
271}
272
273// --------------------------------------------------------------------------
274//
275// Return the number of pixel which are markes as connected and so-called
276// 'normal' pixels in the pix assignment (>0)
277//
278UShort_t MRawRunHeader::GetNumNormalPixels() const
279{
280 const Int_t num = fPixAssignment->GetSize();
281
282 UShort_t rc = 0;
283 for (int i=0; i<num; i++)
284 {
285 if (GetPixAssignment(i)>0)
286 rc++;
287 }
288 return rc;
289}
290
291// --------------------------------------------------------------------------
292//
293// Return the number of pixel which are markes as connected and so-called
294// 'special' pixels in the pix assignment (<0)
295//
296UShort_t MRawRunHeader::GetNumSpecialPixels() const
297{
298 const Int_t num = fPixAssignment->GetSize();
299
300 UShort_t rc = 0;
301 for (int i=0; i<num; i++)
302 {
303 if (GetPixAssignment(i)<0)
304 rc++;
305 }
306 return rc;
307}
308
309// --------------------------------------------------------------------------
310//
311// Return the maximum id which exists in the pix assignment
312//
313UShort_t MRawRunHeader::GetMaxPixId() const
314{
315 const Int_t num = fPixAssignment->GetSize();
316
317 Short_t rc = 0;
318 for (int i=0; i<num; i++)
319 rc = TMath::Max(GetPixAssignment(i), rc);
320
321 return rc;
322}
323
324// --------------------------------------------------------------------------
325//
326// Return minus th minimum id which exists in the pix assignment
327//
328UShort_t MRawRunHeader::GetMinPixId() const
329{
330 const Int_t num = fPixAssignment->GetSize();
331
332 Short_t rc = 0;
333 for (int i=0; i<num; i++)
334 rc = TMath::Min(GetPixAssignment(i), rc);
335
336 return (UShort_t)-rc;
337}
338
339// --------------------------------------------------------------------------
340//
341// Return the number of pixel in this event.
342//
343// WARNING: This is the number of pixels stored in this file which is
344// a multiple of the number of pixels per crate and in general
345// a number which is larger than the camera size!
346//
347// To know the range of the pixel indices please use the geometry
348// container!
349//
350UShort_t MRawRunHeader::GetNumPixel() const
351{
352 return fPixAssignment->GetSize();
353}
354
355// --------------------------------------------------------------------------
356//
357// Returns absolute size in bytes of the run header as read from a raw file.
358// This must be done _after_ the header is read, because the header doesn't
359// have a fixed size (used in MRawSocketRead)
360//
361Int_t MRawRunHeader::GetNumTotalBytes() const
362{
363 switch (fFormatVersion)
364 {
365 case 1:
366 return 80+fNumCrates*fNumPixInCrate*2+16;
367 case 2:
368 return 84+fNumCrates*fNumPixInCrate*2+16;
369 }
370 return 0;
371}
Note: See TracBrowser for help on using the repository browser.