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

Last change on this file since 9453 was 9433, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 41.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-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()!=fTelescopeNumber*10U+5U)
354 {
355 *fLog << err << "ERROR - Telscope number " << fTelescopeNumber << " doesn't match the first two digits of the run number " << fRunNumber << "." << 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 *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 kRTLinearity:
953 return "Linearity";
954 case kRTPointRun:
955 return "Point-Run";
956 case kRTMonteCarlo:
957 return "Monte Carlo";
958 case kRTNone:
959 return "<none>";
960 default:
961 return "<unknown>";
962 }
963}
964
965// --------------------------------------------------------------------------
966//
967// print run header information on *fLog. The option 'header' supresses
968// the pixel index translation table.
969//
970void MRawRunHeader::Print(Option_t *t) const
971{
972 *fLog << all << endl;
973 *fLog << "MagicNumber: 0x" << hex << fMagicNumber << " - ";
974 switch (fMagicNumber)
975 {
976 case kMagicNumber: *fLog << "OK"; break;
977 case kMagicNumber+1: *fLog << "File not closed!"; break;
978 default: *fLog << "Wrong!"; break;
979 }
980 *fLog << endl;
981 *fLog << "Versions: " << dec << "Format=" << fFormatVersion << " ";
982 *fLog << "Software=" << fSoftVersion << " ";
983 if (fFormatVersion>5)
984 *fLog << "Camera=" << fCameraVersion;
985 *fLog << endl;
986 if (fFormatVersion>10)
987 *fLog << "Header sizes: " << fHeaderSizeRun << "b (run), " << fHeaderSizeEvt << "b (evt), " << fHeaderSizeCrate << "b (crate)" << endl;
988 if (fRunNumber>0)
989 {
990 if (fFormatVersion>5)
991 *fLog << "Telescope: " << fTelescopeNumber << endl;
992 *fLog << "RunNumber: " << fRunNumber;
993 if (fFormatVersion>10)
994 *fLog << "/" << fFileNumber << " (id=" << GetFileID() << ")";
995 *fLog << " (Type=" << GetRunTypeStr() << ")" << endl;
996 }
997 if (fFormatVersion>7)
998 {
999 *fLog << "FadcType: " << fFadcType << " (";
1000 switch (fFadcType)
1001 {
1002 case 1: *fLog << "Siegen"; break;
1003 case 2: *fLog << "MUX"; break;
1004 case 0xffff: *fLog << "artificial"; break;
1005 default: *fLog << "unknown";
1006 }
1007 *fLog << ")" << endl;
1008 }
1009 *fLog << "ProjectName: '" << fProjectName << "'" << endl;
1010 if (fFormatVersion>5)
1011 *fLog << "Observation: '" << fObservationMode << "'" << endl;
1012 if (fSourceName[0]!=0 || fSourceEpochChar[0]!=0 || fSourceEpochDate!=0)
1013 {
1014 *fLog << "Source: '" << fSourceName << "' " << " ";
1015 *fLog << fSourceEpochChar << dec << fSourceEpochDate << endl;
1016 }
1017 if (fRunStart)
1018 *fLog << "Run Start: " << fRunStart << endl;
1019 if (fRunStop)
1020 *fLog << "Run Stop: " << fRunStop << endl;
1021 if (fNumCrates>0 || fNumPixInCrate>0)
1022 *fLog << "Crates: " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
1023 if (GetNumConnectedPixels()>0)
1024 *fLog << "Num Pixels: " << GetNumNormalPixels() << " (normal) + " << GetNumSpecialPixels() << " (special) = " << GetNumConnectedPixels() << " (total)" << endl;
1025 if (fFormatVersion>6)
1026 *fLog << "Sampling: " << fSamplingFrequency << "MHz with " << (int)fFadcResolution << " significant bits" << endl;
1027 *fLog << "Samples: " << fNumSamplesHiGain << "/" << fNumSamplesLoGain << " (hi/lo) * " << fNumBytesPerSample << "B/sample = ";
1028 if (fNumCrates>0)
1029 *fLog << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumCrates * fNumPixInCrate * fNumBytesPerSample/1000 << "kB/Evt" << endl;
1030 else
1031 *fLog << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumBytesPerSample << "B/pix" << endl;
1032 if (fNumEvents>0 || fNumEventsRead>0)
1033 {
1034 *fLog << "Evt Counter: " << fNumEvents;
1035 if (fFormatVersion>8)
1036 *fLog << " (read=" << fNumEventsRead << ")";
1037 *fLog << endl;
1038 }
1039
1040 if (TString(t).Contains("header", TString::kIgnoreCase))
1041 return;
1042
1043 *fLog << inf3 << "Assignment:" << hex << endl;
1044 for (int i=0; i<GetNumPixel(); i++)
1045 *fLog << setfill('0') << setw(3) << (*fPixAssignment)[i] << " ";
1046
1047 *fLog << dec << setfill(' ') << endl;
1048}
1049
1050// --------------------------------------------------------------------------
1051//
1052// Return the assigned pixel number for the given FADC channel
1053//
1054Short_t MRawRunHeader::GetPixAssignment(UShort_t i) const
1055{
1056 // FIXME: Do we need a range check here?
1057 return (Short_t)(*fPixAssignment)[i];
1058}
1059
1060// --------------------------------------------------------------------------
1061//
1062// Return the number of pixel which are markes as connected in the
1063// pix assignment (!=0)
1064//
1065UShort_t MRawRunHeader::GetNumConnectedPixels() const
1066{
1067 const Int_t num = fPixAssignment->GetSize();
1068
1069 UShort_t rc = 0;
1070 for (int i=0; i<num; i++)
1071 {
1072 if (GetPixAssignment(i)!=0)
1073 rc++;
1074 }
1075 return rc;
1076}
1077
1078// --------------------------------------------------------------------------
1079//
1080// Return the number of pixel which are markes as connected and so-called
1081// 'normal' pixels in the pix assignment (>0)
1082//
1083UShort_t MRawRunHeader::GetNumNormalPixels() const
1084{
1085 const Int_t num = fPixAssignment->GetSize();
1086
1087 UShort_t rc = 0;
1088 for (int i=0; i<num; i++)
1089 {
1090 if (GetPixAssignment(i)>0)
1091 rc++;
1092 }
1093 return rc;
1094}
1095
1096// --------------------------------------------------------------------------
1097//
1098// Return the number of pixel which are markes as connected and so-called
1099// 'special' pixels in the pix assignment (<0)
1100//
1101UShort_t MRawRunHeader::GetNumSpecialPixels() const
1102{
1103 const Int_t num = fPixAssignment->GetSize();
1104
1105 UShort_t rc = 0;
1106 for (int i=0; i<num; i++)
1107 {
1108 if (GetPixAssignment(i)<0)
1109 rc++;
1110 }
1111 return rc;
1112}
1113
1114// --------------------------------------------------------------------------
1115//
1116// Return the maximum id which exists in the pix assignment
1117//
1118UShort_t MRawRunHeader::GetMaxPixId() const
1119{
1120 const Int_t num = fPixAssignment->GetSize();
1121
1122 Short_t rc = 0;
1123 for (int i=0; i<num; i++)
1124 rc = TMath::Max(GetPixAssignment(i), rc);
1125
1126 return rc;
1127}
1128
1129// --------------------------------------------------------------------------
1130//
1131// Return minus th minimum id which exists in the pix assignment
1132//
1133UShort_t MRawRunHeader::GetMinPixId() const
1134{
1135 const Int_t num = fPixAssignment->GetSize();
1136
1137 Short_t rc = 0;
1138 for (int i=0; i<num; i++)
1139 rc = TMath::Min(GetPixAssignment(i), rc);
1140
1141 return (UShort_t)-rc;
1142}
1143
1144// --------------------------------------------------------------------------
1145//
1146// Return the number of pixel in this event.
1147//
1148// WARNING: This is the number of pixels stored in this file which is
1149// a multiple of the number of pixels per crate and in general
1150// a number which is larger than the camera size!
1151//
1152// To know the range of the pixel indices please use the geometry
1153// container!
1154//
1155UShort_t MRawRunHeader::GetNumPixel() const
1156{
1157 return fPixAssignment->GetSize();
1158}
1159
1160// --------------------------------------------------------------------------
1161//
1162// Returns absolute size in bytes of the run header as read from a raw file.
1163// This must be done _after_ the header is read, because the header doesn't
1164// have a fixed size (used in MRawSocketRead)
1165//
1166Int_t MRawRunHeader::GetNumTotalBytes() const
1167{
1168 switch (fFormatVersion)
1169 {
1170 case 1:
1171 return 80+fNumCrates*fNumPixInCrate*2+16;
1172 case 2:
1173 case 3:
1174 case 4:
1175 case 5:
1176 return 84+fNumCrates*fNumPixInCrate*2+16;
1177 case 6:
1178 return 84+fNumCrates*fNumPixInCrate*2+16 +4+78+58+60+8;
1179 case 7:
1180 return 84+fNumCrates*fNumPixInCrate*2+16 +4+78+58+60+8 +3-16;
1181 }
1182 return 0;
1183}
1184
1185// --------------------------------------------------------------------------
1186//
1187// Monte Carlo Interface
1188//
1189// This is a (prelimiary) way to setup an existing FADC system.
1190//
1191// 1: Siegen FADCs
1192// 2: MUX FADCs
1193//
1194void MRawRunHeader::InitFadcType(UShort_t type)
1195{
1196 switch (type)
1197 {
1198 case 1:
1199 fNumSamplesHiGain = 15;
1200 fNumSamplesLoGain = 15;
1201 fNumBytesPerSample = 1; // number of bytes per sample
1202 fSamplingFrequency = 300; // Sampling Frequency [MHz]
1203 fFadcResolution = 8; // number of significant bits
1204 break;
1205 case 2:
1206 fNumSamplesHiGain = 50;
1207 fNumSamplesLoGain = 0;
1208 fNumBytesPerSample = 2; // number of bytes per sample
1209 fSamplingFrequency = 2000; // Sampling Frequency [MHz]
1210 fFadcResolution = 12; // number of significant bits
1211 break;
1212 case 3:
1213 fNumSamplesHiGain = 150;
1214 fNumSamplesLoGain = 0;
1215 fNumBytesPerSample = 2; // number of bytes per sample
1216 fSamplingFrequency = 1000; // Sampling Frequency [MHz]
1217 fFadcResolution = 12; // number of significant bits
1218 break;
1219 }
1220
1221 fFadcType = type;
1222}
1223
1224// --------------------------------------------------------------------------
1225//
1226// Monte Carlo Interface
1227//
1228// Init a camera
1229//
1230void MRawRunHeader::InitCamera(UShort_t type, UShort_t pix)
1231{
1232 switch (type)
1233 {
1234 case 1:
1235 fNumCrates = 1;
1236 fNumPixInCrate = 577;
1237 break;
1238 case 2:
1239 fNumCrates = 1;
1240 fNumPixInCrate = 703;
1241 break;
1242 case (UShort_t)-1:
1243 fNumCrates = 1;
1244 fNumPixInCrate = pix;
1245 break;
1246 }
1247
1248 fCameraVersion = type;
1249
1250 const Int_t n = fNumCrates*fNumPixInCrate;
1251
1252 fPixAssignment->Set(n);
1253
1254 for (int i=0; i<n; i++)
1255 (*fPixAssignment)[i] = i+1;
1256}
1257
1258// --------------------------------------------------------------------------
1259//
1260// Monte Carlo Interface
1261//
1262// Set telescope number, run-number and file-number
1263//
1264void MRawRunHeader::SetRunInfo(UShort_t tel, UInt_t run, UInt_t file)
1265{
1266 fTelescopeNumber = tel;
1267 fRunNumber = run;
1268 fFileNumber = file;
1269}
1270
1271// --------------------------------------------------------------------------
1272//
1273// Monte Carlo Interface
1274//
1275// Set source-name, epoch (default J) and date (default 2000)
1276//
1277void MRawRunHeader::SetSourceInfo(const TString src, char epoch, UShort_t date)
1278{
1279 strncpy(fSourceName, src.Data(), 80);
1280
1281 fSourceEpochChar[0] = epoch; // epoch char of the source
1282 fSourceEpochDate = date; // epoch date of the source
1283}
1284
1285// --------------------------------------------------------------------------
1286//
1287// Monte Carlo Interface
1288//
1289// Set run-start and -stop time
1290//
1291void MRawRunHeader::SetRunTime(const MTime &start, const MTime &end)
1292{
1293 fRunStart = start;
1294 fRunStop = end;
1295}
1296
1297// --------------------------------------------------------------------------
1298//
1299// Monte Carlo Interface
1300//
1301// Set project name and observation mode
1302//
1303void MRawRunHeader::SetObservation(const TString mode, const TString proj)
1304{
1305 strncpy(fProjectName, proj.Data(), 100);
1306 strncpy(fObservationMode, mode.Data(), 60);
1307}
1308
1309// --------------------------------------------------------------------------
1310//
1311// Monte Carlo Interface
1312//
1313// Set number of events in file.
1314//
1315void MRawRunHeader::SetNumEvents(UInt_t num)
1316{
1317 fNumEvents = num;
1318 fNumEventsRead = num;
1319}
1320
1321// --------------------------------------------------------------------------
1322//
1323// NumSamples: 50
1324// NumBytePerSample: 2
1325// SamplingFrequency: 2000
1326// FadcResolution: 12
1327// FadcType: XXXX
1328//
1329Int_t MRawRunHeader::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
1330{
1331 Bool_t rc = kFALSE;
1332
1333 if (IsEnvDefined(env, prefix, "NumSamples", print))
1334 {
1335 rc = kTRUE;
1336 fNumSamplesHiGain = GetEnvValue(env, prefix, "NumSamples", fNumSamplesHiGain);
1337 fNumSamplesLoGain = 0;
1338 }
1339
1340 if (IsEnvDefined(env, prefix, "NumBytesPerSample", print))
1341 {
1342 rc = kTRUE;
1343 fNumBytesPerSample = GetEnvValue(env, prefix, "NumBytesPerSample", fNumBytesPerSample);
1344 }
1345
1346 if (IsEnvDefined(env, prefix, "SamplingFrequency", print))
1347 {
1348 rc = kTRUE;
1349 fSamplingFrequency = GetEnvValue(env, prefix, "SamplingFrequency", fSamplingFrequency);
1350 }
1351
1352 if (IsEnvDefined(env, prefix, "FadcResolution", print))
1353 {
1354 rc = kTRUE;
1355 fFadcResolution = GetEnvValue(env, prefix, "FadcResolution", fFadcResolution);
1356 }
1357 // Saturation behaviour etc.
1358 if (IsEnvDefined(env, prefix, "FadcType", print))
1359 {
1360 //rc = kTRUE;
1361 //TString type = GetEnvValue(env, prefix, "FadcType", "");
1362 // Eval "Siegen", "MUX", Dwarf"
1363 }
1364 else
1365 if (rc)
1366 fFadcType = 0xffff; // "Artificial"
1367
1368 return rc;
1369}
1370
Note: See TracBrowser for help on using the repository browser.