]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/rec/AliHLTReconstructor.cxx
Compilation of TUHKMgen and EPOS
[u/mrichter/AliRoot.git] / HLT / rec / AliHLTReconstructor.cxx
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   AliHLTReconstructor.cxx
20     @author Matthias Richter
21     @date   
22     @brief  Binding class for HLT reconstruction in AliRoot. */
23
24 #include <TSystem.h>
25 #include <TObjString.h>
26 #include "TFile.h"
27 #include "TTree.h"
28 #include "AliHLTReconstructor.h"
29 #include "AliLog.h"
30 #include "AliRawReader.h"
31 #include "AliESDEvent.h"
32 #include "AliHLTSystem.h"
33 #include "AliHLTOUTRawReader.h"
34 #include "AliHLTOUTDigitReader.h"
35 #include "AliHLTEsdManager.h"
36 #include "AliHLTPluginBase.h"
37
38 ClassImp(AliHLTReconstructor)
39
40 AliHLTReconstructor::AliHLTReconstructor()
41   : 
42   AliReconstructor(),
43   fFctProcessHLTOUT(NULL),
44   fpEsdManager(NULL),
45   fpPluginBase(new AliHLTPluginBase)
46
47   //constructor
48 }
49
50 AliHLTReconstructor::AliHLTReconstructor(const char* options)
51   : 
52   AliReconstructor(),
53   fFctProcessHLTOUT(NULL),
54   fpEsdManager(NULL),
55   fpPluginBase(new AliHLTPluginBase)
56
57   //constructor
58   if (options) Init(options);
59 }
60
61 AliHLTReconstructor::~AliHLTReconstructor()
62
63   //destructor
64
65   if (fpPluginBase) {
66   AliHLTSystem* pSystem=fpPluginBase->GetInstance();
67   if (pSystem) {
68     AliDebug(0, Form("terminate HLT system: status %#x", pSystem->GetStatusFlags()));
69     if (pSystem->CheckStatus(AliHLTSystem::kStarted)) {
70       // send specific 'event' to execute the stop sequence
71       pSystem->Reconstruct(0, NULL, NULL);
72     }
73   }
74   delete fpPluginBase;
75   }
76   fpPluginBase=NULL;
77
78   if (fpEsdManager) AliHLTEsdManager::Delete(fpEsdManager);
79   fpEsdManager=NULL;
80 }
81
82 void AliHLTReconstructor::Init(const char* options)
83 {
84   // init the reconstructor
85   SetOption(options);
86   Init();
87 }
88
89 void AliHLTReconstructor::Init()
90 {
91   // init the reconstructor
92   if (!fpPluginBase) {
93     AliError("internal memory error: can not get AliHLTSystem instance from plugin");
94     return;
95   }
96
97   AliHLTSystem* pSystem=fpPluginBase->GetInstance();
98   if (!pSystem) {
99     AliError("can not create AliHLTSystem object");
100     return;
101   }
102   if (pSystem->CheckStatus(AliHLTSystem::kError)) {
103     AliError("HLT system in error state");
104     return;
105   }
106
107   // the options scan has been moved to AliHLTSystem, the old code
108   // here is kept to be able to run an older version of the HLT code
109   // with newer AliRoot versions.
110   TString libs("");
111   TString option = GetOption();
112   TObjArray* pTokens=option.Tokenize(" ");
113   option="";
114   if (pTokens) {
115     int iEntries=pTokens->GetEntries();
116     for (int i=0; i<iEntries; i++) {
117       TString token=(((TObjString*)pTokens->At(i))->GetString());
118       if (token.Contains("loglevel=")) {
119         TString param=token.ReplaceAll("loglevel=", "");
120         if (param.IsDigit()) {
121           pSystem->SetGlobalLoggingLevel((AliHLTComponentLogSeverity)param.Atoi());
122         } else if (param.BeginsWith("0x") &&
123                    param.Replace(0,2,"",0).IsHex()) {
124           int severity=0;
125           sscanf(param.Data(),"%x", &severity);
126           pSystem->SetGlobalLoggingLevel((AliHLTComponentLogSeverity)severity);
127         } else {
128           AliWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
129         }
130       } else if (token.Contains("alilog=off")) {
131         pSystem->SwitchAliLog(0);
132       } else if (token.BeginsWith("lib") && token.EndsWith(".so")) {
133         libs+=token;
134         libs+=" ";
135       } else {
136         if (option.Length()>0) option+=" ";
137         option+=token;
138       }
139     }
140     delete pTokens;
141   }
142
143   if (!libs.IsNull() &&
144       (!pSystem->CheckStatus(AliHLTSystem::kLibrariesLoaded)) &&
145       (pSystem->LoadComponentLibraries(libs.Data())<0)) {
146     AliError("error while loading HLT libraries");
147     return;
148   }
149
150   if (!pSystem->CheckStatus(AliHLTSystem::kReady)) {
151     typedef int (*AliHLTSystemSetOptions)(AliHLTSystem* pInstance, const char* options);
152     gSystem->Load("libHLTinterface.so");
153     AliHLTSystemSetOptions pFunc=(AliHLTSystemSetOptions)(gSystem->DynFindSymbol("libHLTinterface.so", "AliHLTSystemSetOptions"));
154     if (pFunc) {
155       if ((pFunc)(pSystem, option.Data())<0) {
156       AliError("error setting options for HLT system");
157       return;   
158       }
159     } else if (option.Length()>0) {
160       AliError(Form("version of HLT system does not support the options \'%s\'", option.Data()));
161       return;
162     }
163     if ((pSystem->Configure())<0) {
164       AliError("error during HLT system configuration");
165       return;
166     }
167   }
168
169   gSystem->Load("libHLTinterface.so");
170   fFctProcessHLTOUT=(void (*)())gSystem->DynFindSymbol("libHLTinterface.so", "AliHLTSystemProcessHLTOUT");
171
172   fpEsdManager=AliHLTEsdManager::New();
173 }
174
175 void AliHLTReconstructor::Reconstruct(AliRawReader* rawReader, TTree* /*clustersTree*/) const 
176 {
177   // reconstruction of real data without writing of ESD
178   // For each event, HLT reconstruction chains can be executed and
179   // added to the existing HLTOUT data
180   // The HLTOUT data is finally processed in FillESD
181   if (!fpPluginBase) {
182     AliError("internal memory error: can not get AliHLTSystem instance from plugin");
183     return;
184   }
185
186   int iResult=0;
187   AliHLTSystem* pSystem=fpPluginBase->GetInstance();
188
189   if (pSystem) {
190     if (pSystem->CheckStatus(AliHLTSystem::kError)) {
191       AliError("HLT system in error state");
192       return;
193     }
194     if (!pSystem->CheckStatus(AliHLTSystem::kReady)) {
195       AliError("HLT system in wrong state");
196       return;
197     }
198     if ((iResult=pSystem->Reconstruct(1, NULL, rawReader))>=0) {
199     }
200   }
201 }
202
203 void AliHLTReconstructor::FillESD(AliRawReader* rawReader, TTree* /*clustersTree*/, 
204                                   AliESDEvent* esd) const
205 {
206   // reconstruct real data and fill ESD
207   if (!rawReader || !esd) {
208     AliError("missing raw reader or esd object");
209     return;
210   }
211
212   if (!fpPluginBase) {
213     AliError("internal memory error: can not get AliHLTSystem instance from plugin");
214     return;
215   }
216
217   AliHLTSystem* pSystem=fpPluginBase->GetInstance();
218
219   if (pSystem) {
220     if (pSystem->CheckStatus(AliHLTSystem::kError)) {
221       AliError("HLT system in error state");
222       return;
223     }
224     if (!pSystem->CheckStatus(AliHLTSystem::kReady)) {
225       AliError("HLT system in wrong state");
226       return;
227     }
228     pSystem->FillESD(-1, NULL, esd);
229
230     AliHLTOUTRawReader* pHLTOUT=new AliHLTOUTRawReader(rawReader, esd->GetEventNumberInFile(), fpEsdManager);
231     if (pHLTOUT) {
232       ProcessHLTOUT(pHLTOUT, esd);
233       delete pHLTOUT;
234     } else {
235       AliError("error creating HLTOUT handler");
236     }
237   }
238 }
239
240 void AliHLTReconstructor::Reconstruct(TTree* /*digitsTree*/, TTree* /*clustersTree*/) const
241 {
242   // reconstruct simulated data
243
244   // all reconstruction has been moved to FillESD
245   //AliReconstructor::Reconstruct(digitsTree,clustersTree);
246   AliInfo("running digit data reconstruction");
247 }
248
249 void AliHLTReconstructor::FillESD(TTree* /*digitsTree*/, TTree* /*clustersTree*/, AliESDEvent* esd) const
250 {
251   // reconstruct simulated data and fill ESD
252
253   // later this is the place to extract the simulated HLT data
254   // for now it's only an user failure condition as he tries to run HLT reconstruction
255   // on simulated data 
256   TString option = GetOption();
257   if (!option.IsNull() && 
258       (option.Contains("config=") || option.Contains("chains="))) {
259     AliWarning(Form("HLT reconstruction can be run embedded into Alireconstruction from\n"
260                     "raw data (real or simulated)). Reconstruction of of digit data takes\n"
261                     "place in AliSimulation, appropriate input conversion is needed.\n"
262                     "Consider running embedded into AliSimulation."
263                     "        /***  run macro *****************************************/\n"
264                     "        AliSimulation sim;\n"
265                     "        sim.SetRunHLT(\"%s\");\n"
266                     "        sim.SetRunGeneration(kFALSE);\n"
267                     "        sim.SetMakeDigits(\"\");\n"
268                     "        sim.SetMakeSDigits(\"\");\n"
269                     "        sim.SetMakeDigitsFromHits(\"\");\n"
270                     "        sim.Run();\n"
271                     "        /*********************************************************/", option.Data()));
272   }
273   if (!fpPluginBase) {
274     AliError("internal memory error: can not get AliHLTSystem instance from plugin");
275     return;
276   }
277
278   AliHLTSystem* pSystem=fpPluginBase->GetInstance();
279   if (pSystem) {
280     if (pSystem->CheckStatus(AliHLTSystem::kError)) {
281       AliError("HLT system in error state");
282       return;
283     }
284     if (!pSystem->CheckStatus(AliHLTSystem::kReady)) {
285       AliError("HLT system in wrong state");
286       return;
287     }
288
289     AliHLTOUTDigitReader* pHLTOUT=new AliHLTOUTDigitReader(esd->GetEventNumberInFile(), fpEsdManager);
290     if (pHLTOUT) {
291       ProcessHLTOUT(pHLTOUT, esd);
292       delete pHLTOUT;
293     } else {
294       AliError("error creating HLTOUT handler");
295     }
296   }
297 }
298
299 void AliHLTReconstructor::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd, bool bVerbose) const
300 {
301   // treatment of simulated or real HLTOUT data
302   if (!pHLTOUT) return;
303   if (!fpPluginBase) {
304     AliError("internal memory error: can not get AliHLTSystem instance from plugin");
305     return;
306   }
307
308   AliHLTSystem* pSystem=fpPluginBase->GetInstance();
309   if (!pSystem) {
310     AliError("error getting HLT system instance");
311     return;
312   }
313
314   if (pHLTOUT->Init()<0) {
315     AliError("error : initialization of HLTOUT handler failed");
316     return;
317   }
318
319   if (bVerbose)
320     PrintHLTOUTContent(pHLTOUT);
321
322   if (fFctProcessHLTOUT) {
323     typedef int (*AliHLTSystemProcessHLTOUT)(AliHLTSystem* pInstance, AliHLTOUT* pHLTOUT, AliESDEvent* esd);
324     AliHLTSystemProcessHLTOUT pFunc=(AliHLTSystemProcessHLTOUT)fFctProcessHLTOUT;
325     if ((pFunc)(pSystem, pHLTOUT, esd)<0) {
326       AliError("error processing HLTOUT");
327     }
328   }
329   pHLTOUT->Reset();
330 }
331
332 void AliHLTReconstructor::ProcessHLTOUT(const char* digitFile, AliESDEvent* pEsd) const
333 {
334   // debugging/helper function to examine simulated data
335   if (!digitFile) return;
336
337   // read the number of events
338   TFile f(digitFile);
339   if (f.IsZombie()) return;
340   TTree* pTree=NULL;
341   f.GetObject("rawhltout", pTree);
342   if (!pTree) {
343     AliWarning(Form("can not find tree rawhltout in file %s", digitFile));
344     return ;
345   }
346   int nofEvents=pTree->GetEntries();
347   f.Close();
348   //delete pTree; OF COURSE NOT! its an object in the file
349   pTree=NULL;
350
351   for (int event=0; event<nofEvents; event++) {
352     AliHLTOUTDigitReader* pHLTOUT=new AliHLTOUTDigitReader(event, fpEsdManager, digitFile);
353     if (pHLTOUT) {
354       AliInfo(Form("event %d", event));
355       ProcessHLTOUT(pHLTOUT, pEsd, true);
356       delete pHLTOUT;
357     } else {
358       AliError("error creating HLTOUT handler");
359     }
360   }
361 }
362
363 void AliHLTReconstructor::ProcessHLTOUT(AliRawReader* pRawReader, AliESDEvent* pEsd) const
364 {
365   // debugging/helper function to examine simulated or real HLTOUT data
366   if (!pRawReader) return;
367
368   pRawReader->RewindEvents();
369   for (int event=0; pRawReader->NextEvent(); event++) {
370     AliHLTOUTRawReader* pHLTOUT=new AliHLTOUTRawReader(pRawReader, event, fpEsdManager);
371     if (pHLTOUT) {
372       AliInfo(Form("event %d", event));
373       ProcessHLTOUT(pHLTOUT, pEsd, true);
374       delete pHLTOUT;
375     } else {
376       AliError("error creating HLTOUT handler");
377     }
378   }
379 }
380
381 void AliHLTReconstructor::PrintHLTOUTContent(AliHLTOUT* pHLTOUT) const
382 {
383   // print the block specifications of the HLTOUT data blocks
384   if (!pHLTOUT) return;
385   int iResult=0;
386
387   for (iResult=pHLTOUT->SelectFirstDataBlock();
388        iResult>=0;
389        iResult=pHLTOUT->SelectNextDataBlock()) {
390     AliHLTComponentDataType dt=kAliHLTVoidDataType;
391     AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
392     pHLTOUT->GetDataBlockDescription(dt, spec);
393     const AliHLTUInt8_t* pBuffer=NULL;
394     AliHLTUInt32_t size=0;
395     if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
396       pHLTOUT->ReleaseDataBuffer(pBuffer);
397       pBuffer=NULL; // just a dummy
398     }
399     AliInfo(Form("   %s  0x%x: size %d", AliHLTComponent::DataType2Text(dt).c_str(), spec, size));
400   }
401 }