85a5290f |
1 | /************************************************************************** |
2 | * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. * |
3 | * * |
4 | * Author: The ALICE Off-line Project. * |
5 | * Contributors are mentioned in the code where appropriate. * |
6 | * * |
7 | * Permission to use, copy, modify and distribute this software and its * |
8 | * documentation strictly for non-commercial purposes is hereby granted * |
9 | * without fee, provided that the above copyright notice appears in all * |
10 | * copies and that both the copyright notice and this permission notice * |
11 | * appear in the supporting documentation. The authors make no claims * |
12 | * about the suitability of this software for any purpose. It is * |
13 | * provided "as is" without express or implied warranty. * |
14 | **************************************************************************/ |
15 | |
16 | /* $Id$ */ |
17 | |
18 | /////////////////////////////////////////////////////////////////////////////// |
19 | // // |
20 | // class for running generation, simulation and digitization // |
21 | // // |
22 | // Hits, sdigits and digits are created for all detectors by typing: // |
23 | // // |
24 | // AliSimulation sim; // |
25 | // sim.Run(); // |
26 | // // |
27 | // The Run method returns kTRUE in case of successful execution. // |
28 | // The number of events can be given as argument to the Run method or it // |
29 | // can be set by // |
30 | // // |
31 | // sim.SetNumberOfEvents(n); // |
32 | // // |
95601147 |
33 | // The name of the configuration file can be passed as argument to the // |
34 | // AliSimulation constructor or can be specified by // |
85a5290f |
35 | // // |
36 | // sim.SetConfigFile("..."); // |
37 | // // |
38 | // The generation of particles and the simulation of detector hits can be // |
39 | // switched on or off by // |
40 | // // |
41 | // sim.SetRunGeneration(kTRUE); // generation of primary particles // |
42 | // sim.SetRunSimulation(kFALSE); // but no tracking // |
43 | // // |
44 | // For which detectors sdigits and digits will be created, can be steered // |
45 | // by // |
46 | // // |
47 | // sim.SetMakeSDigits("ALL"); // make sdigits for all detectors // |
48 | // sim.SetMakeDigits("ITS TPC"); // make digits only for ITS and TPC // |
49 | // // |
50 | // The argument is a (case sensitive) string with the names of the // |
51 | // detectors separated by a space. An empty string ("") can be used to // |
52 | // disable the creation of sdigits or digits. The special string "ALL" // |
53 | // selects all available detectors. This is the default. // |
54 | // // |
55 | // The creation of digits from hits instead of from sdigits can be selected // |
56 | // by // |
57 | // // |
58 | // sim.SetMakeDigitsFromHits("TRD"); // |
59 | // // |
60 | // The argument is again a string with the selected detectors. Be aware that // |
61 | // this feature is not available for all detectors and that merging is not // |
62 | // possible, when digits are created directly from hits. // |
63 | // // |
64 | // Backgound events can be merged by calling // |
65 | // // |
66 | // sim.MergeWith("background/galice.root", 2); // |
67 | // // |
68 | // The first argument is the file name of the background galice file. The // |
69 | // second argument is the number of signal events per background event. // |
70 | // The default value for this is 1. MergeWith can be called several times // |
71 | // to merge more than two event streams. It is assumed that the sdigits // |
72 | // were already produced for the background events. // |
73 | // // |
95601147 |
74 | // The methods RunSimulation, RunSDigitization, RunDigitization and // |
75 | // RunHitsDigitization can be used to run only parts of the full simulation // |
76 | // chain. // |
77 | // // |
85a5290f |
78 | /////////////////////////////////////////////////////////////////////////////// |
79 | |
80 | |
81 | #include "AliSimulation.h" |
82 | #include "AliRunLoader.h" |
83 | #include "AliRun.h" |
84 | #include "AliModule.h" |
85 | #include "AliGenerator.h" |
04bae0a0 |
86 | #include "AliVertexGenFile.h" |
85a5290f |
87 | #include "AliRunDigitizer.h" |
4df28b43 |
88 | #include "AliDigitizer.h" |
85a5290f |
89 | #include <TObjString.h> |
90 | |
91 | |
92 | ClassImp(AliSimulation) |
93 | |
94 | |
95 | //_____________________________________________________________________________ |
95601147 |
96 | AliSimulation::AliSimulation(const char* configFileName, |
97 | const char* name, const char* title) : |
98 | TNamed(name, title), |
99 | |
100 | fRunGeneration(kTRUE), |
101 | fRunSimulation(kTRUE), |
102 | fMakeSDigits("ALL"), |
103 | fMakeDigits("ALL"), |
104 | fMakeDigitsFromHits(""), |
105 | fStopOnError(kFALSE), |
106 | |
107 | fNEvents(1), |
108 | fConfigFileName(configFileName), |
109 | fGAliceFileName("galice.root"), |
110 | fBkgrdFileNames(NULL), |
04bae0a0 |
111 | fUseBkgrdVertex(kTRUE), |
95601147 |
112 | fRegionOfInterest(kTRUE) |
85a5290f |
113 | { |
114 | // create simulation object with default parameters |
115 | |
85a5290f |
116 | } |
117 | |
118 | //_____________________________________________________________________________ |
119 | AliSimulation::AliSimulation(const AliSimulation& sim) : |
95601147 |
120 | TNamed(sim), |
121 | |
122 | fRunGeneration(sim.fRunGeneration), |
123 | fRunSimulation(sim.fRunSimulation), |
124 | fMakeSDigits(sim.fMakeSDigits), |
125 | fMakeDigits(sim.fMakeDigits), |
126 | fMakeDigitsFromHits(sim.fMakeDigitsFromHits), |
127 | fStopOnError(sim.fStopOnError), |
128 | |
129 | fNEvents(sim.fNEvents), |
130 | fConfigFileName(sim.fConfigFileName), |
131 | fGAliceFileName(sim.fGAliceFileName), |
132 | fBkgrdFileNames(NULL), |
04bae0a0 |
133 | fUseBkgrdVertex(sim.fUseBkgrdVertex), |
95601147 |
134 | fRegionOfInterest(sim.fRegionOfInterest) |
85a5290f |
135 | { |
136 | // copy constructor |
137 | |
85a5290f |
138 | fBkgrdFileNames = new TObjArray; |
139 | for (Int_t i = 0; i < sim.fBkgrdFileNames->GetEntriesFast(); i++) { |
140 | if (!sim.fBkgrdFileNames->At(i)) continue; |
141 | fBkgrdFileNames->Add(sim.fBkgrdFileNames->At(i)->Clone()); |
142 | } |
85a5290f |
143 | } |
144 | |
145 | //_____________________________________________________________________________ |
146 | AliSimulation& AliSimulation::operator = (const AliSimulation& sim) |
147 | { |
148 | // assignment operator |
149 | |
150 | this->~AliSimulation(); |
151 | new(this) AliSimulation(sim); |
152 | return *this; |
153 | } |
154 | |
155 | //_____________________________________________________________________________ |
156 | AliSimulation::~AliSimulation() |
157 | { |
158 | // clean up |
159 | |
95601147 |
160 | if (fBkgrdFileNames) { |
161 | fBkgrdFileNames->Delete(); |
162 | delete fBkgrdFileNames; |
163 | } |
85a5290f |
164 | } |
165 | |
166 | |
167 | //_____________________________________________________________________________ |
168 | void AliSimulation::SetNumberOfEvents(Int_t nEvents) |
169 | { |
170 | // set the number of events for one run |
171 | |
172 | fNEvents = nEvents; |
173 | } |
174 | |
175 | //_____________________________________________________________________________ |
176 | void AliSimulation::SetConfigFile(const char* fileName) |
177 | { |
178 | // set the name of the config file |
179 | |
180 | fConfigFileName = fileName; |
181 | } |
182 | |
183 | //_____________________________________________________________________________ |
184 | void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd) |
185 | { |
186 | // add a file with background events for merging |
187 | |
188 | TObjString* fileNameStr = new TObjString(fileName); |
189 | fileNameStr->SetUniqueID(nSignalPerBkgrd); |
95601147 |
190 | if (!fBkgrdFileNames) fBkgrdFileNames = new TObjArray; |
85a5290f |
191 | fBkgrdFileNames->Add(fileNameStr); |
192 | } |
193 | |
194 | |
195 | //_____________________________________________________________________________ |
196 | Bool_t AliSimulation::Run(Int_t nEvents) |
197 | { |
198 | // run the generation, simulation and digitization |
199 | |
200 | if (nEvents > 0) fNEvents = nEvents; |
201 | |
202 | // generation and simulation -> hits |
203 | if (fRunGeneration) { |
85a5290f |
204 | if (!RunSimulation()) if (fStopOnError) return kFALSE; |
205 | } |
206 | |
85a5290f |
207 | // hits -> summable digits |
208 | if (!fMakeSDigits.IsNull()) { |
209 | if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE; |
210 | } |
211 | |
212 | // summable digits -> digits |
213 | if (!fMakeDigits.IsNull()) { |
214 | if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) { |
215 | if (fStopOnError) return kFALSE; |
216 | } |
217 | } |
218 | |
219 | // hits -> digits |
220 | if (!fMakeDigitsFromHits.IsNull()) { |
95601147 |
221 | if (fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) { |
85a5290f |
222 | Warning("Run", "Merging and direct creation of digits from hits " |
223 | "was selected for some detectors. " |
224 | "No merging will be done for the following detectors: %s", |
225 | fMakeDigitsFromHits.Data()); |
226 | } |
227 | if (!RunHitsDigitization(fMakeDigitsFromHits)) { |
228 | if (fStopOnError) return kFALSE; |
229 | } |
230 | } |
231 | |
232 | return kTRUE; |
233 | } |
234 | |
235 | //_____________________________________________________________________________ |
95601147 |
236 | Bool_t AliSimulation::RunSimulation(Int_t nEvents) |
85a5290f |
237 | { |
238 | // run the generation and simulation |
239 | |
4df28b43 |
240 | TStopwatch stopwatch; |
241 | stopwatch.Start(); |
242 | |
95601147 |
243 | if (!gAlice) { |
244 | Error("RunSimulation", "no gAlice object. Restart aliroot and try again."); |
245 | return kFALSE; |
246 | } |
247 | if (gAlice->Modules()->GetEntries() > 0) { |
248 | Error("RunSimulation", |
249 | "gAlice was already run. Restart aliroot and try again."); |
250 | return kFALSE; |
251 | } |
252 | |
85a5290f |
253 | Info("RunSimulation", "initializing gAlice with config file %s", |
254 | fConfigFileName.Data()); |
255 | gAlice->Init(fConfigFileName.Data()); |
95601147 |
256 | AliRunLoader* runLoader = gAlice->GetRunLoader(); |
257 | if (!runLoader) { |
85a5290f |
258 | Error("RunSimulation", "gAlice has no run loader object. " |
259 | "Check your config file: %s", fConfigFileName.Data()); |
260 | return kFALSE; |
261 | } |
95601147 |
262 | fGAliceFileName = runLoader->GetFileName(); |
85a5290f |
263 | |
04bae0a0 |
264 | if (!gAlice->Generator()) { |
265 | Error("RunSimulation", "gAlice has no generator object. " |
266 | "Check your config file: %s", fConfigFileName.Data()); |
267 | return kFALSE; |
268 | } |
269 | |
270 | // get vertex from background file in case of merging |
271 | if (fUseBkgrdVertex && |
272 | fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) { |
273 | Int_t signalPerBkgrd = fBkgrdFileNames->At(0)->GetUniqueID(); |
274 | const char* fileName = ((TObjString*) |
275 | (fBkgrdFileNames->At(0)))->GetName(); |
276 | Info("RunSimulation", "The vertex will be taken from the background " |
277 | "file %s with nSignalPerBackground = %d", |
278 | fileName, signalPerBkgrd); |
279 | AliVertexGenFile* vtxGen = new AliVertexGenFile(fileName, signalPerBkgrd); |
280 | gAlice->Generator()->SetVertexGenerator(vtxGen); |
281 | } |
282 | |
85a5290f |
283 | if (!fRunSimulation) { |
85a5290f |
284 | gAlice->Generator()->SetTrackingFlag(0); |
285 | } |
286 | |
4df28b43 |
287 | Info("RunSimulation", "running gAlice"); |
95601147 |
288 | if (nEvents <= 0) nEvents = fNEvents; |
289 | gAlice->Run(nEvents); |
290 | |
291 | delete runLoader; |
85a5290f |
292 | |
4df28b43 |
293 | Info("RunSimulation", "execution time:"); |
294 | stopwatch.Print(); |
295 | |
85a5290f |
296 | return kTRUE; |
297 | } |
298 | |
299 | //_____________________________________________________________________________ |
95601147 |
300 | Bool_t AliSimulation::RunSDigitization(const char* detectors) |
85a5290f |
301 | { |
302 | // run the digitization and produce summable digits |
303 | |
4df28b43 |
304 | TStopwatch stopwatch; |
305 | stopwatch.Start(); |
306 | |
95601147 |
307 | AliRunLoader* runLoader = LoadRun(); |
308 | if (!runLoader) return kFALSE; |
309 | |
85a5290f |
310 | TString detStr = detectors; |
95601147 |
311 | TObjArray* detArray = runLoader->GetAliRun()->Detectors(); |
85a5290f |
312 | for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) { |
313 | AliModule* det = (AliModule*) detArray->At(iDet); |
314 | if (!det || !det->IsActive()) continue; |
315 | if (IsSelected(det->GetName(), detStr)) { |
316 | Info("RunSDigitization", "creating summable digits for %s", |
317 | det->GetName()); |
de76655b |
318 | TStopwatch stopwatchDet; |
319 | stopwatchDet.Start(); |
85a5290f |
320 | det->Hits2SDigits(); |
de76655b |
321 | Info("RunSDigitization", "execution time for %s:", det->GetName()); |
322 | stopwatchDet.Print(); |
85a5290f |
323 | } |
324 | } |
325 | |
326 | if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) { |
327 | Error("RunSDigitization", "the following detectors were not found: %s", |
328 | detStr.Data()); |
329 | if (fStopOnError) return kFALSE; |
330 | } |
331 | |
95601147 |
332 | delete runLoader; |
333 | |
4df28b43 |
334 | Info("RunSDigitization", "execution time:"); |
335 | stopwatch.Print(); |
336 | |
85a5290f |
337 | return kTRUE; |
338 | } |
339 | |
340 | |
341 | //_____________________________________________________________________________ |
95601147 |
342 | Bool_t AliSimulation::RunDigitization(const char* detectors, |
343 | const char* excludeDetectors) |
85a5290f |
344 | { |
345 | // run the digitization and produce digits from sdigits |
346 | |
4df28b43 |
347 | TStopwatch stopwatch; |
348 | stopwatch.Start(); |
349 | |
95601147 |
350 | while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader(); |
351 | if (gAlice) delete gAlice; |
352 | gAlice = NULL; |
353 | |
354 | Int_t nStreams = 1; |
85a5290f |
355 | Int_t signalPerBkgrd = 1; |
95601147 |
356 | if (fBkgrdFileNames) { |
357 | nStreams = fBkgrdFileNames->GetEntriesFast() + 1; |
358 | if (nStreams > 1) signalPerBkgrd = fBkgrdFileNames->At(0)->GetUniqueID(); |
359 | } |
85a5290f |
360 | AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd); |
361 | manager->SetInputStream(0, fGAliceFileName.Data()); |
362 | for (Int_t iStream = 1; iStream < nStreams; iStream++) { |
363 | const char* fileName = ((TObjString*) |
364 | (fBkgrdFileNames->At(iStream-1)))->GetName(); |
365 | manager->SetInputStream(iStream, fileName); |
366 | } |
367 | |
368 | TString detStr = detectors; |
369 | TString detExcl = excludeDetectors; |
95601147 |
370 | manager->GetInputStream(0)->ImportgAlice(); |
371 | AliRunLoader* runLoader = |
372 | AliRunLoader::GetRunLoader(manager->GetInputStream(0)->GetFolderName()); |
373 | TObjArray* detArray = runLoader->GetAliRun()->Detectors(); |
85a5290f |
374 | for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) { |
375 | AliModule* det = (AliModule*) detArray->At(iDet); |
376 | if (!det || !det->IsActive()) continue; |
377 | if (IsSelected(det->GetName(), detStr) && |
378 | !IsSelected(det->GetName(), detExcl)) { |
4df28b43 |
379 | AliDigitizer* digitizer = det->CreateDigitizer(manager); |
380 | if (!digitizer) { |
85a5290f |
381 | Error("RunDigitization", "no digitizer for %s", det->GetName()); |
382 | if (fStopOnError) return kFALSE; |
4df28b43 |
383 | } else { |
384 | digitizer->SetRegionOfInterest(fRegionOfInterest); |
85a5290f |
385 | } |
386 | } |
387 | } |
388 | |
389 | if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) { |
390 | Error("RunDigitization", "the following detectors were not found: %s", |
391 | detStr.Data()); |
392 | if (fStopOnError) return kFALSE; |
393 | } |
394 | |
395 | if (!manager->GetListOfTasks()->IsEmpty()) { |
396 | Info("RunDigitization", "executing digitization"); |
397 | manager->Exec(""); |
398 | } |
95601147 |
399 | |
85a5290f |
400 | delete manager; |
401 | |
4df28b43 |
402 | Info("RunDigitization", "execution time:"); |
403 | stopwatch.Print(); |
404 | |
85a5290f |
405 | return kTRUE; |
406 | } |
407 | |
408 | //_____________________________________________________________________________ |
95601147 |
409 | Bool_t AliSimulation::RunHitsDigitization(const char* detectors) |
85a5290f |
410 | { |
411 | // run the digitization and produce digits from hits |
412 | |
4df28b43 |
413 | TStopwatch stopwatch; |
414 | stopwatch.Start(); |
415 | |
95601147 |
416 | AliRunLoader* runLoader = LoadRun(); |
417 | if (!runLoader) return kFALSE; |
418 | |
85a5290f |
419 | TString detStr = detectors; |
95601147 |
420 | TObjArray* detArray = runLoader->GetAliRun()->Detectors(); |
85a5290f |
421 | for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) { |
422 | AliModule* det = (AliModule*) detArray->At(iDet); |
423 | if (!det || !det->IsActive()) continue; |
424 | if (IsSelected(det->GetName(), detStr)) { |
425 | Info("RunHitsDigitization", "creating digits from hits for %s", |
426 | det->GetName()); |
427 | det->Hits2Digits(); |
428 | } |
429 | } |
430 | |
431 | if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) { |
432 | Error("RunHitsDigitization", "the following detectors were not found: %s", |
433 | detStr.Data()); |
434 | if (fStopOnError) return kFALSE; |
435 | } |
436 | |
95601147 |
437 | delete runLoader; |
438 | |
4df28b43 |
439 | Info("RunHitsDigitization", "execution time:"); |
440 | stopwatch.Print(); |
441 | |
85a5290f |
442 | return kTRUE; |
443 | } |
444 | |
445 | |
95601147 |
446 | //_____________________________________________________________________________ |
447 | AliRunLoader* AliSimulation::LoadRun() const |
448 | { |
449 | // delete existing run loaders, open a new one and load gAlice |
450 | |
451 | while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader(); |
452 | AliRunLoader* runLoader = |
453 | AliRunLoader::Open(fGAliceFileName.Data(), |
454 | AliConfig::fgkDefaultEventFolderName, "UPDATE"); |
455 | if (!runLoader) { |
456 | Error("LoadRun", "no run loader found in file %s", |
457 | fGAliceFileName.Data()); |
458 | return NULL; |
459 | } |
460 | runLoader->LoadgAlice(); |
461 | gAlice = runLoader->GetAliRun(); |
462 | if (!gAlice) { |
463 | Error("LoadRun", "no gAlice object found in file %s", |
464 | fGAliceFileName.Data()); |
465 | return NULL; |
466 | } |
467 | return runLoader; |
468 | } |
469 | |
85a5290f |
470 | //_____________________________________________________________________________ |
471 | Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const |
472 | { |
473 | // check whether detName is contained in detectors |
474 | // if yes, it is removed from detectors |
475 | |
476 | // check if all detectors are selected |
477 | if ((detectors.CompareTo("ALL") == 0) || |
478 | detectors.BeginsWith("ALL ") || |
479 | detectors.EndsWith(" ALL") || |
480 | detectors.Contains(" ALL ")) { |
481 | detectors = "ALL"; |
482 | return kTRUE; |
483 | } |
484 | |
485 | // search for the given detector |
486 | Bool_t result = kFALSE; |
487 | if ((detectors.CompareTo(detName) == 0) || |
488 | detectors.BeginsWith(detName+" ") || |
489 | detectors.EndsWith(" "+detName) || |
490 | detectors.Contains(" "+detName+" ")) { |
491 | detectors.ReplaceAll(detName, ""); |
492 | result = kTRUE; |
493 | } |
494 | |
495 | // clean up the detectors string |
496 | while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " "); |
497 | while (detectors.BeginsWith(" ")) detectors.Remove(0, 1); |
498 | while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1); |
499 | |
500 | return result; |
501 | } |