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