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

Last change on this file since 9394 was 9351, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 38.1 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-2008
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 11:
36// -----------------
37// * all variables got four bytes
38// * header sizes allow to make the format backward compatible
39// + fHeaderSizeRun
40// + fHeaderSizeEvt
41// + fHeaderSizeCrate
42// + fFileNumber
43// + fNumSamplesRemovedHead
44// + fNumSamplesRemovedTail
45//
46// Format Version 10:
47// -----------------
48// ?
49//
50// Format Version 9:
51// -----------------
52// + fNumEventsRead;
53// + fSamplingFrequency
54// - fFreqSampling
55// + fFadcResolution;
56// - fNumSignificantBits
57//
58// Format Version 8:
59// -----------------
60// + fNumBytesPerSample;
61// + fFreqSampling;
62// + fNumSignificantBits
63// * changes in MRawCrateHeader
64//
65// Format Version 7:
66// -----------------
67// - unused
68//
69// Format Version 6:
70// -----------------
71// + added CameraVersion
72// + added TelescopeNumber
73// + added ObservationMode
74// + added dummies for TelescopeRa/Dec
75//
76// Format Version 5:
77// -----------------
78// - now the sub millisecond information of the time is valid and decoded
79// which enhances the precision from 51.2us to 200ns
80//
81// Format Version 4:
82// -----------------
83// - added support for pixels with negative IDs
84//
85// Format Version 3:
86// -----------------
87// - ???
88//
89// Format Version 2:
90// -----------------
91// - removed mjd from data
92// - added start time
93// - added stop time
94//
95//
96// MRawRunHeader CLASS VERSION
97// ===========================
98//
99// Format Version 10:
100// -----------------
101// - added fHeaderSizeRun
102// - added fHeaderSizeEvt
103// - added fHeaderSizeCrate
104// - added fFileNumber
105// - increased fSourceEpochChar
106//
107// Format Version 7:
108// -----------------
109// - added fNumEventsRead;
110// * renamed fFreqSampling to fSamplingFrequency
111// * renamed fNumSignificantBits to fFadcResolution
112//
113// Format Version 6:
114// -----------------
115// - added fNumBytesPerSample;
116// - added fSamplingFrequency;
117// - added fFadcResolution;
118//
119// Class Version 5:
120// -----------------
121// - for compatibility with newer camera versions
122//
123// Class Version 4:
124// -----------------
125// - added fCameraVersion
126// - added fTelescopeNumber
127// - changed length of fProjectName to 101
128// - changed length of fSourceName to 81
129//
130// Class Version 3:
131// ----------------
132// - enhanced SourceName and ProjectName by one character, because
133// without telling us the guranteed trailing \0-character has
134// skipped
135//
136// Class Version 2:
137// ----------------
138// - removed fMJD, fYear, fMonth, fDay
139// - added fRunStart
140// - added fRunStop
141//
142// Class Version 1:
143// ----------------
144// - first implementation
145//
146////////////////////////////////////////////////////////////////////////////
147#include "MRawRunHeader.h"
148
149#include <fstream>
150#include <iomanip>
151
152#include <TArrayC.h>
153
154#include "MLog.h"
155#include "MLogManip.h"
156
157#include "MArrayS.h"
158#include "MString.h"
159
160ClassImp(MRawRunHeader);
161
162using namespace std;
163
164const UShort_t MRawRunHeader::kMagicNumber = 0xc0c0;
165const Byte_t MRawRunHeader::kMaxFormatVersion = 11;
166
167// --------------------------------------------------------------------------
168//
169// Default constructor. Creates array which stores the pixel assignment.
170//
171//
172MRawRunHeader::MRawRunHeader(const char *name, const char *title) : fPixAssignment(NULL)
173{
174 fName = name ? name : "MRawRunHeader";
175 fTitle = title ? title : "Raw Run Header Information";
176
177 fPixAssignment = new MArrayS(0);
178
179 // Remark: If we read old MC data from a root file which do not
180 // yet contain one of these variable, the value given here is
181 // the default. Do not mix files with and without a value if the
182 // files with the value do not match the default!
183 fFormatVersion=11;
184 fSoftVersion=0;
185 fTelescopeNumber=1;
186 fCameraVersion=1;
187 fFadcType=1;
188 fRunType=kRTNone; // use 0xffff for invalidation, 0 means: Data run
189 fRunNumber=0;
190 fFileNumber=0;
191 memset(fProjectName, 0, 101);
192 memset(fSourceName, 0, 81);
193 memset(fObservationMode, 0, 61);
194 fSourceEpochChar[0]=0;
195 fSourceEpochChar[1]=0;
196 fSourceEpochDate=0;
197 fNumCrates=0;
198 fNumPixInCrate=0;
199 fNumSamplesLoGain=0;
200 fNumSamplesHiGain=0;
201 fNumEvents=0;
202 fNumEventsRead=0;
203 fNumBytesPerSample=1;
204 fSamplingFrequency=300;
205 fFadcResolution=8;
206
207 fHeaderSizeRun=0;
208 fHeaderSizeEvt=0;
209 fHeaderSizeCrate=0;
210}
211
212// --------------------------------------------------------------------------
213//
214// Destructor. Deletes the 'pixel-assignment-array'
215//
216MRawRunHeader::~MRawRunHeader()
217{
218 delete fPixAssignment;
219}
220
221// --------------------------------------------------------------------------
222//
223// Swap the assignment of the pixels with hardware id id0 and id1
224//
225Bool_t MRawRunHeader::SwapAssignment(Short_t id0, Short_t id1)
226{
227 const Int_t n = fPixAssignment->GetSize();
228
229 // Look-up-table
230 UShort_t *lut = fPixAssignment->GetArray();
231
232 // Search for one of the hardware indices to get exchanged
233 int i;
234 for (i=0; i<n; i++)
235 if (lut[i]==id0 || lut[i]==id1)
236 break;
237
238 // Check if one of the two pixels were found
239 if (i==n)
240 {
241 *fLog << warn << "WARNING - Assignment of pixels with hardware ID " << id0 << " and " << id1;
242 *fLog << " should be exchanged, but none were found." << endl;
243 return kTRUE;
244 }
245
246 // Store first index
247 const Int_t idx0 = i;
248
249 // Search for the other hardware indices to get exchanged
250 for (i++; i<n; i++)
251 if (lut[i]==id0 || lut[i]==id1)
252 break;
253
254 // Check if the second pixel was found
255 if (i==n)
256 {
257 *fLog << err << "ERROR - Assignment of pixels with hardware ID " << id0 << " and " << id1;
258 *fLog << " should be exchanged, but only one was found." << endl;
259 return kFALSE;
260 }
261
262 const Int_t idx1 = i;
263
264 const Short_t p0 = lut[idx0];
265 const Short_t p1 = lut[idx1];
266
267 lut[idx0] = p1;
268 lut[idx1] = p0;
269
270 *fLog << inf << "Assignment of pixels with hardware ID " << id0 << " and " << id1 << " exchanged." << endl;
271
272 return kTRUE;
273}
274
275// --------------------------------------------------------------------------
276//
277// Return Telescope number, runnumber and filenumber on the form:
278// run
279// as string for runnumber<1000000 and
280// telescope:run/file
281// otherwise.
282//
283TString MRawRunHeader::GetStringID() const
284{
285 return fRunNumber<1000000?MString::Format("%d", fRunNumber):MString::Format("%d:%d/%d", fTelescopeNumber, fRunNumber, fFileNumber);
286}
287
288// --------------------------------------------------------------------------
289//
290// Consistency checks. See code for detils.
291//
292Bool_t MRawRunHeader::IsConsistent() const
293{
294 // FIXME: Match first digits of run-number with telescope number
295
296 if (fFormatVersion>10)
297 {
298 if (GetTypeID()!=fTelescopeNumber &&
299 GetTypeID()!=fTelescopeNumber*10U &&
300 GetTypeID()!=fTelescopeNumber*10U+5U)
301 {
302 *fLog << err << "ERROR - Telscope number " << fTelescopeNumber << " doesn't match the first two digits of the run number " << fRunNumber << "." << endl;
303 return kFALSE;
304 }
305
306 // Old formats can not contain a run number larger 999999
307 if (fRunNumber<1000000)
308 {
309 *fLog << err << "ERROR - Run number " << fRunNumber << " smaller than 1000000." << endl;
310 return kFALSE;
311 }
312 }
313
314 // Check for correct number of bytes in data stream
315 if (fFormatVersion>7 && fNumBytesPerSample!=2)
316 {
317 *fLog << err << "ERROR - " << fNumBytesPerSample << " bytes per sample are not supported!" << endl;
318 return kFALSE;
319 }
320
321 // If we have a vlid stop time check its consistency with the start time
322 if (fRunStop!=MTime() && fRunStop<fRunStart)
323 {
324 *fLog << err << "ERROR - Stop time smaller than start time." << endl;
325 return kFALSE;
326 }
327
328 // No file numbers larger than 999 allowed in general
329 if (fFileNumber>999)
330 {
331 *fLog << err << "ERROR - File number " << fFileNumber << " larger than 999." << endl;
332 return kFALSE;
333 }
334
335 // Old formats can not contain a run number larger 0
336 if (fFormatVersion<11 && fFileNumber>0)
337 {
338 *fLog << err << "ERROR - File number " << fFileNumber << " larger than 0." << endl;
339 return kFALSE;
340 }
341
342 if (fFormatVersion>1)
343 {
344 // For most of the formats the start time must be valid
345 if (fRunStart==MTime())
346 {
347 *fLog << err << "ERROR - Start time invalid." << endl;
348 return kFALSE;
349 }
350
351 // For most of the formats an invalid stop time cannot happen if file closed
352 if (fMagicNumber==kMagicNumber && fRunStop==MTime())
353 {
354 *fLog << err << "ERROR - File closed but stop time invalid." << endl;
355 return kFALSE;
356 }
357 }
358 return kTRUE;
359}
360
361void MRawRunHeader::FixRunNumbers()
362{
363 if (fFormatVersion<11 || fTelescopeNumber!=1)
364 return;
365
366 // Map 1:1001349 to 47:1001395
367 if (fRunNumber<48 &&
368 fRunStart.GetMjd()>54674.5 && fRunStart.GetMjd()<56476.5)
369 {
370 fRunNumber += 1001348;
371 *fLog << warn << "Format >V10: Wrong run number increased by 1001348 to " << fRunNumber << "." << endl;
372 }
373
374 // Map 1001916:1001922 to 1002349:1002355
375 if (fRunNumber>1001915 && fRunNumber<1001923 &&
376 fRunStart.GetMjd()>54710.5 && fRunStart.GetMjd()<54711.5)
377 {
378 fRunNumber += 433;
379 *fLog << warn << "Format >V10: Wrong run number increased by 434 to " << fRunNumber << "." << endl;
380 }
381
382 // Map 10000342:1000343 to 10000351:1000351
383 if (fRunNumber>10000342 && fRunNumber<10000352 &&
384 fRunStart.GetMjd()>54254.5 && fRunStart.GetMjd()<54255.5)
385 {
386 fRunNumber -= 9000000;
387 *fLog << warn << "Format >V10: Wrong run number decreased by 9000000 to " << fRunNumber << "." << endl;
388 }
389
390 if (fRunNumber==1000382 &&
391 fRunStart.GetMjd()>54256.5 && fRunStart.GetMjd()<54257.5 &&
392 !strcmp(fSourceName, "GRB080605-2347"))
393 {
394 fFileNumber += 99;
395 *fLog << warn << "Format >V10: Ambiguous file number increased by 99 to " << fFileNumber << "." << endl;
396 }
397}
398
399// --------------------------------------------------------------------------
400//
401// This implements a fix of the pixel assignment before 25.9.2005
402//
403// From magic_online (the pixel numbers are hardware indices):
404// --------------------------------------------------------------------------
405// From: Florian Goebel <fgoebel@mppmu.mpg.de>
406// Date: Sun Sep 25 2005 - 05:13:19 CEST
407//
408// [...]
409// - problem 2: pixels were swaped
410// - cause: opical fibers were wrongly connected to receiver board
411// 554 <-> 559
412// 555 <-> 558
413// 556 <-> 557
414// - action: reconnect correctly
415// - result: ok now
416// - comment: I don't know when this error was introduced, so backward
417// correction of the data is difficult.
418// Fortunately the effect is not too large since the affected 6 pixels are
419// on the outermost edge of the camera
420// Since this board has special pixels the optical fibers have been
421// unplugged several times.
422// !!!!! Whenever you unplug and reconnect optical fibers make really !!!!!
423// !!!!! sure you connect them back correctly. !!!!!
424// !!!!! Always contact me before you do so and if you have any doubts !!!!!
425// !!!!! how to reconnect the fibers ask me. !!!!!
426// These swapped pixels have only been found by chance when doing the
427// flatfielding.
428// [...]
429//
430// --------------------------------------------------------------------------
431//
432// MAGIC runbook CC_2006_04_22_22_28_52.rbk
433//
434// [2006-04-22 23:14:13]
435//
436// [...]
437// Found 2 pairs of swapped pixels.
438// We corrected swapped pixels 54<->55 in the receiver boards. We probably
439// swapped today in the camera.
440// We did not correct 92<-<93 which are likely swapped. Will check and correct
441// tomorrow.
442//
443// ---
444//
445// comments:
446// - 54<->55 were corrected but have not been swapped, hence they are swapped
447// since then (run 88560 ok, run 88669 swapped; between them mostly dummy and
448// test runs)
449// - 92<->93 are never swapped, always ok.
450//
451// --------------------------------------------------------------------------
452//
453// MAGIC runbook CC_2006_08_28_19_40_18.rbk
454//
455// [2006-08-28 23:09:07]
456// While doing a flatfielding we have found out that the signals for pixels
457// 119 and 120 were swapped. We have fixed it by exchanging the corresponding
458// fibers at the input of the receivers (not before the splitters!).
459//
460// ---
461//
462// MAGIC runbook CC_2006_08_29_15_19_14.rbk
463//
464// [2006-08-29 16:43:09]
465// In the last hours we have found out and fixed a good number of pixels which
466// were swapped: 119-120, 160-161-162 and 210-263. According to Florian,
467// 160-161-162 and 210-263 were swapped since November 2005.
468//
469// ---
470//
471// mail Florian Goebel (08/30/2006 03:13 PM):
472//
473// As far as I can tell pixels 161 and 162 as well as 263 and 210 were
474// swapped in the trigger. This leads to some inefficiency of the trigger.
475// However, they were not swapped in the readout. So, you don't have to
476// correct anything in the data for these pixels.
477//
478// ---
479//
480// comments:
481// - 119-120 swapped between run 93251 (not swapped) and 93283 (swapped)
482// (only testruns between these runs)
483// corrected since run 99354 (== runbook [2006-08-28 23:09:07])
484// - 160 never swapped
485// - 161-162 were only swapped in the trigger, but nevertheless were
486// "corrected" also in the signal. Hence signal swapped since 99354
487//
488Bool_t MRawRunHeader::FixAssignment()
489{
490 if (!fTelescopeNumber==1)
491 return kTRUE;
492
493 if (fRunNumber>=53300 && fRunNumber<=68754)
494 {
495 if (!SwapAssignment(554, 559))
496 return kFALSE;
497 if (!SwapAssignment(555, 558))
498 return kFALSE;
499 if (!SwapAssignment(556, 557))
500 return kFALSE;
501 }
502
503 if (fRunNumber>=93283 && fRunNumber<99354)
504 {
505 if (!SwapAssignment(119, 120))
506 return kFALSE;
507 }
508
509 if (fRunNumber>=99354 && fRunNumber<=101789)
510 {
511 if (!SwapAssignment(161, 162))
512 return kFALSE;
513 if (!SwapAssignment(210, 263))
514 return kFALSE;
515 }
516
517 if (fRunNumber>=88669)
518 {
519 if (!SwapAssignment(54, 55))
520 return kFALSE;
521 }
522
523 if (fRunNumber>=200000)
524 {
525 if (!SwapAssignment(428, 429))
526 return kFALSE;
527 }
528
529 return kTRUE;
530}
531
532// --------------------------------------------------------------------------
533//
534// Fixes to fix bugs in the run header
535//
536Bool_t MRawRunHeader::Fixes()
537{
538 FixRunNumbers();
539
540 if (fFormatVersion>8 && fRunNumber>326152 && fTelescopeNumber==1)
541 {
542 fNumEvents--;
543 fNumEventsRead--;
544 *fLog << inf << "Format >V8 and Run>M1:326152: Stored number of events decreased by 1." << endl;
545 }
546
547 return FixAssignment();
548}
549
550// --------------------------------------------------------------------------
551//
552// Reading function to read/interpret the file formats 1-10
553//
554Bool_t MRawRunHeader::ReadEvtOld(istream& fin)
555{
556 if (fFormatVersion==7)
557 {
558 *fLog << err << "ERROR - File format V7 was for testing only and is not correctly implemented!" << endl;
559 return kFALSE;
560 }
561
562 // ----- DAQ software format version -----
563 fin.read((char*)&fSoftVersion, 2); // Total=6
564
565
566 fFadcType = 1;
567 if (fFormatVersion>7)
568 fin.read((char*)&fFadcType, 2);
569
570 // ----- Camera geometry and telescope number -----
571 fCameraVersion = 1;
572 fTelescopeNumber = 1;
573 if (fFormatVersion>5)
574 {
575 fin.read((char*)&fCameraVersion, 2); // (+2)
576 fin.read((char*)&fTelescopeNumber, 2); // (+2)
577 }
578
579 // ----- Run information -----
580 fin.read((char*)&fRunType, 2); // Total=8
581
582 fin.read((char*)&fRunNumber, 4); // Total=12
583 fin.read((char*)&fProjectName, fFormatVersion>5?100:22); // Total=34 (+78)
584 fin.read((char*)&fSourceName, fFormatVersion>5? 80:12); // Total=46 (+58)
585
586 if (fFormatVersion>5)
587 fin.read((char*)fObservationMode, 60); // (+60)
588 // Maybe we should set fObservationMode to something
589 // in case of fFormatVersion<6
590
591 // ----- Source position -----
592 fin.seekg(fFormatVersion>5 ? 16 : 8, ios::cur);
593 /*
594 if (fFormatVersion>5)
595 {
596 fin.read((char*)&fSourceRa, 4); // F32 SourceRA; Total=48
597 fin.read((char*)&fSourceDec, 4); // F32 SourceDEC; Total=52
598 }
599 fin.read((char*)&fTelescopeRa, 4); // F32 TelescopeRA; (+4)
600 fin.read((char*)&fTelescopeDec, 4); // F32 TelescopeDEC; (+4)
601 */
602
603 // Maybe we should set these to something
604 // in case of fFormatVersion<6
605 fin.read((char*)&fSourceEpochChar, 2); // Total=56
606 fin.read((char*)&fSourceEpochDate, 2); // Total=58
607
608 // ----- Old Start time -----
609 if (fFormatVersion<2) // Total += 10
610 {
611 UShort_t y, m, d;
612 fin.seekg(4, ios::cur); // Former fMJD[4],
613 fin.read((char*)&y, 2); // Former fDateYear[2]
614 fin.read((char*)&m, 2); // Former fDateMonth[2]
615 fin.read((char*)&d, 2); // Former fDateDay[2]
616 fRunStart.Set(y, m, d, 0, 0, 0, 0);
617 }
618
619 // ----- Data Geometry -----
620
621 fin.read((char*)&fNumCrates, 2); // MUX: number of channels
622 fin.read((char*)&fNumPixInCrate, 2); // MUX: number of pix in channel
623 fin.read((char*)&fNumSamplesLoGain, 2); // MUX: dummy (must be 0 for MUX data)
624 fin.read((char*)&fNumSamplesHiGain, 2); // MUX: Number of samples per pixel
625
626 char dummy[16];
627 if (fFormatVersion>8)
628 fin.read(dummy, 4); // 2xU16 (NumSamplesRemovedHead and NumSamplesRemovedTail)
629
630 // ----- Number of events -----
631 fin.read((char*)&fNumEvents, 4); // Total=70
632
633 if (fFormatVersion>8)
634 fin.read((char*)&fNumEventsRead, 4);
635
636 // New in general features: (should they be included in new MAGIC1 formats, too?)
637 fNumBytesPerSample = 1; // 2 for MUX DATA
638 fSamplingFrequency = 300;
639 fFadcResolution = 8;
640
641 if (fFormatVersion>7)
642 {
643 fin.read((char*)&fNumBytesPerSample, 2);
644 fin.read((char*)&fSamplingFrequency, 2); // [MHz], 2000 for MuxFadc
645 fin.read((char*)&fFadcResolution, 1); // nominal resolution [# Bits], 10 for MuxFadc
646 }
647
648 // ----- Start/Stop time -----
649 if (fFormatVersion>1)
650 {
651 fRunStart.ReadBinary(fin); // Total += 7
652 fRunStop.ReadBinary(fin); // Total += 7
653 }
654
655 //
656 // calculate size of array, create it and fill it
657 //
658 const Int_t nPixel = fNumCrates*fNumPixInCrate;
659 fPixAssignment->Set(nPixel);
660
661 // ----- Pixel Assignement -----
662 fin.read((char*)fPixAssignment->GetArray(), nPixel*2);
663
664 if (fFormatVersion<7)
665 fin.read(dummy, 16);
666
667 // ----- Fixes for broken files or contents -----
668 if (!Fixes())
669 return kFALSE;
670
671 // ----- Consistency checks -----
672 return IsConsistent();
673}
674
675// --------------------------------------------------------------------------
676//
677// Read in one run header from the binary file
678//
679Bool_t MRawRunHeader::ReadEvt(istream& fin)
680{
681 //
682 // read one RUN HEADER from the input stream
683 //
684 fMagicNumber = 0;
685
686 fin.read((char*)&fMagicNumber, 2); // Total=2
687
688 //
689 // check whether the the file has the right file type or not
690 //
691 if (fMagicNumber != kMagicNumber && fMagicNumber != kMagicNumber+1)
692 {
693 *fLog << err << "ERROR - Wrong Magic Number (0x" << hex << fMagicNumber << "): Not a Magic File!" << endl;
694 return kFALSE;
695 }
696
697 if (fMagicNumber == kMagicNumber+1)
698 *fLog << warn << "WARNING - This file maybe broken (0xc0c1) - DAQ didn't close it correctly!" << endl;
699
700 // ----- File format version -----
701 fin.read((char*)&fFormatVersion, 2); // Total=4
702 if (fFormatVersion==10 || fFormatVersion>kMaxFormatVersion)
703 {
704 *fLog << err << "ERROR - File format V" << fFormatVersion << " not implemented!" << endl;
705 return kFALSE;
706 }
707
708 // ----- Process old file formats -----
709 if (fFormatVersion<10)
710 return ReadEvtOld(fin);
711
712 // ----- Overwrite format version for format 11 -----
713 fin.read((char*)&fFormatVersion, 4);
714 if (fFormatVersion<11)
715 {
716 *fLog << err << "ERROR - Format Version <11." << endl;
717 return kFALSE;
718 }
719
720 // ----- Read Header by size as written in the header -----
721 fin.read((char*)&fHeaderSizeRun, 4);
722 if (fHeaderSizeRun<346)
723 {
724 *fLog << err << "ERROR - Event header too small (<388b)." << endl;
725 return kFALSE;
726 }
727
728 TArrayC h(fHeaderSizeRun-12);
729 fin.read(h.GetArray(), h.GetSize());
730 if (!fin)
731 return kFALSE;
732
733 // ----- convert -----
734 const Byte_t *Char = reinterpret_cast<Byte_t* >(h.GetArray());
735 const UInt_t *Int = reinterpret_cast<UInt_t* >(h.GetArray());
736 //const Float_t *Float = reinterpret_cast<Float_t*>(h.GetArray());
737
738 // ----- Start interpretation -----
739
740 fHeaderSizeEvt = Int[0];
741 fHeaderSizeCrate = Int[1];
742 fSoftVersion = Int[2];
743 fFadcType = Int[3];
744 fCameraVersion = Int[4];
745 fTelescopeNumber = Int[5];
746 fRunType = Int[6];
747 fRunNumber = Int[7];
748 fFileNumber = Int[8];
749
750 memcpy(fProjectName, Char+ 36, 100); // 25
751 memcpy(fSourceName, Char+136, 80); // 20
752 memcpy(fObservationMode, Char+216, 60); // 15
753
754 //F32 fSourceRA = Float[69];
755 //F32 fSourceDEC = Float[70];
756 //F32 fTelescopeRA = Float[71];
757 //F32 fTelescopeDEC = Float[72];
758
759 memcpy(fSourceEpochChar, Char+232, 4);
760
761 fSourceEpochDate = Int[74];
762 fNumCrates = Int[75];
763 fNumPixInCrate = Int[76];
764 fNumSamplesHiGain = Int[77];
765 fNumSamplesLoGain = 0;
766
767 //fNumSamplesRemovedHead = Int[78];
768 //fNumSamplesRemovedTail = Int[79];
769
770 fNumEvents = Int[80];
771 fNumEventsRead = Int[81];
772 fNumBytesPerSample = Int[82];
773 fSamplingFrequency = Int[83];
774 fFadcResolution = Int[84];
775
776 fRunStart.SetBinary(Int+85);
777 fRunStop.SetBinary(Int+91);
778
779 // ----- 388 bytes so far -----
780
781 const UInt_t n = fNumCrates*fNumPixInCrate;
782 if (fHeaderSizeRun<388+n*4)
783 {
784 *fLog << err << "ERROR - Event header too small to contain pix assignment." << endl;
785 return kFALSE;
786 }
787
788 // ----- Pixel Assignment -----
789 fPixAssignment->Set(n);
790
791 for (UInt_t i=0; i<n; i++)
792 (*fPixAssignment)[i] = Int[97+i];
793
794 // ----- Fixes for broken files or contents -----
795 if (!Fixes())
796 return kFALSE;
797
798 // ----- Consistency checks -----
799 return IsConsistent();
800}
801/*
802Bool_t MRawRunHeader::WriteEvt(ostream& out) const
803{
804 //
805 // write one RUN HEADER from the input stream
806 //
807 const UInt_t n = fNumCrates*fNumPixInCrate;
808
809 const UShort_t magicnumber = kMagicNumber;
810 const UInt_t formatversion = 11;
811 const UInt_t headersizerun = (97+n)*4; //???
812
813 // FIXME: Write fixed number (c0c0)
814 out.write((char*)&magicnumber, 2); // Total=2
815 out.write((char*)&formatversion, 2); // Total=4
816 out.write((char*)&formatversion, 4);
817 out.write((char*)&headersizerun, 4);
818
819 TArrayC h(headersizerun-12);
820
821 // ----- convert -----
822 Byte_t *Char = reinterpret_cast<Byte_t* >(h.GetArray());
823 UInt_t *Int = reinterpret_cast<UInt_t* >(h.GetArray());
824 //const Float_t *Float = reinterpret_cast<Float_t*>(h.GetArray());
825
826 // ----- Start interpretation -----
827
828 Int[0] = 0; // fHeaderSizeEvt;
829 Int[1] = 0; // fHeaderSizeCrate;
830 Int[2] = 0; // fSoftVersion;
831 Int[3] = fFadcType;
832 Int[4] = fCameraVersion;
833 Int[5] = fTelescopeNumber;
834 Int[5] = fRunType;
835 Int[6] = fRunNumber;
836 Int[7] = fFileNumber;
837
838 memcpy(Char+ 36, fProjectName, 100); // 25
839 memcpy(Char+136, fSourceName, 80); // 20
840 memcpy(Char+216, fObservationMode, 60); // 15
841
842 //F32 fSourceRA = Float[69];
843 //F32 fSourceDEC = Float[70];
844 //F32 fTelescopeRA = Float[71];
845 //F32 fTelescopeDEC = Float[72];
846
847 memcpy(Char+232, fSourceEpochChar, 4);
848
849 Int[74] = fSourceEpochDate;
850 Int[75] = fNumCrates;
851 Int[76] = fNumPixInCrate;
852 Int[77] = fNumSamplesHiGain;
853
854 //fNumSamplesRemovedHead = Int[78];
855 //fNumSamplesRemovedTail = Int[79];
856
857 Int[80] = fNumEvents;
858 Int[81] = fNumEvents; //fNumEventsRead;
859 Int[82] = fNumBytesPerSample;
860 Int[83] = fSamplingFrequency;
861 Int[84] = fFadcResolution;
862
863 fRunStart.GetBinary(Int+85);
864 fRunStop.GetBinary(Int+91);
865
866 // ----- 388 bytes so far -----
867
868 //const UInt_t n = fNumCrates*fNumPixInCrate;
869 //if (fHeaderSizeRun<(97+n)*4)
870 //{
871 // *fLog << err << "ERROR - Event header too small to contain pix assignment." << endl;
872 // return kFALSE;
873 //}
874
875 // ----- Pixel Assignment -----
876 for (UInt_t i=0; i<n; i++)
877 Int[97+i] = (*fPixAssignment)[i];
878
879 out.write(h.GetArray(), h.GetSize());
880
881 return out;
882}
883*/
884// --------------------------------------------------------------------------
885//
886// Return the run type as string ("Data", "Pedestal", ...), for example
887// to print it as readable text.
888//
889const char *MRawRunHeader::GetRunTypeStr() const
890{
891 switch (fRunType)
892 {
893 case kRTData:
894 return "Data";
895 case kRTPedestal:
896 return "Pedestal";
897 case kRTCalibration:
898 return "Calibration";
899 case kRTLinearity:
900 return "Linearity";
901 case kRTPointRun:
902 return "Point-Run";
903 case kRTMonteCarlo:
904 return "Monte Carlo";
905 case kRTNone:
906 return "<none>";
907 default:
908 return "<unknown>";
909 }
910}
911
912// --------------------------------------------------------------------------
913//
914// print run header information on *fLog. The option 'header' supresses
915// the pixel index translation table.
916//
917void MRawRunHeader::Print(Option_t *t) const
918{
919 *fLog << all << endl;
920 *fLog << "MagicNumber: 0x" << hex << fMagicNumber << " - ";
921 switch (fMagicNumber)
922 {
923 case kMagicNumber: *fLog << "OK"; break;
924 case kMagicNumber+1: *fLog << "File not closed!"; break;
925 default: *fLog << "Wrong!"; break;
926 }
927 *fLog << endl;
928 *fLog << "Versions: " << dec << "Format=" << fFormatVersion << " ";
929 *fLog << "Software=" << fSoftVersion << " ";
930 if (fFormatVersion>5)
931 *fLog << "Camera=" << fCameraVersion;
932 *fLog << endl;
933 if (fFormatVersion>10)
934 *fLog << "Header sizes: " << fHeaderSizeRun << "b (run), " << fHeaderSizeEvt << "b (evt), " << fHeaderSizeCrate << "b (crate)" << endl;
935 if (fRunNumber>0)
936 {
937 if (fFormatVersion>5)
938 *fLog << "Telescope: " << fTelescopeNumber << endl;
939 *fLog << "RunNumber: " << fRunNumber;
940 if (fFormatVersion>10)
941 *fLog << "/" << fFileNumber << " (id=" << GetFileID() << ")";
942 *fLog << " (Type=" << GetRunTypeStr() << ")" << endl;
943 }
944 if (fFormatVersion>7)
945 {
946 *fLog << "FadcType: " << fFadcType << " (";
947 switch (fFadcType)
948 {
949 case 1: *fLog << "Siegen"; break;
950 case 2: *fLog << "MUX"; break;
951 case 0xffff: *fLog << "artificial"; break;
952 default: *fLog << "unknown";
953 }
954 *fLog << ")" << endl;
955 }
956 *fLog << "ProjectName: '" << fProjectName << "'" << endl;
957 if (fFormatVersion>5)
958 *fLog << "Observation: '" << fObservationMode << "'" << endl;
959 if (fSourceName[0]!=0 || fSourceEpochChar[0]!=0 || fSourceEpochDate!=0)
960 {
961 *fLog << "Source: '" << fSourceName << "' " << " ";
962 *fLog << fSourceEpochChar << dec << fSourceEpochDate << endl;
963 }
964 if (fRunStart)
965 *fLog << "Run Start: " << fRunStart << endl;
966 if (fRunStop)
967 *fLog << "Run Stop: " << fRunStop << endl;
968 if (fNumCrates>0 || fNumPixInCrate>0)
969 *fLog << "Crates: " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
970 if (GetNumConnectedPixels()>0)
971 *fLog << "Num Pixels: " << GetNumNormalPixels() << " (normal) + " << GetNumSpecialPixels() << " (special) = " << GetNumConnectedPixels() << " (total)" << endl;
972 if (fFormatVersion>6)
973 *fLog << "Sampling: " << fSamplingFrequency << "MHz with " << (int)fFadcResolution << " significant bits" << endl;
974 *fLog << "Samples: " << fNumSamplesHiGain << "/" << fNumSamplesLoGain << " (hi/lo) * " << fNumBytesPerSample << "B/sample = ";
975 if (fNumCrates>0)
976 *fLog << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumCrates * fNumPixInCrate * fNumBytesPerSample/1000 << "kB/Evt" << endl;
977 else
978 *fLog << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumBytesPerSample << "B/pix" << endl;
979 if (fNumEvents>0 || fNumEventsRead>0)
980 {
981 *fLog << "Evt Counter: " << fNumEvents;
982 if (fFormatVersion>8)
983 *fLog << " (read=" << fNumEventsRead << ")";
984 *fLog << endl;
985 }
986
987 if (TString(t).Contains("header", TString::kIgnoreCase))
988 return;
989
990 *fLog << inf3 << "Assignment:" << hex << endl;
991 for (int i=0; i<GetNumPixel(); i++)
992 *fLog << setfill('0') << setw(3) << (*fPixAssignment)[i] << " ";
993
994 *fLog << dec << setfill(' ') << endl;
995}
996
997// --------------------------------------------------------------------------
998//
999// Return the assigned pixel number for the given FADC channel
1000//
1001Short_t MRawRunHeader::GetPixAssignment(UShort_t i) const
1002{
1003 // FIXME: Do we need a range check here?
1004 return (Short_t)(*fPixAssignment)[i];
1005}
1006
1007// --------------------------------------------------------------------------
1008//
1009// Return the number of pixel which are markes as connected in the
1010// pix assignment (!=0)
1011//
1012UShort_t MRawRunHeader::GetNumConnectedPixels() const
1013{
1014 const Int_t num = fPixAssignment->GetSize();
1015
1016 UShort_t rc = 0;
1017 for (int i=0; i<num; i++)
1018 {
1019 if (GetPixAssignment(i)!=0)
1020 rc++;
1021 }
1022 return rc;
1023}
1024
1025// --------------------------------------------------------------------------
1026//
1027// Return the number of pixel which are markes as connected and so-called
1028// 'normal' pixels in the pix assignment (>0)
1029//
1030UShort_t MRawRunHeader::GetNumNormalPixels() const
1031{
1032 const Int_t num = fPixAssignment->GetSize();
1033
1034 UShort_t rc = 0;
1035 for (int i=0; i<num; i++)
1036 {
1037 if (GetPixAssignment(i)>0)
1038 rc++;
1039 }
1040 return rc;
1041}
1042
1043// --------------------------------------------------------------------------
1044//
1045// Return the number of pixel which are markes as connected and so-called
1046// 'special' pixels in the pix assignment (<0)
1047//
1048UShort_t MRawRunHeader::GetNumSpecialPixels() const
1049{
1050 const Int_t num = fPixAssignment->GetSize();
1051
1052 UShort_t rc = 0;
1053 for (int i=0; i<num; i++)
1054 {
1055 if (GetPixAssignment(i)<0)
1056 rc++;
1057 }
1058 return rc;
1059}
1060
1061// --------------------------------------------------------------------------
1062//
1063// Return the maximum id which exists in the pix assignment
1064//
1065UShort_t MRawRunHeader::GetMaxPixId() const
1066{
1067 const Int_t num = fPixAssignment->GetSize();
1068
1069 Short_t rc = 0;
1070 for (int i=0; i<num; i++)
1071 rc = TMath::Max(GetPixAssignment(i), rc);
1072
1073 return rc;
1074}
1075
1076// --------------------------------------------------------------------------
1077//
1078// Return minus th minimum id which exists in the pix assignment
1079//
1080UShort_t MRawRunHeader::GetMinPixId() const
1081{
1082 const Int_t num = fPixAssignment->GetSize();
1083
1084 Short_t rc = 0;
1085 for (int i=0; i<num; i++)
1086 rc = TMath::Min(GetPixAssignment(i), rc);
1087
1088 return (UShort_t)-rc;
1089}
1090
1091// --------------------------------------------------------------------------
1092//
1093// Return the number of pixel in this event.
1094//
1095// WARNING: This is the number of pixels stored in this file which is
1096// a multiple of the number of pixels per crate and in general
1097// a number which is larger than the camera size!
1098//
1099// To know the range of the pixel indices please use the geometry
1100// container!
1101//
1102UShort_t MRawRunHeader::GetNumPixel() const
1103{
1104 return fPixAssignment->GetSize();
1105}
1106
1107// --------------------------------------------------------------------------
1108//
1109// Returns absolute size in bytes of the run header as read from a raw file.
1110// This must be done _after_ the header is read, because the header doesn't
1111// have a fixed size (used in MRawSocketRead)
1112//
1113Int_t MRawRunHeader::GetNumTotalBytes() const
1114{
1115 switch (fFormatVersion)
1116 {
1117 case 1:
1118 return 80+fNumCrates*fNumPixInCrate*2+16;
1119 case 2:
1120 case 3:
1121 case 4:
1122 case 5:
1123 return 84+fNumCrates*fNumPixInCrate*2+16;
1124 case 6:
1125 return 84+fNumCrates*fNumPixInCrate*2+16 +4+78+58+60+8;
1126 case 7:
1127 return 84+fNumCrates*fNumPixInCrate*2+16 +4+78+58+60+8 +3-16;
1128 }
1129 return 0;
1130}
1131
1132// --------------------------------------------------------------------------
1133//
1134// Monte Carlo Interface
1135//
1136// This is a (prelimiary) way to setup an existing FADC system.
1137//
1138// 1: Siegen FADCs
1139// 2: MUX FADCs
1140//
1141void MRawRunHeader::InitFadcType(UShort_t type)
1142{
1143 switch (type)
1144 {
1145 case 1:
1146 fNumSamplesHiGain = 15;
1147 fNumSamplesLoGain = 15;
1148 fNumBytesPerSample = 1; // number of bytes per sample
1149 fSamplingFrequency = 300; // Sampling Frequency [MHz]
1150 fFadcResolution = 8; // number of significant bits
1151 break;
1152 case 2:
1153 fNumSamplesHiGain = 50;
1154 fNumSamplesLoGain = 0;
1155 fNumBytesPerSample = 2; // number of bytes per sample
1156 fSamplingFrequency = 2000; // Sampling Frequency [MHz]
1157 fFadcResolution = 12; // number of significant bits
1158 break;
1159 case 3:
1160 fNumSamplesHiGain = 150;
1161 fNumSamplesLoGain = 0;
1162 fNumBytesPerSample = 2; // number of bytes per sample
1163 fSamplingFrequency = 1000; // Sampling Frequency [MHz]
1164 fFadcResolution = 12; // number of significant bits
1165 break;
1166 }
1167
1168 fFadcType = type;
1169}
1170
1171// --------------------------------------------------------------------------
1172//
1173// Monte Carlo Interface
1174//
1175// Init a camera
1176//
1177void MRawRunHeader::InitCamera(UShort_t type, UShort_t pix)
1178{
1179 switch (type)
1180 {
1181 case 1:
1182 fNumCrates = 1;
1183 fNumPixInCrate = 577;
1184 break;
1185 case 2:
1186 fNumCrates = 1;
1187 fNumPixInCrate = 703;
1188 break;
1189 case (UShort_t)-1:
1190 fNumCrates = 1;
1191 fNumPixInCrate = pix;
1192 break;
1193 }
1194
1195 fCameraVersion = type;
1196
1197 const Int_t n = fNumCrates*fNumPixInCrate;
1198
1199 fPixAssignment->Set(n);
1200
1201 for (int i=0; i<n; i++)
1202 (*fPixAssignment)[i] = i+1;
1203}
1204
1205// --------------------------------------------------------------------------
1206//
1207// Monte Carlo Interface
1208//
1209// Set telescope number, run-number and file-number
1210//
1211void MRawRunHeader::SetRunInfo(UShort_t tel, UInt_t run, UInt_t file)
1212{
1213 fTelescopeNumber = tel;
1214 fRunNumber = run;
1215 fFileNumber = file;
1216}
1217
1218// --------------------------------------------------------------------------
1219//
1220// Monte Carlo Interface
1221//
1222// Set source-name, epoch (default J) and date (default 2000)
1223//
1224void MRawRunHeader::SetSourceInfo(const TString src, char epoch, UShort_t date)
1225{
1226 strncpy(fSourceName, src.Data(), 80);
1227
1228 fSourceEpochChar[0] = epoch; // epoch char of the source
1229 fSourceEpochDate = date; // epoch date of the source
1230}
1231
1232// --------------------------------------------------------------------------
1233//
1234// Monte Carlo Interface
1235//
1236// Set run-start and -stop time
1237//
1238void MRawRunHeader::SetRunTime(const MTime &start, const MTime &end)
1239{
1240 fRunStart = start;
1241 fRunStop = end;
1242}
1243
1244// --------------------------------------------------------------------------
1245//
1246// Monte Carlo Interface
1247//
1248// Set project name and observation mode
1249//
1250void MRawRunHeader::SetObservation(const TString mode, const TString proj)
1251{
1252 strncpy(fProjectName, proj.Data(), 100);
1253 strncpy(fObservationMode, mode.Data(), 60);
1254}
1255
1256// --------------------------------------------------------------------------
1257//
1258// Monte Carlo Interface
1259//
1260// Set number of events in file.
1261//
1262void MRawRunHeader::SetNumEvents(UInt_t num)
1263{
1264 fNumEvents = num;
1265 fNumEventsRead = num;
1266}
1267
1268// --------------------------------------------------------------------------
1269//
1270// NumSamples: 50
1271// NumBytePerSample: 2
1272// SamplingFrequency: 2000
1273// FadcResolution: 12
1274// FadcType: XXXX
1275//
1276Int_t MRawRunHeader::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
1277{
1278 Bool_t rc = kFALSE;
1279
1280 if (IsEnvDefined(env, prefix, "NumSamples", print))
1281 {
1282 rc = kTRUE;
1283 fNumSamplesHiGain = GetEnvValue(env, prefix, "NumSamples", fNumSamplesHiGain);
1284 fNumSamplesLoGain = 0;
1285 }
1286
1287 if (IsEnvDefined(env, prefix, "NumBytesPerSample", print))
1288 {
1289 rc = kTRUE;
1290 fNumBytesPerSample = GetEnvValue(env, prefix, "NumBytesPerSample", fNumBytesPerSample);
1291 }
1292
1293 if (IsEnvDefined(env, prefix, "SamplingFrequency", print))
1294 {
1295 rc = kTRUE;
1296 fSamplingFrequency = GetEnvValue(env, prefix, "SamplingFrequency", fSamplingFrequency);
1297 }
1298
1299 if (IsEnvDefined(env, prefix, "FadcResolution", print))
1300 {
1301 rc = kTRUE;
1302 fFadcResolution = GetEnvValue(env, prefix, "FadcResolution", fFadcResolution);
1303 }
1304 // Saturation behaviour etc.
1305 if (IsEnvDefined(env, prefix, "FadcType", print))
1306 {
1307 //rc = kTRUE;
1308 //TString type = GetEnvValue(env, prefix, "FadcType", "");
1309 // Eval "Siegen", "MUX", Dwarf"
1310 }
1311 else
1312 if (rc)
1313 fFadcType = 0xffff; // "Artificial"
1314
1315 return rc;
1316}
1317
Note: See TracBrowser for help on using the repository browser.