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

Last change on this file since 7825 was 7684, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 16.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 6:
36// -----------------
37// + added CameraVersion
38// + added TelescopeNumber
39// + added ObservationMode
40// + added dummies for TelescopeRa/Dec
41//
42// Format Version 5:
43// -----------------
44// - now the sub millisecond information of the time is valid and decoded
45// which enhances the precision from 51.2us to 200ns
46//
47// Format Version 4:
48// -----------------
49// - added support for pixels with negative IDs
50//
51// Format Version 3:
52// -----------------
53// - ???
54//
55// Format Version 2:
56// -----------------
57// - removed mjd from data
58// - added start time
59// - added stop time
60//
61//
62// MRawRunHeader CLASS VERSION
63// ===========================
64//
65// Class Version 4:
66// -----------------
67// - added fCameraVersion
68// - added fTelescopeNumber
69// - changed length of fProjectName to 101
70// - changed length of fSourceName to 81
71//
72// Class Version 3:
73// ----------------
74// - enhanced SourceName and ProjectName by one character, because
75// without telling us the guranteed trailing \0-character has
76// skipped
77//
78// Class Version 2:
79// ----------------
80// - removed fMJD, fYear, fMonth, fDay
81// - added fRunStart
82// - added fRunStop
83//
84// Class Version 1:
85// ----------------
86// - first implementation
87//
88////////////////////////////////////////////////////////////////////////////
89
90#include "MRawRunHeader.h"
91
92#include <fstream>
93#include <iomanip>
94
95#include "MLog.h"
96#include "MLogManip.h"
97
98#include "MArrayS.h"
99
100ClassImp(MRawRunHeader);
101
102using namespace std;
103
104const UShort_t MRawRunHeader::kMagicNumber = 0xc0c0;
105const Byte_t MRawRunHeader::kMaxFormatVersion = 6;
106
107// --------------------------------------------------------------------------
108//
109// Default constructor. Creates array which stores the pixel assignment.
110//
111//
112MRawRunHeader::MRawRunHeader(const char *name, const char *title) : fPixAssignment(NULL)
113{
114 fName = name ? name : "MRawRunHeader";
115 fTitle = title ? title : "Raw Run Header Information";
116
117 fPixAssignment = new MArrayS(0);
118
119 fFormatVersion=0;
120 fSoftVersion=0;
121 fTelescopeNumber=0;
122 fCameraVersion=0;
123 fRunType=kRTNone; // use 0xffff for invalidation, 0 means: Data run
124 fRunNumber=0;
125 memset(fProjectName, 0, 101);
126 memset(fSourceName, 0, 81);
127 memset(fObservationMode, 0, 61);
128 fSourceEpochChar[0]=0;
129 fSourceEpochDate=0;
130 fNumCrates=0;
131 fNumPixInCrate=0;
132 fNumSamplesLoGain=0;
133 fNumSamplesHiGain=0;
134 fNumEvents=0;
135}
136
137// --------------------------------------------------------------------------
138//
139// Destructor. Deletes the 'pixel-assignment-array'
140//
141MRawRunHeader::~MRawRunHeader()
142{
143 delete fPixAssignment;
144}
145
146// --------------------------------------------------------------------------
147//
148// Swap the assignment of the pixels with hardware id id0 and id1
149//
150Bool_t MRawRunHeader::SwapAssignment(Short_t id0, Short_t id1)
151{
152 const Int_t n = fPixAssignment->GetSize();
153
154 // Look-up-table
155 UShort_t *lut = fPixAssignment->GetArray();
156
157 // Search for one of the hardware indices to get exchanged
158 int i;
159 for (i=0; i<n; i++)
160 if (lut[i]==id0 || lut[i]==id1)
161 break;
162
163 // Check if one of the two pixels were found
164 if (i==n)
165 {
166 *fLog << warn << "WARNING - Assignment of pixels with hardware ID " << id0 << " and " << id1;
167 *fLog << " should be exchanged, but none were found." << endl;
168 return kTRUE;
169 }
170
171 // Store first index
172 const Int_t idx0 = i;
173
174 // Search for the other hardware indices to get exchanged
175 for (i++; i<n; i++)
176 if (lut[i]==id0 || lut[i]==id1)
177 break;
178
179 // Check if the second pixel was found
180 if (i==n)
181 {
182 *fLog << err << "ERROR - Assignment of pixels with hardware ID " << id0 << " and " << id1;
183 *fLog << " should be exchanged, but only one was found." << endl;
184 return kFALSE;
185 }
186
187 const Int_t idx1 = i;
188
189 const Short_t p0 = lut[idx0];
190 const Short_t p1 = lut[idx1];
191
192 lut[idx0] = p1;
193 lut[idx1] = p0;
194
195 *fLog << inf << "Assignment of pixels with hardware ID " << id0 << " and " << id1 << " exchanged." << endl;
196
197 return kTRUE;
198}
199
200// --------------------------------------------------------------------------
201//
202// This implements a fix of the pixel assignment before 25.9.2005
203//
204// From magic_online (the pixel numbers are hardware indices):
205// --------------------------------------------------------------------------
206// From: Florian Goebel <fgoebel@mppmu.mpg.de>
207// Date: Sun Sep 25 2005 - 05:13:19 CEST
208//
209// [...]
210// - problem 2: pixels were swaped
211// - cause: opical fibers were wrongly connected to receiver board
212// 554 <-> 559
213// 555 <-> 558
214// 556 <-> 557
215// - action: reconnect correctly
216// - result: ok now
217// - comment: I don't know when this error was introduced, so backward
218// correction of the data is difficult.
219// Fortunately the effect is not too large since the affected 6 pixels are
220// on the outermost edge of the camera
221// Since this board has special pixels the optical fibers have been
222// unplugged several times.
223// !!!!! Whenever you unplug and reconnect optical fibers make really !!!!!
224// !!!!! sure you connect them back correctly. !!!!!
225// !!!!! Always contact me before you do so and if you have any doubts !!!!!
226// !!!!! how to reconnect the fibers ask me. !!!!!
227// These swapped pixels have only been found by chance when doing the
228// flatfielding.
229// [...]
230//
231Bool_t MRawRunHeader::FixAssignment()
232{
233 if (fRunNumber<53300 || fRunNumber>68754)
234 return kTRUE;
235
236 if (!SwapAssignment(554, 559))
237 return kFALSE;
238
239 if (!SwapAssignment(555, 558))
240 return kFALSE;
241
242 if (!SwapAssignment(556, 557))
243 return kFALSE;
244
245 return kTRUE;
246}
247
248// --------------------------------------------------------------------------
249//
250// Read in one run header from the binary file
251//
252Bool_t MRawRunHeader::ReadEvt(istream& fin)
253{
254 //
255 // read one RUN HEADER from the input stream
256 //
257 fMagicNumber = 0;
258
259 fin.read((char*)&fMagicNumber, 2); // Total=2
260
261 //
262 // check whether the the file has the right file type or not
263 //
264 if (fMagicNumber != kMagicNumber && fMagicNumber != kMagicNumber+1)
265 {
266 *fLog << err << "ERROR - Wrong Magic Number (0x" << hex << fMagicNumber << "): Not a Magic File!" << endl;
267 return kFALSE;
268 }
269
270 if (fMagicNumber == kMagicNumber+1)
271 *fLog << warn << "WARNING - This file maybe broken (0xc0c1) - DAQ didn't close it correctly!" << endl;
272
273 Byte_t dummy[16];
274
275 // ----- File format version -----
276 fin.read((char*)&fFormatVersion, 2); // Total=4
277 if (fFormatVersion>kMaxFormatVersion)
278 {
279 *fLog << err << "ERROR - File format V" << fFormatVersion << " not implemented!" << endl;
280 return kFALSE;
281 }
282
283 // ----- DAQ software format version -----
284 fin.read((char*)&fSoftVersion, 2); // Total=6
285
286 // ----- Camera geometry and telescope number -----
287 if (fFormatVersion>5)
288 {
289 fin.read((char*)&fCameraVersion, 2); // (+2)
290 fin.read((char*)&fTelescopeNumber, 2); // (+2)
291 }
292 // Maybe we should set fCameraVersion and fTelescopeNumber to 1
293 // in case of fFormatVersion<6
294
295 // ----- Run information -----
296 fin.read((char*)&fRunType, 2); // Total=8
297 fin.read((char*)&fRunNumber, 4); // Total=12
298 fin.read((char*)&fProjectName, fFormatVersion>5?100:22); // Total=34 (+78)
299 fin.read((char*)&fSourceName, fFormatVersion>5? 80:12); // Total=46 (+58)
300
301 if (fFormatVersion>5)
302 fin.read((char*)fObservationMode, 60); // (+60)
303 // Maybe we should set fObservationMode to something
304 // in case of fFormatVersion<6
305
306 // ----- Source position -----
307 if (fFormatVersion>5)
308 {
309 fin.read((char*)dummy, 4); // F32 SourceRA; Total=48
310 fin.read((char*)dummy, 4); // F32 SourceDEC; Total=52
311 }
312 // Maybe we should set these to something
313 // in case of fFormatVersion<6
314 fin.read((char*)dummy, 4); // F32 TelescopeRA; (+4)
315 fin.read((char*)dummy, 4); // F32 TelescopeDEC; (+4)
316 fin.read((char*)&fSourceEpochChar, 2); // Total=56
317 fin.read((char*)&fSourceEpochDate, 2); // Total=58
318
319 // ----- Old Start time -----
320 if (fFormatVersion<2) // Total += 10
321 {
322 UShort_t y, m, d;
323 fin.read((char*)dummy, 4); // Former fMJD[4],
324 fin.read((char*)&y, 2); // Former fDateYear[2]
325 fin.read((char*)&m, 2); // Former fDateMonth[2]
326 fin.read((char*)&d, 2); // Former fDateDay[2]
327 fRunStart.Set(y, m, d, 0, 0, 0, 0);
328 }
329
330 // ----- Data Geometry -----
331 fin.read((char*)&fNumCrates, 2); // Total=60
332 fin.read((char*)&fNumPixInCrate, 2); // Total=62
333 fin.read((char*)&fNumSamplesLoGain, 2); // Total=64
334 fin.read((char*)&fNumSamplesHiGain, 2); // Total=66
335
336 // ----- Number of events -----
337 fin.read((char*)&fNumEvents, 4); // Total=70
338
339 // ----- Start/Stop time -----
340 if (fFormatVersion>1)
341 {
342 fRunStart.ReadBinary(fin); // Total += 7
343 fRunStop.ReadBinary(fin); // Total += 7
344 }
345
346 //
347 // calculate size of array, create it and fill it
348 //
349 Int_t nPixel = fNumCrates*fNumPixInCrate;
350 fPixAssignment->Set(nPixel);
351
352 // ----- Pixel Assignement -----
353 fin.read((char*)fPixAssignment->GetArray(), nPixel*2);
354 fin.read((char*)&dummy, 16);
355
356 return FixAssignment();
357}
358
359// --------------------------------------------------------------------------
360//
361// Return the run type as string ("Data", "Pedestal", ...), for example
362// to print it as readable text.
363//
364const char *MRawRunHeader::GetRunTypeStr() const
365{
366 switch (fRunType)
367 {
368 case kRTData:
369 return "Data";
370 case kRTPedestal:
371 return "Pedestal";
372 case kRTCalibration:
373 return "Calibration";
374 case kRTPointRun:
375 return "Point-Run";
376 case kRTMonteCarlo:
377 return "Monte Carlo";
378 case kRTNone:
379 return "<none>";
380 default:
381 return "<unknown>";
382 }
383}
384
385// --------------------------------------------------------------------------
386//
387// print run header information on *fLog. The option 'header' supresses
388// the pixel index translation table.
389//
390void MRawRunHeader::Print(Option_t *t) const
391{
392 *fLog << all << endl;
393 *fLog << "MagicNumber: 0x" << hex << fMagicNumber << " - ";
394 switch (fMagicNumber)
395 {
396 case kMagicNumber: *fLog << "OK"; break;
397 case kMagicNumber+1: *fLog << "File not closed!"; break;
398 default: *fLog << "Wrong!"; break;
399 }
400 *fLog << endl;
401 *fLog << "Versions: " << dec << "Format=" << fFormatVersion << " ";
402 *fLog << "Software=" << fSoftVersion << " ";
403 if (fFormatVersion>5)
404 *fLog << "Camera=" << fCameraVersion;
405 *fLog << endl;
406 if (fFormatVersion>5)
407 *fLog << "Telescope: " << fTelescopeNumber << endl;
408 *fLog << "RunNumber: " << fRunNumber << " (Type=" << GetRunTypeStr() << ")" << endl;
409 *fLog << "ProjectName: '" << fProjectName << "'" << endl;
410 if (fFormatVersion>5)
411 *fLog << "Observation: '" << fObservationMode << "'" << endl;
412 *fLog << "Source: '" << fSourceName << "' " << " ";
413 *fLog << fSourceEpochChar << dec << fSourceEpochDate << endl;
414 *fLog << "Run Start: " << fRunStart << endl;
415 *fLog << "Run Stop: " << fRunStop << endl;
416 *fLog << "Crates: " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
417 *fLog << "Num Pixels: " << GetNumNormalPixels() << " (normal) + " << GetNumSpecialPixels() << " (special) = " << GetNumConnectedPixels() << " (total)" << endl;
418 *fLog << "Samples: " << fNumSamplesHiGain << "/" << fNumSamplesLoGain << " (hi/lo) = " << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumCrates * fNumPixInCrate /1024 << "kiB/Evt" << endl;
419 *fLog << "Evt Counter: " << fNumEvents << endl;
420
421 if (TString(t).Contains("header", TString::kIgnoreCase))
422 return;
423
424 *fLog << inf << "Assignment:" << hex << endl;
425 for (int i=0; i<GetNumPixel(); i++)
426 *fLog << setfill('0') << setw(3) << (*fPixAssignment)[i] << " ";
427
428 *fLog << dec << endl;
429}
430
431// --------------------------------------------------------------------------
432//
433// Return the assigned pixel number for the given FADC channel
434//
435Short_t MRawRunHeader::GetPixAssignment(UShort_t i) const
436{
437 // FIXME: Do we need a range check here?
438 return (Short_t)(*fPixAssignment)[i];
439}
440
441// --------------------------------------------------------------------------
442//
443// Return the number of pixel which are markes as connected in the
444// pix assignment (!=0)
445//
446UShort_t MRawRunHeader::GetNumConnectedPixels() const
447{
448 const Int_t num = fPixAssignment->GetSize();
449
450 UShort_t rc = 0;
451 for (int i=0; i<num; i++)
452 {
453 if (GetPixAssignment(i)!=0)
454 rc++;
455 }
456 return rc;
457}
458
459// --------------------------------------------------------------------------
460//
461// Return the number of pixel which are markes as connected and so-called
462// 'normal' pixels in the pix assignment (>0)
463//
464UShort_t MRawRunHeader::GetNumNormalPixels() const
465{
466 const Int_t num = fPixAssignment->GetSize();
467
468 UShort_t rc = 0;
469 for (int i=0; i<num; i++)
470 {
471 if (GetPixAssignment(i)>0)
472 rc++;
473 }
474 return rc;
475}
476
477// --------------------------------------------------------------------------
478//
479// Return the number of pixel which are markes as connected and so-called
480// 'special' pixels in the pix assignment (<0)
481//
482UShort_t MRawRunHeader::GetNumSpecialPixels() const
483{
484 const Int_t num = fPixAssignment->GetSize();
485
486 UShort_t rc = 0;
487 for (int i=0; i<num; i++)
488 {
489 if (GetPixAssignment(i)<0)
490 rc++;
491 }
492 return rc;
493}
494
495// --------------------------------------------------------------------------
496//
497// Return the maximum id which exists in the pix assignment
498//
499UShort_t MRawRunHeader::GetMaxPixId() const
500{
501 const Int_t num = fPixAssignment->GetSize();
502
503 Short_t rc = 0;
504 for (int i=0; i<num; i++)
505 rc = TMath::Max(GetPixAssignment(i), rc);
506
507 return rc;
508}
509
510// --------------------------------------------------------------------------
511//
512// Return minus th minimum id which exists in the pix assignment
513//
514UShort_t MRawRunHeader::GetMinPixId() const
515{
516 const Int_t num = fPixAssignment->GetSize();
517
518 Short_t rc = 0;
519 for (int i=0; i<num; i++)
520 rc = TMath::Min(GetPixAssignment(i), rc);
521
522 return (UShort_t)-rc;
523}
524
525// --------------------------------------------------------------------------
526//
527// Return the number of pixel in this event.
528//
529// WARNING: This is the number of pixels stored in this file which is
530// a multiple of the number of pixels per crate and in general
531// a number which is larger than the camera size!
532//
533// To know the range of the pixel indices please use the geometry
534// container!
535//
536UShort_t MRawRunHeader::GetNumPixel() const
537{
538 return fPixAssignment->GetSize();
539}
540
541// --------------------------------------------------------------------------
542//
543// Returns absolute size in bytes of the run header as read from a raw file.
544// This must be done _after_ the header is read, because the header doesn't
545// have a fixed size (used in MRawSocketRead)
546//
547Int_t MRawRunHeader::GetNumTotalBytes() const
548{
549 switch (fFormatVersion)
550 {
551 case 1:
552 return 80+fNumCrates*fNumPixInCrate*2+16;
553 case 2:
554 case 3:
555 case 4:
556 case 5:
557 return 84+fNumCrates*fNumPixInCrate*2+16;
558 case 6:
559 return 84+fNumCrates*fNumPixInCrate*2+16 +4+78+58+60+8;
560 }
561 return 0;
562}
Note: See TracBrowser for help on using the repository browser.