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