source: trunk/Mars/mraw/MRawRunHeader.cc@ 10961

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