- added libPHOSshuttle to PHOS libraries
[u/mrichter/AliRoot.git] / HLT / rec / test / testAliHLTEsdManager.C
CommitLineData
652cf9d2 1// $Id$
2
3/**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
6 * *
7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
9 * *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
18
19/** @file testAliHLTEsdManager.C
20 @author Matthias Richter
21 @date
22 @brief Test macro/program for the AliHLTEsdManager class
23 */
24
25#ifndef __CINT__
26#include "TFile.h"
27#include "TDatime.h"
28#include "TRandom.h"
29#include "TArrayI.h"
30#include "TSystem.h"
31#include "TTree.h"
32#include "TFile.h"
33#include "AliESDEvent.h"
34#include "AliHLTDataTypes.h"
35#include "AliHLTComponent.h"
36#include "AliHLTEsdManager.h"
37#include "AliHLTMessage.h"
38#include "AliHLTSystem.h"
39#include <ostream>
40#endif //__CINT__
41
42/////////////////////////////////////////////////////////////////
43/////////////////////////////////////////////////////////////////
44/////////////////////////////////////////////////////////////////
45//
46// configuration of the test program
47//
48
49// printouts or not
50const bool bVerbose=false;
51
52
53/////////////////////////////////////////////////////////////////
54/////////////////////////////////////////////////////////////////
55/////////////////////////////////////////////////////////////////
56//
57// forward declarations
58//
59class AliHLTEsdManager;
60int GetRandom(int min, int max);
61int CreateAndWriteESD(AliHLTEsdManager& manager, int eventno, AliHLTComponentDataType dt, AliESDEvent* pTgt);
62int CheckFields(const char* file, TArrayI* fields);
63int CheckFields(TTree* pTree, TArrayI* fields, const char* file);
64
65/////////////////////////////////////////////////////////////////
66/////////////////////////////////////////////////////////////////
67/////////////////////////////////////////////////////////////////
68//
69// compiled version and root macro are not equivalent for this unit test
70// The macro just tests with one data type
71// This is mainly due to some restrictions in CINT which can not handle
72// the array of data types correctly
73int testAliHLTEsdManager()
74{
75 cout << "macro still not working with CINT, sorry!" << endl;
76 return 0;
77
78 int iResult=0;
79#ifdef __CINT__
80 if (gSystem->Load("libHLTrec.so")<0) {
81 cerr << "error loading libHLTrec.so library" << endl;
82 return -1;
83 }
84#endif
85
86 AliHLTEsdManager manager;
87 manager.SetDirectory(gSystem->TempDirectory());
88
89 int nofEvents=10;
90 AliHLTComponentDataType tpcesd;
91 AliHLTComponent::SetDataType(tpcesd, "ESD_TREE", "TPC ");
92 cout << AliHLTComponent::DataType2Text(tpcesd).c_str() << endl;
93 for (int event=0; event<nofEvents && iResult>=0; event++) {
94 cout << AliHLTComponent::DataType2Text(tpcesd).c_str() << endl;
95 CreateAndWriteESD(manager, event, tpcesd, NULL);
96 }
97
98 return iResult;
99}
100
101/////////////////////////////////////////////////////////////////
102/////////////////////////////////////////////////////////////////
103/////////////////////////////////////////////////////////////////
104//
105int main(int /*argc*/, const char** /*argv*/)
106{
107 int iResult=0;
108 int nofEvents=10;
109 AliHLTSystem gHLT;
110 gHLT.SetGlobalLoggingLevel(kHLTLogDefault);
111
112 AliHLTEsdManager manager;
113 manager.SetDirectory(gSystem->TempDirectory());
114
115 AliHLTComponentDataType types[] = {
116 // first entry is special, ESD is written to the global target ESD
117 //kAliHLTDataTypeESDTree|kAliHLTDataOriginTPC,
118 kAliHLTDataTypeESDTree|kAliHLTDataOriginTPC,
119 kAliHLTDataTypeESDTree|kAliHLTDataOriginPHOS,
120 kAliHLTDataTypeESDTree|kAliHLTDataOriginTRD,
121 kAliHLTDataTypeDDLRaw|kAliHLTDataOriginTRD,
122 kAliHLTVoidDataType
123 };
124
125 TTree* pMasterTree=new TTree("esdTree", "Tree with HLT ESD objects");
126 AliESDEvent* pMasterESD=new AliESDEvent;
127 pMasterESD->CreateStdContent();
128 pMasterESD->WriteToTree(pMasterTree);
129
130 vector<TArrayI*> randomFields;
131 for (int event=0; event<nofEvents && iResult>=0; event++) {
132 pMasterESD->ResetStdContent();
133 for (unsigned int type=0; types[type]!=kAliHLTVoidDataType && iResult>=0; type++) {
134 if (randomFields.size()<=type) {
135 randomFields.push_back(new TArrayI(nofEvents));
136 }
137 AliESDEvent* pTgt=NULL;
138 //if (type==0) pTgt=pMasterESD;
139 int field=CreateAndWriteESD(manager, event, types[type], pTgt);
140 if (field>=0) {
141 (*randomFields[type])[event]=field;
142 } else {
143 iResult=-1;
144 break;
145 }
146 }
147 pMasterTree->Fill();
148 }
149
150 if (iResult>=0) {
151 manager.PadESDs(nofEvents);
152 }
153
154 for (int type=0; types[type]!=kAliHLTVoidDataType; type++) {
155 TString filename=manager.GetFileNames(types[type]);
156 if (iResult>=0) {
157 iResult=CheckFields(filename, dynamic_cast<TArrayI*>(randomFields[type]));
158 }
159 TString shellcmd="rm -f ";
160 shellcmd+=filename;
161 gSystem->Exec(shellcmd);
162 }
163
164 vector<TArrayI*>::iterator element;
165 while ((element=randomFields.end())!=randomFields.begin()) {
166 element--;
167 if (*element) delete *element;
168 randomFields.pop_back();
169 }
170
171 delete pMasterESD;
172
173 return iResult;
174}
175
176Bool_t seedSet=kFALSE;
177
178/**
179 * Get a random number in the given range.
180 */
181int GetRandom(int min, int max)
182{
183 if (max-min<2) return min;
184 static TRandom rand;
185 if (!seedSet) {
186 TDatime dt;
187 rand.SetSeed(dt.Get());
188 seedSet=kTRUE;
189 }
190 return rand.Integer(max-min);
191}
192
193/**
194 * Creates a dummy ESD object and sets the magnetic field to a random number.
195 * The ESD is streamed via AliHLTMessage and processed by the AliHLTEsdmanager.
196 * @return 0 no ESD created, >0 random number of the magnetic field,
197 * neg error if failed
198 */
199int CreateAndWriteESD(AliHLTEsdManager& manager, int eventno, AliHLTComponentDataType dt, AliESDEvent* pTgt)
200{
201 int iResult=0;
202 int magField=0;
203 const char* message="";
204 if ((GetRandom(0,10)%3)==0) {
205 message=": adding ESD for block ";
206 TTree* pTree=new TTree;
207 AliESDEvent* pESD=new AliESDEvent;
208 pESD->CreateStdContent();
209 magField=GetRandom(1, 1000);
210 pESD->SetMagneticField(magField);
211 pESD->WriteToTree(pTree);
212 pTree->Fill();
213 pTree->GetUserInfo()->Add(pESD);
214 AliHLTMessage msg(kMESS_OBJECT);
215 msg.WriteObject(pTree);
216 Int_t iMsgLength=msg.Length();
217 if (iMsgLength>0) {
218 msg.SetLength(); // sets the length to the first (reserved) word
219 iResult=manager.WriteESD((AliHLTUInt8_t*)msg.Buffer(), iMsgLength, dt, pTgt, eventno);
220 }
221 pTree->GetUserInfo()->Clear();
222 delete pTree;
223 delete pESD;
224 } else {
225 message=": omitting block ";
226 }
227 if (iResult>=0) iResult=magField;
228 if (bVerbose) cout << "event " << eventno << message << AliHLTComponent::DataType2Text(dt).c_str() << ": " << iResult << endl;
229 return iResult;
230}
231
232/**
233 * Read the ESD from the file and compare with the
234 * random field values previously set to the ESDs
235 */
236int CheckFields(const char* file, TArrayI* fields)
237{
238 if (!file || !fields) {
239 cerr << "invalid parameters" << endl;
240 return 0;
241 }
242 TFile esdfile(file);
243 if (!esdfile.IsZombie()) {
244 TTree* pTree=NULL;
245 esdfile.GetObject("esdTree", pTree);
246 if (pTree) {
247 int res=CheckFields(pTree, fields, file);
248 if (res<0) return res;
249 } else {
250 cerr << "can not find esdTree in file " << file << endl;
251 }
252 } else {
253 cerr << "can not open file " << file << endl;
254 return -1;
255 }
256 cout << "checking: " << file << " ok" << endl;
257 return 0;
258}
259
260/**
261 * Compare ESD from tree with the
262 * random field values previously set to the ESDs
263 */
264int CheckFields(TTree* pTree, TArrayI* fields, const char* file)
265{
266 if (fields->GetSize()!=pTree->GetEntries()) {
267 cerr << "event number mismatch in file " << file << " : expected " << fields->GetSize() << " found " << pTree->GetEntries() << endl;
268 return -1;
269 }
270 AliESDEvent* pESD=new AliESDEvent;
271 pESD->ReadFromTree(pTree);
272 for (int event=0; event<pTree->GetEntries(); event++) {
273 pTree->GetEvent(event);
274 if (fields->At(event)!=pESD->GetMagneticField()) {
275 cerr << "magnetic field mismatch in file " << file << " event " << event << ": expected " << fields->At(event) << " found " << pESD->GetMagneticField() << endl;
276 return -1;
277 }
278 }
279
280 delete pESD;
281 return 0;
282}