1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // class for running generation, simulation and digitization //
22 // Hits, sdigits and digits are created for all detectors by typing: //
24 // AliSimulation sim; //
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 //
31 // sim.SetNumberOfEvents(n); //
33 // The name of the configuration file can be specified by //
35 // sim.SetConfigFile("..."); //
37 // The generation of particles and the simulation of detector hits can be //
38 // switched on or off by //
40 // sim.SetRunGeneration(kTRUE); // generation of primary particles //
41 // sim.SetRunSimulation(kFALSE); // but no tracking //
43 // For which detectors sdigits and digits will be created, can be steered //
46 // sim.SetMakeSDigits("ALL"); // make sdigits for all detectors //
47 // sim.SetMakeDigits("ITS TPC"); // make digits only for ITS and TPC //
49 // The argument is a (case sensitive) string with the names of the //
50 // detectors separated by a space. An empty string ("") can be used to //
51 // disable the creation of sdigits or digits. The special string "ALL" //
52 // selects all available detectors. This is the default. //
54 // The creation of digits from hits instead of from sdigits can be selected //
57 // sim.SetMakeDigitsFromHits("TRD"); //
59 // The argument is again a string with the selected detectors. Be aware that //
60 // this feature is not available for all detectors and that merging is not //
61 // possible, when digits are created directly from hits. //
63 // Backgound events can be merged by calling //
65 // sim.MergeWith("background/galice.root", 2); //
67 // The first argument is the file name of the background galice file. The //
68 // second argument is the number of signal events per background event. //
69 // The default value for this is 1. MergeWith can be called several times //
70 // to merge more than two event streams. It is assumed that the sdigits //
71 // were already produced for the background events. //
73 ///////////////////////////////////////////////////////////////////////////////
76 #include "AliSimulation.h"
77 #include "AliRunLoader.h"
79 #include "AliModule.h"
80 #include "AliGenerator.h"
81 #include "AliRunDigitizer.h"
82 #include "AliDigitizer.h"
83 #include <TObjString.h>
86 ClassImp(AliSimulation)
89 //_____________________________________________________________________________
90 AliSimulation::AliSimulation(const char* name, const char* title) :
93 // create simulation object with default parameters
98 //_____________________________________________________________________________
99 AliSimulation::AliSimulation(const AliSimulation& sim) :
104 fRunGeneration = sim.fRunGeneration;
105 fRunSimulation = sim.fRunSimulation;
106 fMakeSDigits = sim.fMakeSDigits;
107 fMakeDigits = sim.fMakeDigits;
108 fMakeDigitsFromHits = sim.fMakeDigitsFromHits;
109 fStopOnError = sim.fStopOnError;
111 fNEvents = sim.fNEvents;
112 fConfigFileName = sim.fConfigFileName;
113 fGAliceFileName = sim.fGAliceFileName;
114 fBkgrdFileNames = new TObjArray;
115 for (Int_t i = 0; i < sim.fBkgrdFileNames->GetEntriesFast(); i++) {
116 if (!sim.fBkgrdFileNames->At(i)) continue;
117 fBkgrdFileNames->Add(sim.fBkgrdFileNames->At(i)->Clone());
123 //_____________________________________________________________________________
124 AliSimulation& AliSimulation::operator = (const AliSimulation& sim)
126 // assignment operator
128 this->~AliSimulation();
129 new(this) AliSimulation(sim);
133 //_____________________________________________________________________________
134 AliSimulation::~AliSimulation()
138 fBkgrdFileNames->Delete();
139 delete fBkgrdFileNames;
142 //_____________________________________________________________________________
143 void AliSimulation::Init()
145 // set default parameters
147 fRunGeneration = kTRUE;
148 fRunSimulation = kTRUE;
149 fMakeSDigits = "ALL";
151 fMakeDigitsFromHits = "";
152 fStopOnError = kFALSE;
155 fConfigFileName = "Config.C";
156 fGAliceFileName = "galice.root";
157 fBkgrdFileNames = new TObjArray;
158 fRegionOfInterest = kTRUE;
164 //_____________________________________________________________________________
165 void AliSimulation::SetNumberOfEvents(Int_t nEvents)
167 // set the number of events for one run
172 //_____________________________________________________________________________
173 void AliSimulation::SetConfigFile(const char* fileName)
175 // set the name of the config file
177 fConfigFileName = fileName;
180 //_____________________________________________________________________________
181 void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
183 // add a file with background events for merging
185 TObjString* fileNameStr = new TObjString(fileName);
186 fileNameStr->SetUniqueID(nSignalPerBkgrd);
187 fBkgrdFileNames->Add(fileNameStr);
191 //_____________________________________________________________________________
192 Bool_t AliSimulation::Run(Int_t nEvents)
194 // run the generation, simulation and digitization
196 if (nEvents > 0) fNEvents = nEvents;
198 // generation and simulation -> hits
199 if (fRunGeneration) {
201 Error("Run", "no gAlice object. Restart aliroot and try again.");
204 if (gAlice->Modules()->GetEntries() > 0) {
205 Error("Run", "gAlice was already run. Restart aliroot and try again.");
208 if (!RunSimulation()) if (fStopOnError) return kFALSE;
211 // reopen the run loader
212 if (fRunLoader) delete fRunLoader;
213 fRunLoader = AliRunLoader::Open(fGAliceFileName.Data(),AliConfig::fgkDefaultEventFolderName,"UPDATE");
215 Error("Run", "no run loader found in file %s",
216 fGAliceFileName.Data());
219 fRunLoader->LoadgAlice();
220 gAlice = fRunLoader->GetAliRun();
222 Error("Run", "no gAlice object found in file %s",
223 fGAliceFileName.Data());
227 // hits -> summable digits
228 if (!fMakeSDigits.IsNull()) {
229 if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
232 // summable digits -> digits
233 if (!fMakeDigits.IsNull()) {
234 if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
235 if (fStopOnError) return kFALSE;
240 if (!fMakeDigitsFromHits.IsNull()) {
241 if (fBkgrdFileNames->GetEntriesFast() > 0) {
242 Warning("Run", "Merging and direct creation of digits from hits "
243 "was selected for some detectors. "
244 "No merging will be done for the following detectors: %s",
245 fMakeDigitsFromHits.Data());
247 if (!RunHitsDigitization(fMakeDigitsFromHits)) {
248 if (fStopOnError) return kFALSE;
255 //_____________________________________________________________________________
256 Bool_t AliSimulation::RunSimulation()
258 // run the generation and simulation
260 TStopwatch stopwatch;
263 Info("RunSimulation", "initializing gAlice with config file %s",
264 fConfigFileName.Data());
265 gAlice->Init(fConfigFileName.Data());
266 fRunLoader = gAlice->GetRunLoader();
268 Error("RunSimulation", "gAlice has no run loader object. "
269 "Check your config file: %s", fConfigFileName.Data());
272 fGAliceFileName = fRunLoader->GetFileName();
274 if (!fRunSimulation) {
275 if (!gAlice->Generator()) {
276 Error("RunSimulation", "gAlice has no generator object. "
277 "Check your config file: %s", fConfigFileName.Data());
280 gAlice->Generator()->SetTrackingFlag(0);
283 Info("RunSimulation", "running gAlice");
284 gAlice->Run(fNEvents);
286 Info("RunSimulation", "execution time:");
292 //_____________________________________________________________________________
293 Bool_t AliSimulation::RunSDigitization(const TString& detectors)
295 // run the digitization and produce summable digits
297 TStopwatch stopwatch;
300 TString detStr = detectors;
301 TObjArray* detArray = gAlice->Detectors();
302 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
303 AliModule* det = (AliModule*) detArray->At(iDet);
304 if (!det || !det->IsActive()) continue;
305 if (IsSelected(det->GetName(), detStr)) {
306 Info("RunSDigitization", "creating summable digits for %s",
312 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
313 Error("RunSDigitization", "the following detectors were not found: %s",
315 if (fStopOnError) return kFALSE;
318 Info("RunSDigitization", "execution time:");
325 //_____________________________________________________________________________
326 Bool_t AliSimulation::RunDigitization(const TString& detectors,
327 const TString& excludeDetectors)
329 // run the digitization and produce digits from sdigits
331 TStopwatch stopwatch;
334 Int_t nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
335 Int_t signalPerBkgrd = 1;
336 if (nStreams > 1) signalPerBkgrd = fBkgrdFileNames->At(0)->GetUniqueID();
337 AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
338 manager->SetInputStream(0, fGAliceFileName.Data());
339 for (Int_t iStream = 1; iStream < nStreams; iStream++) {
340 const char* fileName = ((TObjString*)
341 (fBkgrdFileNames->At(iStream-1)))->GetName();
342 manager->SetInputStream(iStream, fileName);
345 TString detStr = detectors;
346 TString detExcl = excludeDetectors;
347 TObjArray* detArray = gAlice->Detectors();
348 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
349 AliModule* det = (AliModule*) detArray->At(iDet);
350 if (!det || !det->IsActive()) continue;
351 if (IsSelected(det->GetName(), detStr) &&
352 !IsSelected(det->GetName(), detExcl)) {
353 AliDigitizer* digitizer = det->CreateDigitizer(manager);
355 Error("RunDigitization", "no digitizer for %s", det->GetName());
356 if (fStopOnError) return kFALSE;
358 digitizer->SetRegionOfInterest(fRegionOfInterest);
363 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
364 Error("RunDigitization", "the following detectors were not found: %s",
366 if (fStopOnError) return kFALSE;
369 if (!manager->GetListOfTasks()->IsEmpty()) {
370 Info("RunDigitization", "executing digitization");
375 Info("RunDigitization", "execution time:");
381 //_____________________________________________________________________________
382 Bool_t AliSimulation::RunHitsDigitization(const TString& detectors)
384 // run the digitization and produce digits from hits
386 TStopwatch stopwatch;
389 TString detStr = detectors;
390 TObjArray* detArray = gAlice->Detectors();
391 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
392 AliModule* det = (AliModule*) detArray->At(iDet);
393 if (!det || !det->IsActive()) continue;
394 if (IsSelected(det->GetName(), detStr)) {
395 Info("RunHitsDigitization", "creating digits from hits for %s",
401 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
402 Error("RunHitsDigitization", "the following detectors were not found: %s",
404 if (fStopOnError) return kFALSE;
407 Info("RunHitsDigitization", "execution time:");
414 //_____________________________________________________________________________
415 Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
417 // check whether detName is contained in detectors
418 // if yes, it is removed from detectors
420 // check if all detectors are selected
421 if ((detectors.CompareTo("ALL") == 0) ||
422 detectors.BeginsWith("ALL ") ||
423 detectors.EndsWith(" ALL") ||
424 detectors.Contains(" ALL ")) {
429 // search for the given detector
430 Bool_t result = kFALSE;
431 if ((detectors.CompareTo(detName) == 0) ||
432 detectors.BeginsWith(detName+" ") ||
433 detectors.EndsWith(" "+detName) ||
434 detectors.Contains(" "+detName+" ")) {
435 detectors.ReplaceAll(detName, "");
439 // clean up the detectors string
440 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
441 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
442 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);