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

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