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

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