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

Last change on this file since 7443 was 7441, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 14.8 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// Swap the assignment of the pixels with hardware id id0 and id1
132//
133Bool_t MRawRunHeader::SwapAssignment(Short_t id0, Short_t id1)
134{
135 const Int_t n = fPixAssignment->GetSize();
136
137 // Look-up-table
138 UShort_t *lut = fPixAssignment->GetArray();
139
140 // Search for one of the hardware indices to get exchanged
141 int i;
142 for (i=0; i<n; i++)
143 if (lut[i]==id0 || lut[i]==id1)
144 break;
145
146 // Check if one of the two pixels were found
147 if (i==n)
148 {
149 *fLog << err << "ERROR - Assignment of pixels with hardware ID " << id0 << " and " << id1;
150 *fLog << " should be exchanged, but none were found." << endl;
151 return kFALSE;
152 }
153
154 // Store first index
155 const Int_t idx0 = i;
156
157 // Search for the other hardware indices to get exchanged
158 for (i++; i<n; i++)
159 if (lut[i]==id0 || lut[i]==id1)
160 break;
161
162 // Check if the second pixel was found
163 if (i==n)
164 {
165 *fLog << err << "ERROR - Assignment of pixels with hardware ID " << id0 << " and " << id1;
166 *fLog << " should be exchanged, but only one was found." << endl;
167 return kFALSE;
168 }
169
170 const Int_t idx1 = i;
171
172 const Short_t p0 = lut[idx0];
173 const Short_t p1 = lut[idx1];
174
175 lut[idx0] = p1;
176 lut[idx1] = p0;
177
178 *fLog << inf << "Assignment of pixels with hardware ID " << id0 << " and " << id1 << " exchanged." << endl;
179
180 return kTRUE;
181}
182
183// --------------------------------------------------------------------------
184//
185// This implements a fix of the pixel assignment before 25.9.2005
186//
187// From magic_online (the pixel numbers are hardware indices):
188// --------------------------------------------------------------------------
189// From: Florian Goebel <fgoebel@mppmu.mpg.de>
190// Date: Sun Sep 25 2005 - 05:13:19 CEST
191//
192// [...]
193// - problem 2: pixels were swaped
194// - cause: opical fibers were wrongly connected to receiver board
195// 554 <-> 559
196// 555 <-> 558
197// 556 <-> 557
198// - action: reconnect correctly
199// - result: ok now
200// - comment: I don't know when this error was introduced, so backward
201// correction of the data is difficult.
202// Fortunately the effect is not too large since the affected 6 pixels are
203// on the outermost edge of the camera
204// Since this board has special pixels the optical fibers have been
205// unplugged several times.
206// !!!!! Whenever you unplug and reconnect optical fibers make really !!!!!
207// !!!!! sure you connect them back correctly. !!!!!
208// !!!!! Always contact me before you do so and if you have any doubts !!!!!
209// !!!!! how to reconnect the fibers ask me. !!!!!
210// These swapped pixels have only been found by chance when doing the
211// flatfielding.
212// [...]
213//
214Bool_t MRawRunHeader::FixAssignment()
215{
216 if (fRunNumber<53300 || fRunNumber>68754)
217 return kTRUE;
218
219 if (!SwapAssignment(554, 559))
220 return kFALSE;
221
222 if (!SwapAssignment(555, 558))
223 return kFALSE;
224
225 if (!SwapAssignment(556, 557))
226 return kFALSE;
227
228 return kTRUE;
229}
230
231// --------------------------------------------------------------------------
232//
233// Read in one run header from the binary file
234//
235Bool_t MRawRunHeader::ReadEvt(istream& fin)
236{
237 //
238 // read one RUN HEADER from the input stream
239 //
240 fMagicNumber = 0;
241
242 fin.read((char*)&fMagicNumber, 2); // Total=2
243
244 //
245 // check whether the the file has the right file type or not
246 //
247 if (fMagicNumber != kMagicNumber && fMagicNumber != kMagicNumber+1)
248 {
249 *fLog << err << "ERROR - Wrong Magic Number (0x" << hex << fMagicNumber << "): Not a Magic File!" << endl;
250 return kFALSE;
251 }
252
253 if (fMagicNumber == kMagicNumber+1)
254 *fLog << warn << "WARNING - This file maybe broken (0xc0c1) - DAQ didn't close it correctly!" << endl;
255
256 Byte_t dummy[16];
257
258 fin.read((char*)&fFormatVersion, 2); // Total=4
259 if (fFormatVersion>kMaxFormatVersion)
260 {
261 *fLog << err << "ERROR - File vormat V" << fFormatVersion << " not implemented!" << endl;
262 return kFALSE;
263 }
264
265 fin.read((char*)&fSoftVersion, 2); // Total=6
266 fin.read((char*)&fRunType, 2); // Total=8
267 fin.read((char*)&fRunNumber, 4); // Total=12
268 fin.read((char*)&fProjectName, 22); // Total=34
269 fin.read((char*)&fSourceName, 12); // Total=46
270 fin.read((char*)dummy, 4); // was RA (moved to tracking system)
271 fin.read((char*)dummy, 4); // was DEC (moved to tracking system)
272 fin.read((char*)&fSourceEpochChar, 2); // Total=56
273 fin.read((char*)&fSourceEpochDate, 2); // Total=58
274 if (fFormatVersion<2) // Total += 10
275 {
276 UShort_t y, m, d;
277 fin.read((char*)dummy, 4); // Former fMJD[4],
278 fin.read((char*)&y, 2); // Former fDateYear[2]
279 fin.read((char*)&m, 2); // Former fDateMonth[2]
280 fin.read((char*)&d, 2); // Former fDateDay[2]
281 fRunStart.Set(y, m, d, 0, 0, 0, 0);
282 }
283 fin.read((char*)&fNumCrates, 2); // Total=60
284 fin.read((char*)&fNumPixInCrate, 2); // Total=62
285 fin.read((char*)&fNumSamplesLoGain, 2); // Total=64
286 fin.read((char*)&fNumSamplesHiGain, 2); // Total=66
287 fin.read((char*)&fNumEvents, 4); // Total=70
288 if (fFormatVersion>1)
289 {
290 fRunStart.ReadBinary(fin); // Total += 7
291 fRunStop.ReadBinary(fin); // Total += 7
292 }
293
294 //
295 // calculate size of array, create it and fill it
296 //
297 Int_t nPixel = fNumCrates*fNumPixInCrate;
298 fPixAssignment->Set(nPixel);
299
300 fin.read((char*)fPixAssignment->GetArray(), nPixel*2);
301 fin.read((char*)&dummy, 16);
302
303 return FixAssignment();
304}
305
306// --------------------------------------------------------------------------
307//
308// Return the run type as string ("Data", "Pedestal", ...), for example
309// to print it as readable text.
310//
311const char *MRawRunHeader::GetRunTypeStr() const
312{
313 switch (fRunType)
314 {
315 case kRTData:
316 return "Data";
317 case kRTPedestal:
318 return "Pedestal";
319 case kRTCalibration:
320 return "Calibration";
321 case kRTPointRun:
322 return "Point-Run";
323 case kRTMonteCarlo:
324 return "Monte Carlo";
325 case kRTNone:
326 return "<none>";
327 default:
328 return "<unknown>";
329 }
330}
331
332// --------------------------------------------------------------------------
333//
334// print run header information on *fLog. The option 'header' supresses
335// the pixel index translation table.
336//
337void MRawRunHeader::Print(Option_t *t) const
338{
339 *fLog << all << endl;
340 *fLog << "MagicNumber: 0x" << hex << fMagicNumber << " - ";
341 switch (fMagicNumber)
342 {
343 case kMagicNumber: *fLog << "OK"; break;
344 case kMagicNumber+1: *fLog << "File not closed!"; break;
345 default: *fLog << "Wrong!"; break;
346 }
347 *fLog << endl;
348 *fLog << "Version: " << dec << "Format=" << fFormatVersion << " ";
349 *fLog << "Software=" << fSoftVersion << endl;
350 *fLog << "RunNumber: " << fRunNumber << " (Type=" << GetRunTypeStr() << ")" << endl;
351 *fLog << "ProjectName: '" << fProjectName << "'" << endl;
352 *fLog << "Source: '" << fSourceName << "' " << " ";
353 *fLog << fSourceEpochChar << dec << fSourceEpochDate << endl;
354 *fLog << "Run Start: " << fRunStart << endl;
355 *fLog << "Run Stop: " << fRunStop << endl;
356 *fLog << "Crates: " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
357 *fLog << "Num Pixels: " << GetNumNormalPixels() << " (normal) + " << GetNumSpecialPixels() << " (special) = " << GetNumConnectedPixels() << " (total)" << endl;
358 *fLog << "Samples: " << fNumSamplesHiGain << "/" << fNumSamplesLoGain << " (hi/lo) = " << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumCrates * fNumPixInCrate /1024 << "kB/Evt" << endl;
359 *fLog << "Evt Counter: " << fNumEvents << endl;
360
361 if (TString(t).Contains("header", TString::kIgnoreCase))
362 return;
363
364 *fLog << inf << hex;
365 for (int i=0; i<GetNumPixel(); i++)
366 *fLog << setfill('0') << setw(3) << (*fPixAssignment)[i] << " ";
367
368 *fLog << dec << endl;
369}
370
371// --------------------------------------------------------------------------
372//
373// Return the assigned pixel number for the given FADC channel
374//
375Short_t MRawRunHeader::GetPixAssignment(UShort_t i) const
376{
377 // FIXME: Do we need a range check here?
378 return (Short_t)(*fPixAssignment)[i];
379}
380
381// --------------------------------------------------------------------------
382//
383// Return the number of pixel which are markes as connected in the
384// pix assignment (!=0)
385//
386UShort_t MRawRunHeader::GetNumConnectedPixels() const
387{
388 const Int_t num = fPixAssignment->GetSize();
389
390 UShort_t rc = 0;
391 for (int i=0; i<num; i++)
392 {
393 if (GetPixAssignment(i)!=0)
394 rc++;
395 }
396 return rc;
397}
398
399// --------------------------------------------------------------------------
400//
401// Return the number of pixel which are markes as connected and so-called
402// 'normal' pixels in the pix assignment (>0)
403//
404UShort_t MRawRunHeader::GetNumNormalPixels() const
405{
406 const Int_t num = fPixAssignment->GetSize();
407
408 UShort_t rc = 0;
409 for (int i=0; i<num; i++)
410 {
411 if (GetPixAssignment(i)>0)
412 rc++;
413 }
414 return rc;
415}
416
417// --------------------------------------------------------------------------
418//
419// Return the number of pixel which are markes as connected and so-called
420// 'special' pixels in the pix assignment (<0)
421//
422UShort_t MRawRunHeader::GetNumSpecialPixels() const
423{
424 const Int_t num = fPixAssignment->GetSize();
425
426 UShort_t rc = 0;
427 for (int i=0; i<num; i++)
428 {
429 if (GetPixAssignment(i)<0)
430 rc++;
431 }
432 return rc;
433}
434
435// --------------------------------------------------------------------------
436//
437// Return the maximum id which exists in the pix assignment
438//
439UShort_t MRawRunHeader::GetMaxPixId() const
440{
441 const Int_t num = fPixAssignment->GetSize();
442
443 Short_t rc = 0;
444 for (int i=0; i<num; i++)
445 rc = TMath::Max(GetPixAssignment(i), rc);
446
447 return rc;
448}
449
450// --------------------------------------------------------------------------
451//
452// Return minus th minimum id which exists in the pix assignment
453//
454UShort_t MRawRunHeader::GetMinPixId() const
455{
456 const Int_t num = fPixAssignment->GetSize();
457
458 Short_t rc = 0;
459 for (int i=0; i<num; i++)
460 rc = TMath::Min(GetPixAssignment(i), rc);
461
462 return (UShort_t)-rc;
463}
464
465// --------------------------------------------------------------------------
466//
467// Return the number of pixel in this event.
468//
469// WARNING: This is the number of pixels stored in this file which is
470// a multiple of the number of pixels per crate and in general
471// a number which is larger than the camera size!
472//
473// To know the range of the pixel indices please use the geometry
474// container!
475//
476UShort_t MRawRunHeader::GetNumPixel() const
477{
478 return fPixAssignment->GetSize();
479}
480
481// --------------------------------------------------------------------------
482//
483// Returns absolute size in bytes of the run header as read from a raw file.
484// This must be done _after_ the header is read, because the header doesn't
485// have a fixed size (used in MRawSocketRead)
486//
487Int_t MRawRunHeader::GetNumTotalBytes() const
488{
489 switch (fFormatVersion)
490 {
491 case 1:
492 return 80+fNumCrates*fNumPixInCrate*2+16;
493 case 2:
494 return 84+fNumCrates*fNumPixInCrate*2+16;
495 }
496 return 0;
497}
Note: See TracBrowser for help on using the repository browser.