Fixed bug in the documentation (Marian)
[u/mrichter/AliRoot.git] / RAW / AliRawEventHeaderBase.cxx
CommitLineData
f2dc6b20 1/**************************************************************************
2 * Copyright(c) 1998-2003, 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
9edefa04 16/* $Id$ */
17
18// Author: Cvetan Cheshkov 10/10/2005
19
f2dc6b20 20//////////////////////////////////////////////////////////////////////////
21// //
22// AliRawEventHeaderBase //
23// This a new versioning scheme for raw data root-ification and reading //
24// For details look at offline weekly meeting 20/10/2005 //
25// //
26//////////////////////////////////////////////////////////////////////////
27
28#include <unistd.h>
29
9edefa04 30#include <Bytes.h>
f2dc6b20 31#include <TClass.h>
32#include <TDataMember.h>
7ca4655f 33#include <TList.h>
f2dc6b20 34#include <TMethodCall.h>
35
36#include "AliLog.h"
37#include "AliRawEventHeaderBase.h"
38
12e921cc 39#include <Riostream.h>
f2dc6b20 40
41ClassImp(AliRawEventHeaderBase)
42
f3c1e83c 43//______________________________________________________________________________
44AliRawEventHeaderBase::AliRawEventHeaderBase():
45fSize(0),
46fMagic(0),
47fHeadSize(0),
48fVersion(0),
49fExtendedDataSize(0),
33778962 50fExtendedAllocSize(0),
f3c1e83c 51fExtendedData(NULL),
52fIsSwapped(kFALSE)
53{
54 // Default constructor
55}
f2dc6b20 56
57//______________________________________________________________________________
58void *AliRawEventHeaderBase::HeaderBegin()
59{
60 // Returns the pointer to the first data member
61 // beyond the base class data members
62
63 TList *datalist = IsA()->GetListOfDataMembers();
64 TIter next(datalist);
65 TDataMember *member = (TDataMember *)next();
66
67 if(!strcmp(member->GetTypeName(),"TClass"))
68 member = (TDataMember *)next();
69
70 return (void *)((char *)this+member->GetOffset());
71}
72
73//______________________________________________________________________________
74Int_t AliRawEventHeaderBase::HeaderSize() const
75{
76 // Returns the size of the data members list
77 // beyond the base class data members
78
79 Int_t size = 0;
80
81 TList *datalist = IsA()->GetListOfDataMembers();
82 TIter next(datalist);
83 TDataMember *member;
84 while ((member=(TDataMember *)next()) != 0x0) {
85 if (!strcmp(member->GetTypeName(),"TClass")) continue;
86 UInt_t unitsize = member->GetUnitSize();
87 UInt_t ndim = member->GetArrayDim();
88 if (ndim == 0)
89 size += unitsize;
90 else
91 for(UInt_t i=0;i<ndim;i++) size += member->GetMaxIndex(i)*unitsize;
92 }
93
94 return size;
95}
96
97//______________________________________________________________________________
e653e3e1 98UInt_t AliRawEventHeaderBase::SwapWord(UInt_t x) const
99{
100 // Swap the endianess of the integer value 'x'
101
102 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
103 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
104}
105
f2dc6b20 106void AliRawEventHeaderBase::Swap()
107{
108 // Swap base header data.
109 // Update the fIsSwapped flag which
110 // is then use to copy in an appropriate way
111 // the rest of the header data from the raw data stream
112
113 if (IsSwapped()) {
114 fIsSwapped = kTRUE;
e653e3e1 115 fSize = SwapWord(fSize);
116 fMagic = SwapWord(fMagic);
117 fHeadSize = SwapWord(fHeadSize);
118 fVersion = SwapWord(fVersion);
f2dc6b20 119 }
120}
121
122//______________________________________________________________________________
12e921cc 123const char *AliRawEventHeaderBase::GetTypeName() const
f2dc6b20 124{
125 // Get event type as a string.
126 // Will fail in case data header
127 // does not contain eventType field
128 Int_t eventType = Get("Type");
129
130 switch (eventType) {
131 case kStartOfRun:
132 return "START_OF_RUN";
133 break;
134 case kEndOfRun:
135 return "END_OF_RUN";
136 break;
137 case kStartOfRunFiles:
138 return "START_OF_RUN_FILES";
139 break;
140 case kEndOfRunFiles:
141 return "END_OF_RUN_FILES";
142 break;
143 case kStartOfBurst:
144 return "START_OF_BURST";
145 break;
146 case kEndOfBurst:
147 return "END_OF_BURST";
148 break;
149 case kPhysicsEvent:
150 return "PHYSICS_EVENT";
151 break;
152 case kCalibrationEvent:
153 return "CALIBRATION_EVENT";
154 break;
155 case kFormatError:
156 return "EVENT_FORMAT_ERROR";
157 break;
cce65444 158 case kStartOfData:
159 return "START_OF_DATA";
160 break;
161 case kEndOfData:
162 return "END_OF_DATA";
163 break;
164 case kSystemSoftwareTriggerEvent:
165 return "SYSTEM_SOFTWARE_TRIGGER_EVENT";
166 break;
167 case kDetectorSoftwareTriggerEvent:
168 return "DETECTOR_SOFTWARE_TRIGGER_EVENT";
169 break;
f2dc6b20 170 default:
171 return "UNKNOWN EVENT TYPE NUMBER";
172 break;
173 }
174}
175
176//______________________________________________________________________________
177AliRawEventHeaderBase* AliRawEventHeaderBase::Create(char*& data)
178{
179 // Static method to create AliRawEventHeaderVX object
180 // The actual header class version is taken from the
181 // raw data
182
183 // First create AlirawEVentHeaderBase class object
184 AliRawEventHeaderBase header;
185
186 // Copy the first common part of the raw data header
187 memcpy(header.HeaderBaseBegin(), data, header.HeaderBaseSize());
188
189 // Swap header data if needed
190 if (header.IsSwapped())
191 header.Swap();
192
193 // Is header valid...
194 if (!header.IsValid()) {
195 AliFatalClass("Invalid header format!");
196 // try recovery... how?
197 return 0x0;
198 }
199
200 if (header.GetEventSize() < (UInt_t)header.HeaderBaseSize()) {
201 AliFatalClass("Invalid header base size!");
202 // try recovery... how?
203 return 0x0;
204 }
205
206 // Now check the DATE version and create the corresponding header
207 // class object
208 UInt_t version = header.GetVersion();
209 UInt_t majorversion = (version>>16)&0x0000ffff;
210 UInt_t minorversion = version&0x0000ffff;
211 TString classname;
212 classname.Form("AliRawEventHeaderV%d_%d",majorversion,minorversion);
213
214 TClass *tcl = TClass::GetClass(classname.Data());
215 if (!tcl) {
216 AliFatalClass(Form("Unknown header version (%s)!",classname.Data()));
217 return 0x0;
218 }
219
220 // header.Dump(); tcl->Dump();
221
222 AliRawEventHeaderBase *hdr = (AliRawEventHeaderBase *)tcl->New();
223 if (!hdr) {
224 AliFatalClass(Form("Can not create object of class %s",classname.Data()));
225 return 0x0;
226 }
227
228 // Copy the base header data members and initialize other data members
229 memcpy(hdr->HeaderBaseBegin(),header.HeaderBaseBegin(), header.HeaderBaseSize());
230 memset(hdr->HeaderBegin(),0, hdr->HeaderSize());
231 hdr->fIsSwapped = header.fIsSwapped;
232
233 // Consistency check
234 if (hdr->GetEventSize() < ((UInt_t)hdr->HeaderBaseSize() + (UInt_t)hdr->HeaderSize())) {
235 AliFatalClass(Form("Invalid header size (%d < %d +%d)!",
236 hdr->GetEventSize(),hdr->HeaderBaseSize(),hdr->HeaderSize()));
237 // try recovery... how?
238 return 0x0;
239 }
240
241 // Check for the presence of header extension and its size
242 Int_t extsize = (Int_t)hdr->GetHeadSize() - (hdr->HeaderBaseSize() + hdr->HeaderSize());
243 if (extsize < 0) {
244 AliFatalClass(Form("Invalid header size (%d < %d +%d)!",
245 hdr->GetHeadSize(),hdr->HeaderBaseSize(),hdr->HeaderSize()));
246 // try recovery... how?
247 return 0x0;
248 }
249 else {
250 if (extsize > 0) {
33778962 251 hdr->AllocateExtendedData(extsize);
f2dc6b20 252 }
253 }
254
255 return hdr;
256}
257
e653e3e1 258void AliRawEventHeaderBase::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
259 // The method swaps the contents of the
260 // raw-data event header
261 UInt_t intCount = size/sizeof(UInt_t);
262
263 UInt_t* buf = (UInt_t*) inbuf; // temporary integers buffer
264 for (UInt_t i=0; i<intCount; i++, buf++) {
265 UInt_t value = SwapWord(*buf);
266 memcpy((UInt_t*)outbuf+i, &value, sizeof(UInt_t));
267 }
268}
269
f2dc6b20 270//______________________________________________________________________________
271Int_t AliRawEventHeaderBase::ReadHeader(char*& data)
272{
273 // Read header info from DATE data stream.
274 // Returns bytes read
275
276 Long_t start = (Long_t)data;
277 // Swap header data if needed
278 if (DataIsSwapped()) {
e653e3e1 279 SwapData(data, HeaderBaseBegin(), HeaderBaseSize());
f2dc6b20 280 data += HeaderBaseSize();
e653e3e1 281 SwapData(data, HeaderBegin(), HeaderSize());
f2dc6b20 282 data += HeaderSize();
f2dc6b20 283 }
284 else {
285 memcpy(HeaderBaseBegin(), data, HeaderBaseSize());
286 data += HeaderBaseSize();
287 memcpy(HeaderBegin(), data, HeaderSize());
288 data += HeaderSize();
f2dc6b20 289 }
33778962 290 data += ReadExtendedData(data);
f2dc6b20 291
292 return (Int_t)((Long_t)data - start);
293}
294
295//______________________________________________________________________________
33778962 296void AliRawEventHeaderBase::AllocateExtendedData(Int_t extsize)
297{
298 // Allocate the space for the header
299 // extended data
300 if (fExtendedData) delete [] fExtendedData;
301
302 fExtendedDataSize = fExtendedAllocSize = extsize;
303 fExtendedData = new char[fExtendedAllocSize];
304 memset(fExtendedData,0,fExtendedAllocSize);
305}
306
307//______________________________________________________________________________
308Int_t AliRawEventHeaderBase::ReadExtendedData(char*& data)
309{
310 // Read extended header data
311 // Reallocates memory if the present
312 // buffer is insufficient
313 Int_t extsize = (Int_t)GetHeadSize() - (HeaderBaseSize() + HeaderSize());
314
315 if (extsize == 0) {
316 fExtendedDataSize = 0;
317 return 0;
318 }
319
320 if (extsize < 0) {
321 AliFatal(Form("Invalid header size (%d < %d +%d)!",
322 GetHeadSize(),HeaderBaseSize(),HeaderSize()));
323 // try recovery... how?
324 return 0;
325 }
326
327 fExtendedDataSize = extsize;
328 if (fExtendedDataSize > fExtendedAllocSize)
329 AllocateExtendedData(fExtendedDataSize);
330
331 if (DataIsSwapped())
332 SwapData(data, fExtendedData, fExtendedDataSize);
333 else
334 memcpy(fExtendedData, data, fExtendedDataSize);
335
336 return fExtendedDataSize;
337}
338
339//______________________________________________________________________________
11d31ad9 340UInt_t AliRawEventHeaderBase::Get(const char *datamember) const
f2dc6b20 341{
342 // The method to get a data member from the header object
343 // Except for the data members of the base class, all the
344 // other header data should be retrieved ONLY by this method
345 // The name of the data member should be supplied without "f"
346 // in front
347
348 char buf[256] = "f";
349 strcat(buf,datamember);
350
351 TDataMember *member = IsA()->GetDataMember(buf);
352 if (!member) {
353 AliFatal(Form("No data member %s is found! Check the raw data version!",buf));
354 return 0;
355 }
356
357 if (member->GetArrayDim() != 0) {
358 AliFatal(Form("Member %s is an array! Use the GetP() method!",buf));
359 return 0;
360 }
361
362 if (strcmp(member->GetTypeName(),"UInt_t") != 0) {
363 AliFatal(Form("Member %s is not of type UInt_t!",buf));
364 return 0;
365 }
366
367 const void *pointer = (char *)this+member->GetOffset();
368
369 return *((UInt_t *)pointer);
370}
371
372//______________________________________________________________________________
11d31ad9 373const UInt_t* AliRawEventHeaderBase::GetP(const char *datamember) const
f2dc6b20 374{
375 // The method to get a data member from the header object
376 // Except for the data members of the base class, all the
377 // other header data should be retrieved ONLY by this method
378 // The name of the data member should be supplied without "f"
379 // in front
380
381 char buf[256] = "f";
382 strcat(buf,datamember);
383
384 TDataMember *member = IsA()->GetDataMember(buf);
385 if (!member) {
386 AliFatal(Form("No data member %s is found! Check the raw data version!",buf));
387 return 0;
388 }
389
39848565 390 // if (member->GetArrayDim() == 0) {
391 // AliFatal(Form("Member %s is not an array! Use the Get() method!",buf));
392 // return 0;
393 // }
f2dc6b20 394
395 if (strcmp(member->GetTypeName(),"UInt_t") != 0) {
396 AliFatal(Form("Member %s is not of type UInt_t*!",buf));
397 return 0;
398 }
399
400 const void *pointer = (char *)this+member->GetOffset();
401
402 return (const UInt_t*)pointer;
403}
12e921cc 404
405//_____________________________________________________________________________
406void AliRawEventHeaderBase::Print( const Option_t* opt ) const
407{
408 // Dumps the event or sub-event
409 // header fields
410
411 cout << opt << " Event size: " << GetEventSize() << endl;
412 cout << opt << " Event header size: " << GetHeadSize() << endl;
413 cout << opt << " Event header version: " << GetMajorVersion() << "." << GetMinorVersion() << endl;
414 cout << opt << " Event type: " << Get("Type") << "( " << GetTypeName() << " )" << endl;
415 cout << opt << " Run Number: " << Get("RunNb") << endl;
416 const UInt_t *id = GetP("Id");
417 cout << opt << " Period: " << (((id)[0]>>4)&0x0fffffff) << " Orbit: " << ((((id)[0]<<20)&0xf00000)|(((id)[1]>>12)&0xfffff)) << " Bunch-crossing: " << ((id)[1]&0x00000fff) << endl;
418 cout << opt << " Trigger pattern: " << GetP("TriggerPattern")[0] << "-" << GetP("TriggerPattern")[1] << endl;
419 cout << opt << " Detector pattern: " << Get("DetectorPattern") << endl;
420 cout << opt << " Type attribute: " << GetP("TypeAttribute")[0] << "-" << GetP("TypeAttribute")[1] << "-" << GetP("TypeAttribute")[2] << endl;
421 cout << opt << " GDC: " << Get("GdcId") << " LDC: " << Get("LdcId") << endl;
422}