]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderRoot.cxx
ec45eff76fc6926c374aaa34b7d8817779d8c50c
[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 ///////////////////////////////////////////////////////////////////////////////
17 //
18 // This is a class for reading a raw data from a root file and providing
19 // information about digits
20 //
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "AliRawReaderRoot.h"
24 #include "AliRawEvent.h"
25
26
27 ClassImp(AliRawReaderRoot)
28
29
30 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber)
31 {
32 // create an object to read digits from the given input file for the
33 // event with the given number
34
35   fEvent = NULL;
36   TDirectory* dir = gDirectory;
37   fFile = TFile::Open(fileName);
38   dir->cd();
39   if (!fFile || !fFile->IsOpen()) {
40     Error("AliRawReaderRoot", "could not open file %s", fileName);
41     return;
42   }
43   TTree* tree = (TTree*) fFile->Get("RAW");
44   if (!tree) {
45     Error("AliRawReaderRoot", "no raw data tree found");
46     return;
47   }
48   TBranch* branch = tree->GetBranch("rawevent");
49   if (!branch) {
50     Error("AliRawReaderRoot", "no raw data branch found");
51     return;
52   }
53
54   fEvent = new AliRawEvent;
55   branch->SetAddress(&fEvent);
56   if (branch->GetEntry(eventNumber) <= 0) {
57     Error("AliRawReaderRoot", "no event with number %d found", eventNumber);
58     return;
59   }
60   
61   fSubEventIndex = 0;
62   fSubEvent = NULL;
63   fRawData = NULL;
64   fMiniHeader = NULL;
65
66   fCount = 0;
67   fPosition = fEnd = NULL;
68 }
69
70 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event)
71 {
72 // create an object to read digits from the given raw event
73
74   fFile = NULL;
75   fEvent = event;
76   
77   fSubEventIndex = 0;
78   fSubEvent = NULL;
79   fRawData = NULL;
80   fMiniHeader = NULL;
81
82   fCount = 0;
83   fPosition = fEnd = NULL;
84 }
85
86 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
87   AliRawReader(rawReader)
88 {
89 // copy constructor
90
91   fFile = NULL;
92   fEvent = rawReader.fEvent;
93   
94   fSubEventIndex = rawReader.fSubEventIndex;
95   fSubEvent = rawReader.fSubEvent;
96   fRawData = rawReader.fRawData;
97   fMiniHeader = rawReader.fMiniHeader;
98
99   fCount = rawReader.fCount;
100   fPosition = rawReader.fPosition;
101   fEnd = rawReader.fEnd;
102 }
103
104 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot& 
105                                                 rawReader)
106 {
107 // assignment operator
108
109   this->~AliRawReaderRoot();
110   new(this) AliRawReaderRoot(rawReader);
111   return *this;
112 }
113
114 AliRawReaderRoot::~AliRawReaderRoot()
115 {
116 // delete objects and close root file
117
118   if (fFile) {
119     if (fEvent) delete fEvent;
120     fFile->Close();
121     delete fFile;
122   }
123 }
124
125
126 UInt_t AliRawReaderRoot::GetType() const
127 {
128 // get the type from the event header
129
130   if (!fEvent) return 0;
131   return fEvent->GetHeader()->GetType();
132 }
133
134 UInt_t AliRawReaderRoot::GetRunNumber() const
135 {
136 // get the run number from the event header
137
138   if (!fEvent) return 0;
139   return fEvent->GetHeader()->GetRunNumber();
140 }
141
142 const UInt_t* AliRawReaderRoot::GetEventId() const
143 {
144 // get the event id from the event header
145
146   if (!fEvent) return NULL;
147   return fEvent->GetHeader()->GetId();
148 }
149
150 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
151 {
152 // get the trigger pattern from the event header
153
154   if (!fEvent) return NULL;
155   return fEvent->GetHeader()->GetTriggerPattern();
156 }
157
158 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
159 {
160 // get the detector pattern from the event header
161
162   if (!fEvent) return NULL;
163   return fEvent->GetHeader()->GetDetectorPattern();
164 }
165
166 const UInt_t* AliRawReaderRoot::GetAttributes() const
167 {
168 // get the type attributes from the event header
169
170   if (!fEvent) return NULL;
171   return fEvent->GetHeader()->GetTypeAttribute();
172 }
173
174 UInt_t AliRawReaderRoot::GetGDCId() const
175 {
176 // get the GDC Id from the event header
177
178   if (!fEvent) return 0;
179   return fEvent->GetHeader()->GetGDCId();
180 }
181
182
183 Bool_t AliRawReaderRoot::ReadMiniHeader()
184 {
185 // read a mini header at the current position
186 // returns kFALSE if the mini header could not be read
187
188   fErrorCode = 0;
189   if (!fEvent) return kFALSE;
190
191   do {
192     // skip payload (if event was not selected)
193     if (fCount > 0) fPosition += fCount;
194
195     // get the first or the next sub event if at the end of a sub event
196     if (!fSubEvent || (fPosition >= fEnd)) {
197
198       // check for end of event data
199       if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
200       fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
201
202       // check the magic word of the sub event
203       if (!fSubEvent->GetHeader()->IsValid()) {
204         Error("ReadMiniHeader", "wrong magic number in sub event!");
205         fSubEvent->GetHeader()->Dump();
206         fErrorCode = kErrMagic;
207         return kFALSE;
208       }
209
210       fRawData = fSubEvent->GetRawData();
211       fCount = 0;
212       fPosition = (UChar_t*) fRawData->GetBuffer();
213       fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
214     }
215
216     // continue with the next sub event if no data left in the payload
217     if (fPosition >= fEnd) continue;
218
219     // check that there are enough bytes left for the mini header
220     if (fPosition + sizeof(AliMiniHeader) > fEnd) {
221       Error("ReadMiniHeader", "could not read mini header data!");
222       Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
223       fSubEvent->GetHeader()->Dump();
224       fCount = 0;
225       fPosition = fEnd;
226       fErrorCode = kErrNoMiniHeader;
227       continue;
228     }
229
230     // "read" and check the mini header
231     fMiniHeader = (AliMiniHeader*) fPosition;
232     fPosition += sizeof(AliMiniHeader);
233     if (!CheckMiniHeader()) {
234       Error("ReadMiniHeader", "wrong magic word in mini header!");
235       Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
236       fSubEvent->GetHeader()->Dump();
237       fCount = 0;
238       fPosition = fEnd;
239       fErrorCode = kErrMiniMagic;
240       continue;
241     }
242     fCount = fMiniHeader->fSize;
243
244     // check consistency of data size in the mini header and in the sub event
245     if (fPosition + fCount > fEnd) {  
246       Error("ReadMiniHeader", "size in mini header exceeds event size!");
247       Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
248       fSubEvent->GetHeader()->Dump();
249       fCount = 0;
250       fPosition = fEnd;
251       fErrorCode = kErrSize;
252       continue;
253     }
254
255   } while (!IsSelected());
256
257   return kTRUE;
258 }
259
260 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
261 {
262 // reads the next payload at the current position
263 // returns kFALSE if the data could not be read
264
265   fErrorCode = 0;
266   while (fCount == 0) {
267     if (!ReadMiniHeader()) return kFALSE;
268   }
269   data = fPosition;
270   fPosition += fCount;  
271   fCount = 0;
272   return kTRUE;
273 }
274
275 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
276 {
277 // reads the next block of data at the current position
278 // returns kFALSE if the data could not be read
279
280   fErrorCode = 0;
281   if (fPosition + size > fEnd) {
282     Error("ReadNext", "could not read data!");
283     fErrorCode = kErrOutOfBounds;
284     return kFALSE;
285   }
286   memcpy(data, fPosition, size);
287   fPosition += size;
288   fCount -= size;
289   return kTRUE;
290 }
291
292
293 Bool_t AliRawReaderRoot::Reset()
294 {
295 // reset the current position to the beginning of the event
296
297   fSubEventIndex = 0;
298   fSubEvent = NULL;
299   fRawData = NULL;
300   fMiniHeader = NULL;
301
302   fCount = 0;
303   fPosition = fEnd = NULL;
304   return kTRUE;
305 }
306
307
308 Int_t AliRawReaderRoot::CheckData() const
309 {
310 // check the consistency of the data
311
312   if (!fEvent) return 0;
313
314   AliRawEvent* subEvent = NULL;
315   Int_t subEventIndex = 0;
316   UChar_t* position = 0;
317   UChar_t* end = 0;
318   Int_t result = 0;
319
320   while (kTRUE) {
321     // get the first or the next sub event if at the end of a sub event
322     if (!subEvent || (position >= end)) {
323
324       // check for end of event data
325       if (subEventIndex >= fEvent->GetNSubEvents()) return result;
326       subEvent = fEvent->GetSubEvent(subEventIndex++);
327
328       // check the magic word of the sub event
329       if (!fSubEvent->GetHeader()->IsValid()) {
330         result |= kErrMagic;
331         return result;
332       }
333
334       AliRawData* rawData = subEvent->GetRawData();
335       position = (UChar_t*) rawData->GetBuffer();
336       end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
337     }
338
339     // continue with the next sub event if no data left in the payload
340     if (position >= end) continue;
341
342     // check that there are enough bytes left for the mini header
343     if (position + sizeof(AliMiniHeader) > end) {
344       result |= kErrNoMiniHeader;
345       position = end;
346       continue;
347     }
348
349     // "read" and check the mini header
350     AliMiniHeader* miniHeader = (AliMiniHeader*) position;
351     position += sizeof(AliMiniHeader);
352     if (!CheckMiniHeader(miniHeader)) {
353       result |= kErrMiniMagic;
354       position = end;
355       continue;
356     }
357
358     // check consistency of data size in the mini header and in the sub event
359     if (position + miniHeader->fSize > end) result |= kErrSize;
360     position += miniHeader->fSize;
361   };
362 }