Added workarounds for bugs #51285 and #51281: if the AliMagF instance
[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"
90c37647 33#include "AliCDBManager.h"
34#include "AliCDBEntry.h"
35#include "AliCDBPath.h"
36#include "AliCDBId.h"
37#include "AliCDBMetaData.h"
8dd79235 38#include "AliCDBStorage.h"
39#include "AliGRPObject.h"
4cbaf07b 40#include "AliHLTSystem.h"
7bf6c76d 41#include "AliHLTPluginBase.h"
7777fa45 42#include "AliRawReaderFile.h"
43#include "AliRawReaderDate.h"
44#include "AliRawReaderRoot.h"
a8420176 45#include "AliESDEvent.h"
2c0e5942 46#include "AliHLTOUTComponent.h"
8dd79235 47#include "AliMagF.h"
48#include "TGeoGlobalMagField.h"
49#include "TSystem.h"
4cbaf07b 50
51#if ALIHLTSIMULATION_LIBRARY_VERSION != LIBHLTSIM_VERSION
52#error library version in header file and lib*.pkg do not match
53#endif
54
55/** ROOT macro for the implementation of ROOT specific class methods */
56ClassImp(AliHLTSimulation);
57
58AliHLTSimulation::AliHLTSimulation()
59 :
60 fOptions(),
7bf6c76d 61 fpPluginBase(new AliHLTPluginBase),
7777fa45 62 fpRawReader(NULL)
4cbaf07b 63{
64 // see header file for class documentation
65 // or
66 // refer to README to build package
67 // or
68 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
69}
70
71AliHLTSimulation::~AliHLTSimulation()
72{
73 // see header file for function documentation
7bf6c76d 74 if (fpPluginBase) delete fpPluginBase;
75 fpPluginBase=NULL;
76
7777fa45 77 if (fpRawReader) {
78 delete fpRawReader;
79 }
80 fpRawReader=NULL;
4cbaf07b 81}
82
83AliHLTSimulation* AliHLTSimulation::CreateInstance()
84{
85 // see header file for function documentation
86 return new AliHLTSimulation;
87}
88
89int AliHLTSimulation::DeleteInstance(AliHLTSimulation* pSim)
90{
91 // see header file for function documentation
92 assert(pSim!=NULL);
93 delete pSim;
94 return 0;
95}
96
97int AliHLTSimulation::Init(AliRunLoader* pRunLoader, const char* options)
98{
99 // init the simulation
100 fOptions=options;
7777fa45 101 TString sysOp;
4cbaf07b 102
7bf6c76d 103 if(!fpPluginBase) {
104 AliError("internal initialization failed");
105 return -EINVAL;
106 }
107
108 AliHLTSystem* pSystem=fpPluginBase->GetInstance();
109 if (!pSystem) {
110 AliError("can not get AliHLTSystem instance");
4cbaf07b 111 return -ENOMEM;
112 }
7bf6c76d 113 if (pSystem->CheckStatus(AliHLTSystem::kError)) {
4cbaf07b 114 AliError("HLT system in error state");
115 return -EFAULT;
116 }
117
7777fa45 118 // scan options for specific entries
119 TObjArray* pTokens=fOptions.Tokenize(" ");
120 if (pTokens) {
121 int iEntries=pTokens->GetEntries();
122 for (int i=0; i<iEntries; i++) {
123 TString token=(((TObjString*)pTokens->At(i))->GetString());
124 if (token.Contains("rawfile=")) {
125 TString param=token.ReplaceAll("rawfile=", "");
126 if (param.EndsWith("/")) {
127 AliInfo(Form("creating AliRawReaderFile (%s)", param.Data()));
128 fpRawReader = new AliRawReaderFile(param);
129 } else if (param.EndsWith(".root")) {
130 AliInfo(Form("creating AliRawReaderRoot (%s)", param.Data()));
131 fpRawReader = new AliRawReaderRoot(param);
132 } else if (!param.IsNull()) {
133 AliInfo(Form("creating AliRawReaderDate (%s)", param.Data()));
134 fpRawReader = new AliRawReaderDate(param);
7777fa45 135 }
136 if (fpRawReader) {
137 fpRawReader->RewindEvents();
138 int count=0;
32f88fc5 139 for ( ; fpRawReader->NextEvent(); count++) {/* empty body */};
7777fa45 140 if (count!=pRunLoader->GetNumberOfEvents()) {
c5123824 141 AliError(Form("mismatch in event count: runloader %d, rawreader %d; ignoring rawreader",
7777fa45 142 pRunLoader->GetNumberOfEvents(), count));
143 count=0;
144 }
145 if (count>0) {
146 fpRawReader->RewindEvents();
147 fpRawReader->NextEvent();
148 } else {
149 delete fpRawReader;
150 fpRawReader=NULL;
151 }
152 }
2c0e5942 153 } else if (token.Contains("writerawfiles=")) {
154 if (!token.ReplaceAll("writerawfiles=", "").Contains("HLT")) {
155 AliHLTOUTComponent::ClearGlobalOption(AliHLTOUTComponent::kWriteRawFiles);
156 }
7777fa45 157 } else {
158 if (sysOp.Length()>0) sysOp+=" ";
159 sysOp+=token;
160 }
161 }
162 delete pTokens;
163 }
164
90c37647 165 AliCDBManager* man = AliCDBManager::Instance();
166 if (man && man->IsDefaultStorageSet())
167 {
90c37647 168 int runNo=pRunLoader->GetHeader()->GetRun();
8dd79235 169
170 // init solenoid field
171 Double_t solenoidBz=0;
172 AliMagF *field = (AliMagF*)TGeoGlobalMagField::Instance()->GetField();
173 if (field) {
174 solenoidBz=field->SolenoidField()*field->Factor();
175 AliDebug(0,Form("magnetic field: %f %f", field->SolenoidField(),field->Factor()));
176 } else {
177 // workaround for bug #51285
178 AliError("can not get the AliMagF instance, falling back to GRP entry");
179 AliCDBEntry *pGRPEntry = man->Get("GRP/GRP/Data", runNo);
180 if (pGRPEntry) {
181 AliGRPObject* pGRPData=dynamic_cast<AliGRPObject*>(pGRPEntry->GetObject());
182 assert(pGRPData!=NULL);
183 if (pGRPData) {
184 // this is just a workaround at the moment, common functionality in AliReconstruction
185 // is needed to reconstruct the magnetic field in a common way
186 // the code is partly taken from AliReconstruction::InitGRP
187 Bool_t ok = kTRUE;
188 Float_t l3Current = pGRPData->GetL3Current((AliGRPObject::Stats)0);
189 if (l3Current == AliGRPObject::GetInvalidFloat()) {
190 AliError("GRP/GRP/Data entry: missing value for the L3 current !");
191 ok = kFALSE;
192 }
193
194 Char_t l3Polarity = pGRPData->GetL3Polarity();
195 if (l3Polarity == AliGRPObject::GetInvalidChar()) {
196 AliError("GRP/GRP/Data entry: missing value for the L3 polarity !");
197 ok = kFALSE;
198 }
199
200 if (ok) {
201 solenoidBz=l3Current/6000;
202 if (l3Polarity) solenoidBz*=-1;
203 } else {
204 AliError("invalid L3 field information in GRP entry");
205 }
206 }
207 }
208 }
209 const char* cdbSolenoidPath="HLT/ConfigHLT/SolenoidBz";
90c37647 210 TString cdbSolenoidParam;
211 cdbSolenoidParam.Form("-solenoidBz %f", solenoidBz);
212
213 // check if the entry is already there
214 AliCDBEntry *pEntry = man->Get(cdbSolenoidPath, runNo);
215 TObjString* pString=NULL;
216 if (pEntry) pString=dynamic_cast<TObjString*>(pEntry->GetObject());
217
218 if (!pEntry || !pString || pString->GetString().CompareTo(cdbSolenoidParam)!=0) {
219 TObjString obj(cdbSolenoidParam);
220 AliCDBPath cdbSolenoidEntry(cdbSolenoidPath);
8dd79235 221 AliCDBId cdbSolenoidId(cdbSolenoidEntry, runNo, runNo, 0, 0);
90c37647 222 AliCDBMetaData cdbMetaData;
8dd79235 223 cdbMetaData.SetResponsible("Matthias.Richter@cern.ch");
224 cdbMetaData.SetComment("Automatically produced GRP entry (AliHLTSimulation) for the magnetic field initialization of HLT components");
90c37647 225 man->Put(&obj, cdbSolenoidId, &cdbMetaData);
8dd79235 226
227 // unload the cache due to bug #51281
228 man->UnloadFromCache(cdbSolenoidPath);
90c37647 229 }
8dd79235 230 } else if (man) {
231 AliError("OCDB default storage not yet set, can not prepare OCDB entries");
232 } else {
233 AliError("unable to get instance of AliCDBMetaData, can not prepare OCDB entries");
90c37647 234 }
235
7777fa45 236 // scan options
7bf6c76d 237 if (pSystem->ScanOptions(sysOp.Data())<0) {
4cbaf07b 238 AliError("error setting options for HLT system");
239 return -EINVAL;
240 }
241
7bf6c76d 242 if (!pSystem->CheckStatus(AliHLTSystem::kReady)) {
243 if ((pSystem->Configure(fpRawReader, pRunLoader))<0) {
4cbaf07b 244 AliError("error during HLT system configuration");
245 return -EFAULT;
246 }
247 }
248
249 return 0;
250}
251
252
253int AliHLTSimulation::Run(AliRunLoader* pRunLoader)
254{
255 // HLT reconstruction for simulated data
7bf6c76d 256 if(!fpPluginBase) {
257 AliError("internal initialization failed");
258 return -EINVAL;
259 }
260
4cbaf07b 261 if(!pRunLoader) {
262 AliError("Missing RunLoader! 0x0");
263 return -EINVAL;
264 }
265
7777fa45 266 int nEvents = pRunLoader->GetNumberOfEvents();
4cbaf07b 267 int iResult=0;
268
7bf6c76d 269 AliHLTSystem* pSystem=fpPluginBase->GetInstance();
270 if (!pSystem) {
271 AliError("can not get AliHLTSystem instance");
272 return -ENOMEM;
273 }
274
275 if (pSystem->CheckStatus(AliHLTSystem::kError)) {
4cbaf07b 276 AliError("HLT system in error state");
277 return -EFAULT;
278 }
7777fa45 279
280 // Note: the rawreader is already placed at the first event
7bf6c76d 281 if ((iResult=pSystem->Reconstruct(1, pRunLoader, fpRawReader))>=0) {
282 pSystem->FillESD(0, pRunLoader, NULL);
7777fa45 283 for (int i=1; i<nEvents; i++) {
284 if (fpRawReader && !fpRawReader->NextEvent()) {
c5123824 285 AliError("mismatch in event count, rawreader corrupted");
7777fa45 286 break;
287 }
7bf6c76d 288 pSystem->Reconstruct(1, pRunLoader, fpRawReader);
289 pSystem->FillESD(i, pRunLoader, NULL);
7777fa45 290 }
dee38f1b 291 // send specific 'event' to execute the stop sequence
7bf6c76d 292 pSystem->Reconstruct(0, NULL, NULL);
4cbaf07b 293 }
294 return iResult;
295}
296
297
298AliHLTSimulation* AliHLTSimulationCreateInstance()
299{
300 // see header file for function documentation
301 return AliHLTSimulation::CreateInstance();
302}
303
304int AliHLTSimulationDeleteInstance(AliHLTSimulation* pSim)
305{
306 // see header file for function documentation
307 return AliHLTSimulation::DeleteInstance(pSim);
308}
309
310int AliHLTSimulationInit(AliHLTSimulation* pSim, AliRunLoader* pRunLoader, const char* options)
311{
312 assert(pSim!=NULL);
313 if (pSim) {
314 return pSim->Init(pRunLoader, options);
315 }
316 return -ENODEV;
317}
318
319int AliHLTSimulationRun(AliHLTSimulation* pSim, AliRunLoader* pRunLoader)
320{
321 assert(pSim!=NULL);
322 if (pSim) {
323 return pSim->Run(pRunLoader);
324 }
325 return -ENODEV;
326}
327
328int AliHLTSimulationGetLibraryVersion()
329{
330 // see header file for function documentation
331 return LIBHLTSIM_VERSION;
332}
b50357c9 333
8dd79235 334int AliHLTSimulationInitOCDB(AliHLTSimulation* /*pSim*/)
335{
336 // see header file for function documentation
337
338 // this is an attempt to solve issue #48360
339 // since there are many jobs running in parallel during the production,
340 // all the jobs want to put entries into the OCDB. The solution is to
341 // make them temporary, since they are only used to propagate information
342 // from the simulation to the reconstruction.
343 AliCDBManager* man = AliCDBManager::Instance();
344 if (man && man->IsDefaultStorageSet())
345 {
346 man->SetSpecificStorage("HLT/ConfigHLT/SolenoidBz", Form("local://%s",gSystem->pwd()));
347 man->SetSpecificStorage("HLT/ConfigHLT/esdLayout", Form("local://%s",gSystem->pwd()));
348 }
349}
350
b50357c9 351extern "C" void AliHLTSimulationCompileInfo(const char*& date, const char*& time)
352{
353 // the fall back compile info of the HLTsim library
354 // this is not up-to-date if other files have been changed and recompiled
355 date=__DATE__; time=__TIME__;
356 return;
357}