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

Last change on this file since 8433 was 8372, checked in by snruegam, 18 years ago
*** empty log message ***
File size: 21.0 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 if (fRunNumber>=200000)
346 {
347 if (!SwapAssignment(428, 429))
348 return kFALSE;
349 }
350
351 return kTRUE;
352}
353
354// --------------------------------------------------------------------------
355//
356// Read in one run header from the binary file
357//
358Bool_t MRawRunHeader::ReadEvt(istream& fin)
359{
360 //
361 // read one RUN HEADER from the input stream
362 //
363 fMagicNumber = 0;
364
365 fin.read((char*)&fMagicNumber, 2); // Total=2
366
367 //
368 // check whether the the file has the right file type or not
369 //
370 if (fMagicNumber != kMagicNumber && fMagicNumber != kMagicNumber+1)
371 {
372 *fLog << err << "ERROR - Wrong Magic Number (0x" << hex << fMagicNumber << "): Not a Magic File!" << endl;
373 return kFALSE;
374 }
375
376 if (fMagicNumber == kMagicNumber+1)
377 *fLog << warn << "WARNING - This file maybe broken (0xc0c1) - DAQ didn't close it correctly!" << endl;
378
379 Byte_t dummy[16];
380
381 // ----- File format version -----
382 fin.read((char*)&fFormatVersion, 2); // Total=4
383 if (fFormatVersion>kMaxFormatVersion)
384 {
385 *fLog << err << "ERROR - File format V" << fFormatVersion << " not implemented!" << endl;
386 return kFALSE;
387 }
388
389 if (fFormatVersion==7)
390 {
391 *fLog << err << "ERROR - File format V7 was for testing only and is not correctly implemented!" << endl;
392 return kFALSE;
393 }
394
395 // ----- DAQ software format version -----
396 fin.read((char*)&fSoftVersion, 2); // Total=6
397
398
399 fFadcType = 0;
400 if (fFormatVersion>7)
401 fin.read((char*)&fFadcType, 2);
402
403 // ----- Camera geometry and telescope number -----
404 fCameraVersion = 1;
405 fTelescopeNumber = 1;
406 if (fFormatVersion>5)
407 {
408 fin.read((char*)&fCameraVersion, 2); // (+2)
409 fin.read((char*)&fTelescopeNumber, 2); // (+2)
410 }
411
412 // ----- Run information -----
413 fin.read((char*)&fRunType, 2); // Total=8
414
415 fin.read((char*)&fRunNumber, 4); // Total=12
416 fin.read((char*)&fProjectName, fFormatVersion>5?100:22); // Total=34 (+78)
417 fin.read((char*)&fSourceName, fFormatVersion>5? 80:12); // Total=46 (+58)
418
419 if (fFormatVersion>5)
420 fin.read((char*)fObservationMode, 60); // (+60)
421 // Maybe we should set fObservationMode to something
422 // in case of fFormatVersion<6
423
424 // ----- Source position -----
425 if (fFormatVersion>5)
426 {
427 fin.read((char*)dummy, 4); // F32 SourceRA; Total=48
428 fin.read((char*)dummy, 4); // F32 SourceDEC; Total=52
429 }
430 // Maybe we should set these to something
431 // in case of fFormatVersion<6
432 fin.read((char*)dummy, 4); // F32 TelescopeRA; (+4)
433 fin.read((char*)dummy, 4); // F32 TelescopeDEC; (+4)
434 fin.read((char*)&fSourceEpochChar, 2); // Total=56
435 fin.read((char*)&fSourceEpochDate, 2); // Total=58
436
437 // ----- Old Start time -----
438 if (fFormatVersion<2) // Total += 10
439 {
440 UShort_t y, m, d;
441 fin.read((char*)dummy, 4); // Former fMJD[4],
442 fin.read((char*)&y, 2); // Former fDateYear[2]
443 fin.read((char*)&m, 2); // Former fDateMonth[2]
444 fin.read((char*)&d, 2); // Former fDateDay[2]
445 fRunStart.Set(y, m, d, 0, 0, 0, 0);
446 }
447
448 // ----- Data Geometry -----
449
450 fin.read((char*)&fNumCrates, 2); // MUX: number of channels
451 fin.read((char*)&fNumPixInCrate, 2); // MUX: number of pix in channel
452 fin.read((char*)&fNumSamplesLoGain, 2); // MUX: dummy (must be 0 for MUX data)
453 fin.read((char*)&fNumSamplesHiGain, 2); // MUX: Number of samples per pixel
454
455 // ----- Number of events -----
456 fin.read((char*)&fNumEvents, 4); // Total=70
457
458 // New in general features: (should they be included in new MAGIC1 formats, too?)
459 fNumBytesPerSample = 1; // 2 for MUX DATA
460 fFreqSampling = 300;
461 fNumSignificantBits = 8;
462 if (fFormatVersion>7)
463 {
464 fin.read((char*)&fNumBytesPerSample, 2);
465 fin.read((char*)&fFreqSampling, 2); // [MHz], 2000 for MuxFadc
466 fin.read((char*)&fNumSignificantBits, 1); // nominal resolution [# Bits], 10 for MuxFadc
467
468 if (fNumBytesPerSample!=2)
469 {
470 *fLog << err << "ERROR - " << fNumBytesPerSample << " bytes per sample are not supported!" << endl;
471 return kFALSE;
472 }
473 }
474
475 // ----- Start/Stop time -----
476 if (fFormatVersion>1)
477 {
478 fRunStart.ReadBinary(fin); // Total += 7
479 fRunStop.ReadBinary(fin); // Total += 7
480 }
481
482 //
483 // calculate size of array, create it and fill it
484 //
485 const Int_t nPixel = fNumCrates*fNumPixInCrate;
486 fPixAssignment->Set(nPixel);
487
488 // ----- Pixel Assignement -----
489 fin.read((char*)fPixAssignment->GetArray(), nPixel*2);
490
491 if (fFormatVersion<7)
492 fin.read((char*)&dummy, 16);
493
494 return FixAssignment();
495}
496
497// --------------------------------------------------------------------------
498//
499// Return the run type as string ("Data", "Pedestal", ...), for example
500// to print it as readable text.
501//
502const char *MRawRunHeader::GetRunTypeStr() const
503{
504 switch (fRunType)
505 {
506 case kRTData:
507 return "Data";
508 case kRTPedestal:
509 return "Pedestal";
510 case kRTCalibration:
511 return "Calibration";
512 case kRTPointRun:
513 return "Point-Run";
514 case kRTMonteCarlo:
515 return "Monte Carlo";
516 case kRTNone:
517 return "<none>";
518 default:
519 return "<unknown>";
520 }
521}
522
523// --------------------------------------------------------------------------
524//
525// print run header information on *fLog. The option 'header' supresses
526// the pixel index translation table.
527//
528void MRawRunHeader::Print(Option_t *t) const
529{
530 *fLog << all << endl;
531 *fLog << "MagicNumber: 0x" << hex << fMagicNumber << " - ";
532 switch (fMagicNumber)
533 {
534 case kMagicNumber: *fLog << "OK"; break;
535 case kMagicNumber+1: *fLog << "File not closed!"; break;
536 default: *fLog << "Wrong!"; break;
537 }
538 *fLog << endl;
539 *fLog << "Versions: " << dec << "Format=" << fFormatVersion << " ";
540 *fLog << "Software=" << fSoftVersion << " ";
541 if (fFormatVersion>5)
542 *fLog << "Camera=" << fCameraVersion;
543 *fLog << endl;
544 if (fFormatVersion>5)
545 *fLog << "Telescope: " << fTelescopeNumber << endl;
546 if (fFormatVersion>7)
547 *fLog << "FadcType: " << fFadcType << endl;
548 *fLog << "RunNumber: " << fRunNumber << " (Type=" << GetRunTypeStr() << ")" << endl;
549 *fLog << "ProjectName: '" << fProjectName << "'" << endl;
550 if (fFormatVersion>5)
551 *fLog << "Observation: '" << fObservationMode << "'" << endl;
552 *fLog << "Source: '" << fSourceName << "' " << " ";
553 *fLog << fSourceEpochChar << dec << fSourceEpochDate << endl;
554 *fLog << "Run Start: " << fRunStart << endl;
555 *fLog << "Run Stop: " << fRunStop << endl;
556 *fLog << "Crates: " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
557 *fLog << "Num Pixels: " << GetNumNormalPixels() << " (normal) + " << GetNumSpecialPixels() << " (special) = " << GetNumConnectedPixels() << " (total)" << endl;
558 if (fFormatVersion>6)
559 *fLog << "Sampling: " << fFreqSampling << "MHz with " << (int)fNumSignificantBits << " significant bits" << endl;
560 *fLog << "Samples: " << fNumSamplesHiGain << "/" << fNumSamplesLoGain << " (hi/lo) * " << fNumBytesPerSample << "B/sample = " << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumCrates * fNumPixInCrate * fNumBytesPerSample/1000 << "kB/Evt" << endl;
561 *fLog << "Evt Counter: " << fNumEvents << endl;
562
563 if (TString(t).Contains("header", TString::kIgnoreCase))
564 return;
565
566 *fLog << inf << "Assignment:" << hex << endl;
567 for (int i=0; i<GetNumPixel(); i++)
568 *fLog << setfill('0') << setw(3) << (*fPixAssignment)[i] << " ";
569
570 *fLog << dec << setfill(' ') << endl;
571}
572
573// --------------------------------------------------------------------------
574//
575// Return the assigned pixel number for the given FADC channel
576//
577Short_t MRawRunHeader::GetPixAssignment(UShort_t i) const
578{
579 // FIXME: Do we need a range check here?
580 return (Short_t)(*fPixAssignment)[i];
581}
582
583// --------------------------------------------------------------------------
584//
585// Return the number of pixel which are markes as connected in the
586// pix assignment (!=0)
587//
588UShort_t MRawRunHeader::GetNumConnectedPixels() const
589{
590 const Int_t num = fPixAssignment->GetSize();
591
592 UShort_t rc = 0;
593 for (int i=0; i<num; i++)
594 {
595 if (GetPixAssignment(i)!=0)
596 rc++;
597 }
598 return rc;
599}
600
601// --------------------------------------------------------------------------
602//
603// Return the number of pixel which are markes as connected and so-called
604// 'normal' pixels in the pix assignment (>0)
605//
606UShort_t MRawRunHeader::GetNumNormalPixels() const
607{
608 const Int_t num = fPixAssignment->GetSize();
609
610 UShort_t rc = 0;
611 for (int i=0; i<num; i++)
612 {
613 if (GetPixAssignment(i)>0)
614 rc++;
615 }
616 return rc;
617}
618
619// --------------------------------------------------------------------------
620//
621// Return the number of pixel which are markes as connected and so-called
622// 'special' pixels in the pix assignment (<0)
623//
624UShort_t MRawRunHeader::GetNumSpecialPixels() const
625{
626 const Int_t num = fPixAssignment->GetSize();
627
628 UShort_t rc = 0;
629 for (int i=0; i<num; i++)
630 {
631 if (GetPixAssignment(i)<0)
632 rc++;
633 }
634 return rc;
635}
636
637// --------------------------------------------------------------------------
638//
639// Return the maximum id which exists in the pix assignment
640//
641UShort_t MRawRunHeader::GetMaxPixId() const
642{
643 const Int_t num = fPixAssignment->GetSize();
644
645 Short_t rc = 0;
646 for (int i=0; i<num; i++)
647 rc = TMath::Max(GetPixAssignment(i), rc);
648
649 return rc;
650}
651
652// --------------------------------------------------------------------------
653//
654// Return minus th minimum id which exists in the pix assignment
655//
656UShort_t MRawRunHeader::GetMinPixId() const
657{
658 const Int_t num = fPixAssignment->GetSize();
659
660 Short_t rc = 0;
661 for (int i=0; i<num; i++)
662 rc = TMath::Min(GetPixAssignment(i), rc);
663
664 return (UShort_t)-rc;
665}
666
667// --------------------------------------------------------------------------
668//
669// Return the number of pixel in this event.
670//
671// WARNING: This is the number of pixels stored in this file which is
672// a multiple of the number of pixels per crate and in general
673// a number which is larger than the camera size!
674//
675// To know the range of the pixel indices please use the geometry
676// container!
677//
678UShort_t MRawRunHeader::GetNumPixel() const
679{
680 return fPixAssignment->GetSize();
681}
682
683// --------------------------------------------------------------------------
684//
685// Returns absolute size in bytes of the run header as read from a raw file.
686// This must be done _after_ the header is read, because the header doesn't
687// have a fixed size (used in MRawSocketRead)
688//
689Int_t MRawRunHeader::GetNumTotalBytes() const
690{
691 switch (fFormatVersion)
692 {
693 case 1:
694 return 80+fNumCrates*fNumPixInCrate*2+16;
695 case 2:
696 case 3:
697 case 4:
698 case 5:
699 return 84+fNumCrates*fNumPixInCrate*2+16;
700 case 6:
701 return 84+fNumCrates*fNumPixInCrate*2+16 +4+78+58+60+8;
702 case 7:
703 return 84+fNumCrates*fNumPixInCrate*2+16 +4+78+58+60+8 +3-16;
704 }
705 return 0;
706}
Note: See TracBrowser for help on using the repository browser.