]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/rec/AliHLTReconstructor.cxx
decoupling ESDManager interface class from AliESDEvent definition, adding method...
[u/mrichter/AliRoot.git] / HLT / rec / AliHLTReconstructor.cxx
CommitLineData
3e820254 1// $Id$
2
c5123824 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//**************************************************************************
c534985b 18
69edd0d6 19// @file AliHLTReconstructor.cxx
20// @author Matthias Richter
21// @date
22// @brief Binding class for HLT reconstruction in AliRoot
23// Implements bot the interface to run HLT chains embedded into
24// AliReconstruction and the unpacking and treatment of HLTOUT
3e820254 25
26#include <TSystem.h>
27#include <TObjString.h>
032c5e5e 28#include "TFile.h"
29#include "TTree.h"
dc5556e5 30#include "TObject.h"
31#include "TObjArray.h"
32#include "TClass.h"
33#include "TStreamerInfo.h"
3e820254 34#include "AliHLTReconstructor.h"
35#include "AliLog.h"
032c5e5e 36#include "AliRawReader.h"
c5123824 37#include "AliESDEvent.h"
3e820254 38#include "AliHLTSystem.h"
c5123824 39#include "AliHLTOUTRawReader.h"
40#include "AliHLTOUTDigitReader.h"
41#include "AliHLTEsdManager.h"
00ddfaca 42#include "AliHLTPluginBase.h"
dc5556e5 43#include "AliHLTMisc.h"
44#include "AliCDBManager.h"
45#include "AliCDBEntry.h"
d3382be1 46#include "AliHLTMessage.h"
c4ffab2c 47#include "AliCentralTrigger.h"
48#include "AliTriggerConfiguration.h"
49#include "AliTriggerClass.h"
50#include "AliTriggerCluster.h"
51#include "AliDAQ.h"
dc5556e5 52
53class AliCDBEntry;
3e820254 54
69edd0d6 55/** ROOT macro for the implementation of ROOT specific class methods */
3e820254 56ClassImp(AliHLTReconstructor)
57
58AliHLTReconstructor::AliHLTReconstructor()
69edd0d6 59 : AliReconstructor()
60 , fpEsdManager(NULL)
61 , fpPluginBase(new AliHLTPluginBase)
3eb6bcbe 62 , fFlags(0)
3e820254 63{
64 //constructor
65}
66
032c5e5e 67AliHLTReconstructor::AliHLTReconstructor(const char* options)
69edd0d6 68 : AliReconstructor()
69 , fpEsdManager(NULL)
70 , fpPluginBase(new AliHLTPluginBase)
3eb6bcbe 71 , fFlags(0)
032c5e5e 72{
73 //constructor
74 if (options) Init(options);
75}
76
3e820254 77AliHLTReconstructor::~AliHLTReconstructor()
78{
79 //destructor
dee38f1b 80
00ddfaca 81 if (fpPluginBase) {
82 AliHLTSystem* pSystem=fpPluginBase->GetInstance();
a3ef3c1d 83 if (pSystem) {
00ddfaca 84 AliDebug(0, Form("terminate HLT system: status %#x", pSystem->GetStatusFlags()));
3dd8541e 85 if (pSystem->CheckStatus(AliHLTSystem::kStarted)) {
dee38f1b 86 // send specific 'event' to execute the stop sequence
a3ef3c1d 87 pSystem->Reconstruct(0, NULL, NULL);
dee38f1b 88 }
3e820254 89 }
00ddfaca 90 delete fpPluginBase;
91 }
92 fpPluginBase=NULL;
c5123824 93
f1207f29 94 if (fpEsdManager) AliHLTEsdManager::Delete(fpEsdManager);
c5123824 95 fpEsdManager=NULL;
3e820254 96}
97
032c5e5e 98void AliHLTReconstructor::Init(const char* options)
99{
100 // init the reconstructor
101 SetOption(options);
102 Init();
103}
104
3e820254 105void AliHLTReconstructor::Init()
106{
107 // init the reconstructor
00ddfaca 108 if (!fpPluginBase) {
109 AliError("internal memory error: can not get AliHLTSystem instance from plugin");
110 return;
111 }
112
113 AliHLTSystem* pSystem=fpPluginBase->GetInstance();
a3ef3c1d 114 if (!pSystem) {
3e820254 115 AliError("can not create AliHLTSystem object");
116 return;
117 }
a3ef3c1d 118 if (pSystem->CheckStatus(AliHLTSystem::kError)) {
3e820254 119 AliError("HLT system in error state");
120 return;
121 }
122
21d97a90 123 TString esdManagerOptions;
124
3e820254 125 // the options scan has been moved to AliHLTSystem, the old code
126 // here is kept to be able to run an older version of the HLT code
127 // with newer AliRoot versions.
3e820254 128 TString option = GetOption();
129 TObjArray* pTokens=option.Tokenize(" ");
130 option="";
131 if (pTokens) {
132 int iEntries=pTokens->GetEntries();
133 for (int i=0; i<iEntries; i++) {
134 TString token=(((TObjString*)pTokens->At(i))->GetString());
135 if (token.Contains("loglevel=")) {
136 TString param=token.ReplaceAll("loglevel=", "");
137 if (param.IsDigit()) {
a3ef3c1d 138 pSystem->SetGlobalLoggingLevel((AliHLTComponentLogSeverity)param.Atoi());
3e820254 139 } else if (param.BeginsWith("0x") &&
140 param.Replace(0,2,"",0).IsHex()) {
141 int severity=0;
142 sscanf(param.Data(),"%x", &severity);
a3ef3c1d 143 pSystem->SetGlobalLoggingLevel((AliHLTComponentLogSeverity)severity);
3e820254 144 } else {
145 AliWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
146 }
147 } else if (token.Contains("alilog=off")) {
a3ef3c1d 148 pSystem->SwitchAliLog(0);
3eb6bcbe 149 } else if (token.CompareTo("ignore-hltout")==0) {
150 fFlags|=kAliHLTReconstructorIgnoreHLTOUT;
75cbb181 151 } else if (token.CompareTo("ignore-ctp")==0) {
152 fFlags|=kAliHLTReconstructorIgnoreCTP;
21d97a90 153 } else if (token.Contains("esdmanager=")) {
154 token.ReplaceAll("esdmanager=", "");
155 token.ReplaceAll(","," ");
156 token.ReplaceAll("'","");
157 esdManagerOptions=token;
3e820254 158 } else {
159 if (option.Length()>0) option+=" ";
160 option+=token;
161 }
162 }
163 delete pTokens;
164 }
7f9ab840 165
c4ffab2c 166 TString ecsParam;
167 TString ctpParam;
75cbb181 168 if ((fFlags&kAliHLTReconstructorIgnoreCTP)==0 &&
169 BuildCTPTriggerClassString(ctpParam)>=0) {
c4ffab2c 170 if (!ecsParam.IsNull()) ecsParam+=";";
171 ecsParam+="CTP_TRIGGER_CLASS=";
172 ecsParam+=ctpParam;
173 }
174
175 if (!ecsParam.IsNull()) {
176 option+=" ECS=";
177 option+=ecsParam;
178 }
179
a3ef3c1d 180 if (!pSystem->CheckStatus(AliHLTSystem::kReady)) {
69edd0d6 181 if (pSystem->ScanOptions(option.Data())<0) {
3e820254 182 AliError("error setting options for HLT system");
3e820254 183 return;
184 }
a3ef3c1d 185 if ((pSystem->Configure())<0) {
3e820254 186 AliError("error during HLT system configuration");
187 return;
188 }
189 }
c5123824 190
f1207f29 191 fpEsdManager=AliHLTEsdManager::New();
21d97a90 192 fpEsdManager->SetOption(esdManagerOptions.Data());
dc5556e5 193
b251fffc 194 AliHLTMisc::Instance().InitStreamerInfos(fgkCalibStreamerInfoEntry);
dc5556e5 195}
196
197const char* AliHLTReconstructor::fgkCalibStreamerInfoEntry="HLT/Calib/StreamerInfo";
198
06995b88 199void AliHLTReconstructor::Reconstruct(AliRawReader* rawReader, TTree* /*clustersTree*/) const
3e820254 200{
201 // reconstruction of real data without writing of ESD
c5123824 202 // For each event, HLT reconstruction chains can be executed and
203 // added to the existing HLTOUT data
204 // The HLTOUT data is finally processed in FillESD
00ddfaca 205 if (!fpPluginBase) {
206 AliError("internal memory error: can not get AliHLTSystem instance from plugin");
207 return;
208 }
209
3e820254 210 int iResult=0;
00ddfaca 211 AliHLTSystem* pSystem=fpPluginBase->GetInstance();
06995b88 212
a3ef3c1d 213 if (pSystem) {
214 if (pSystem->CheckStatus(AliHLTSystem::kError)) {
3e820254 215 AliError("HLT system in error state");
216 return;
217 }
a3ef3c1d 218 if (!pSystem->CheckStatus(AliHLTSystem::kReady)) {
3e820254 219 AliError("HLT system in wrong state");
220 return;
221 }
a3ef3c1d 222 if ((iResult=pSystem->Reconstruct(1, NULL, rawReader))>=0) {
c5123824 223 }
224 }
06995b88 225}
226
227void AliHLTReconstructor::FillESD(AliRawReader* rawReader, TTree* /*clustersTree*/,
228 AliESDEvent* esd) const
229{
230 // reconstruct real data and fill ESD
231 if (!rawReader || !esd) {
232 AliError("missing raw reader or esd object");
233 return;
234 }
235
00ddfaca 236 if (!fpPluginBase) {
237 AliError("internal memory error: can not get AliHLTSystem instance from plugin");
238 return;
239 }
240
241 AliHLTSystem* pSystem=fpPluginBase->GetInstance();
c5123824 242
243 if (pSystem) {
244 if (pSystem->CheckStatus(AliHLTSystem::kError)) {
245 AliError("HLT system in error state");
246 return;
247 }
248 if (!pSystem->CheckStatus(AliHLTSystem::kReady)) {
249 AliError("HLT system in wrong state");
250 return;
251 }
252 pSystem->FillESD(-1, NULL, esd);
253
3eb6bcbe 254 AliRawReader* input=NULL;
255 if ((fFlags&kAliHLTReconstructorIgnoreHLTOUT) == 0 ) {
256 input=rawReader;
257 }
258 AliHLTOUTRawReader* pHLTOUT=new AliHLTOUTRawReader(input, esd->GetEventNumberInFile(), fpEsdManager);
c5123824 259 if (pHLTOUT) {
0bba252a 260 ProcessHLTOUT(pHLTOUT, esd, (pSystem->GetGlobalLoggingLevel()&kHLTLogDebug)!=0);
f94e8c27 261 delete pHLTOUT;
c5123824 262 } else {
263 AliError("error creating HLTOUT handler");
3e820254 264 }
265 }
266}
7f9ab840 267
29312178 268void AliHLTReconstructor::Reconstruct(TTree* /*digitsTree*/, TTree* /*clustersTree*/) const
7f9ab840 269{
270 // reconstruct simulated data
271
272 // all reconstruction has been moved to FillESD
273 //AliReconstructor::Reconstruct(digitsTree,clustersTree);
032c5e5e 274 AliInfo("running digit data reconstruction");
7f9ab840 275}
276
c5123824 277void AliHLTReconstructor::FillESD(TTree* /*digitsTree*/, TTree* /*clustersTree*/, AliESDEvent* esd) const
7f9ab840 278{
279 // reconstruct simulated data and fill ESD
280
281 // later this is the place to extract the simulated HLT data
282 // for now it's only an user failure condition as he tries to run HLT reconstruction
283 // on simulated data
284 TString option = GetOption();
c5123824 285 if (!option.IsNull() &&
286 (option.Contains("config=") || option.Contains("chains="))) {
f8e0e3d3 287 AliWarning(Form("HLT reconstruction can be run embedded into Alireconstruction from\n"
288 "raw data (real or simulated)). Reconstruction of of digit data takes\n"
289 "place in AliSimulation, appropriate input conversion is needed.\n"
290 "Consider running embedded into AliSimulation."
7f9ab840 291 " /*** run macro *****************************************/\n"
292 " AliSimulation sim;\n"
293 " sim.SetRunHLT(\"%s\");\n"
294 " sim.SetRunGeneration(kFALSE);\n"
295 " sim.SetMakeDigits(\"\");\n"
296 " sim.SetMakeSDigits(\"\");\n"
297 " sim.SetMakeDigitsFromHits(\"\");\n"
298 " sim.Run();\n"
299 " /*********************************************************/", option.Data()));
300 }
00ddfaca 301 if (!fpPluginBase) {
302 AliError("internal memory error: can not get AliHLTSystem instance from plugin");
303 return;
304 }
305
306 AliHLTSystem* pSystem=fpPluginBase->GetInstance();
c5123824 307 if (pSystem) {
308 if (pSystem->CheckStatus(AliHLTSystem::kError)) {
309 AliError("HLT system in error state");
310 return;
311 }
312 if (!pSystem->CheckStatus(AliHLTSystem::kReady)) {
313 AliError("HLT system in wrong state");
314 return;
315 }
316
317 AliHLTOUTDigitReader* pHLTOUT=new AliHLTOUTDigitReader(esd->GetEventNumberInFile(), fpEsdManager);
318 if (pHLTOUT) {
fbe9adaf 319 ProcessHLTOUT(pHLTOUT, esd, (pSystem->GetGlobalLoggingLevel()&kHLTLogDebug)!=0);
f94e8c27 320 delete pHLTOUT;
c5123824 321 } else {
322 AliError("error creating HLTOUT handler");
323 }
324 }
325}
326
b005ef92 327void AliHLTReconstructor::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd, bool bVerbose) const
c5123824 328{
f94e8c27 329 // treatment of simulated or real HLTOUT data
c5123824 330 if (!pHLTOUT) return;
00ddfaca 331 if (!fpPluginBase) {
332 AliError("internal memory error: can not get AliHLTSystem instance from plugin");
333 return;
334 }
335
336 AliHLTSystem* pSystem=fpPluginBase->GetInstance();
c5123824 337 if (!pSystem) {
338 AliError("error getting HLT system instance");
339 return;
340 }
341
342 if (pHLTOUT->Init()<0) {
343 AliError("error : initialization of HLTOUT handler failed");
344 return;
345 }
346
b005ef92 347 if (bVerbose)
348 PrintHLTOUTContent(pHLTOUT);
349
d3382be1 350 int blockindex=pHLTOUT->SelectFirstDataBlock(kAliHLTDataTypeStreamerInfo);
351 if (blockindex>=0) {
352 const AliHLTUInt8_t* pBuffer=NULL;
353 AliHLTUInt32_t size=0;
354 if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
355 TObject* pObject=AliHLTMessage::Extract(pBuffer, size);
356 if (pObject) {
357 TObjArray* pArray=dynamic_cast<TObjArray*>(pObject);
358 if (pArray) {
b251fffc 359 AliHLTMisc::Instance().InitStreamerInfos(pArray);
d3382be1 360 } else {
361 AliError(Form("wrong class type of streamer info list: expected TObjArray, but object is of type %s", pObject->Class()->GetName()));
362 }
363 } else {
364 AliError(Form("failed to extract object from data block of type %s", AliHLTComponent::DataType2Text(kAliHLTDataTypeStreamerInfo).c_str()));
365 }
366 } else {
367 AliError(Form("failed to get data buffer for block of type %s", AliHLTComponent::DataType2Text(kAliHLTDataTypeStreamerInfo).c_str()));
368 }
369 }
370
69edd0d6 371 if (pSystem->ProcessHLTOUT(pHLTOUT, esd)<0) {
372 AliError("error processing HLTOUT");
c5123824 373 }
69edd0d6 374
fbe9adaf 375 if (bVerbose) {
376 AliInfo("HLT ESD content:");
377 esd->Print();
378 }
b005ef92 379 pHLTOUT->Reset();
7f9ab840 380}
032c5e5e 381
382void AliHLTReconstructor::ProcessHLTOUT(const char* digitFile, AliESDEvent* pEsd) const
383{
384 // debugging/helper function to examine simulated data
385 if (!digitFile) return;
386
387 // read the number of events
388 TFile f(digitFile);
389 if (f.IsZombie()) return;
390 TTree* pTree=NULL;
391 f.GetObject("rawhltout", pTree);
392 if (!pTree) {
393 AliWarning(Form("can not find tree rawhltout in file %s", digitFile));
394 return ;
395 }
396 int nofEvents=pTree->GetEntries();
397 f.Close();
398 //delete pTree; OF COURSE NOT! its an object in the file
399 pTree=NULL;
400
401 for (int event=0; event<nofEvents; event++) {
402 AliHLTOUTDigitReader* pHLTOUT=new AliHLTOUTDigitReader(event, fpEsdManager, digitFile);
403 if (pHLTOUT) {
404 AliInfo(Form("event %d", event));
b005ef92 405 ProcessHLTOUT(pHLTOUT, pEsd, true);
032c5e5e 406 delete pHLTOUT;
407 } else {
408 AliError("error creating HLTOUT handler");
409 }
410 }
411}
412
413void AliHLTReconstructor::ProcessHLTOUT(AliRawReader* pRawReader, AliESDEvent* pEsd) const
414{
415 // debugging/helper function to examine simulated or real HLTOUT data
416 if (!pRawReader) return;
417
418 pRawReader->RewindEvents();
419 for (int event=0; pRawReader->NextEvent(); event++) {
420 AliHLTOUTRawReader* pHLTOUT=new AliHLTOUTRawReader(pRawReader, event, fpEsdManager);
421 if (pHLTOUT) {
422 AliInfo(Form("event %d", event));
c63e8be4 423 // the two event fields contain: period - orbit - bunch crossing counter
424 // id[0] id[1]
425 // |32 0|32 0|
426 //
91dc3cdd 427 // | 28 bit | 24 bit | 12|
c63e8be4 428 // period orbit bcc
429 AliHLTUInt64_t eventId=0;
430 const UInt_t* rawreaderEventId=pRawReader->GetEventId();
431 if (rawreaderEventId) {
432 eventId=rawreaderEventId[0];
433 eventId=eventId<<32;
434 eventId|=rawreaderEventId[1];
435 }
436 AliInfo(Form("Event Id from rawreader:\t 0x%016llx", eventId));
b005ef92 437 ProcessHLTOUT(pHLTOUT, pEsd, true);
032c5e5e 438 delete pHLTOUT;
439 } else {
440 AliError("error creating HLTOUT handler");
441 }
442 }
443}
444
445void AliHLTReconstructor::PrintHLTOUTContent(AliHLTOUT* pHLTOUT) const
446{
447 // print the block specifications of the HLTOUT data blocks
448 if (!pHLTOUT) return;
449 int iResult=0;
450
c63e8be4 451 AliInfo(Form("Event Id from hltout:\t 0x%016llx", pHLTOUT->EventId()));
032c5e5e 452 for (iResult=pHLTOUT->SelectFirstDataBlock();
453 iResult>=0;
454 iResult=pHLTOUT->SelectNextDataBlock()) {
455 AliHLTComponentDataType dt=kAliHLTVoidDataType;
456 AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
457 pHLTOUT->GetDataBlockDescription(dt, spec);
458 const AliHLTUInt8_t* pBuffer=NULL;
459 AliHLTUInt32_t size=0;
460 if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
461 pHLTOUT->ReleaseDataBuffer(pBuffer);
462 pBuffer=NULL; // just a dummy
463 }
464 AliInfo(Form(" %s 0x%x: size %d", AliHLTComponent::DataType2Text(dt).c_str(), spec, size));
465 }
466}
c4ffab2c 467
468int AliHLTReconstructor::BuildCTPTriggerClassString(TString& triggerclasses) const
469{
470 // build the CTP trigger class string from the OCDB entry of the CTP trigger
471 int iResult=0;
472
473 triggerclasses.Clear();
474 AliCentralTrigger* pCTP = new AliCentralTrigger();
475 AliTriggerConfiguration *config=NULL;
476 TString configstr("");
477 if (pCTP->LoadConfiguration(configstr) &&
478 (config = pCTP->GetConfiguration())!=NULL) {
479 const TObjArray& classesArray = config->GetClasses();
480 int nclasses = classesArray.GetEntriesFast();
481 for( int iclass=0; iclass < nclasses; iclass++ ) {
482 AliTriggerClass* trclass = NULL;
483 if (classesArray.At(iclass) && (trclass=dynamic_cast<AliTriggerClass*>(classesArray.At(iclass)))!=NULL) {
484 TString entry;
485 int trindex = TMath::Nint(TMath::Log2(trclass->GetMask()));
486 entry.Form("%02d:%s:", trindex, trclass->GetName());
487 AliTriggerCluster* cluster=NULL;
488 TObject* clusterobj=config->GetClusters().FindObject(trclass->GetCluster());
489 if (clusterobj && (cluster=dynamic_cast<AliTriggerCluster*>(clusterobj))!=NULL) {
490 TString detectors=cluster->GetDetectorsInCluster();
491 TObjArray* pTokens=detectors.Tokenize(" ");
492 if (pTokens) {
493 for (int dix=0; dix<pTokens->GetEntriesFast(); dix++) {
494 int id=AliDAQ::DetectorID(((TObjString*)pTokens->At(dix))->GetString());
495 if (id>=0) {
496 TString detstr; detstr.Form("%s%02d", dix>0?"-":"", id);
497 entry+=detstr;
498 } else {
499 AliError(Form("invalid detector name extracted from trigger cluster: %s (%s)", ((TObjString*)pTokens->At(dix))->GetString().Data(), detectors.Data()));
500 iResult=-EPROTO;
501 break;
502 }
503 }
504 delete pTokens;
505 }
506 } else {
b251fffc 507 AliError(Form("can not find trigger cluster %s in config", trclass->GetCluster()?trclass->GetCluster()->GetName():"NULL"));
c4ffab2c 508 iResult=-EPROTO;
509 break;
510 }
511 if (!triggerclasses.IsNull()) triggerclasses+=",";
512 triggerclasses+=entry;
513 }
514 }
515 }
516
517 return iResult;
518}