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

Last change on this file since 8352 was 8344, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 20.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//
32// RAW DATA FORMAT VERSION
33// =======================
34//
35// Format Version 8:
36// -----------------
37// + fNumBytesPerSample;
38// + fFreqSampling;
39// + fNumSignificantBits;
40// * changes in MRawCrateHeader
41//
42// Format Version 7:
43// -----------------
44// - unused
45//
46// Format Version 6:
47// -----------------
48// + added CameraVersion
49// + added TelescopeNumber
50// + added ObservationMode
51// + added dummies for TelescopeRa/Dec
52//
53// Format Version 5:
54// -----------------
55// - now the sub millisecond information of the time is valid and decoded
56// which enhances the precision from 51.2us to 200ns
57//
58// Format Version 4:
59// -----------------
60// - added support for pixels with negative IDs
61//
62// Format Version 3:
63// -----------------
64// - ???
65//
66// Format Version 2:
67// -----------------
68// - removed mjd from data
69// - added start time
70// - added stop time
71//
72//
73// MRawRunHeader CLASS VERSION
74// ===========================
75//
76// Format Version 6:
77// -----------------
78// - added fNumBytesPerSample;
79// - added fFreqSampling;
80// - added fNumSignificantBits;
81//
82// Class Version 5:
83// -----------------
84// - for compatibility with newer camera versions
85//
86// Class Version 4:
87// -----------------
88// - added fCameraVersion
89// - added fTelescopeNumber
90// - changed length of fProjectName to 101
91// - changed length of fSourceName to 81
92//
93// Class Version 3:
94// ----------------
95// - enhanced SourceName and ProjectName by one character, because
96// without telling us the guranteed trailing \0-character has
97// skipped
98//
99// Class Version 2:
100// ----------------
101// - removed fMJD, fYear, fMonth, fDay
102// - added fRunStart
103// - added fRunStop
104//
105// Class Version 1:
106// ----------------
107// - first implementation
108//
109////////////////////////////////////////////////////////////////////////////
110
111#include "MRawRunHeader.h"
112
113#include <fstream>
114#include <iomanip>
115
116#include "MLog.h"
117#include "MLogManip.h"
118
119#include "MArrayS.h"
120
121ClassImp(MRawRunHeader);
122
123using namespace std;
124
125const UShort_t MRawRunHeader::kMagicNumber = 0xc0c0;
126const Byte_t MRawRunHeader::kMaxFormatVersion = 8;
127
128// --------------------------------------------------------------------------
129//
130// Default constructor. Creates array which stores the pixel assignment.
131//
132//
133MRawRunHeader::MRawRunHeader(const char *name, const char *title) : fPixAssignment(NULL)
134{
135 fName = name ? name : "MRawRunHeader";
136 fTitle = title ? title : "Raw Run Header Information";
137
138 fPixAssignment = new MArrayS(0);
139
140 fFormatVersion=0;
141 fSoftVersion=0;
142 fTelescopeNumber=0;
143 fCameraVersion=0;
144 fFadcType = 0;
145 fRunType=kRTNone; // use 0xffff for invalidation, 0 means: Data run
146 fRunNumber=0;
147 memset(fProjectName, 0, 101);
148 memset(fSourceName, 0, 81);
149 memset(fObservationMode, 0, 61);
150 fSourceEpochChar[0]=0;
151 fSourceEpochDate=0;
152 fNumCrates=0;
153 fNumPixInCrate=0;
154 fNumSamplesLoGain=0;
155 fNumSamplesHiGain=0;
156 fNumEvents=0;
157}
158
159// --------------------------------------------------------------------------
160//
161// Destructor. Deletes the 'pixel-assignment-array'
162//
163MRawRunHeader::~MRawRunHeader()
164{
165 delete fPixAssignment;
166}
167
168// --------------------------------------------------------------------------
169//
170// Swap the assignment of the pixels with hardware id id0 and id1
171//
172Bool_t MRawRunHeader::SwapAssignment(Short_t id0, Short_t id1)
173{
174 const Int_t n = fPixAssignment->GetSize();
175
176 // Look-up-table
177 UShort_t *lut = fPixAssignment->GetArray();
178
179 // Search for one of the hardware indices to get exchanged
180 int i;
181 for (i=0; i<n; i++)
182 if (lut[i]==id0 || lut[i]==id1)
183 break;
184
185 // Check if one of the two pixels were found
186 if (i==n)
187 {
188 *fLog << warn << "WARNING - Assignment of pixels with hardware ID " << id0 << " and " << id1;
189 *fLog << " should be exchanged, but none were found." << endl;
190 return kTRUE;
191 }
192
193 // Store first index
194 const Int_t idx0 = i;
195
196 // Search for the other hardware indices to get exchanged
197 for (i++; i<n; i++)
198 if (lut[i]==id0 || lut[i]==id1)
199 break;
200
201 // Check if the second pixel was found
202 if (i==n)
203 {
204 *fLog << err << "ERROR - Assignment of pixels with hardware ID " << id0 << " and " << id1;
205 *fLog << " should be exchanged, but only one was found." << endl;
206 return kFALSE;
207 }
208
209 const Int_t idx1 = i;
210
211 const Short_t p0 = lut[idx0];
212 const Short_t p1 = lut[idx1];
213
214 lut[idx0] = p1;
215 lut[idx1] = p0;
216
217 *fLog << inf << "Assignment of pixels with hardware ID " << id0 << " and " << id1 << " exchanged." << endl;
218
219 return kTRUE;
220}
221
222// --------------------------------------------------------------------------
223//
224// This implements a fix of the pixel assignment before 25.9.2005
225//
226// From magic_online (the pixel numbers are hardware indices):
227// --------------------------------------------------------------------------
228// From: Florian Goebel <fgoebel@mppmu.mpg.de>
229// Date: Sun Sep 25 2005 - 05:13:19 CEST
230//
231// [...]
232// - problem 2: pixels were swaped
233// - cause: opical fibers were wrongly connected to receiver board
234// 554 <-> 559
235// 555 <-> 558
236// 556 <-> 557
237// - action: reconnect correctly
238// - result: ok now
239// - comment: I don't know when this error was introduced, so backward
240// correction of the data is difficult.
241// Fortunately the effect is not too large since the affected 6 pixels are
242// on the outermost edge of the camera
243// Since this board has special pixels the optical fibers have been
244// unplugged several times.
245// !!!!! Whenever you unplug and reconnect optical fibers make really !!!!!
246// !!!!! sure you connect them back correctly. !!!!!
247// !!!!! Always contact me before you do so and if you have any doubts !!!!!
248// !!!!! how to reconnect the fibers ask me. !!!!!
249// These swapped pixels have only been found by chance when doing the
250// flatfielding.
251// [...]
252//
253// --------------------------------------------------------------------------
254//
255// MAGIC runbook CC_2006_04_22_22_28_52.rbk
256//
257// [2006-04-22 23:14:13]
258//
259// [...]
260// Found 2 pairs of swapped pixels.
261// We corrected swapped pixels 54<->55 in the receiver boards. We probably
262// swapped today in the camera.
263// We did not correct 92<-<93 which are likely swapped. Will check and correct
264// tomorrow.
265//
266// ---
267//
268// comments:
269// - 54<->55 were corrected but have not been swapped, hence they are swapped
270// since then (run 88560 ok, run 88669 swapped; between them mostly dummy and
271// test runs)
272// - 92<->93 are never swapped, always ok.
273//
274// --------------------------------------------------------------------------
275//
276// MAGIC runbook CC_2006_08_28_19_40_18.rbk
277//
278// [2006-08-28 23:09:07]
279// While doing a flatfielding we have found out that the signals for pixels
280// 119 and 120 were swapped. We have fixed it by exchanging the corresponding
281// fibers at the input of the receivers (not before the splitters!).
282//
283// ---
284//
285// MAGIC runbook CC_2006_08_29_15_19_14.rbk
286//
287// [2006-08-29 16:43:09]
288// In the last hours we have found out and fixed a good number of pixels which
289// were swapped: 119-120, 160-161-162 and 210-263. According to Florian,
290// 160-161-162 and 210-263 were swapped since November 2005.
291//
292// ---
293//
294// mail Florian Goebel (08/30/2006 03:13 PM):
295//
296// As far as I can tell pixels 161 and 162 as well as 263 and 210 were
297// swapped in the trigger. This leads to some inefficiency of the trigger.
298// However, they were not swapped in the readout. So, you don't have to
299// correct anything in the data for these pixels.
300//
301// ---
302//
303// comments:
304// - 119-120 swapped between run 93251 (not swapped) and 93283 (swapped)
305// (only testruns between these runs)
306// corrected since run 99354 (== runbook [2006-08-28 23:09:07])
307// - 160 never swapped
308// - 161-162 were only swapped in the trigger, but nevertheless were
309// "corrected" also in the signal. Hence signal swapped since 99354
310//
311// --------------------------------------------------------------------------
312
313Bool_t MRawRunHeader::FixAssignment()
314{
315 if (fRunNumber>=53300 && fRunNumber<=68754)
316 {
317 if (!SwapAssignment(554, 559))
318 return kFALSE;
319 if (!SwapAssignment(555, 558))
320 return kFALSE;
321 if (!SwapAssignment(556, 557))
322 return kFALSE;
323 }
324
325 if (fRunNumber>=93283 && fRunNumber<99354)
326 {
327 if (!SwapAssignment(119, 120))
328 return kFALSE;
329 }
330
331 if (fRunNumber>=99354 && fRunNumber<=101789)
332 {
333 if (!SwapAssignment(161, 162))
334 return kFALSE;
335 if (!SwapAssignment(210, 263))
336 return kFALSE;
337 }
338
339 if (fRunNumber>=88669)
340 {
341 if (!SwapAssignment(54, 55))
342 return kFALSE;
343 }
344
345 return kTRUE;
346}
347
348// --------------------------------------------------------------------------
349//
350// Read in one run header from the binary file
351//
352Bool_t MRawRunHeader::ReadEvt(istream& fin)
353{
354 //
355 // read one RUN HEADER from the input stream
356 //
357 fMagicNumber = 0;
358
359 fin.read((char*)&fMagicNumber, 2); // Total=2
360
361 //
362 // check whether the the file has the right file type or not
363 //
364 if (fMagicNumber != kMagicNumber && fMagicNumber != kMagicNumber+1)
365 {
366 *fLog << err << "ERROR - Wrong Magic Number (0x" << hex << fMagicNumber << "): Not a Magic File!" << endl;
367 return kFALSE;
368 }
369
370 if (fMagicNumber == kMagicNumber+1)
371 *fLog << warn << "WARNING - This file maybe broken (0xc0c1) - DAQ didn't close it correctly!" << endl;
372
373 Byte_t dummy[16];
374
375 // ----- File format version -----
376 fin.read((char*)&fFormatVersion, 2); // Total=4
377 if (fFormatVersion>kMaxFormatVersion)
378 {
379 *fLog << err << "ERROR - File format V" << fFormatVersion << " not implemented!" << endl;
380 return kFALSE;
381 }
382
383 if (fFormatVersion==7)
384 {
385 *fLog << err << "ERROR - File format V7 was for testing only and is not correctly implemented!" << endl;
386 return kFALSE;
387 }
388
389 // ----- DAQ software format version -----
390 fin.read((char*)&fSoftVersion, 2); // Total=6
391
392
393 fFadcType = 0;
394 if (fFormatVersion>7)
395 fin.read((char*)&fFadcType, 2);
396
397 // ----- Camera geometry and telescope number -----
398 fCameraVersion = 1;
399 fTelescopeNumber = 1;
400 if (fFormatVersion>5)
401 {
402 fin.read((char*)&fCameraVersion, 2); // (+2)
403 fin.read((char*)&fTelescopeNumber, 2); // (+2)
404 }
405
406 // ----- Run information -----
407 fin.read((char*)&fRunType, 2); // Total=8
408
409 fin.read((char*)&fRunNumber, 4); // Total=12
410 fin.read((char*)&fProjectName, fFormatVersion>5?100:22); // Total=34 (+78)
411 fin.read((char*)&fSourceName, fFormatVersion>5? 80:12); // Total=46 (+58)
412
413 if (fFormatVersion>5)
414 fin.read((char*)fObservationMode, 60); // (+60)
415 // Maybe we should set fObservationMode to something
416 // in case of fFormatVersion<6
417
418 // ----- Source position -----
419 if (fFormatVersion>5)
420 {
421 fin.read((char*)dummy, 4); // F32 SourceRA; Total=48
422 fin.read((char*)dummy, 4); // F32 SourceDEC; Total=52
423 }
424 // Maybe we should set these to something
425 // in case of fFormatVersion<6
426 fin.read((char*)dummy, 4); // F32 TelescopeRA; (+4)
427 fin.read((char*)dummy, 4); // F32 TelescopeDEC; (+4)
428 fin.read((char*)&fSourceEpochChar, 2); // Total=56
429 fin.read((char*)&fSourceEpochDate, 2); // Total=58
430
431 // ----- Old Start time -----
432 if (fFormatVersion<2) // Total += 10
433 {
434 UShort_t y, m, d;
435 fin.read((char*)dummy, 4); // Former fMJD[4],
436 fin.read((char*)&y, 2); // Former fDateYear[2]
437 fin.read((char*)&m, 2); // Former fDateMonth[2]
438 fin.read((char*)&d, 2); // Former fDateDay[2]
439 fRunStart.Set(y, m, d, 0, 0, 0, 0);
440 }
441
442 // ----- Data Geometry -----
443
444 fin.read((char*)&fNumCrates, 2); // MUX: number of channels
445 fin.read((char*)&fNumPixInCrate, 2); // MUX: number of pix in channel
446 fin.read((char*)&fNumSamplesLoGain, 2); // MUX: dummy (must be 0 for MUX data)
447 fin.read((char*)&fNumSamplesHiGain, 2); // MUX: Number of samples per pixel
448
449 // ----- Number of events -----
450 fin.read((char*)&fNumEvents, 4); // Total=70
451
452 // New in general features: (should they be included in new MAGIC1 formats, too?)
453 fNumBytesPerSample = 1; // 2 for MUX DATA
454 fFreqSampling = 300;
455 fNumSignificantBits = 8;
456 if (fFormatVersion>7)
457 {
458 fin.read((char*)&fNumBytesPerSample, 2);
459 fin.read((char*)&fFreqSampling, 2); // [MHz], 2000 for MuxFadc
460 fin.read((char*)&fNumSignificantBits, 1); // nominal resolution [# Bits], 10 for MuxFadc
461
462 if (fNumBytesPerSample!=2)
463 {
464 *fLog << err << "ERROR - " << fNumBytesPerSample << " bytes per sample are not supported!" << endl;
465 return kFALSE;
466 }
467 }
468
469 // ----- Start/Stop time -----
470 if (fFormatVersion>1)
471 {
472 fRunStart.ReadBinary(fin); // Total += 7
473 fRunStop.ReadBinary(fin); // Total += 7
474 }
475
476 //
477 // calculate size of array, create it and fill it
478 //
479 const Int_t nPixel = fNumCrates*fNumPixInCrate;
480 fPixAssignment->Set(nPixel);
481
482 // ----- Pixel Assignement -----
483 fin.read((char*)fPixAssignment->GetArray(), nPixel*2);
484
485 if (fFormatVersion<7)
486 fin.read((char*)&dummy, 16);
487
488 return FixAssignment();
489}
490
491// --------------------------------------------------------------------------
492//
493// Return the run type as string ("Data", "Pedestal", ...), for example
494// to print it as readable text.
495//
496const char *MRawRunHeader::GetRunTypeStr() const
497{
498 switch (fRunType)
499 {
500 case kRTData:
501 return "Data";
502 case kRTPedestal:
503 return "Pedestal";
504 case kRTCalibration:
505 return "Calibration";
506 case kRTPointRun:
507 return "Point-Run";
508 case kRTMonteCarlo:
509 return "Monte Carlo";
510 case kRTNone:
511 return "<none>";
512 default:
513 return "<unknown>";
514 }
515}
516
517// --------------------------------------------------------------------------
518//
519// print run header information on *fLog. The option 'header' supresses
520// the pixel index translation table.
521//
522void MRawRunHeader::Print(Option_t *t) const
523{
524 *fLog << all << endl;
525 *fLog << "MagicNumber: 0x" << hex << fMagicNumber << " - ";
526 switch (fMagicNumber)
527 {
528 case kMagicNumber: *fLog << "OK"; break;
529 case kMagicNumber+1: *fLog << "File not closed!"; break;
530 default: *fLog << "Wrong!"; break;
531 }
532 *fLog << endl;
533 *fLog << "Versions: " << dec << "Format=" << fFormatVersion << " ";
534 *fLog << "Software=" << fSoftVersion << " ";
535 if (fFormatVersion>5)
536 *fLog << "Camera=" << fCameraVersion;
537 *fLog << endl;
538 if (fFormatVersion>5)
539 *fLog << "Telescope: " << fTelescopeNumber << endl;
540 if (fFormatVersion>7)
541 *fLog << "FadcType: " << fFadcType << endl;
542 *fLog << "RunNumber: " << fRunNumber << " (Type=" << GetRunTypeStr() << ")" << endl;
543 *fLog << "ProjectName: '" << fProjectName << "'" << endl;
544 if (fFormatVersion>5)
545 *fLog << "Observation: '" << fObservationMode << "'" << endl;
546 *fLog << "Source: '" << fSourceName << "' " << " ";
547 *fLog << fSourceEpochChar << dec << fSourceEpochDate << endl;
548 *fLog << "Run Start: " << fRunStart << endl;
549 *fLog << "Run Stop: " << fRunStop << endl;
550 *fLog << "Crates: " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
551 *fLog << "Num Pixels: " << GetNumNormalPixels() << " (normal) + " << GetNumSpecialPixels() << " (special) = " << GetNumConnectedPixels() << " (total)" << endl;
552 if (fFormatVersion>6)
553 *fLog << "Sampling: " << fFreqSampling << "MHz with " << (int)fNumSignificantBits << " significant bits" << endl;
554 *fLog << "Samples: " << fNumSamplesHiGain << "/" << fNumSamplesLoGain << " (hi/lo) * " << fNumBytesPerSample << "B/sample = " << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumCrates * fNumPixInCrate * fNumBytesPerSample/1000 << "kB/Evt" << endl;
555 *fLog << "Evt Counter: " << fNumEvents << endl;
556
557 if (TString(t).Contains("header", TString::kIgnoreCase))
558 return;
559
560 *fLog << inf << "Assignment:" << hex << endl;
561 for (int i=0; i<GetNumPixel(); i++)
562 *fLog << setfill('0') << setw(3) << (*fPixAssignment)[i] << " ";
563
564 *fLog << dec << setfill(' ') << endl;
565}
566
567// --------------------------------------------------------------------------
568//
569// Return the assigned pixel number for the given FADC channel
570//
571Short_t MRawRunHeader::GetPixAssignment(UShort_t i) const
572{
573 // FIXME: Do we need a range check here?
574 return (Short_t)(*fPixAssignment)[i];
575}
576
577// --------------------------------------------------------------------------
578//
579// Return the number of pixel which are markes as connected in the
580// pix assignment (!=0)
581//
582UShort_t MRawRunHeader::GetNumConnectedPixels() const
583{
584 const Int_t num = fPixAssignment->GetSize();
585
586 UShort_t rc = 0;
587 for (int i=0; i<num; i++)
588 {
589 if (GetPixAssignment(i)!=0)
590 rc++;
591 }
592 return rc;
593}
594
595// --------------------------------------------------------------------------
596//
597// Return the number of pixel which are markes as connected and so-called
598// 'normal' pixels in the pix assignment (>0)
599//
600UShort_t MRawRunHeader::GetNumNormalPixels() const
601{
602 const Int_t num = fPixAssignment->GetSize();
603
604 UShort_t rc = 0;
605 for (int i=0; i<num; i++)
606 {
607 if (GetPixAssignment(i)>0)
608 rc++;
609 }
610 return rc;
611}
612
613// --------------------------------------------------------------------------
614//
615// Return the number of pixel which are markes as connected and so-called
616// 'special' pixels in the pix assignment (<0)
617//
618UShort_t MRawRunHeader::GetNumSpecialPixels() const
619{
620 const Int_t num = fPixAssignment->GetSize();
621
622 UShort_t rc = 0;
623 for (int i=0; i<num; i++)
624 {
625 if (GetPixAssignment(i)<0)
626 rc++;
627 }
628 return rc;
629}
630
631// --------------------------------------------------------------------------
632//
633// Return the maximum id which exists in the pix assignment
634//
635UShort_t MRawRunHeader::GetMaxPixId() const
636{
637 const Int_t num = fPixAssignment->GetSize();
638
639 Short_t rc = 0;
640 for (int i=0; i<num; i++)
641 rc = TMath::Max(GetPixAssignment(i), rc);
642
643 return rc;
644}
645
646// --------------------------------------------------------------------------
647//
648// Return minus th minimum id which exists in the pix assignment
649//
650UShort_t MRawRunHeader::GetMinPixId() const
651{
652 const Int_t num = fPixAssignment->GetSize();
653
654 Short_t rc = 0;
655 for (int i=0; i<num; i++)
656 rc = TMath::Min(GetPixAssignment(i), rc);
657
658 return (UShort_t)-rc;
659}
660
661// --------------------------------------------------------------------------
662//
663// Return the number of pixel in this event.
664//
665// WARNING: This is the number of pixels stored in this file which is
666// a multiple of the number of pixels per crate and in general
667// a number which is larger than the camera size!
668//
669// To know the range of the pixel indices please use the geometry
670// container!
671//
672UShort_t MRawRunHeader::GetNumPixel() const
673{
674 return fPixAssignment->GetSize();
675}
676
677// --------------------------------------------------------------------------
678//
679// Returns absolute size in bytes of the run header as read from a raw file.
680// This must be done _after_ the header is read, because the header doesn't
681// have a fixed size (used in MRawSocketRead)
682//
683Int_t MRawRunHeader::GetNumTotalBytes() const
684{
685 switch (fFormatVersion)
686 {
687 case 1:
688 return 80+fNumCrates*fNumPixInCrate*2+16;
689 case 2:
690 case 3:
691 case 4:
692 case 5:
693 return 84+fNumCrates*fNumPixInCrate*2+16;
694 case 6:
695 return 84+fNumCrates*fNumPixInCrate*2+16 +4+78+58+60+8;
696 case 7:
697 return 84+fNumCrates*fNumPixInCrate*2+16 +4+78+58+60+8 +3-16;
698 }
699 return 0;
700}
Note: See TracBrowser for help on using the repository browser.