23-jan-2007 NvE Bug fixed by Garmt in IceMakeHits.cxx.
[u/mrichter/AliRoot.git] / RAW / AliRawEventHeaderBase.cxx
CommitLineData
f2dc6b20 1// Author: Cvetan Cheshkov 10/10/2005
2
3/**************************************************************************
4 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
5 * *
6 * Author: The ALICE Off-line Project. *
7 * Contributors are mentioned in the code where appropriate. *
8 * *
9 * Permission to use, copy, modify and distribute this software and its *
10 * documentation strictly for non-commercial purposes is hereby granted *
11 * without fee, provided that the above copyright notice appears in all *
12 * copies and that both the copyright notice and this permission notice *
13 * appear in the supporting documentation. The authors make no claims *
14 * about the suitability of this software for any purpose. It is *
15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
17
18//////////////////////////////////////////////////////////////////////////
19// //
20// AliRawEventHeaderBase //
21// This a new versioning scheme for raw data root-ification and reading //
22// For details look at offline weekly meeting 20/10/2005 //
23// //
24//////////////////////////////////////////////////////////////////////////
25
26#include <unistd.h>
27
28#include <TClass.h>
29#include <TDataMember.h>
30#include <TMethodCall.h>
31
32#include "AliLog.h"
33#include "AliRawEventHeaderBase.h"
34
35
36ClassImp(AliRawEventHeaderBase)
37
f3c1e83c 38//______________________________________________________________________________
39AliRawEventHeaderBase::AliRawEventHeaderBase():
40fSize(0),
41fMagic(0),
42fHeadSize(0),
43fVersion(0),
44fExtendedDataSize(0),
45fExtendedData(NULL),
46fIsSwapped(kFALSE)
47{
48 // Default constructor
49}
f2dc6b20 50
51//______________________________________________________________________________
52void *AliRawEventHeaderBase::HeaderBegin()
53{
54 // Returns the pointer to the first data member
55 // beyond the base class data members
56
57 TList *datalist = IsA()->GetListOfDataMembers();
58 TIter next(datalist);
59 TDataMember *member = (TDataMember *)next();
60
61 if(!strcmp(member->GetTypeName(),"TClass"))
62 member = (TDataMember *)next();
63
64 return (void *)((char *)this+member->GetOffset());
65}
66
67//______________________________________________________________________________
68Int_t AliRawEventHeaderBase::HeaderSize() const
69{
70 // Returns the size of the data members list
71 // beyond the base class data members
72
73 Int_t size = 0;
74
75 TList *datalist = IsA()->GetListOfDataMembers();
76 TIter next(datalist);
77 TDataMember *member;
78 while ((member=(TDataMember *)next()) != 0x0) {
79 if (!strcmp(member->GetTypeName(),"TClass")) continue;
80 UInt_t unitsize = member->GetUnitSize();
81 UInt_t ndim = member->GetArrayDim();
82 if (ndim == 0)
83 size += unitsize;
84 else
85 for(UInt_t i=0;i<ndim;i++) size += member->GetMaxIndex(i)*unitsize;
86 }
87
88 return size;
89}
90
91//______________________________________________________________________________
92void AliRawEventHeaderBase::Swap()
93{
94 // Swap base header data.
95 // Update the fIsSwapped flag which
96 // is then use to copy in an appropriate way
97 // the rest of the header data from the raw data stream
98
99 if (IsSwapped()) {
100 fIsSwapped = kTRUE;
101 fSize = net2host(fSize);
102 fMagic = net2host(fMagic);
103 fHeadSize = net2host(fHeadSize);
104 fVersion = net2host(fVersion);
105 }
106}
107
108//______________________________________________________________________________
109const char *AliRawEventHeaderBase::GetTypeName()
110{
111 // Get event type as a string.
112 // Will fail in case data header
113 // does not contain eventType field
114 Int_t eventType = Get("Type");
115
116 switch (eventType) {
117 case kStartOfRun:
118 return "START_OF_RUN";
119 break;
120 case kEndOfRun:
121 return "END_OF_RUN";
122 break;
123 case kStartOfRunFiles:
124 return "START_OF_RUN_FILES";
125 break;
126 case kEndOfRunFiles:
127 return "END_OF_RUN_FILES";
128 break;
129 case kStartOfBurst:
130 return "START_OF_BURST";
131 break;
132 case kEndOfBurst:
133 return "END_OF_BURST";
134 break;
135 case kPhysicsEvent:
136 return "PHYSICS_EVENT";
137 break;
138 case kCalibrationEvent:
139 return "CALIBRATION_EVENT";
140 break;
141 case kFormatError:
142 return "EVENT_FORMAT_ERROR";
143 break;
cce65444 144 case kStartOfData:
145 return "START_OF_DATA";
146 break;
147 case kEndOfData:
148 return "END_OF_DATA";
149 break;
150 case kSystemSoftwareTriggerEvent:
151 return "SYSTEM_SOFTWARE_TRIGGER_EVENT";
152 break;
153 case kDetectorSoftwareTriggerEvent:
154 return "DETECTOR_SOFTWARE_TRIGGER_EVENT";
155 break;
f2dc6b20 156 default:
157 return "UNKNOWN EVENT TYPE NUMBER";
158 break;
159 }
160}
161
162//______________________________________________________________________________
163AliRawEventHeaderBase* AliRawEventHeaderBase::Create(char*& data)
164{
165 // Static method to create AliRawEventHeaderVX object
166 // The actual header class version is taken from the
167 // raw data
168
169 // First create AlirawEVentHeaderBase class object
170 AliRawEventHeaderBase header;
171
172 // Copy the first common part of the raw data header
173 memcpy(header.HeaderBaseBegin(), data, header.HeaderBaseSize());
174
175 // Swap header data if needed
176 if (header.IsSwapped())
177 header.Swap();
178
179 // Is header valid...
180 if (!header.IsValid()) {
181 AliFatalClass("Invalid header format!");
182 // try recovery... how?
183 return 0x0;
184 }
185
186 if (header.GetEventSize() < (UInt_t)header.HeaderBaseSize()) {
187 AliFatalClass("Invalid header base size!");
188 // try recovery... how?
189 return 0x0;
190 }
191
192 // Now check the DATE version and create the corresponding header
193 // class object
194 UInt_t version = header.GetVersion();
195 UInt_t majorversion = (version>>16)&0x0000ffff;
196 UInt_t minorversion = version&0x0000ffff;
197 TString classname;
198 classname.Form("AliRawEventHeaderV%d_%d",majorversion,minorversion);
199
200 TClass *tcl = TClass::GetClass(classname.Data());
201 if (!tcl) {
202 AliFatalClass(Form("Unknown header version (%s)!",classname.Data()));
203 return 0x0;
204 }
205
206 // header.Dump(); tcl->Dump();
207
208 AliRawEventHeaderBase *hdr = (AliRawEventHeaderBase *)tcl->New();
209 if (!hdr) {
210 AliFatalClass(Form("Can not create object of class %s",classname.Data()));
211 return 0x0;
212 }
213
214 // Copy the base header data members and initialize other data members
215 memcpy(hdr->HeaderBaseBegin(),header.HeaderBaseBegin(), header.HeaderBaseSize());
216 memset(hdr->HeaderBegin(),0, hdr->HeaderSize());
217 hdr->fIsSwapped = header.fIsSwapped;
218
219 // Consistency check
220 if (hdr->GetEventSize() < ((UInt_t)hdr->HeaderBaseSize() + (UInt_t)hdr->HeaderSize())) {
221 AliFatalClass(Form("Invalid header size (%d < %d +%d)!",
222 hdr->GetEventSize(),hdr->HeaderBaseSize(),hdr->HeaderSize()));
223 // try recovery... how?
224 return 0x0;
225 }
226
227 // Check for the presence of header extension and its size
228 Int_t extsize = (Int_t)hdr->GetHeadSize() - (hdr->HeaderBaseSize() + hdr->HeaderSize());
229 if (extsize < 0) {
230 AliFatalClass(Form("Invalid header size (%d < %d +%d)!",
231 hdr->GetHeadSize(),hdr->HeaderBaseSize(),hdr->HeaderSize()));
232 // try recovery... how?
233 return 0x0;
234 }
235 else {
236 if (extsize > 0) {
237 hdr->SetExtendedDataSize(extsize);
238 char *extdata = new char[extsize];
239 memset(extdata,0,extsize);
240 hdr->SetExtendedData(extdata);
241 }
242 }
243
244 return hdr;
245}
246
247//______________________________________________________________________________
248Int_t AliRawEventHeaderBase::ReadHeader(char*& data)
249{
250 // Read header info from DATE data stream.
251 // Returns bytes read
252
253 Long_t start = (Long_t)data;
254 // Swap header data if needed
255 if (DataIsSwapped()) {
256 swab(data,HeaderBaseBegin(), HeaderBaseSize());
257 data += HeaderBaseSize();
258 swab(data, HeaderBegin(), HeaderSize());
259 data += HeaderSize();
260 if(GetExtendedDataSize()>0) {
261 swab(data, GetExtendedData(), GetExtendedDataSize());
262 data += GetExtendedDataSize();
263 }
264 }
265 else {
266 memcpy(HeaderBaseBegin(), data, HeaderBaseSize());
267 data += HeaderBaseSize();
268 memcpy(HeaderBegin(), data, HeaderSize());
269 data += HeaderSize();
270 if(GetExtendedDataSize()>0) {
271 memcpy(GetExtendedData(), data, GetExtendedDataSize());
272 data += GetExtendedDataSize();
273 }
274 }
275
276 return (Int_t)((Long_t)data - start);
277}
278
279//______________________________________________________________________________
11d31ad9 280UInt_t AliRawEventHeaderBase::Get(const char *datamember) const
f2dc6b20 281{
282 // The method to get a data member from the header object
283 // Except for the data members of the base class, all the
284 // other header data should be retrieved ONLY by this method
285 // The name of the data member should be supplied without "f"
286 // in front
287
288 char buf[256] = "f";
289 strcat(buf,datamember);
290
291 TDataMember *member = IsA()->GetDataMember(buf);
292 if (!member) {
293 AliFatal(Form("No data member %s is found! Check the raw data version!",buf));
294 return 0;
295 }
296
297 if (member->GetArrayDim() != 0) {
298 AliFatal(Form("Member %s is an array! Use the GetP() method!",buf));
299 return 0;
300 }
301
302 if (strcmp(member->GetTypeName(),"UInt_t") != 0) {
303 AliFatal(Form("Member %s is not of type UInt_t!",buf));
304 return 0;
305 }
306
307 const void *pointer = (char *)this+member->GetOffset();
308
309 return *((UInt_t *)pointer);
310}
311
312//______________________________________________________________________________
11d31ad9 313const UInt_t* AliRawEventHeaderBase::GetP(const char *datamember) const
f2dc6b20 314{
315 // The method to get a data member from the header object
316 // Except for the data members of the base class, all the
317 // other header data should be retrieved ONLY by this method
318 // The name of the data member should be supplied without "f"
319 // in front
320
321 char buf[256] = "f";
322 strcat(buf,datamember);
323
324 TDataMember *member = IsA()->GetDataMember(buf);
325 if (!member) {
326 AliFatal(Form("No data member %s is found! Check the raw data version!",buf));
327 return 0;
328 }
329
39848565 330 // if (member->GetArrayDim() == 0) {
331 // AliFatal(Form("Member %s is not an array! Use the Get() method!",buf));
332 // return 0;
333 // }
f2dc6b20 334
335 if (strcmp(member->GetTypeName(),"UInt_t") != 0) {
336 AliFatal(Form("Member %s is not of type UInt_t*!",buf));
337 return 0;
338 }
339
340 const void *pointer = (char *)this+member->GetOffset();
341
342 return (const UInt_t*)pointer;
343}