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

Last change on this file since 8339 was 8326, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 19.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-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MRawRunHeader
28//
29// Root storage container for the RUN HEADER information
30//
31//
32// RAW DATA FORMAT VERSION
33// =======================
34//
35// Format Version 6:
36// -----------------
37// + added CameraVersion
38// + added TelescopeNumber
39// + added ObservationMode
40// + added dummies for TelescopeRa/Dec
41//
42// Format Version 5:
43// -----------------
44// - now the sub millisecond information of the time is valid and decoded
45// which enhances the precision from 51.2us to 200ns
46//
47// Format Version 4:
48// -----------------
49// - added support for pixels with negative IDs
50//
51// Format Version 3:
52// -----------------
53// - ???
54//
55// Format Version 2:
56// -----------------
57// - removed mjd from data
58// - added start time
59// - added stop time
60//
61//
62// MRawRunHeader CLASS VERSION
63// ===========================
64//
65// Class Version 5:
66// -----------------
67// - for compatibility with newer camera versions
68//
69// Class Version 4:
70// -----------------
71// - added fCameraVersion
72// - added fTelescopeNumber
73// - changed length of fProjectName to 101
74// - changed length of fSourceName to 81
75//
76// Class Version 3:
77// ----------------
78// - enhanced SourceName and ProjectName by one character, because
79// without telling us the guranteed trailing \0-character has
80// skipped
81//
82// Class Version 2:
83// ----------------
84// - removed fMJD, fYear, fMonth, fDay
85// - added fRunStart
86// - added fRunStop
87//
88// Class Version 1:
89// ----------------
90// - first implementation
91//
92////////////////////////////////////////////////////////////////////////////
93
94#include "MRawRunHeader.h"
95
96#include <fstream>
97#include <iomanip>
98
99#include "MLog.h"
100#include "MLogManip.h"
101
102#include "MArrayS.h"
103
104ClassImp(MRawRunHeader);
105
106using namespace std;
107
108const UShort_t MRawRunHeader::kMagicNumber = 0xc0c0;
109const Byte_t MRawRunHeader::kMaxFormatVersion = 6;
110
111// --------------------------------------------------------------------------
112//
113// Default constructor. Creates array which stores the pixel assignment.
114//
115//
116MRawRunHeader::MRawRunHeader(const char *name, const char *title) : fPixAssignment(NULL)
117{
118 fName = name ? name : "MRawRunHeader";
119 fTitle = title ? title : "Raw Run Header Information";
120
121 fPixAssignment = new MArrayS(0);
122
123 fFormatVersion=0;
124 fSoftVersion=0;
125 fTelescopeNumber=0;
126 fCameraVersion=0;
127 fRunType=kRTNone; // use 0xffff for invalidation, 0 means: Data run
128 fRunNumber=0;
129 memset(fProjectName, 0, 101);
130 memset(fSourceName, 0, 81);
131 memset(fObservationMode, 0, 61);
132 fSourceEpochChar[0]=0;
133 fSourceEpochDate=0;
134 fNumCrates=0;
135 fNumPixInCrate=0;
136 fNumSamplesLoGain=0;
137 fNumSamplesHiGain=0;
138 fNumEvents=0;
139}
140
141// --------------------------------------------------------------------------
142//
143// Destructor. Deletes the 'pixel-assignment-array'
144//
145MRawRunHeader::~MRawRunHeader()
146{
147 delete fPixAssignment;
148}
149
150// --------------------------------------------------------------------------
151//
152// Swap the assignment of the pixels with hardware id id0 and id1
153//
154Bool_t MRawRunHeader::SwapAssignment(Short_t id0, Short_t id1)
155{
156 const Int_t n = fPixAssignment->GetSize();
157
158 // Look-up-table
159 UShort_t *lut = fPixAssignment->GetArray();
160
161 // Search for one of the hardware indices to get exchanged
162 int i;
163 for (i=0; i<n; i++)
164 if (lut[i]==id0 || lut[i]==id1)
165 break;
166
167 // Check if one of the two pixels were found
168 if (i==n)
169 {
170 *fLog << warn << "WARNING - Assignment of pixels with hardware ID " << id0 << " and " << id1;
171 *fLog << " should be exchanged, but none were found." << endl;
172 return kTRUE;
173 }
174
175 // Store first index
176 const Int_t idx0 = i;
177
178 // Search for the other hardware indices to get exchanged
179 for (i++; i<n; i++)
180 if (lut[i]==id0 || lut[i]==id1)
181 break;
182
183 // Check if the second pixel was found
184 if (i==n)
185 {
186 *fLog << err << "ERROR - Assignment of pixels with hardware ID " << id0 << " and " << id1;
187 *fLog << " should be exchanged, but only one was found." << endl;
188 return kFALSE;
189 }
190
191 const Int_t idx1 = i;
192
193 const Short_t p0 = lut[idx0];
194 const Short_t p1 = lut[idx1];
195
196 lut[idx0] = p1;
197 lut[idx1] = p0;
198
199 *fLog << inf << "Assignment of pixels with hardware ID " << id0 << " and " << id1 << " exchanged." << endl;
200
201 return kTRUE;
202}
203
204// --------------------------------------------------------------------------
205//
206// This implements a fix of the pixel assignment before 25.9.2005
207//
208// From magic_online (the pixel numbers are hardware indices):
209// --------------------------------------------------------------------------
210// From: Florian Goebel <fgoebel@mppmu.mpg.de>
211// Date: Sun Sep 25 2005 - 05:13:19 CEST
212//
213// [...]
214// - problem 2: pixels were swaped
215// - cause: opical fibers were wrongly connected to receiver board
216// 554 <-> 559
217// 555 <-> 558
218// 556 <-> 557
219// - action: reconnect correctly
220// - result: ok now
221// - comment: I don't know when this error was introduced, so backward
222// correction of the data is difficult.
223// Fortunately the effect is not too large since the affected 6 pixels are
224// on the outermost edge of the camera
225// Since this board has special pixels the optical fibers have been
226// unplugged several times.
227// !!!!! Whenever you unplug and reconnect optical fibers make really !!!!!
228// !!!!! sure you connect them back correctly. !!!!!
229// !!!!! Always contact me before you do so and if you have any doubts !!!!!
230// !!!!! how to reconnect the fibers ask me. !!!!!
231// These swapped pixels have only been found by chance when doing the
232// flatfielding.
233// [...]
234//
235// --------------------------------------------------------------------------
236//
237// MAGIC runbook CC_2006_04_22_22_28_52.rbk
238//
239// [2006-04-22 23:14:13]
240//
241// [...]
242// Found 2 pairs of swapped pixels.
243// We corrected swapped pixels 54<->55 in the receiver boards. We probably
244// swapped today in the camera.
245// We did not correct 92<-<93 which are likely swapped. Will check and correct
246// tomorrow.
247//
248// ---
249//
250// comments:
251// - 54<->55 were corrected but have not been swapped, hence they are swapped
252// since then (run 88560 ok, run 88669 swapped; between them mostly dummy and
253// test runs)
254// - 92<->93 are never swapped, always ok.
255//
256// --------------------------------------------------------------------------
257//
258// MAGIC runbook CC_2006_08_28_19_40_18.rbk
259//
260// [2006-08-28 23:09:07]
261// While doing a flatfielding we have found out that the signals for pixels
262// 119 and 120 were swapped. We have fixed it by exchanging the corresponding
263// fibers at the input of the receivers (not before the splitters!).
264//
265// ---
266//
267// MAGIC runbook CC_2006_08_29_15_19_14.rbk
268//
269// [2006-08-29 16:43:09]
270// In the last hours we have found out and fixed a good number of pixels which
271// were swapped: 119-120, 160-161-162 and 210-263. According to Florian,
272// 160-161-162 and 210-263 were swapped since November 2005.
273//
274// ---
275//
276// mail Florian Goebel (08/30/2006 03:13 PM):
277//
278// As far as I can tell pixels 161 and 162 as well as 263 and 210 were
279// swapped in the trigger. This leads to some inefficiency of the trigger.
280// However, they were not swapped in the readout. So, you don't have to
281// correct anything in the data for these pixels.
282//
283// ---
284//
285// comments:
286// - 119-120 swapped between run 93251 (not swapped) and 93283 (swapped)
287// (only testruns between these runs)
288// corrected since run 99354 (== runbook [2006-08-28 23:09:07])
289// - 160 never swapped
290// - 161-162 were only swapped in the trigger, but nevertheless were
291// "corrected" also in the signal. Hence signal swapped since 99354
292//
293// --------------------------------------------------------------------------
294
295Bool_t MRawRunHeader::FixAssignment()
296{
297 if (fRunNumber>=53300 && fRunNumber<=68754)
298 {
299 if (!SwapAssignment(554, 559))
300 return kFALSE;
301 if (!SwapAssignment(555, 558))
302 return kFALSE;
303 if (!SwapAssignment(556, 557))
304 return kFALSE;
305 }
306
307 if (fRunNumber>=93283 && fRunNumber<99354)
308 {
309 if (!SwapAssignment(119, 120))
310 return kFALSE;
311 }
312
313 if (fRunNumber>=99354 && fRunNumber<=101789)
314 {
315 if (!SwapAssignment(161, 162))
316 return kFALSE;
317 if (!SwapAssignment(210, 263))
318 return kFALSE;
319 }
320
321 if (fRunNumber>=88669)
322 {
323 if (!SwapAssignment(54, 55))
324 return kFALSE;
325 }
326
327 return kTRUE;
328}
329
330// --------------------------------------------------------------------------
331//
332// Read in one run header from the binary file
333//
334Bool_t MRawRunHeader::ReadEvt(istream& fin)
335{
336 //
337 // read one RUN HEADER from the input stream
338 //
339 fMagicNumber = 0;
340
341 fin.read((char*)&fMagicNumber, 2); // Total=2
342
343 //
344 // check whether the the file has the right file type or not
345 //
346 if (fMagicNumber != kMagicNumber && fMagicNumber != kMagicNumber+1)
347 {
348 *fLog << err << "ERROR - Wrong Magic Number (0x" << hex << fMagicNumber << "): Not a Magic File!" << endl;
349 return kFALSE;
350 }
351
352 if (fMagicNumber == kMagicNumber+1)
353 *fLog << warn << "WARNING - This file maybe broken (0xc0c1) - DAQ didn't close it correctly!" << endl;
354
355 Byte_t dummy[16];
356
357 // ----- File format version -----
358 fin.read((char*)&fFormatVersion, 2); // Total=4
359 if (fFormatVersion>kMaxFormatVersion)
360 {
361 *fLog << err << "ERROR - File format V" << fFormatVersion << " not implemented!" << endl;
362 return kFALSE;
363 }
364
365 // ----- DAQ software format version -----
366 fin.read((char*)&fSoftVersion, 2); // Total=6
367
368 // ----- Camera geometry and telescope number -----
369 if (fFormatVersion>5)
370 {
371 fin.read((char*)&fCameraVersion, 2); // (+2)
372 fin.read((char*)&fTelescopeNumber, 2); // (+2)
373 }
374 // Maybe we should set fCameraVersion and fTelescopeNumber to 1
375 // in case of fFormatVersion<6
376
377 // ----- Run information -----
378 fin.read((char*)&fRunType, 2); // Total=8
379 fin.read((char*)&fRunNumber, 4); // Total=12
380 fin.read((char*)&fProjectName, fFormatVersion>5?100:22); // Total=34 (+78)
381 fin.read((char*)&fSourceName, fFormatVersion>5? 80:12); // Total=46 (+58)
382
383 if (fFormatVersion>5)
384 fin.read((char*)fObservationMode, 60); // (+60)
385 // Maybe we should set fObservationMode to something
386 // in case of fFormatVersion<6
387
388 // ----- Source position -----
389 if (fFormatVersion>5)
390 {
391 fin.read((char*)dummy, 4); // F32 SourceRA; Total=48
392 fin.read((char*)dummy, 4); // F32 SourceDEC; Total=52
393 }
394 // Maybe we should set these to something
395 // in case of fFormatVersion<6
396 fin.read((char*)dummy, 4); // F32 TelescopeRA; (+4)
397 fin.read((char*)dummy, 4); // F32 TelescopeDEC; (+4)
398 fin.read((char*)&fSourceEpochChar, 2); // Total=56
399 fin.read((char*)&fSourceEpochDate, 2); // Total=58
400
401 // ----- Old Start time -----
402 if (fFormatVersion<2) // Total += 10
403 {
404 UShort_t y, m, d;
405 fin.read((char*)dummy, 4); // Former fMJD[4],
406 fin.read((char*)&y, 2); // Former fDateYear[2]
407 fin.read((char*)&m, 2); // Former fDateMonth[2]
408 fin.read((char*)&d, 2); // Former fDateDay[2]
409 fRunStart.Set(y, m, d, 0, 0, 0, 0);
410 }
411
412 // ----- Data Geometry -----
413 fin.read((char*)&fNumCrates, 2); // Total=60
414 fin.read((char*)&fNumPixInCrate, 2); // Total=62
415 fin.read((char*)&fNumSamplesLoGain, 2); // Total=64
416 fin.read((char*)&fNumSamplesHiGain, 2); // Total=66
417
418 // ----- Number of events -----
419 fin.read((char*)&fNumEvents, 4); // Total=70
420
421 // ----- Start/Stop time -----
422 if (fFormatVersion>1)
423 {
424 fRunStart.ReadBinary(fin); // Total += 7
425 fRunStop.ReadBinary(fin); // Total += 7
426 }
427
428 //
429 // calculate size of array, create it and fill it
430 //
431 Int_t nPixel = fNumCrates*fNumPixInCrate;
432 fPixAssignment->Set(nPixel);
433
434 // ----- Pixel Assignement -----
435 fin.read((char*)fPixAssignment->GetArray(), nPixel*2);
436 fin.read((char*)&dummy, 16);
437
438 return FixAssignment();
439}
440
441// --------------------------------------------------------------------------
442//
443// Return the run type as string ("Data", "Pedestal", ...), for example
444// to print it as readable text.
445//
446const char *MRawRunHeader::GetRunTypeStr() const
447{
448 switch (fRunType)
449 {
450 case kRTData:
451 return "Data";
452 case kRTPedestal:
453 return "Pedestal";
454 case kRTCalibration:
455 return "Calibration";
456 case kRTPointRun:
457 return "Point-Run";
458 case kRTMonteCarlo:
459 return "Monte Carlo";
460 case kRTNone:
461 return "<none>";
462 default:
463 return "<unknown>";
464 }
465}
466
467// --------------------------------------------------------------------------
468//
469// print run header information on *fLog. The option 'header' supresses
470// the pixel index translation table.
471//
472void MRawRunHeader::Print(Option_t *t) const
473{
474 *fLog << all << endl;
475 *fLog << "MagicNumber: 0x" << hex << fMagicNumber << " - ";
476 switch (fMagicNumber)
477 {
478 case kMagicNumber: *fLog << "OK"; break;
479 case kMagicNumber+1: *fLog << "File not closed!"; break;
480 default: *fLog << "Wrong!"; break;
481 }
482 *fLog << endl;
483 *fLog << "Versions: " << dec << "Format=" << fFormatVersion << " ";
484 *fLog << "Software=" << fSoftVersion << " ";
485 if (fFormatVersion>5)
486 *fLog << "Camera=" << fCameraVersion;
487 *fLog << endl;
488 if (fFormatVersion>5)
489 *fLog << "Telescope: " << fTelescopeNumber << endl;
490 *fLog << "RunNumber: " << fRunNumber << " (Type=" << GetRunTypeStr() << ")" << endl;
491 *fLog << "ProjectName: '" << fProjectName << "'" << endl;
492 if (fFormatVersion>5)
493 *fLog << "Observation: '" << fObservationMode << "'" << endl;
494 *fLog << "Source: '" << fSourceName << "' " << " ";
495 *fLog << fSourceEpochChar << dec << fSourceEpochDate << endl;
496 *fLog << "Run Start: " << fRunStart << endl;
497 *fLog << "Run Stop: " << fRunStop << endl;
498 *fLog << "Crates: " << fNumCrates << " x " << fNumPixInCrate << " Pixel/Crate = " << fNumCrates*fNumPixInCrate << " Pixel/Evt" << endl;
499 *fLog << "Num Pixels: " << GetNumNormalPixels() << " (normal) + " << GetNumSpecialPixels() << " (special) = " << GetNumConnectedPixels() << " (total)" << endl;
500 *fLog << "Samples: " << fNumSamplesHiGain << "/" << fNumSamplesLoGain << " (hi/lo) = " << (fNumSamplesLoGain+fNumSamplesHiGain) * fNumCrates * fNumPixInCrate /1024 << "kiB/Evt" << endl;
501 *fLog << "Evt Counter: " << fNumEvents << endl;
502
503 if (TString(t).Contains("header", TString::kIgnoreCase))
504 return;
505
506 *fLog << inf << "Assignment:" << hex << endl;
507 for (int i=0; i<GetNumPixel(); i++)
508 *fLog << setfill('0') << setw(3) << (*fPixAssignment)[i] << " ";
509
510 *fLog << dec << setfill(' ') << endl;
511}
512
513// --------------------------------------------------------------------------
514//
515// Return the assigned pixel number for the given FADC channel
516//
517Short_t MRawRunHeader::GetPixAssignment(UShort_t i) const
518{
519 // FIXME: Do we need a range check here?
520 return (Short_t)(*fPixAssignment)[i];
521}
522
523// --------------------------------------------------------------------------
524//
525// Return the number of pixel which are markes as connected in the
526// pix assignment (!=0)
527//
528UShort_t MRawRunHeader::GetNumConnectedPixels() const
529{
530 const Int_t num = fPixAssignment->GetSize();
531
532 UShort_t rc = 0;
533 for (int i=0; i<num; i++)
534 {
535 if (GetPixAssignment(i)!=0)
536 rc++;
537 }
538 return rc;
539}
540
541// --------------------------------------------------------------------------
542//
543// Return the number of pixel which are markes as connected and so-called
544// 'normal' pixels in the pix assignment (>0)
545//
546UShort_t MRawRunHeader::GetNumNormalPixels() const
547{
548 const Int_t num = fPixAssignment->GetSize();
549
550 UShort_t rc = 0;
551 for (int i=0; i<num; i++)
552 {
553 if (GetPixAssignment(i)>0)
554 rc++;
555 }
556 return rc;
557}
558
559// --------------------------------------------------------------------------
560//
561// Return the number of pixel which are markes as connected and so-called
562// 'special' pixels in the pix assignment (<0)
563//
564UShort_t MRawRunHeader::GetNumSpecialPixels() const
565{
566 const Int_t num = fPixAssignment->GetSize();
567
568 UShort_t rc = 0;
569 for (int i=0; i<num; i++)
570 {
571 if (GetPixAssignment(i)<0)
572 rc++;
573 }
574 return rc;
575}
576
577// --------------------------------------------------------------------------
578//
579// Return the maximum id which exists in the pix assignment
580//
581UShort_t MRawRunHeader::GetMaxPixId() const
582{
583 const Int_t num = fPixAssignment->GetSize();
584
585 Short_t rc = 0;
586 for (int i=0; i<num; i++)
587 rc = TMath::Max(GetPixAssignment(i), rc);
588
589 return rc;
590}
591
592// --------------------------------------------------------------------------
593//
594// Return minus th minimum id which exists in the pix assignment
595//
596UShort_t MRawRunHeader::GetMinPixId() const
597{
598 const Int_t num = fPixAssignment->GetSize();
599
600 Short_t rc = 0;
601 for (int i=0; i<num; i++)
602 rc = TMath::Min(GetPixAssignment(i), rc);
603
604 return (UShort_t)-rc;
605}
606
607// --------------------------------------------------------------------------
608//
609// Return the number of pixel in this event.
610//
611// WARNING: This is the number of pixels stored in this file which is
612// a multiple of the number of pixels per crate and in general
613// a number which is larger than the camera size!
614//
615// To know the range of the pixel indices please use the geometry
616// container!
617//
618UShort_t MRawRunHeader::GetNumPixel() const
619{
620 return fPixAssignment->GetSize();
621}
622
623// --------------------------------------------------------------------------
624//
625// Returns absolute size in bytes of the run header as read from a raw file.
626// This must be done _after_ the header is read, because the header doesn't
627// have a fixed size (used in MRawSocketRead)
628//
629Int_t MRawRunHeader::GetNumTotalBytes() const
630{
631 switch (fFormatVersion)
632 {
633 case 1:
634 return 80+fNumCrates*fNumPixInCrate*2+16;
635 case 2:
636 case 3:
637 case 4:
638 case 5:
639 return 84+fNumCrates*fNumPixInCrate*2+16;
640 case 6:
641 return 84+fNumCrates*fNumPixInCrate*2+16 +4+78+58+60+8;
642 }
643 return 0;
644}
Note: See TracBrowser for help on using the repository browser.