added unit test for AliRawReaderFile
[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);
521766bd 61int CreateAndWriteESD(AliHLTEsdManager* manager, int eventno, AliHLTComponentDataType dt, AliESDEvent* pTgt);
652cf9d2 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) {
521766bd 81 cerr << "error: error loading libHLTrec.so library" << endl;
652cf9d2 82 return -1;
83 }
84#endif
85
521766bd 86 AliHLTEsdManager* pManager=AliHLTEsdManager::New();
87 if (!pManager) {
88 cerr << "error: can not create manager instance" << endl;
89 return -1;
90 }
91 pManager->SetDirectory(gSystem->TempDirectory());
652cf9d2 92
93 int nofEvents=10;
94 AliHLTComponentDataType tpcesd;
95 AliHLTComponent::SetDataType(tpcesd, "ESD_TREE", "TPC ");
96 cout << AliHLTComponent::DataType2Text(tpcesd).c_str() << endl;
97 for (int event=0; event<nofEvents && iResult>=0; event++) {
98 cout << AliHLTComponent::DataType2Text(tpcesd).c_str() << endl;
521766bd 99 CreateAndWriteESD(pManager, event, tpcesd, NULL);
652cf9d2 100 }
101
521766bd 102 AliHLTEsdManager::Delete(pManager);
652cf9d2 103 return iResult;
104}
105
106/////////////////////////////////////////////////////////////////
107/////////////////////////////////////////////////////////////////
108/////////////////////////////////////////////////////////////////
109//
110int main(int /*argc*/, const char** /*argv*/)
111{
112 int iResult=0;
113 int nofEvents=10;
114 AliHLTSystem gHLT;
115 gHLT.SetGlobalLoggingLevel(kHLTLogDefault);
116
521766bd 117 AliHLTEsdManager* pManager=AliHLTEsdManager::New();
118 if (!pManager) {
119 cerr << "error: can not create manager instance" << endl;
120 return -1;
121 }
122 pManager->SetDirectory(gSystem->TempDirectory());
652cf9d2 123
124 AliHLTComponentDataType types[] = {
125 // first entry is special, ESD is written to the global target ESD
126 //kAliHLTDataTypeESDTree|kAliHLTDataOriginTPC,
127 kAliHLTDataTypeESDTree|kAliHLTDataOriginTPC,
128 kAliHLTDataTypeESDTree|kAliHLTDataOriginPHOS,
129 kAliHLTDataTypeESDTree|kAliHLTDataOriginTRD,
130 kAliHLTDataTypeDDLRaw|kAliHLTDataOriginTRD,
131 kAliHLTVoidDataType
132 };
133
134 TTree* pMasterTree=new TTree("esdTree", "Tree with HLT ESD objects");
521766bd 135 pMasterTree->SetDirectory(0);
652cf9d2 136 AliESDEvent* pMasterESD=new AliESDEvent;
137 pMasterESD->CreateStdContent();
138 pMasterESD->WriteToTree(pMasterTree);
139
140 vector<TArrayI*> randomFields;
141 for (int event=0; event<nofEvents && iResult>=0; event++) {
142 pMasterESD->ResetStdContent();
143 for (unsigned int type=0; types[type]!=kAliHLTVoidDataType && iResult>=0; type++) {
144 if (randomFields.size()<=type) {
145 randomFields.push_back(new TArrayI(nofEvents));
146 }
147 AliESDEvent* pTgt=NULL;
148 //if (type==0) pTgt=pMasterESD;
521766bd 149 int field=CreateAndWriteESD(pManager, event, types[type], pTgt);
652cf9d2 150 if (field>=0) {
151 (*randomFields[type])[event]=field;
152 } else {
153 iResult=-1;
154 break;
155 }
156 }
157 pMasterTree->Fill();
158 }
159
160 if (iResult>=0) {
521766bd 161 pManager->PadESDs(nofEvents);
652cf9d2 162 }
163
164 for (int type=0; types[type]!=kAliHLTVoidDataType; type++) {
521766bd 165 TString filename=pManager->GetFileNames(types[type]);
652cf9d2 166 if (iResult>=0) {
167 iResult=CheckFields(filename, dynamic_cast<TArrayI*>(randomFields[type]));
168 }
169 TString shellcmd="rm -f ";
170 shellcmd+=filename;
171 gSystem->Exec(shellcmd);
172 }
173
174 vector<TArrayI*>::iterator element;
175 while ((element=randomFields.end())!=randomFields.begin()) {
176 element--;
177 if (*element) delete *element;
178 randomFields.pop_back();
179 }
180
181 delete pMasterESD;
521766bd 182 AliHLTEsdManager::Delete(pManager);
652cf9d2 183
184 return iResult;
185}
186
187Bool_t seedSet=kFALSE;
188
189/**
190 * Get a random number in the given range.
191 */
192int GetRandom(int min, int max)
193{
194 if (max-min<2) return min;
195 static TRandom rand;
196 if (!seedSet) {
197 TDatime dt;
198 rand.SetSeed(dt.Get());
199 seedSet=kTRUE;
200 }
201 return rand.Integer(max-min);
202}
203
204/**
205 * Creates a dummy ESD object and sets the magnetic field to a random number.
206 * The ESD is streamed via AliHLTMessage and processed by the AliHLTEsdmanager.
207 * @return 0 no ESD created, >0 random number of the magnetic field,
208 * neg error if failed
209 */
521766bd 210int CreateAndWriteESD(AliHLTEsdManager* pManager, int eventno, AliHLTComponentDataType dt, AliESDEvent* pTgt)
652cf9d2 211{
212 int iResult=0;
213 int magField=0;
521766bd 214 if (!pManager) {
215 cerr << "error: missing manager instance" << endl;
216 return -1;
217 }
652cf9d2 218 const char* message="";
219 if ((GetRandom(0,10)%3)==0) {
220 message=": adding ESD for block ";
221 TTree* pTree=new TTree;
222 AliESDEvent* pESD=new AliESDEvent;
223 pESD->CreateStdContent();
224 magField=GetRandom(1, 1000);
225 pESD->SetMagneticField(magField);
226 pESD->WriteToTree(pTree);
227 pTree->Fill();
228 pTree->GetUserInfo()->Add(pESD);
229 AliHLTMessage msg(kMESS_OBJECT);
230 msg.WriteObject(pTree);
231 Int_t iMsgLength=msg.Length();
232 if (iMsgLength>0) {
233 msg.SetLength(); // sets the length to the first (reserved) word
521766bd 234 iResult=pManager->WriteESD((AliHLTUInt8_t*)msg.Buffer(), iMsgLength, dt, pTgt, eventno);
652cf9d2 235 }
236 pTree->GetUserInfo()->Clear();
237 delete pTree;
238 delete pESD;
239 } else {
240 message=": omitting block ";
241 }
242 if (iResult>=0) iResult=magField;
243 if (bVerbose) cout << "event " << eventno << message << AliHLTComponent::DataType2Text(dt).c_str() << ": " << iResult << endl;
244 return iResult;
245}
246
247/**
248 * Read the ESD from the file and compare with the
249 * random field values previously set to the ESDs
250 */
251int CheckFields(const char* file, TArrayI* fields)
252{
253 if (!file || !fields) {
521766bd 254 cerr << "error: invalid parameters" << endl;
652cf9d2 255 return 0;
256 }
257 TFile esdfile(file);
258 if (!esdfile.IsZombie()) {
259 TTree* pTree=NULL;
260 esdfile.GetObject("esdTree", pTree);
261 if (pTree) {
262 int res=CheckFields(pTree, fields, file);
263 if (res<0) return res;
264 } else {
521766bd 265 cerr << "error: can not find esdTree in file " << file << endl;
652cf9d2 266 }
267 } else {
521766bd 268 cerr << "error: can not open file " << file << endl;
652cf9d2 269 return -1;
270 }
271 cout << "checking: " << file << " ok" << endl;
272 return 0;
273}
274
275/**
276 * Compare ESD from tree with the
277 * random field values previously set to the ESDs
278 */
279int CheckFields(TTree* pTree, TArrayI* fields, const char* file)
280{
281 if (fields->GetSize()!=pTree->GetEntries()) {
521766bd 282 cerr << "error: event number mismatch in file " << file << " : expected " << fields->GetSize() << " found " << pTree->GetEntries() << endl;
652cf9d2 283 return -1;
284 }
285 AliESDEvent* pESD=new AliESDEvent;
286 pESD->ReadFromTree(pTree);
287 for (int event=0; event<pTree->GetEntries(); event++) {
288 pTree->GetEvent(event);
289 if (fields->At(event)!=pESD->GetMagneticField()) {
521766bd 290 cerr << "error: magnetic field mismatch in file " << file << " event " << event << ": expected " << fields->At(event) << " found " << pESD->GetMagneticField() << endl;
652cf9d2 291 return -1;
292 }
293 }
294
295 delete pESD;
296 return 0;
297}