]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderRoot.cxx
Restoring backward compatibility of the SSD calibration objects + output of the SSD...
[u/mrichter/AliRoot.git] / RAW / AliRawReaderRoot.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 ///
20 /// This is a class for reading raw data from a root file.
21 ///
22 /// The root file is expected to contain a tree of name "RAW" with
23 /// a branch of name "rawevent" which contains objects of type
24 /// AliRawEvent.
25 /// 
26 /// The file name and the event number are arguments of the constructor
27 /// of AliRawReaderRoot.
28 ///
29 ///////////////////////////////////////////////////////////////////////////////
30
31 #include <TFile.h>
32 #include <TTree.h>
33 #include "AliRawReaderRoot.h"
34 #include "AliRawEvent.h"
35 #include "AliRawEventHeaderBase.h"
36 #include "AliRawEquipment.h"
37 #include "AliRawEquipmentHeader.h"
38 #include "AliRawData.h"
39
40
41 ClassImp(AliRawReaderRoot)
42
43 AliRawReaderRoot::AliRawReaderRoot() :
44   fFile(NULL),
45   fBranch(NULL),
46   fEventIndex(-1),
47   fEvent(NULL),
48   fSubEventIndex(0),
49   fSubEvent(NULL),
50   fEquipmentIndex(0),
51   fEquipment(NULL),
52   fRawData(NULL),
53   fPosition(NULL),
54   fEnd(NULL)
55 {
56 // default constructor
57
58 }
59
60 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
61   fFile(NULL),
62   fBranch(NULL),
63   fEventIndex(eventNumber),
64   fEvent(NULL),
65   fSubEventIndex(0),
66   fSubEvent(NULL),
67   fEquipmentIndex(0),
68   fEquipment(NULL),
69   fRawData(NULL),
70   fPosition(NULL),
71   fEnd(NULL)
72 {
73 // create an object to read digits from the given input file for the
74 // event with the given number
75
76   TDirectory* dir = gDirectory;
77   fFile = TFile::Open(fileName);
78   dir->cd();
79   if (!fFile || !fFile->IsOpen()) {
80     Error("AliRawReaderRoot", "could not open file %s", fileName);
81     return;
82   }
83   TTree* tree = (TTree*) fFile->Get("RAW");
84   if (!tree) {
85     Error("AliRawReaderRoot", "no raw data tree found");
86     return;
87   }
88   fBranch = tree->GetBranch("rawevent");
89   if (!fBranch) {
90     Error("AliRawReaderRoot", "no raw data branch found");
91     return;
92   }
93
94   fEvent = new AliRawEvent;
95   fBranch->SetAddress(&fEvent);
96   if (fEventIndex >= 0) {
97     if (fBranch->GetEntry(fEventIndex) <= 0) {
98       Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
99       return;
100     }
101   }
102 }
103
104 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
105   fFile(NULL),
106   fBranch(NULL),
107   fEventIndex(-1),
108   fEvent(event),
109   fSubEventIndex(0),
110   fSubEvent(NULL),
111   fEquipmentIndex(0),
112   fEquipment(NULL),
113   fRawData(NULL),
114   fPosition(NULL),
115   fEnd(NULL)
116 {
117 // create an object to read digits from the given raw event
118
119 }
120
121 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
122   AliRawReader(rawReader),
123   fFile(NULL),
124   fBranch(NULL),
125   fEventIndex(rawReader.fEventIndex),
126   fEvent(NULL),
127   fSubEventIndex(rawReader.fSubEventIndex),
128   fSubEvent(NULL),
129   fEquipmentIndex(rawReader.fEquipmentIndex),
130   fEquipment(NULL),
131   fRawData(NULL),
132   fPosition(NULL),
133   fEnd(NULL)
134 {
135 // copy constructor
136
137   if (rawReader.fFile) {
138     TDirectory* dir = gDirectory;
139     fFile = TFile::Open(rawReader.fFile->GetName());
140     dir->cd();
141     if (!fFile || !fFile->IsOpen()) {
142       Error("AliRawReaderRoot", "could not open file %s", 
143             rawReader.fFile->GetName());
144       return;
145     }
146     TTree* tree = (TTree*) fFile->Get("RAW");
147     if (!tree) {
148       Error("AliRawReaderRoot", "no raw data tree found");
149       return;
150     }
151     fBranch = tree->GetBranch("rawevent");
152     if (!fBranch) {
153       Error("AliRawReaderRoot", "no raw data branch found");
154       return;
155     }
156
157     fEvent = new AliRawEvent;
158     fBranch->SetAddress(&fEvent);
159     if (fEventIndex >= 0) {
160       if (fBranch->GetEntry(fEventIndex) <= 0) {
161         Error("AliRawReaderRoot", "no event with number %d found", 
162               fEventIndex);
163         return;
164       }
165     }
166   } else {
167     fEvent = rawReader.fEvent;
168   }
169
170   if (fSubEventIndex > 0) {
171     fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
172     fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
173     fRawData = fEquipment->GetRawData();
174       fCount = 0;
175     fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() + 
176       ((UChar_t*) rawReader.fHeader - 
177        (UChar_t*) rawReader.fRawData->GetBuffer()));
178     fPosition = (UChar_t*) fRawData->GetBuffer() + 
179       (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
180     fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
181   }
182 }
183
184 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot& 
185                                                 rawReader)
186 {
187 // assignment operator
188
189   this->~AliRawReaderRoot();
190   new(this) AliRawReaderRoot(rawReader);
191   return *this;
192 }
193
194 AliRawReaderRoot::~AliRawReaderRoot()
195 {
196 // delete objects and close root file
197
198   if (fFile) {
199     if (fEvent) delete fEvent;
200     fFile->Close();
201     delete fFile;
202   }
203 }
204
205 const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
206 {
207   // Get the even header
208   // Return NULL in case of failure
209   if (!fEvent) return NULL;
210   return fEvent->GetHeader();
211 }
212
213 UInt_t AliRawReaderRoot::GetType() const
214 {
215 // get the type from the event header
216
217   if (!fEvent) return 0;
218   return fEvent->GetHeader()->Get("Type");
219 }
220
221 UInt_t AliRawReaderRoot::GetRunNumber() const
222 {
223 // get the run number from the event header
224
225   if (!fEvent) return 0;
226   return fEvent->GetHeader()->Get("RunNb");
227 }
228
229 const UInt_t* AliRawReaderRoot::GetEventId() const
230 {
231 // get the event id from the event header
232
233   if (!fEvent) return NULL;
234   return fEvent->GetHeader()->GetP("Id");
235 }
236
237 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
238 {
239 // get the trigger pattern from the event header
240
241   if (!fEvent) return NULL;
242   return fEvent->GetHeader()->GetP("TriggerPattern");
243 }
244
245 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
246 {
247 // get the detector pattern from the event header
248
249   if (!fEvent) return NULL;
250   return fEvent->GetHeader()->GetP("DetectorPattern");
251 }
252
253 const UInt_t* AliRawReaderRoot::GetAttributes() const
254 {
255 // get the type attributes from the event header
256
257   if (!fEvent) return NULL;
258   return fEvent->GetHeader()->GetP("TypeAttribute");
259 }
260
261 const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
262 {
263 // get the type attributes from the sub event header
264
265   if (!fSubEvent) return NULL;
266   return fSubEvent->GetHeader()->GetP("TypeAttribute");
267 }
268
269 UInt_t AliRawReaderRoot::GetLDCId() const
270 {
271 // get the LDC Id from the event header
272
273   if (!fEvent || !fSubEvent) return 0;
274   return fSubEvent->GetHeader()->Get("LdcId");
275 }
276
277 UInt_t AliRawReaderRoot::GetGDCId() const
278 {
279 // get the GDC Id from the event header
280
281   if (!fEvent) return 0;
282   return fEvent->GetHeader()->Get("GdcId");
283 }
284
285 UInt_t AliRawReaderRoot::GetTimestamp() const
286 {
287   if (!fEvent) return 0;
288   return fEvent->GetHeader()->Get("Timestamp");
289 }
290
291 Int_t AliRawReaderRoot::GetEquipmentSize() const
292 {
293 // get the size of the equipment
294
295   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
296   return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
297 }
298
299 Int_t AliRawReaderRoot::GetEquipmentType() const
300 {
301 // get the type from the equipment header
302
303   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
304   return fEquipment->GetEquipmentHeader()->GetEquipmentType();
305 }
306
307 Int_t AliRawReaderRoot::GetEquipmentId() const
308 {
309 // get the ID from the equipment header
310
311   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
312   return fEquipment->GetEquipmentHeader()->GetId();
313 }
314
315 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
316 {
317 // get the attributes from the equipment header
318
319   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
320   return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
321 }
322
323 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
324 {
325 // get the basic element size from the equipment header
326
327   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
328   return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
329 }
330
331 Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
332 {
333 // get the size of the equipment header (28 bytes by default)
334
335   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
336   return fEquipment->GetEquipmentHeader()->HeaderSize();
337 }
338
339 // _________________________________________________________________________
340 void AliRawReaderRoot::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
341   // The method swaps the contents of the
342   // raw-data event header
343   UInt_t  intCount = (size+3)/sizeof(UInt_t);
344
345   UInt_t* buf = (UInt_t*) inbuf;    // temporary integers buffer
346   for (UInt_t i=0; i<intCount; i++, buf++) {
347       UInt_t value = SwapWord(*buf);
348       if (i==(intCount-1))
349          memcpy((UInt_t*)outbuf+i, &value, size%sizeof(UInt_t));
350       else
351          memcpy((UInt_t*)outbuf+i, &value, sizeof(UInt_t));
352   }
353 }
354 // _________________________________________________________________________
355
356 Bool_t AliRawReaderRoot::ReadHeader()
357 {
358 // read a data header at the current position
359 // returns kFALSE if the data header could not be read
360
361   fErrorCode = 0;
362   if (!fEvent) return kFALSE;
363
364   do {
365     // skip payload (if event was not selected)
366     if (fCount > 0) fPosition += fCount;
367
368     // get the first or the next equipment if at the end of an equipment
369     if (!fEquipment || (fPosition >= fEnd)) {
370
371       // get the first or the next sub event if at the end of a sub event
372       if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
373
374         // check for end of event data
375         if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
376         fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
377
378         // check the magic word of the sub event
379         if (!fSubEvent->GetHeader()->IsValid()) {
380           Error("ReadHeader", "wrong magic number in sub event!");
381           fSubEvent->GetHeader()->Dump();
382           fErrorCode = kErrMagic;
383           return kFALSE;
384         }
385
386         fEquipmentIndex = 0;
387         fEquipment = NULL;
388         fRawData = NULL;
389       }
390
391       // get the next equipment and raw data
392       fCount = 0;
393       fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
394       if (!fEquipment) continue;
395       if (!IsSelected()) {
396         fPosition = fEnd;
397         continue;
398       }
399       fRawData = fEquipment->GetRawData();
400       if (!fRawData) {
401         fPosition = fEnd;
402         continue;
403       }
404       fPosition = (UChar_t*) fRawData->GetBuffer();
405       fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
406     }
407
408     // continue with the next equipment if no data left in the payload
409     if (fPosition >= fEnd) continue;
410
411     if (fRequireHeader) {
412       // check that there are enough bytes left for the data header
413       if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
414         Error("ReadHeader", "could not read data header!");
415         Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
416         fEquipment->GetEquipmentHeader()->Dump();
417         fCount = 0;
418         fPosition = fEnd;
419         fErrorCode = kErrNoDataHeader;
420         continue;
421       }
422
423       // "read" the data header
424       fHeader = (AliRawDataHeader*) fPosition;
425 #ifndef R__BYTESWAP
426       SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
427       fHeader=fHeaderSwapped;
428 #endif
429       if ((fPosition + fHeader->fSize) != fEnd) {
430         if (fHeader->fSize != 0xFFFFFFFF)
431           Warning("ReadHeader",
432                   "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
433                   fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
434         fHeader->fSize = fEnd - fPosition;
435       }
436       fPosition += sizeof(AliRawDataHeader);
437     }
438
439     if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
440       fCount = fHeader->fSize - sizeof(AliRawDataHeader);
441
442       // check consistency of data size in the header and in the sub event
443       if (fPosition + fCount > fEnd) {  
444         Error("ReadHeader", "size in data header exceeds event size!");
445         Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
446         fEquipment->GetEquipmentHeader()->Dump();
447         fCount = 0;
448         fPosition = fEnd;
449         fErrorCode = kErrSize;
450         continue;
451       }
452
453     } else {
454       fCount = fEnd - fPosition;
455     }
456
457   } while (!IsSelected());
458
459   return kTRUE;
460 }
461
462 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
463 {
464 // reads the next payload at the current position
465 // returns kFALSE if the data could not be read
466
467   fErrorCode = 0;
468   while (fCount == 0) {
469     if (!ReadHeader()) return kFALSE;
470   }
471   data = fPosition;
472   fPosition += fCount;  
473   fCount = 0;
474   return kTRUE;
475 }
476
477 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
478 {
479 // reads the next block of data at the current position
480 // returns kFALSE if the data could not be read
481
482   fErrorCode = 0;
483   if (fPosition + size > fEnd) {
484     Error("ReadNext", "could not read data!");
485     fErrorCode = kErrOutOfBounds;
486     return kFALSE;
487   }
488   memcpy(data, fPosition, size);
489
490   fPosition += size;
491   fCount -= size;
492   return kTRUE;
493 }
494
495
496 Bool_t AliRawReaderRoot::Reset()
497 {
498 // reset the current position to the beginning of the event
499
500   fSubEventIndex = 0;
501   fSubEvent = NULL;
502   fEquipmentIndex = 0;
503   fEquipment = NULL;
504   fRawData = NULL;
505   fHeader = NULL;
506
507   fCount = 0;
508   fPosition = fEnd = NULL;
509   return kTRUE;
510 }
511
512
513 Bool_t AliRawReaderRoot::NextEvent()
514 {
515 // go to the next event in the root file
516
517   if (!fBranch) return kFALSE;
518
519   do {
520     delete fEvent;
521     fEvent = new AliRawEvent;
522     fBranch->SetAddress(&fEvent);
523     if (fBranch->GetEntry(fEventIndex+1) <= 0)
524       return kFALSE;
525     fEventIndex++;
526   } while (!IsEventSelected());
527   fEventNumber++;
528   return Reset();
529 }
530
531 Bool_t AliRawReaderRoot::RewindEvents()
532 {
533 // go back to the beginning of the root file
534
535   if (!fBranch) return kFALSE;
536
537   fEventIndex = -1;
538   delete fEvent;
539   fEvent = new AliRawEvent;
540   fBranch->SetAddress(&fEvent);
541   fEventNumber = -1;
542   return Reset();
543 }
544
545
546 Int_t AliRawReaderRoot::CheckData() const
547 {
548 // check the consistency of the data
549
550   if (!fEvent) return 0;
551
552   AliRawEvent* subEvent = NULL;
553   Int_t subEventIndex = 0;
554   AliRawEquipment* equipment = NULL;
555   Int_t equipmentIndex = 0;
556   UChar_t* position = 0;
557   UChar_t* end = 0;
558   Int_t result = 0;
559
560   while (kTRUE) {
561     // get the first or the next sub event if at the end of an equipment
562     if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
563
564       // check for end of event data
565       if (subEventIndex >= fEvent->GetNSubEvents()) return result;
566       subEvent = fEvent->GetSubEvent(subEventIndex++);
567
568       // check the magic word of the sub event
569       if (!fSubEvent->GetHeader()->IsValid()) {
570         result |= kErrMagic;
571         return result;
572       }
573
574       equipmentIndex = 0;
575     }
576
577     // get the next equipment and raw data
578     equipment = subEvent->GetEquipment(equipmentIndex++);
579     if (!equipment) continue;
580     AliRawData* rawData = equipment->GetRawData();
581     if (!rawData) continue;
582     position = (UChar_t*) rawData->GetBuffer();
583     end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
584
585     // continue with the next sub event if no data left in the payload
586     if (position >= end) continue;
587
588     if (fRequireHeader) {
589     // check that there are enough bytes left for the data header
590       if (position + sizeof(AliRawDataHeader) > end) {
591         result |= kErrNoDataHeader;
592         continue;
593       }
594
595       // check consistency of data size in the header and in the equipment
596       AliRawDataHeader* header = (AliRawDataHeader*) position;
597       if ((position + header->fSize) != end) {
598         if (header->fSize != 0xFFFFFFFF)
599           Warning("ReadHeader",
600                   "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
601                   equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
602         header->fSize = end - position;
603         result |= kErrSize;
604       }
605     }
606     position = end;
607   };
608
609   return result;
610 }