- workaround for copying and merging of ESDs: The HLTOUT contains ESDs for every...
[u/mrichter/AliRoot.git] / HLT / sim / AliHLTSimulation.cxx
CommitLineData
4cbaf07b 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//**************************************************************************
4cbaf07b 18
19/** @file AliHLTSimulation.cxx
20 @author Matthias Richter
21 @date
22 @brief Binding class for HLT simulation in AliRoot. */
23
24#include <cassert>
25#include <cerrno>
7777fa45 26#include "TObjArray.h"
27#include "TObjString.h"
4cbaf07b 28#include "AliHLTSimulation.h"
29#include "AliLog.h"
90c37647 30#include "AliRun.h"
4cbaf07b 31#include "AliRunLoader.h"
90c37647 32#include "AliHeader.h"
33#include "AliTracker.h"
34#include "AliCDBManager.h"
35#include "AliCDBEntry.h"
36#include "AliCDBPath.h"
37#include "AliCDBId.h"
38#include "AliCDBMetaData.h"
4cbaf07b 39#include "AliHLTSystem.h"
7777fa45 40#include "AliRawReaderFile.h"
41#include "AliRawReaderDate.h"
42#include "AliRawReaderRoot.h"
a8420176 43#include "AliESDEvent.h"
2c0e5942 44#include "AliHLTOUTComponent.h"
4cbaf07b 45
46#if ALIHLTSIMULATION_LIBRARY_VERSION != LIBHLTSIM_VERSION
47#error library version in header file and lib*.pkg do not match
48#endif
49
50/** ROOT macro for the implementation of ROOT specific class methods */
51ClassImp(AliHLTSimulation);
52
53AliHLTSimulation::AliHLTSimulation()
54 :
55 fOptions(),
57034f7f 56 fpSystem(NULL),
7777fa45 57 fpRawReader(NULL)
4cbaf07b 58{
59 // see header file for class documentation
60 // or
61 // refer to README to build package
62 // or
63 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
64}
65
66AliHLTSimulation::~AliHLTSimulation()
67{
68 // see header file for function documentation
57034f7f 69 if (fpSystem) {
70 delete fpSystem;
71 }
72 fpSystem=NULL;
7777fa45 73 if (fpRawReader) {
74 delete fpRawReader;
75 }
76 fpRawReader=NULL;
4cbaf07b 77}
78
79AliHLTSimulation* AliHLTSimulation::CreateInstance()
80{
81 // see header file for function documentation
82 return new AliHLTSimulation;
83}
84
85int AliHLTSimulation::DeleteInstance(AliHLTSimulation* pSim)
86{
87 // see header file for function documentation
88 assert(pSim!=NULL);
89 delete pSim;
90 return 0;
91}
92
93int AliHLTSimulation::Init(AliRunLoader* pRunLoader, const char* options)
94{
95 // init the simulation
96 fOptions=options;
7777fa45 97 TString sysOp;
4cbaf07b 98
57034f7f 99 if (!fpSystem) fpSystem=new AliHLTSystem;
100 if (!fpSystem) {
101 AliError("can not create AliHLTSystem object");
4cbaf07b 102 return -ENOMEM;
103 }
57034f7f 104 if (fpSystem->CheckStatus(AliHLTSystem::kError)) {
4cbaf07b 105 AliError("HLT system in error state");
106 return -EFAULT;
107 }
108
7777fa45 109 // scan options for specific entries
110 TObjArray* pTokens=fOptions.Tokenize(" ");
111 if (pTokens) {
112 int iEntries=pTokens->GetEntries();
113 for (int i=0; i<iEntries; i++) {
114 TString token=(((TObjString*)pTokens->At(i))->GetString());
115 if (token.Contains("rawfile=")) {
116 TString param=token.ReplaceAll("rawfile=", "");
117 if (param.EndsWith("/")) {
118 AliInfo(Form("creating AliRawReaderFile (%s)", param.Data()));
119 fpRawReader = new AliRawReaderFile(param);
120 } else if (param.EndsWith(".root")) {
121 AliInfo(Form("creating AliRawReaderRoot (%s)", param.Data()));
122 fpRawReader = new AliRawReaderRoot(param);
123 } else if (!param.IsNull()) {
124 AliInfo(Form("creating AliRawReaderDate (%s)", param.Data()));
125 fpRawReader = new AliRawReaderDate(param);
126 fpRawReader->SelectEvents(7);
127 }
128 if (fpRawReader) {
129 fpRawReader->RewindEvents();
130 int count=0;
131 for (; fpRawReader->NextEvent(); count++);
132 if (count!=pRunLoader->GetNumberOfEvents()) {
c5123824 133 AliError(Form("mismatch in event count: runloader %d, rawreader %d; ignoring rawreader",
7777fa45 134 pRunLoader->GetNumberOfEvents(), count));
135 count=0;
136 }
137 if (count>0) {
138 fpRawReader->RewindEvents();
139 fpRawReader->NextEvent();
140 } else {
141 delete fpRawReader;
142 fpRawReader=NULL;
143 }
144 }
2c0e5942 145 } else if (token.Contains("writerawfiles=")) {
146 if (!token.ReplaceAll("writerawfiles=", "").Contains("HLT")) {
147 AliHLTOUTComponent::ClearGlobalOption(AliHLTOUTComponent::kWriteRawFiles);
148 }
7777fa45 149 } else {
150 if (sysOp.Length()>0) sysOp+=" ";
151 sysOp+=token;
152 }
153 }
154 delete pTokens;
155 }
156
90c37647 157 // init solenoid field
158 Bool_t bUniformField=kTRUE;
159 AliTracker::SetFieldMap(pRunLoader->GetAliRun()->Field(),bUniformField);
160 Double_t solenoidBz=AliTracker::GetBz();
161 AliCDBManager* man = AliCDBManager::Instance();
162 if (man && man->IsDefaultStorageSet())
163 {
164 const char* cdbSolenoidPath="HLT/ConfigHLT/SolenoidBz";
165 int runNo=pRunLoader->GetHeader()->GetRun();
166 TString cdbSolenoidParam;
167 cdbSolenoidParam.Form("-solenoidBz %f", solenoidBz);
168
169 // check if the entry is already there
170 AliCDBEntry *pEntry = man->Get(cdbSolenoidPath, runNo);
171 TObjString* pString=NULL;
172 if (pEntry) pString=dynamic_cast<TObjString*>(pEntry->GetObject());
173
174 if (!pEntry || !pString || pString->GetString().CompareTo(cdbSolenoidParam)!=0) {
175 TObjString obj(cdbSolenoidParam);
176 AliCDBPath cdbSolenoidEntry(cdbSolenoidPath);
177 AliCDBId cdbSolenoidId(cdbSolenoidEntry, runNo, runNo);
178 AliCDBMetaData cdbMetaData;
179 man->Put(&obj, cdbSolenoidId, &cdbMetaData);
180 }
181 }
182
7777fa45 183 // scan options
57034f7f 184 if (fpSystem->ScanOptions(sysOp.Data())<0) {
4cbaf07b 185 AliError("error setting options for HLT system");
186 return -EINVAL;
187 }
188
57034f7f 189 if (!fpSystem->CheckStatus(AliHLTSystem::kReady)) {
190 if ((fpSystem->Configure(pRunLoader))<0) {
4cbaf07b 191 AliError("error during HLT system configuration");
192 return -EFAULT;
193 }
194 }
195
196 return 0;
197}
198
199
200int AliHLTSimulation::Run(AliRunLoader* pRunLoader)
201{
202 // HLT reconstruction for simulated data
203 if(!pRunLoader) {
204 AliError("Missing RunLoader! 0x0");
205 return -EINVAL;
206 }
207
7777fa45 208 int nEvents = pRunLoader->GetNumberOfEvents();
4cbaf07b 209 int iResult=0;
210
57034f7f 211 if (fpSystem->CheckStatus(AliHLTSystem::kError)) {
4cbaf07b 212 AliError("HLT system in error state");
213 return -EFAULT;
214 }
7777fa45 215
216 // Note: the rawreader is already placed at the first event
57034f7f 217 if ((iResult=fpSystem->Reconstruct(1, pRunLoader, fpRawReader))>=0) {
218 fpSystem->FillESD(0, pRunLoader, NULL);
7777fa45 219 for (int i=1; i<nEvents; i++) {
220 if (fpRawReader && !fpRawReader->NextEvent()) {
c5123824 221 AliError("mismatch in event count, rawreader corrupted");
7777fa45 222 break;
223 }
57034f7f 224 fpSystem->Reconstruct(1, pRunLoader, fpRawReader);
225 fpSystem->FillESD(i, pRunLoader, NULL);
7777fa45 226 }
dee38f1b 227 // send specific 'event' to execute the stop sequence
57034f7f 228 fpSystem->Reconstruct(0, NULL, NULL);
4cbaf07b 229 }
230 return iResult;
231}
232
233
234AliHLTSimulation* AliHLTSimulationCreateInstance()
235{
236 // see header file for function documentation
237 return AliHLTSimulation::CreateInstance();
238}
239
240int AliHLTSimulationDeleteInstance(AliHLTSimulation* pSim)
241{
242 // see header file for function documentation
243 return AliHLTSimulation::DeleteInstance(pSim);
244}
245
246int AliHLTSimulationInit(AliHLTSimulation* pSim, AliRunLoader* pRunLoader, const char* options)
247{
248 assert(pSim!=NULL);
249 if (pSim) {
250 return pSim->Init(pRunLoader, options);
251 }
252 return -ENODEV;
253}
254
255int AliHLTSimulationRun(AliHLTSimulation* pSim, AliRunLoader* pRunLoader)
256{
257 assert(pSim!=NULL);
258 if (pSim) {
259 return pSim->Run(pRunLoader);
260 }
261 return -ENODEV;
262}
263
264int AliHLTSimulationGetLibraryVersion()
265{
266 // see header file for function documentation
267 return LIBHLTSIM_VERSION;
268}