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 <TObjString.h>
85 ClassImp(AliSimulation)
88 //_____________________________________________________________________________
89 AliSimulation::AliSimulation(const char* name, const char* title) :
92 // create simulation object with default parameters
97 //_____________________________________________________________________________
98 AliSimulation::AliSimulation(const AliSimulation& sim) :
103 fRunGeneration = sim.fRunGeneration;
104 fRunSimulation = sim.fRunSimulation;
105 fMakeSDigits = sim.fMakeSDigits;
106 fMakeDigits = sim.fMakeDigits;
107 fMakeDigitsFromHits = sim.fMakeDigitsFromHits;
108 fStopOnError = sim.fStopOnError;
110 fNEvents = sim.fNEvents;
111 fConfigFileName = sim.fConfigFileName;
112 fGAliceFileName = sim.fGAliceFileName;
113 fBkgrdFileNames = new TObjArray;
114 for (Int_t i = 0; i < sim.fBkgrdFileNames->GetEntriesFast(); i++) {
115 if (!sim.fBkgrdFileNames->At(i)) continue;
116 fBkgrdFileNames->Add(sim.fBkgrdFileNames->At(i)->Clone());
122 //_____________________________________________________________________________
123 AliSimulation& AliSimulation::operator = (const AliSimulation& sim)
125 // assignment operator
127 this->~AliSimulation();
128 new(this) AliSimulation(sim);
132 //_____________________________________________________________________________
133 AliSimulation::~AliSimulation()
137 fBkgrdFileNames->Delete();
138 delete fBkgrdFileNames;
141 //_____________________________________________________________________________
142 void AliSimulation::Init()
144 // set default parameters
146 fRunGeneration = kTRUE;
147 fRunSimulation = kTRUE;
148 fMakeSDigits = "ALL";
150 fMakeDigitsFromHits = "";
151 fStopOnError = kFALSE;
154 fConfigFileName = "Config.C";
155 fGAliceFileName = "galice.root";
156 fBkgrdFileNames = new TObjArray;
162 //_____________________________________________________________________________
163 void AliSimulation::SetNumberOfEvents(Int_t nEvents)
165 // set the number of events for one run
170 //_____________________________________________________________________________
171 void AliSimulation::SetConfigFile(const char* fileName)
173 // set the name of the config file
175 fConfigFileName = fileName;
178 //_____________________________________________________________________________
179 void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
181 // add a file with background events for merging
183 TObjString* fileNameStr = new TObjString(fileName);
184 fileNameStr->SetUniqueID(nSignalPerBkgrd);
185 fBkgrdFileNames->Add(fileNameStr);
189 //_____________________________________________________________________________
190 Bool_t AliSimulation::Run(Int_t nEvents)
192 // run the generation, simulation and digitization
194 if (nEvents > 0) fNEvents = nEvents;
196 // generation and simulation -> hits
197 if (fRunGeneration) {
199 Error("Run", "no gAlice object. Restart aliroot and try again.");
202 if (gAlice->Modules()->GetEntries() > 0) {
203 Error("Run", "gAlice was already run. Restart aliroot and try again.");
206 if (!RunSimulation()) if (fStopOnError) return kFALSE;
209 // reopen the run loader
210 if (fRunLoader) delete fRunLoader;
211 fRunLoader = AliRunLoader::Open(fGAliceFileName.Data(),AliConfig::fgkDefaultEventFolderName,"UPDATE");
213 Error("Run", "no run loader found in file %s",
214 fGAliceFileName.Data());
217 fRunLoader->LoadgAlice();
218 gAlice = fRunLoader->GetAliRun();
220 Error("GetLoadersAndDetectors", "no gAlice object found in file %s",
221 fGAliceFileName.Data());
225 // hits -> summable digits
226 if (!fMakeSDigits.IsNull()) {
227 if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
230 // summable digits -> digits
231 if (!fMakeDigits.IsNull()) {
232 if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
233 if (fStopOnError) return kFALSE;
238 if (!fMakeDigitsFromHits.IsNull()) {
239 if (fBkgrdFileNames->GetEntriesFast() > 0) {
240 Warning("Run", "Merging and direct creation of digits from hits "
241 "was selected for some detectors. "
242 "No merging will be done for the following detectors: %s",
243 fMakeDigitsFromHits.Data());
245 if (!RunHitsDigitization(fMakeDigitsFromHits)) {
246 if (fStopOnError) return kFALSE;
253 //_____________________________________________________________________________
254 Bool_t AliSimulation::RunSimulation()
256 // run the generation and simulation
258 Info("RunSimulation", "initializing gAlice with config file %s",
259 fConfigFileName.Data());
260 gAlice->Init(fConfigFileName.Data());
261 fRunLoader = gAlice->GetRunLoader();
263 Error("RunSimulation", "gAlice has no run loader object. "
264 "Check your config file: %s", fConfigFileName.Data());
267 fGAliceFileName = fRunLoader->GetFileName();
269 if (!fRunSimulation) {
270 if (!gAlice->Generator()) {
271 Error("RunSimulation", "gAlice has no generator object. "
272 "Check your config file: %s", fConfigFileName.Data());
275 gAlice->Generator()->SetTrackingFlag(0);
278 Info("Run", "running gAlice");
279 gAlice->Run(fNEvents);
284 //_____________________________________________________________________________
285 Bool_t AliSimulation::RunSDigitization(const TString& detectors)
287 // run the digitization and produce summable digits
289 TString detStr = detectors;
290 TObjArray* detArray = gAlice->Detectors();
291 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
292 AliModule* det = (AliModule*) detArray->At(iDet);
293 if (!det || !det->IsActive()) continue;
294 if (IsSelected(det->GetName(), detStr)) {
295 Info("RunSDigitization", "creating summable digits for %s",
301 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
302 Error("RunSDigitization", "the following detectors were not found: %s",
304 if (fStopOnError) return kFALSE;
311 //_____________________________________________________________________________
312 Bool_t AliSimulation::RunDigitization(const TString& detectors,
313 const TString& excludeDetectors)
315 // run the digitization and produce digits from sdigits
317 Int_t nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
318 Int_t signalPerBkgrd = 1;
319 if (nStreams > 1) signalPerBkgrd = fBkgrdFileNames->At(0)->GetUniqueID();
320 AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
321 manager->SetInputStream(0, fGAliceFileName.Data());
322 for (Int_t iStream = 1; iStream < nStreams; iStream++) {
323 const char* fileName = ((TObjString*)
324 (fBkgrdFileNames->At(iStream-1)))->GetName();
325 manager->SetInputStream(iStream, fileName);
328 TString detStr = detectors;
329 TString detExcl = excludeDetectors;
330 TObjArray* detArray = gAlice->Detectors();
331 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
332 AliModule* det = (AliModule*) detArray->At(iDet);
333 if (!det || !det->IsActive()) continue;
334 if (IsSelected(det->GetName(), detStr) &&
335 !IsSelected(det->GetName(), detExcl)) {
336 if (!det->CreateDigitizer(manager)) {
337 Error("RunDigitization", "no digitizer for %s", det->GetName());
338 if (fStopOnError) return kFALSE;
343 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
344 Error("RunDigitization", "the following detectors were not found: %s",
346 if (fStopOnError) return kFALSE;
349 if (!manager->GetListOfTasks()->IsEmpty()) {
350 Info("RunDigitization", "executing digitization");
358 //_____________________________________________________________________________
359 Bool_t AliSimulation::RunHitsDigitization(const TString& detectors)
361 // run the digitization and produce digits from hits
363 TString detStr = detectors;
364 TObjArray* detArray = gAlice->Detectors();
365 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
366 AliModule* det = (AliModule*) detArray->At(iDet);
367 if (!det || !det->IsActive()) continue;
368 if (IsSelected(det->GetName(), detStr)) {
369 Info("RunHitsDigitization", "creating digits from hits for %s",
375 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
376 Error("RunHitsDigitization", "the following detectors were not found: %s",
378 if (fStopOnError) return kFALSE;
385 //_____________________________________________________________________________
386 Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
388 // check whether detName is contained in detectors
389 // if yes, it is removed from detectors
391 // check if all detectors are selected
392 if ((detectors.CompareTo("ALL") == 0) ||
393 detectors.BeginsWith("ALL ") ||
394 detectors.EndsWith(" ALL") ||
395 detectors.Contains(" ALL ")) {
400 // search for the given detector
401 Bool_t result = kFALSE;
402 if ((detectors.CompareTo(detName) == 0) ||
403 detectors.BeginsWith(detName+" ") ||
404 detectors.EndsWith(" "+detName) ||
405 detectors.Contains(" "+detName+" ")) {
406 detectors.ReplaceAll(detName, "");
410 // clean up the detectors string
411 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
412 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
413 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);