In Simple(): corrected index in clusters[]
[u/mrichter/AliRoot.git] / STEER / AliSimulation.cxx
CommitLineData
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// //
05526d44 64// Background events can be merged by calling //
85a5290f 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. //
05526d44 70// By default this number is calculated from the number of available //
71// background events. MergeWith can be called several times to merge more //
72// than two event streams. It is assumed that the sdigits were already //
73// produced for the background events. //
85a5290f 74// //
0421c3d1 75// The output of raw data can be switched on by calling //
76// //
77// sim.SetWriteRawData("MUON"); // write raw data for MUON //
78// //
06c7e392 79// The default output format of the raw data are DDL files. They are //
80// converted to a DATE file, if a file name is given as second argument. //
81// For this conversion the program "dateStream" is required. If the file //
82// name has the extension ".root", the DATE file is converted to a root //
83// file. The program "alimdc" is used for this purpose. For the conversion //
84// to DATE and root format the two conversion programs have to be installed. //
85// Only the raw data in the final format is kept if the third argument is //
86// kTRUE. //
87// //
0421c3d1 88// The methods RunSimulation, RunSDigitization, RunDigitization, //
89// RunHitsDigitization and WriteRawData can be used to run only parts of //
06c7e392 90// the full simulation chain. The creation of raw data DDL files and their //
91// conversion to the DATE or root format can be run directly by calling //
92// the methods WriteRawFiles, ConvertRawFilesToDate and ConvertDateToRoot. //
95601147 93// //
18b43626 94// The default number of events per file, which is usually set in the //
95// config file, can be changed for individual detectors and data types //
96// by calling //
97// //
98// sim.SetEventsPerFile("PHOS", "Reconstructed Points", 3); //
99// //
100// The first argument is the detector, the second one the data type and the //
101// last one the number of events per file. Valid data types are "Hits", //
102// "Summable Digits", "Digits", "Reconstructed Points" and "Tracks". //
103// The number of events per file has to be set before the simulation of //
104// hits. Otherwise it has no effect. //
105// //
85a5290f 106///////////////////////////////////////////////////////////////////////////////
107
e8d02863 108#include <TGeoManager.h>
109#include <TGeoShape.h>
110#include <TGeoNode.h>
111#include <TGeoMatrix.h>
112#include <TGeoPhysicalNode.h>
85a5290f 113#include <TObjString.h>
fd46e2d2 114#include <TStopwatch.h>
af7ba10c 115#include <TSystem.h>
85a5290f 116
e8d02863 117#include "AliCDBStorage.h"
118#include "AliCDBEntry.h"
119#include "AliCDBManager.h"
120#include "AliAlignObj.h"
121#include "AliCentralTrigger.h"
122#include "AliDAQConfig.h"
af7ba10c 123#include "AliDigitizer.h"
124#include "AliGenerator.h"
e8d02863 125#include "AliLog.h"
af7ba10c 126#include "AliModule.h"
127#include "AliRun.h"
128#include "AliRunDigitizer.h"
129#include "AliRunLoader.h"
130#include "AliSimulation.h"
131#include "AliVertexGenFile.h"
a5a091ce 132#include "AliCentralTrigger.h"
85a5290f 133
134ClassImp(AliSimulation)
135
136
137//_____________________________________________________________________________
95601147 138AliSimulation::AliSimulation(const char* configFileName,
139 const char* name, const char* title) :
140 TNamed(name, title),
141
142 fRunGeneration(kTRUE),
143 fRunSimulation(kTRUE),
144 fMakeSDigits("ALL"),
145 fMakeDigits("ALL"),
a5a091ce 146 fMakeTrigger(""),
95601147 147 fMakeDigitsFromHits(""),
0421c3d1 148 fWriteRawData(""),
06c7e392 149 fRawDataFileName(""),
150 fDeleteIntermediateFiles(kFALSE),
95601147 151 fStopOnError(kFALSE),
152
153 fNEvents(1),
154 fConfigFileName(configFileName),
155 fGAliceFileName("galice.root"),
18b43626 156 fEventsPerFile(),
95601147 157 fBkgrdFileNames(NULL),
04bae0a0 158 fUseBkgrdVertex(kTRUE),
815c2b38 159 fRegionOfInterest(kFALSE)
85a5290f 160{
161// create simulation object with default parameters
162
0421c3d1 163 SetGAliceFile("galice.root");
85a5290f 164}
165
166//_____________________________________________________________________________
167AliSimulation::AliSimulation(const AliSimulation& sim) :
95601147 168 TNamed(sim),
169
170 fRunGeneration(sim.fRunGeneration),
171 fRunSimulation(sim.fRunSimulation),
172 fMakeSDigits(sim.fMakeSDigits),
173 fMakeDigits(sim.fMakeDigits),
a5a091ce 174 fMakeTrigger(sim.fMakeTrigger),
95601147 175 fMakeDigitsFromHits(sim.fMakeDigitsFromHits),
0421c3d1 176 fWriteRawData(sim.fWriteRawData),
06c7e392 177 fRawDataFileName(""),
178 fDeleteIntermediateFiles(kFALSE),
95601147 179 fStopOnError(sim.fStopOnError),
180
181 fNEvents(sim.fNEvents),
182 fConfigFileName(sim.fConfigFileName),
183 fGAliceFileName(sim.fGAliceFileName),
18b43626 184 fEventsPerFile(),
95601147 185 fBkgrdFileNames(NULL),
04bae0a0 186 fUseBkgrdVertex(sim.fUseBkgrdVertex),
95601147 187 fRegionOfInterest(sim.fRegionOfInterest)
85a5290f 188{
189// copy constructor
190
18b43626 191 for (Int_t i = 0; i < sim.fEventsPerFile.GetEntriesFast(); i++) {
192 if (!sim.fEventsPerFile[i]) continue;
193 fEventsPerFile.Add(sim.fEventsPerFile[i]->Clone());
194 }
195
85a5290f 196 fBkgrdFileNames = new TObjArray;
197 for (Int_t i = 0; i < sim.fBkgrdFileNames->GetEntriesFast(); i++) {
198 if (!sim.fBkgrdFileNames->At(i)) continue;
199 fBkgrdFileNames->Add(sim.fBkgrdFileNames->At(i)->Clone());
200 }
85a5290f 201}
202
203//_____________________________________________________________________________
204AliSimulation& AliSimulation::operator = (const AliSimulation& sim)
205{
206// assignment operator
207
208 this->~AliSimulation();
209 new(this) AliSimulation(sim);
210 return *this;
211}
212
213//_____________________________________________________________________________
214AliSimulation::~AliSimulation()
215{
216// clean up
217
18b43626 218 fEventsPerFile.Delete();
219
95601147 220 if (fBkgrdFileNames) {
221 fBkgrdFileNames->Delete();
222 delete fBkgrdFileNames;
223 }
85a5290f 224}
225
226
227//_____________________________________________________________________________
228void AliSimulation::SetNumberOfEvents(Int_t nEvents)
229{
230// set the number of events for one run
231
232 fNEvents = nEvents;
233}
234
235//_____________________________________________________________________________
236void AliSimulation::SetConfigFile(const char* fileName)
237{
238// set the name of the config file
239
240 fConfigFileName = fileName;
241}
242
243//_____________________________________________________________________________
0421c3d1 244void AliSimulation::SetGAliceFile(const char* fileName)
245{
246// set the name of the galice file
247// the path is converted to an absolute one if it is relative
248
249 fGAliceFileName = fileName;
250 if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
251 char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
252 fGAliceFileName);
253 fGAliceFileName = absFileName;
254 delete[] absFileName;
255 }
ac1671c0 256
257 AliDebug(2, Form("galice file name set to %s", fileName));
0421c3d1 258}
259
260//_____________________________________________________________________________
18b43626 261void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
262 Int_t nEvents)
263{
264// set the number of events per file for the given detector and data type
265// ("Hits", "Summable Digits", "Digits", "Reconstructed Points" or "Tracks")
266
267 TNamed* obj = new TNamed(detector, type);
268 obj->SetUniqueID(nEvents);
269 fEventsPerFile.Add(obj);
270}
271
272//_____________________________________________________________________________
8c30cb69 273Bool_t AliSimulation::ApplyDisplacements(const char* fileName, const char* ClArrayName)
b9bc0f82 274{
275 // read collection of alignment objects (AliAlignObj derived) saved
276 // in the TClonesArray ClArrayName in the file fileName and apply
277 // them to the TGeo geometry passed as argument
278 //
279
280 TFile* inFile = TFile::Open(fileName,"READ");
281 if (!inFile || !inFile->IsOpen()) {
282 AliErrorClass(Form("Could not open file %s !",fileName));
283 return kFALSE;
284 }
285
286 TClonesArray* AlObjArray = ((TClonesArray*) inFile->Get(ClArrayName));
8c30cb69 287 inFile->Close();
288
289 return AliSimulation::ApplyDisplacements(AlObjArray);
290
291}
292
293//_____________________________________________________________________________
294Bool_t AliSimulation::ApplyDisplacements(TClonesArray* AlObjArray)
295{
296 // Read collection of alignment objects (AliAlignObj derived) saved
297 // in the TClonesArray ClArrayName and apply them to the geometry
298 // manager singleton.
299 //
b9bc0f82 300 Int_t nvols = AlObjArray->GetEntriesFast();
301
302 AliAlignObj::ELayerID layerId; // unique identity for volume in the alobj
303 Int_t modId; // unique identity for volume in the alobj
304 Bool_t ispathvalid; // false if volume path for alobj is not valid for TGeo
305
306 TGeoHMatrix dm;
307
308 for(Int_t j=0; j<nvols; j++)
309 {
310 AliAlignObj* alobj = (AliAlignObj*) AlObjArray->UncheckedAt(j);
311 const char* volpath = alobj->GetVolPath();
8c30cb69 312 TGeoPhysicalNode* node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(volpath);
b9bc0f82 313 alobj->GetMatrix(dm);
314 alobj->GetVolUID(layerId, modId);
8c30cb69 315 ispathvalid = gGeoManager->cd(volpath);
b9bc0f82 316 if(!ispathvalid){
317 AliWarningClass(Form("Volume path %s not valid!",volpath));
318 return kFALSE;
319 }
8c30cb69 320 TGeoHMatrix* hm = gGeoManager->GetCurrentMatrix();
b9bc0f82 321 hm->MultiplyLeft(&dm);
322 AliInfoClass(Form("Aligning volume %s of detector layer %d with local ID %d",volpath,layerId,modId));
323 node->Align(hm);
b9bc0f82 324 }
8c30cb69 325
b9bc0f82 326 return kTRUE;
327
328}
329
330//_____________________________________________________________________________
8c30cb69 331Bool_t AliSimulation::ApplyDisplacements(AliCDBParam* param, AliCDBId& Id)
b9bc0f82 332{
333 // read collection of alignment objects (AliAlignObj derived) saved
334 // in the TClonesArray ClArrayName in the AliCDBEntry identified by
335 // param (to get the AliCDBStorage) and Id; apply the alignment objects
336 // to the TGeo geometry passed as argument
337 //
338
339 AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage(param);
340 AliCDBEntry* entry = storage->Get(Id);
341 TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
342
8c30cb69 343 return AliSimulation::ApplyDisplacements(AlObjArray);
b9bc0f82 344
345}
346
347//_____________________________________________________________________________
8c30cb69 348Bool_t AliSimulation::ApplyDisplacements(const char* uri, const char* path, Int_t runnum, Int_t version, Int_t sversion)
b9bc0f82 349{
350 // read collection of alignment objects (AliAlignObj derived) saved
351 // in the TClonesArray ClArrayName in the AliCDBEntry identified by
352 // param (to get the AliCDBStorage) and Id; apply the alignment objects
353 // to the TGeo geometry passed as argument
354 //
355
356 AliCDBParam* param = AliCDBManager::Instance()->CreateParameter(uri);
357 AliCDBId id(path, runnum, runnum, version, sversion);
b9bc0f82 358
8c30cb69 359 return ApplyDisplacements(param, id);
b9bc0f82 360
361}
362
363//_____________________________________________________________________________
85a5290f 364void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
365{
366// add a file with background events for merging
367
368 TObjString* fileNameStr = new TObjString(fileName);
369 fileNameStr->SetUniqueID(nSignalPerBkgrd);
95601147 370 if (!fBkgrdFileNames) fBkgrdFileNames = new TObjArray;
85a5290f 371 fBkgrdFileNames->Add(fileNameStr);
372}
373
374
375//_____________________________________________________________________________
376Bool_t AliSimulation::Run(Int_t nEvents)
377{
378// run the generation, simulation and digitization
379
380 if (nEvents > 0) fNEvents = nEvents;
381
382 // generation and simulation -> hits
383 if (fRunGeneration) {
85a5290f 384 if (!RunSimulation()) if (fStopOnError) return kFALSE;
385 }
386
85a5290f 387 // hits -> summable digits
388 if (!fMakeSDigits.IsNull()) {
389 if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
390 }
391
392 // summable digits -> digits
393 if (!fMakeDigits.IsNull()) {
394 if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
395 if (fStopOnError) return kFALSE;
396 }
397 }
398
399 // hits -> digits
400 if (!fMakeDigitsFromHits.IsNull()) {
95601147 401 if (fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
ac1671c0 402 AliWarning(Form("Merging and direct creation of digits from hits "
403 "was selected for some detectors. "
404 "No merging will be done for the following detectors: %s",
405 fMakeDigitsFromHits.Data()));
85a5290f 406 }
407 if (!RunHitsDigitization(fMakeDigitsFromHits)) {
408 if (fStopOnError) return kFALSE;
409 }
410 }
411
a5a091ce 412 // digits -> trigger
413 if (!fMakeTrigger.IsNull()) {
414 if (!RunTrigger(fMakeTrigger)) {
415 if (fStopOnError) return kFALSE;
416 }
417 }
418
0421c3d1 419 // digits -> raw data
420 if (!fWriteRawData.IsNull()) {
06c7e392 421 if (!WriteRawData(fWriteRawData, fRawDataFileName,
422 fDeleteIntermediateFiles)) {
0421c3d1 423 if (fStopOnError) return kFALSE;
424 }
425 }
426
85a5290f 427 return kTRUE;
428}
429
430//_____________________________________________________________________________
a5a091ce 431Bool_t AliSimulation::RunTrigger(const char* descriptors)
432{
433 // run the trigger
434
435 TStopwatch stopwatch;
436 stopwatch.Start();
437
438 AliRunLoader* runLoader = LoadRun("READ");
439 if (!runLoader) return kFALSE;
440 TString des = descriptors;
441 // Load Descriptors
442 AliCentralTrigger* aCTP = new AliCentralTrigger( des );
443
444 // digits -> trigger
445 if( !aCTP->RunTrigger( runLoader ) ) {
446 if (fStopOnError) {
447 delete aCTP;
448 return kFALSE;
449 }
450 }
451
452/*
453 // Process each event
454 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
455 AliInfo(Form("processing event %d", iEvent));
456 runLoader->GetEvent(iEvent);
457
458 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
459 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
460 AliModule* det = (AliModule*) detArray->At(iDet);
461 if (!det || !det->IsActive()) continue;
462 if (IsSelected(det->GetName(), detStr)) {
463 AliInfo(Form("triggering from digits for %s", det->GetName()));
464
465 // AliLoader* loader = fLoader[iDet];
466 // loader->LoadDigits("read");
467 // TTree* digitsTree = loader->TreeD();
468 // det->Trigger( digitsTree );
469 // or
470 AliTriggerDetector* tdet = det->CreateTriggerDetector();
471 TObjArray* detInp = dtrg->GetTriggerInputs();
472 for( Int_t i=0; i<detInp->GetEntriesFast(); i++ )
473 fInputs.AddLast( detInp->At(i) );
474
475 AliInfo(Form("Execution time for %s: R:%.2fs C:%.2fs",
476 det->GetName(),stopwatchDet.RealTime(),stopwatchDet.CpuTime()));
477 }
478 }
479
480 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
481 AliError(Form("the following detectors were not found: %s",
482 detStr.Data()));
483 if (fStopOnError) {
484 delete centralTP;
485 return kFALSE;
486 }
487 }
488
489 // Check trigger conditions
490 centralTP->TriggerConditions();
491
492 // Write trigger ????
493 centralTP->Write();
494
495 } */
496
497 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
498 stopwatch.RealTime(),stopwatch.CpuTime()));
499
500 delete aCTP;
501 delete runLoader;
502
503 return kTRUE;
504}
505
506
507
508//_____________________________________________________________________________
95601147 509Bool_t AliSimulation::RunSimulation(Int_t nEvents)
85a5290f 510{
511// run the generation and simulation
512
4df28b43 513 TStopwatch stopwatch;
514 stopwatch.Start();
515
95601147 516 if (!gAlice) {
ac1671c0 517 AliError("no gAlice object. Restart aliroot and try again.");
95601147 518 return kFALSE;
519 }
520 if (gAlice->Modules()->GetEntries() > 0) {
ac1671c0 521 AliError("gAlice was already run. Restart aliroot and try again.");
95601147 522 return kFALSE;
523 }
524
ac1671c0 525 AliInfo(Form("initializing gAlice with config file %s",
526 fConfigFileName.Data()));
815c2b38 527 StdoutToAliInfo(StderrToAliError(
528 gAlice->Init(fConfigFileName.Data());
529 ););
95601147 530 AliRunLoader* runLoader = gAlice->GetRunLoader();
531 if (!runLoader) {
ac1671c0 532 AliError(Form("gAlice has no run loader object. "
533 "Check your config file: %s", fConfigFileName.Data()));
85a5290f 534 return kFALSE;
535 }
0421c3d1 536 SetGAliceFile(runLoader->GetFileName());
85a5290f 537
04bae0a0 538 if (!gAlice->Generator()) {
ac1671c0 539 AliError(Form("gAlice has no generator object. "
540 "Check your config file: %s", fConfigFileName.Data()));
04bae0a0 541 return kFALSE;
542 }
05526d44 543 if (nEvents <= 0) nEvents = fNEvents;
04bae0a0 544
545 // get vertex from background file in case of merging
546 if (fUseBkgrdVertex &&
547 fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
05526d44 548 Int_t signalPerBkgrd = GetNSignalPerBkgrd(nEvents);
04bae0a0 549 const char* fileName = ((TObjString*)
550 (fBkgrdFileNames->At(0)))->GetName();
ac1671c0 551 AliInfo(Form("The vertex will be taken from the background "
552 "file %s with nSignalPerBackground = %d",
553 fileName, signalPerBkgrd));
04bae0a0 554 AliVertexGenFile* vtxGen = new AliVertexGenFile(fileName, signalPerBkgrd);
555 gAlice->Generator()->SetVertexGenerator(vtxGen);
556 }
557
85a5290f 558 if (!fRunSimulation) {
85a5290f 559 gAlice->Generator()->SetTrackingFlag(0);
560 }
561
18b43626 562 // set the number of events per file for given detectors and data types
563 for (Int_t i = 0; i < fEventsPerFile.GetEntriesFast(); i++) {
564 if (!fEventsPerFile[i]) continue;
565 const char* detName = fEventsPerFile[i]->GetName();
566 const char* typeName = fEventsPerFile[i]->GetTitle();
567 TString loaderName(detName);
568 loaderName += "Loader";
569 AliLoader* loader = runLoader->GetLoader(loaderName);
570 if (!loader) {
ac1671c0 571 AliError(Form("RunSimulation", "no loader for %s found\n"
572 "Number of events per file not set for %s %s",
573 detName, typeName, detName));
18b43626 574 continue;
575 }
576 AliDataLoader* dataLoader =
577 loader->GetDataLoader(typeName);
578 if (!dataLoader) {
ac1671c0 579 AliError(Form("no data loader for %s found\n"
580 "Number of events per file not set for %s %s",
581 typeName, detName, typeName));
18b43626 582 continue;
583 }
584 dataLoader->SetNumberOfEventsPerFile(fEventsPerFile[i]->GetUniqueID());
ac1671c0 585 AliDebug(1, Form("number of events per file set to %d for %s %s",
586 fEventsPerFile[i]->GetUniqueID(), detName, typeName));
18b43626 587 }
588
ac1671c0 589 AliInfo("running gAlice");
815c2b38 590 StdoutToAliInfo(StderrToAliError(
591 gAlice->Run(nEvents);
592 ););
95601147 593
594 delete runLoader;
85a5290f 595
5f8272e1 596 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
597 stopwatch.RealTime(),stopwatch.CpuTime()));
4df28b43 598
85a5290f 599 return kTRUE;
600}
601
602//_____________________________________________________________________________
95601147 603Bool_t AliSimulation::RunSDigitization(const char* detectors)
85a5290f 604{
605// run the digitization and produce summable digits
606
4df28b43 607 TStopwatch stopwatch;
608 stopwatch.Start();
609
95601147 610 AliRunLoader* runLoader = LoadRun();
611 if (!runLoader) return kFALSE;
612
85a5290f 613 TString detStr = detectors;
95601147 614 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 615 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
616 AliModule* det = (AliModule*) detArray->At(iDet);
617 if (!det || !det->IsActive()) continue;
618 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 619 AliInfo(Form("creating summable digits for %s", det->GetName()));
de76655b 620 TStopwatch stopwatchDet;
621 stopwatchDet.Start();
85a5290f 622 det->Hits2SDigits();
5f8272e1 623 AliInfo(Form("Execution time for %s: R:%.2fs C:%.2fs",
624 det->GetName(),stopwatchDet.RealTime(),stopwatchDet.CpuTime()));
85a5290f 625 }
626 }
627
628 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 629 AliError(Form("the following detectors were not found: %s",
630 detStr.Data()));
85a5290f 631 if (fStopOnError) return kFALSE;
632 }
633
95601147 634 delete runLoader;
635
5f8272e1 636 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
637 stopwatch.RealTime(),stopwatch.CpuTime()));
4df28b43 638
85a5290f 639 return kTRUE;
640}
641
642
643//_____________________________________________________________________________
95601147 644Bool_t AliSimulation::RunDigitization(const char* detectors,
645 const char* excludeDetectors)
85a5290f 646{
647// run the digitization and produce digits from sdigits
648
4df28b43 649 TStopwatch stopwatch;
650 stopwatch.Start();
651
95601147 652 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
653 if (gAlice) delete gAlice;
654 gAlice = NULL;
655
656 Int_t nStreams = 1;
05526d44 657 if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
658 Int_t signalPerBkgrd = GetNSignalPerBkgrd();
85a5290f 659 AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
660 manager->SetInputStream(0, fGAliceFileName.Data());
661 for (Int_t iStream = 1; iStream < nStreams; iStream++) {
662 const char* fileName = ((TObjString*)
663 (fBkgrdFileNames->At(iStream-1)))->GetName();
664 manager->SetInputStream(iStream, fileName);
665 }
666
667 TString detStr = detectors;
668 TString detExcl = excludeDetectors;
95601147 669 manager->GetInputStream(0)->ImportgAlice();
670 AliRunLoader* runLoader =
671 AliRunLoader::GetRunLoader(manager->GetInputStream(0)->GetFolderName());
672 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 673 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
674 AliModule* det = (AliModule*) detArray->At(iDet);
675 if (!det || !det->IsActive()) continue;
676 if (IsSelected(det->GetName(), detStr) &&
677 !IsSelected(det->GetName(), detExcl)) {
4df28b43 678 AliDigitizer* digitizer = det->CreateDigitizer(manager);
679 if (!digitizer) {
ac1671c0 680 AliError(Form("no digitizer for %s", det->GetName()));
85a5290f 681 if (fStopOnError) return kFALSE;
4df28b43 682 } else {
683 digitizer->SetRegionOfInterest(fRegionOfInterest);
85a5290f 684 }
685 }
686 }
687
688 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 689 AliError(Form("the following detectors were not found: %s",
690 detStr.Data()));
85a5290f 691 if (fStopOnError) return kFALSE;
692 }
693
694 if (!manager->GetListOfTasks()->IsEmpty()) {
ac1671c0 695 AliInfo("executing digitization");
85a5290f 696 manager->Exec("");
697 }
95601147 698
85a5290f 699 delete manager;
700
5f8272e1 701 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
702 stopwatch.RealTime(),stopwatch.CpuTime()));
703
85a5290f 704 return kTRUE;
705}
706
707//_____________________________________________________________________________
95601147 708Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
85a5290f 709{
710// run the digitization and produce digits from hits
711
4df28b43 712 TStopwatch stopwatch;
713 stopwatch.Start();
714
591f51bb 715 AliRunLoader* runLoader = LoadRun("READ");
95601147 716 if (!runLoader) return kFALSE;
717
85a5290f 718 TString detStr = detectors;
95601147 719 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 720 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
721 AliModule* det = (AliModule*) detArray->At(iDet);
722 if (!det || !det->IsActive()) continue;
723 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 724 AliInfo(Form("creating digits from hits for %s", det->GetName()));
85a5290f 725 det->Hits2Digits();
726 }
727 }
728
729 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 730 AliError(Form("the following detectors were not found: %s",
731 detStr.Data()));
85a5290f 732 if (fStopOnError) return kFALSE;
733 }
734
b17cac0f 735 delete runLoader;
591f51bb 736 //PH Temporary fix to avoid interference with the PHOS loder/getter
737 //PH The problem has to be solved in more general way 09/06/05
95601147 738
5f8272e1 739 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
740 stopwatch.RealTime(),stopwatch.CpuTime()));
4df28b43 741
85a5290f 742 return kTRUE;
743}
744
0421c3d1 745//_____________________________________________________________________________
06c7e392 746Bool_t AliSimulation::WriteRawData(const char* detectors,
747 const char* fileName,
748 Bool_t deleteIntermediateFiles)
0421c3d1 749{
750// convert the digits to raw data
06c7e392 751// First DDL raw data files for the given detectors are created.
752// If a file name is given, the DDL files are then converted to a DATE file.
753// If deleteIntermediateFiles is true, the DDL raw files are deleted
754// afterwards.
755// If the file name has the extension ".root", the DATE file is converted
756// to a root file.
757// If deleteIntermediateFiles is true, the DATE file is deleted afterwards.
0421c3d1 758
759 TStopwatch stopwatch;
760 stopwatch.Start();
761
06c7e392 762 if (!WriteRawFiles(detectors)) {
763 if (fStopOnError) return kFALSE;
764 }
765
766 TString dateFileName(fileName);
767 if (!dateFileName.IsNull()) {
768 Bool_t rootOutput = dateFileName.EndsWith(".root");
769 if (rootOutput) dateFileName += ".date";
770 if (!ConvertRawFilesToDate(dateFileName)) {
771 if (fStopOnError) return kFALSE;
772 }
773 if (deleteIntermediateFiles) {
774 AliRunLoader* runLoader = LoadRun("READ");
775 if (runLoader) for (Int_t iEvent = 0;
776 iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
777 char command[256];
778 sprintf(command, "rm -r raw%d", iEvent);
779 gSystem->Exec(command);
780 }
781 }
782
783 if (rootOutput) {
784 if (!ConvertDateToRoot(dateFileName, fileName)) {
785 if (fStopOnError) return kFALSE;
786 }
787 if (deleteIntermediateFiles) {
788 gSystem->Unlink(dateFileName);
789 }
790 }
791 }
792
5f8272e1 793 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
794 stopwatch.RealTime(),stopwatch.CpuTime()));
06c7e392 795
796 return kTRUE;
797}
798
799//_____________________________________________________________________________
800Bool_t AliSimulation::WriteRawFiles(const char* detectors)
801{
802// convert the digits to raw data DDL files
803
804 AliRunLoader* runLoader = LoadRun("READ");
0421c3d1 805 if (!runLoader) return kFALSE;
806
06c7e392 807 // write raw data to DDL files
0421c3d1 808 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
ac1671c0 809 AliInfo(Form("processing event %d", iEvent));
0421c3d1 810 runLoader->GetEvent(iEvent);
811 TString baseDir = gSystem->WorkingDirectory();
812 char dirName[256];
813 sprintf(dirName, "raw%d", iEvent);
814 gSystem->MakeDirectory(dirName);
815 if (!gSystem->ChangeDirectory(dirName)) {
ac1671c0 816 AliError(Form("couldn't change to directory %s", dirName));
0421c3d1 817 if (fStopOnError) return kFALSE; else continue;
818 }
819
820 TString detStr = detectors;
821 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
822 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
823 AliModule* det = (AliModule*) detArray->At(iDet);
824 if (!det || !det->IsActive()) continue;
825 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 826 AliInfo(Form("creating raw data from digits for %s", det->GetName()));
0421c3d1 827 det->Digits2Raw();
828 }
829 }
830
831 gSystem->ChangeDirectory(baseDir);
832 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 833 AliError(Form("the following detectors were not found: %s",
834 detStr.Data()));
0421c3d1 835 if (fStopOnError) return kFALSE;
836 }
837 }
838
839 delete runLoader;
06c7e392 840 return kTRUE;
841}
0421c3d1 842
06c7e392 843//_____________________________________________________________________________
844Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName)
845{
846// convert raw data DDL files to a DATE file with the program "dateStream"
847
06c7e392 848 char* path = gSystem->Which(gSystem->Getenv("PATH"), "dateStream");
849 if (!path) {
ac1671c0 850 AliError("the program dateStream was not found");
06c7e392 851 if (fStopOnError) return kFALSE;
852 } else {
853 delete[] path;
854 }
0421c3d1 855
06c7e392 856 AliRunLoader* runLoader = LoadRun("READ");
857 if (!runLoader) return kFALSE;
858
ac1671c0 859 AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
06c7e392 860 char command[256];
3c166bf6 861 sprintf(command, "dateStream -o %s -# %d -C",
06c7e392 862 dateFileName, runLoader->GetNumberOfEvents());
863 FILE* pipe = gSystem->OpenPipe(command, "w");
864
865 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
866 fprintf(pipe, "GDC\n");
867 Float_t ldc = 0;
868 Int_t prevLDC = -1;
869
870 // loop over detectors and DDLs
871 for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
872 for (Int_t iDDL = 0; iDDL < kDetectorDDLs[iDet]; iDDL++) {
873
874 Int_t ddlID = 0x100*iDet + iDDL;
875 Int_t ldcID = Int_t(ldc + 0.0001);
876 ldc += kDetectorLDCs[iDet] / kDetectorDDLs[iDet];
877
878 char rawFileName[256];
879 sprintf(rawFileName, "raw%d/%s_%d.ddl",
880 iEvent, kDetectors[iDet], ddlID);
881
882 // check existence and size of raw data file
883 FILE* file = fopen(rawFileName, "rb");
884 if (!file) continue;
885 fseek(file, 0, SEEK_END);
886 unsigned long size = ftell(file);
aa04f81b 887 fclose(file);
888 if (!size) continue;
06c7e392 889
890 if (ldcID != prevLDC) {
891 fprintf(pipe, " LDC Id %d\n", ldcID);
892 prevLDC = ldcID;
893 }
894 fprintf(pipe, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
895 }
896 }
897 }
898
899 Int_t result = gSystem->ClosePipe(pipe);
900
901 delete runLoader;
902 return (result == 0);
903}
904
905//_____________________________________________________________________________
906Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
907 const char* rootFileName)
908{
909// convert a DATE file to a root file with the program "alimdc"
910
911 // ALIMDC setup
912 const Int_t kDBSize = 1000000000;
c83eb8f4 913 const Int_t kTagDBSize = 1000000000;
06c7e392 914 const Bool_t kFilter = kFALSE;
915 const Int_t kCompression = 1;
916
917 char* path = gSystem->Which(gSystem->Getenv("PATH"), "alimdc");
918 if (!path) {
ac1671c0 919 AliError("the program alimdc was not found");
06c7e392 920 if (fStopOnError) return kFALSE;
921 } else {
922 delete[] path;
923 }
924
ac1671c0 925 AliInfo(Form("converting DATE file %s to root file %s",
926 dateFileName, rootFileName));
06c7e392 927
928 gSystem->Exec("rm -rf /tmp/mdc1");
929 gSystem->Exec("rm -rf /tmp/mdc2");
c83eb8f4 930
931 gSystem->Exec("mkdir /tmp/mdc1");
932 gSystem->Exec("mkdir /tmp/mdc2");
933
06c7e392 934 char command[256];
c83eb8f4 935 sprintf(command, "alimdc %d %d %d %d %s",
936 kDBSize, kTagDBSize, kFilter, kCompression, dateFileName);
06c7e392 937 Int_t result = gSystem->Exec(command);
938 sprintf(command, "mv /tmp/mdc1/*.root %s", rootFileName);
939 gSystem->Exec(command);
940 gSystem->Exec("rm -rf /tmp/mdc1");
941 gSystem->Exec("rm -rf /tmp/mdc2");
942
943 return (result == 0);
0421c3d1 944}
945
85a5290f 946
947//_____________________________________________________________________________
06c7e392 948AliRunLoader* AliSimulation::LoadRun(const char* mode) const
95601147 949{
950// delete existing run loaders, open a new one and load gAlice
951
952 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
953 AliRunLoader* runLoader =
954 AliRunLoader::Open(fGAliceFileName.Data(),
06c7e392 955 AliConfig::GetDefaultEventFolderName(), mode);
95601147 956 if (!runLoader) {
ac1671c0 957 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
95601147 958 return NULL;
959 }
960 runLoader->LoadgAlice();
961 gAlice = runLoader->GetAliRun();
962 if (!gAlice) {
ac1671c0 963 AliError(Form("no gAlice object found in file %s",
964 fGAliceFileName.Data()));
95601147 965 return NULL;
966 }
967 return runLoader;
968}
969
970//_____________________________________________________________________________
05526d44 971Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
972{
973// get or calculate the number of signal events per background event
974
975 if (!fBkgrdFileNames) return 1;
976 Int_t nBkgrdFiles = fBkgrdFileNames->GetEntriesFast();
977 if (nBkgrdFiles == 0) return 1;
978
979 // get the number of signal events
980 if (nEvents <= 0) {
981 AliRunLoader* runLoader =
982 AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
983 if (!runLoader) return 1;
984 nEvents = runLoader->GetNumberOfEvents();
985 delete runLoader;
986 }
987
988 Int_t result = 0;
989 for (Int_t iBkgrdFile = 0; iBkgrdFile < nBkgrdFiles; iBkgrdFile++) {
990 // get the number of background events
991 const char* fileName = ((TObjString*)
992 (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
993 AliRunLoader* runLoader =
994 AliRunLoader::Open(fileName, "BKGRD");
995 if (!runLoader) continue;
996 Int_t nBkgrdEvents = runLoader->GetNumberOfEvents();
997 delete runLoader;
998
999 // get or calculate the number of signal per background events
1000 Int_t nSignalPerBkgrd = fBkgrdFileNames->At(iBkgrdFile)->GetUniqueID();
1001 if (nSignalPerBkgrd <= 0) {
1002 nSignalPerBkgrd = (nEvents-1) / nBkgrdEvents + 1;
1003 } else if (result && (result != nSignalPerBkgrd)) {
ac1671c0 1004 AliInfo(Form("the number of signal events per background event "
1005 "will be changed from %d to %d for stream %d",
1006 nSignalPerBkgrd, result, iBkgrdFile+1));
05526d44 1007 nSignalPerBkgrd = result;
1008 }
1009
1010 if (!result) result = nSignalPerBkgrd;
1011 if (nSignalPerBkgrd * nBkgrdEvents < nEvents) {
ac1671c0 1012 AliWarning(Form("not enough background events (%d) for %d signal events "
1013 "using %d signal per background events for stream %d",
1014 nBkgrdEvents, nEvents, nSignalPerBkgrd, iBkgrdFile+1));
05526d44 1015 }
1016 }
1017
1018 return result;
1019}
1020
1021//_____________________________________________________________________________
85a5290f 1022Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
1023{
1024// check whether detName is contained in detectors
1025// if yes, it is removed from detectors
1026
1027 // check if all detectors are selected
1028 if ((detectors.CompareTo("ALL") == 0) ||
1029 detectors.BeginsWith("ALL ") ||
1030 detectors.EndsWith(" ALL") ||
1031 detectors.Contains(" ALL ")) {
1032 detectors = "ALL";
1033 return kTRUE;
1034 }
1035
1036 // search for the given detector
1037 Bool_t result = kFALSE;
1038 if ((detectors.CompareTo(detName) == 0) ||
1039 detectors.BeginsWith(detName+" ") ||
1040 detectors.EndsWith(" "+detName) ||
1041 detectors.Contains(" "+detName+" ")) {
1042 detectors.ReplaceAll(detName, "");
1043 result = kTRUE;
1044 }
1045
1046 // clean up the detectors string
1047 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
1048 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
1049 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
1050
1051 return result;
1052}