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

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