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