Seperate process tags for mb, non-diff, single and double-diffraction.
[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>
85a5290f 109#include <TObjString.h>
fd46e2d2 110#include <TStopwatch.h>
af7ba10c 111#include <TSystem.h>
bfbd5665 112#include <TFile.h>
85a5290f 113
e8d02863 114#include "AliCDBStorage.h"
115#include "AliCDBEntry.h"
116#include "AliCDBManager.h"
117#include "AliAlignObj.h"
118#include "AliCentralTrigger.h"
362c9d61 119#include "AliDAQ.h"
af7ba10c 120#include "AliDigitizer.h"
121#include "AliGenerator.h"
e8d02863 122#include "AliLog.h"
af7ba10c 123#include "AliModule.h"
124#include "AliRun.h"
125#include "AliRunDigitizer.h"
126#include "AliRunLoader.h"
127#include "AliSimulation.h"
128#include "AliVertexGenFile.h"
a5a091ce 129#include "AliCentralTrigger.h"
007155cc 130#include "AliCTPRawData.h"
ca30848f 131#include "AliRawReaderFile.h"
132#include "AliESD.h"
133#include "AliHeader.h"
134#include "AliGenEventHeader.h"
85a5290f 135
136ClassImp(AliSimulation)
137
138
139//_____________________________________________________________________________
024cf675 140AliSimulation::AliSimulation(const char* configFileName, const char* cdbUri,
95601147 141 const char* name, const char* title) :
142 TNamed(name, title),
143
144 fRunGeneration(kTRUE),
145 fRunSimulation(kTRUE),
024cf675 146 fLoadAlignFromCDB(kTRUE),
147 fLoadAlignData("ALL"),
95601147 148 fMakeSDigits("ALL"),
149 fMakeDigits("ALL"),
a5a091ce 150 fMakeTrigger(""),
95601147 151 fMakeDigitsFromHits(""),
0421c3d1 152 fWriteRawData(""),
06c7e392 153 fRawDataFileName(""),
154 fDeleteIntermediateFiles(kFALSE),
95601147 155 fStopOnError(kFALSE),
156
157 fNEvents(1),
158 fConfigFileName(configFileName),
159 fGAliceFileName("galice.root"),
18b43626 160 fEventsPerFile(),
95601147 161 fBkgrdFileNames(NULL),
995ad051 162 fAlignObjArray(NULL),
04bae0a0 163 fUseBkgrdVertex(kTRUE),
024cf675 164 fRegionOfInterest(kFALSE),
ec92bee0 165 fCDBUri(cdbUri),
ca30848f 166 fSpecCDBUri(),
167 fEmbeddingFlag(kFALSE)
85a5290f 168{
169// create simulation object with default parameters
170
0421c3d1 171 SetGAliceFile("galice.root");
85a5290f 172}
173
174//_____________________________________________________________________________
175AliSimulation::AliSimulation(const AliSimulation& sim) :
95601147 176 TNamed(sim),
177
178 fRunGeneration(sim.fRunGeneration),
179 fRunSimulation(sim.fRunSimulation),
024cf675 180 fLoadAlignFromCDB(sim.fLoadAlignFromCDB),
181 fLoadAlignData(sim.fLoadAlignData),
95601147 182 fMakeSDigits(sim.fMakeSDigits),
183 fMakeDigits(sim.fMakeDigits),
a5a091ce 184 fMakeTrigger(sim.fMakeTrigger),
95601147 185 fMakeDigitsFromHits(sim.fMakeDigitsFromHits),
0421c3d1 186 fWriteRawData(sim.fWriteRawData),
06c7e392 187 fRawDataFileName(""),
188 fDeleteIntermediateFiles(kFALSE),
95601147 189 fStopOnError(sim.fStopOnError),
190
191 fNEvents(sim.fNEvents),
192 fConfigFileName(sim.fConfigFileName),
193 fGAliceFileName(sim.fGAliceFileName),
18b43626 194 fEventsPerFile(),
95601147 195 fBkgrdFileNames(NULL),
995ad051 196 fAlignObjArray(NULL),
04bae0a0 197 fUseBkgrdVertex(sim.fUseBkgrdVertex),
024cf675 198 fRegionOfInterest(sim.fRegionOfInterest),
ec92bee0 199 fCDBUri(sim.fCDBUri),
ca30848f 200 fSpecCDBUri(),
201 fEmbeddingFlag(sim.fEmbeddingFlag)
85a5290f 202{
203// copy constructor
204
18b43626 205 for (Int_t i = 0; i < sim.fEventsPerFile.GetEntriesFast(); i++) {
206 if (!sim.fEventsPerFile[i]) continue;
207 fEventsPerFile.Add(sim.fEventsPerFile[i]->Clone());
208 }
209
85a5290f 210 fBkgrdFileNames = new TObjArray;
211 for (Int_t i = 0; i < sim.fBkgrdFileNames->GetEntriesFast(); i++) {
212 if (!sim.fBkgrdFileNames->At(i)) continue;
213 fBkgrdFileNames->Add(sim.fBkgrdFileNames->At(i)->Clone());
214 }
ec92bee0 215
216 for (Int_t i = 0; i < sim.fSpecCDBUri.GetEntriesFast(); i++) {
217 if (sim.fSpecCDBUri[i]) fSpecCDBUri.Add(sim.fSpecCDBUri[i]->Clone());
218 }
219
85a5290f 220}
221
222//_____________________________________________________________________________
223AliSimulation& AliSimulation::operator = (const AliSimulation& sim)
224{
225// assignment operator
226
227 this->~AliSimulation();
228 new(this) AliSimulation(sim);
229 return *this;
230}
231
232//_____________________________________________________________________________
233AliSimulation::~AliSimulation()
234{
235// clean up
236
18b43626 237 fEventsPerFile.Delete();
024cf675 238// if(fAlignObjArray) fAlignObjArray->Delete(); // fAlignObjArray->RemoveAll() ???
239// delete fAlignObjArray; fAlignObjArray=0;
18b43626 240
95601147 241 if (fBkgrdFileNames) {
242 fBkgrdFileNames->Delete();
243 delete fBkgrdFileNames;
244 }
ec92bee0 245
246 fSpecCDBUri.Delete();
85a5290f 247}
248
249
250//_____________________________________________________________________________
251void AliSimulation::SetNumberOfEvents(Int_t nEvents)
252{
253// set the number of events for one run
254
255 fNEvents = nEvents;
256}
257
258//_____________________________________________________________________________
ec92bee0 259void AliSimulation::InitCDBStorage()
024cf675 260{
261// activate a default CDB storage
262// First check if we have any CDB storage set, because it is used
263// to retrieve the calibration and alignment constants
264
265 AliCDBManager* man = AliCDBManager::Instance();
ec92bee0 266 if (man->IsDefaultStorageSet())
024cf675 267 {
ec92bee0 268 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
269 AliWarning("Default CDB storage has been already set !");
270 AliWarning(Form("Ignoring the default storage declared in AliReconstruction: %s",fCDBUri.Data()));
271 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
272 fCDBUri = "";
273 }
274 else {
275 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
276 AliWarning(Form("Default CDB storage is set to: %s",fCDBUri.Data()));
277 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
278 man->SetDefaultStorage(fCDBUri);
279 }
280
281 // Now activate the detector specific CDB storage locations
282 for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
283 TObject* obj = fSpecCDBUri[i];
284 if (!obj) continue;
285 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
286 AliWarning(Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
287 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
c3a7b59a 288 man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
ec92bee0 289 }
727d0766 290 man->Print();
024cf675 291}
292
293//_____________________________________________________________________________
294void AliSimulation::SetDefaultStorage(const char* uri) {
ec92bee0 295// Store the desired default CDB storage location
296// Activate it later within the Run() method
024cf675 297
ec92bee0 298 fCDBUri = uri;
024cf675 299
300}
301
302//_____________________________________________________________________________
c3a7b59a 303void AliSimulation::SetSpecificStorage(const char* calibType, const char* uri) {
ec92bee0 304// Store a detector-specific CDB storage location
305// Activate it later within the Run() method
024cf675 306
c3a7b59a 307 AliCDBPath aPath(calibType);
308 if(!aPath.IsValid()){
309 AliError(Form("Not a valid path: %s", calibType));
310 return;
311 }
312
313 TObject* obj = fSpecCDBUri.FindObject(calibType);
ec92bee0 314 if (obj) fSpecCDBUri.Remove(obj);
c3a7b59a 315 fSpecCDBUri.Add(new TNamed(calibType, uri));
024cf675 316
317}
318
319//_____________________________________________________________________________
85a5290f 320void AliSimulation::SetConfigFile(const char* fileName)
321{
322// set the name of the config file
323
324 fConfigFileName = fileName;
325}
326
327//_____________________________________________________________________________
0421c3d1 328void AliSimulation::SetGAliceFile(const char* fileName)
329{
330// set the name of the galice file
331// the path is converted to an absolute one if it is relative
332
333 fGAliceFileName = fileName;
334 if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
335 char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
336 fGAliceFileName);
337 fGAliceFileName = absFileName;
338 delete[] absFileName;
339 }
ac1671c0 340
341 AliDebug(2, Form("galice file name set to %s", fileName));
0421c3d1 342}
343
344//_____________________________________________________________________________
18b43626 345void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
346 Int_t nEvents)
347{
348// set the number of events per file for the given detector and data type
349// ("Hits", "Summable Digits", "Digits", "Reconstructed Points" or "Tracks")
350
351 TNamed* obj = new TNamed(detector, type);
352 obj->SetUniqueID(nEvents);
353 fEventsPerFile.Add(obj);
354}
355
356//_____________________________________________________________________________
6bae477a 357Bool_t AliSimulation::ApplyAlignObjsToGeom(TObjArray* alObjArray)
358{
359 // Read collection of alignment objects (AliAlignObj derived) saved
360 // in the TClonesArray ClArrayName and apply them to the geometry
361 // manager singleton.
362 //
363 alObjArray->Sort();
364 Int_t nvols = alObjArray->GetEntriesFast();
365
b5874ce3 366 Bool_t flag = kTRUE;
367
6bae477a 368 for(Int_t j=0; j<nvols; j++)
369 {
370 AliAlignObj* alobj = (AliAlignObj*) alObjArray->UncheckedAt(j);
b5874ce3 371 if (alobj->ApplyToGeometry() == kFALSE) flag = kFALSE;
6bae477a 372 }
373
374 if (AliDebugLevelClass() >= 1) {
da5b935b 375 gGeoManager->GetTopNode()->CheckOverlaps(1);
6bae477a 376 TObjArray* ovexlist = gGeoManager->GetListOfOverlaps();
377 if(ovexlist->GetEntriesFast()){
378 AliError("The application of alignment objects to the geometry caused huge overlaps/extrusions!");
379 }
380 }
381
b5874ce3 382 return flag;
6bae477a 383
384}
385
386//_____________________________________________________________________________
178a256c 387Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* fileName, const char* clArrayName)
b9bc0f82 388{
389 // read collection of alignment objects (AliAlignObj derived) saved
390 // in the TClonesArray ClArrayName in the file fileName and apply
391 // them to the TGeo geometry passed as argument
392 //
393
394 TFile* inFile = TFile::Open(fileName,"READ");
395 if (!inFile || !inFile->IsOpen()) {
396 AliErrorClass(Form("Could not open file %s !",fileName));
397 return kFALSE;
398 }
399
995ad051 400 TClonesArray* alObjArray = ((TClonesArray*) inFile->Get(clArrayName));
8c30cb69 401 inFile->Close();
995ad051 402 if (!alObjArray) {
403 AliErrorClass(Form("Could not get array (%s) from file (%s) !",clArrayName,fileName));
404 return kFALSE;
405 }
8c30cb69 406
6bae477a 407 return ApplyAlignObjsToGeom(alObjArray);
b9bc0f82 408
409}
410
411//_____________________________________________________________________________
178a256c 412Bool_t AliSimulation::ApplyAlignObjsToGeom(AliCDBParam* param, AliCDBId& Id)
b9bc0f82 413{
414 // read collection of alignment objects (AliAlignObj derived) saved
415 // in the TClonesArray ClArrayName in the AliCDBEntry identified by
416 // param (to get the AliCDBStorage) and Id; apply the alignment objects
417 // to the TGeo geometry passed as argument
418 //
419
420 AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage(param);
421 AliCDBEntry* entry = storage->Get(Id);
422 TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
423
6bae477a 424 return ApplyAlignObjsToGeom(AlObjArray);
b9bc0f82 425
426}
427
428//_____________________________________________________________________________
178a256c 429Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* uri, const char* path, Int_t runnum, Int_t version, Int_t sversion)
b9bc0f82 430{
431 // read collection of alignment objects (AliAlignObj derived) saved
432 // in the TClonesArray ClArrayName in the AliCDBEntry identified by
433 // param (to get the AliCDBStorage) and Id; apply the alignment objects
434 // to the TGeo geometry passed as argument
435 //
436
437 AliCDBParam* param = AliCDBManager::Instance()->CreateParameter(uri);
438 AliCDBId id(path, runnum, runnum, version, sversion);
b9bc0f82 439
178a256c 440 return ApplyAlignObjsToGeom(param, id);
b9bc0f82 441
442}
443
444//_____________________________________________________________________________
024cf675 445Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* detName, Int_t runnum, Int_t version, Int_t sversion)
446{
447 // read collection of alignment objects (AliAlignObj derived) saved
448 // in the TClonesArray ClArrayName in the AliCDBEntry identified by
449 // param (to get the AliCDBStorage) and Id; apply the alignment objects
450 // to the TGeo geometry passed as argument
451 //
452
024cf675 453 AliCDBPath path(detName,"Align","Data");
454 AliCDBEntry* entry = AliCDBManager::Instance()->Get(path.GetPath(),runnum,version,sversion);
455
456 if(!entry) return kFALSE;
457 TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
458
6bae477a 459 return ApplyAlignObjsToGeom(AlObjArray);
024cf675 460}
461
462//_____________________________________________________________________________
463Bool_t AliSimulation::SetAlignObjArraySingleDet(const char* detName)
464{
465 // Fills array of single detector's alignable objects from CDB
466
467 AliDebug(2, Form("Loading alignment data for detector: %s",detName));
468
469 AliCDBEntry *entry;
470
471 AliCDBPath path(detName,"Align","Data");
472
473 entry=AliCDBManager::Instance()->Get(path.GetPath());
474 if(!entry){
475 AliDebug(2,Form("Couldn't load alignment data for detector %s",detName));
476 return kFALSE;
477 }
478 entry->SetOwner(1);
479 TClonesArray *alignArray = (TClonesArray*) entry->GetObject();
480 alignArray->SetOwner(0);
481 AliDebug(2,Form("Found %d alignment objects for %s",
482 alignArray->GetEntries(),detName));
483
484 AliAlignObj *alignObj=0;
485 TIter iter(alignArray);
486
487 // loop over align objects in detector
488 while( ( alignObj=(AliAlignObj *) iter.Next() ) ){
489 fAlignObjArray->Add(alignObj);
490 }
491 // delete entry --- Don't delete, it is cached!
492
493 AliDebug(2, Form("fAlignObjArray entries: %d",fAlignObjArray->GetEntries() ));
494 return kTRUE;
495
496}
497
498//_____________________________________________________________________________
6bae477a 499Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
500{
501 // Read the alignment objects from CDB.
502 // Each detector is supposed to have the
503 // alignment objects in DET/Align/Data CDB path.
504 // All the detector objects are then collected,
505 // sorted by geometry level (starting from ALIC) and
506 // then applied to the TGeo geometry.
507 // Finally an overlaps check is performed.
508
509 Bool_t delRunLoader = kFALSE;
510 if (!runLoader) {
511 runLoader = LoadRun("READ");
512 if (!runLoader) return kFALSE;
513 delRunLoader = kTRUE;
514 }
515
516 // Load alignment data from CDB and fill fAlignObjArray
517 if(fLoadAlignFromCDB){
518 if(!fAlignObjArray) fAlignObjArray = new TObjArray();
519
520 //fAlignObjArray->RemoveAll();
521 fAlignObjArray->Clear();
522 fAlignObjArray->SetOwner(0);
523
524 TString detStr = fLoadAlignData;
525 TString dataNotLoaded="";
526 TString dataLoaded="";
527
528 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
529 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
530 AliModule* det = (AliModule*) detArray->At(iDet);
7250c343 531 if (!det || !det->IsActive()) continue;
6bae477a 532 if (IsSelected(det->GetName(), detStr)) {
533 if(!SetAlignObjArraySingleDet(det->GetName())){
534 dataNotLoaded += det->GetName();
535 dataNotLoaded += " ";
536 } else {
537 dataLoaded += det->GetName();
538 dataLoaded += " ";
539 }
540 }
541 } // end loop over detectors
542
543 if ((detStr.CompareTo("ALL") == 0)) detStr = "";
544 dataNotLoaded += detStr;
7250c343 545 if(!dataLoaded.IsNull()) AliInfo(Form("Alignment data loaded for: %s",
6bae477a 546 dataLoaded.Data()));
7250c343 547 if(!dataNotLoaded.IsNull()) AliInfo(Form("Didn't/couldn't load alignment data for: %s",
6bae477a 548 dataNotLoaded.Data()));
549 } // fLoadAlignFromCDB flag
550
551 // Check if the array with alignment objects was
552 // provided by the user. If yes, apply the objects
553 // to the present TGeo geometry
554 if (fAlignObjArray) {
555 if (gGeoManager && gGeoManager->IsClosed()) {
556 if (ApplyAlignObjsToGeom(fAlignObjArray) == kFALSE) {
b5874ce3 557 AliError("The misalignment of one or more volumes failed!"
558 "Compare the list of simulated detectors and the list of detector alignment data!");
559 if (delRunLoader) delete runLoader;
6bae477a 560 return kFALSE;
561 }
562 }
563 else {
564 AliError("Can't apply the misalignment! gGeoManager doesn't exist or it is still opened!");
b5874ce3 565 if (delRunLoader) delete runLoader;
6bae477a 566 return kFALSE;
567 }
568 }
569
ef7ff1f9 570
571 // Update the internal geometry of modules (ITS needs it)
572 TString detStr = fLoadAlignData;
573 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
574 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
575
576 AliModule* det = (AliModule*) detArray->At(iDet);
577 if (!det || !det->IsActive()) continue;
578 if (IsSelected(det->GetName(), detStr)) {
579 det->UpdateInternalGeometry();
580 }
581 } // end loop over detectors
582
583
6bae477a 584 if (delRunLoader) delete runLoader;
585
586 return kTRUE;
587}
588
589
590//_____________________________________________________________________________
591Bool_t AliSimulation::SetRunNumber()
592{
593 // Set the CDB manager run number
594 // The run number is retrieved from gAlice
595
596 if(AliCDBManager::Instance()->GetRun() < 0) {
597 AliRunLoader* runLoader = LoadRun("READ");
598 if (!runLoader) return kFALSE;
599 else {
600 AliCDBManager::Instance()->SetRun(runLoader->GetAliRun()->GetRunNumber());
601 AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
602 delete runLoader;
603 }
604 }
605 return kTRUE;
606}
607
608//_____________________________________________________________________________
85a5290f 609void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
610{
611// add a file with background events for merging
612
613 TObjString* fileNameStr = new TObjString(fileName);
614 fileNameStr->SetUniqueID(nSignalPerBkgrd);
95601147 615 if (!fBkgrdFileNames) fBkgrdFileNames = new TObjArray;
85a5290f 616 fBkgrdFileNames->Add(fileNameStr);
617}
618
ca30848f 619void AliSimulation::EmbedInto(const char* fileName, Int_t nSignalPerBkgrd)
620{
621// add a file with background events for embeddin
622 MergeWith(fileName, nSignalPerBkgrd);
623 fEmbeddingFlag = kTRUE;
624}
85a5290f 625
626//_____________________________________________________________________________
627Bool_t AliSimulation::Run(Int_t nEvents)
628{
629// run the generation, simulation and digitization
630
ec92bee0 631 InitCDBStorage();
b0314964 632
85a5290f 633 if (nEvents > 0) fNEvents = nEvents;
634
635 // generation and simulation -> hits
636 if (fRunGeneration) {
85a5290f 637 if (!RunSimulation()) if (fStopOnError) return kFALSE;
638 }
639
6bae477a 640 // Set run number in CDBManager (if it is not already set in RunSimulation)
641 if (!SetRunNumber()) if (fStopOnError) return kFALSE;
642
643 // Load and misalign the geometry
644 if (!gGeoManager) {
645 TGeoManager::Import("geometry.root");
646 if (!gGeoManager) if (fStopOnError) return kFALSE;
647 if (!MisalignGeometry()) if (fStopOnError) return kFALSE;
648 }
ca30848f 649
85a5290f 650 // hits -> summable digits
651 if (!fMakeSDigits.IsNull()) {
652 if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
653 }
654
655 // summable digits -> digits
656 if (!fMakeDigits.IsNull()) {
657 if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
658 if (fStopOnError) return kFALSE;
659 }
660 }
661
662 // hits -> digits
663 if (!fMakeDigitsFromHits.IsNull()) {
95601147 664 if (fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
ac1671c0 665 AliWarning(Form("Merging and direct creation of digits from hits "
666 "was selected for some detectors. "
667 "No merging will be done for the following detectors: %s",
668 fMakeDigitsFromHits.Data()));
85a5290f 669 }
670 if (!RunHitsDigitization(fMakeDigitsFromHits)) {
671 if (fStopOnError) return kFALSE;
672 }
673 }
674
a5a091ce 675 // digits -> trigger
087426de 676 if (!RunTrigger(fMakeTrigger)) {
677 if (fStopOnError) return kFALSE;
a5a091ce 678 }
679
0421c3d1 680 // digits -> raw data
681 if (!fWriteRawData.IsNull()) {
06c7e392 682 if (!WriteRawData(fWriteRawData, fRawDataFileName,
683 fDeleteIntermediateFiles)) {
0421c3d1 684 if (fStopOnError) return kFALSE;
685 }
686 }
687
85a5290f 688 return kTRUE;
689}
690
691//_____________________________________________________________________________
a5a091ce 692Bool_t AliSimulation::RunTrigger(const char* descriptors)
693{
694 // run the trigger
695
bacbe0fd 696 TStopwatch stopwatch;
697 stopwatch.Start();
a5a091ce 698
bacbe0fd 699 AliRunLoader* runLoader = LoadRun("READ");
700 if (!runLoader) return kFALSE;
701 TString des = descriptors;
702
087426de 703 if (des.IsNull()) {
704 if (gAlice->GetTriggerDescriptor() != "") {
705 des = gAlice->GetTriggerDescriptor();
706 }
707 else {
708 AliWarning("No trigger descriptor is specified. Skipping the trigger simulation...");
709 return kTRUE;
710 }
711 }
712
bacbe0fd 713 runLoader->MakeTree( "CT" );
714 AliCentralTrigger* aCTP = runLoader->GetTrigger();
a5a091ce 715 // Load Descriptors
bacbe0fd 716 aCTP->LoadDescriptor( des );
a5a091ce 717
718 // digits -> trigger
bacbe0fd 719 if( !aCTP->RunTrigger( runLoader ) ) {
a5a091ce 720 if (fStopOnError) {
bacbe0fd 721 // delete aCTP;
a5a091ce 722 return kFALSE;
723 }
bacbe0fd 724 }
a5a091ce 725
bacbe0fd 726 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
727 stopwatch.RealTime(),stopwatch.CpuTime()));
a5a091ce 728
bacbe0fd 729 delete runLoader;
a5a091ce 730
bacbe0fd 731 return kTRUE;
a5a091ce 732}
733
007155cc 734//_____________________________________________________________________________
735Bool_t AliSimulation::WriteTriggerRawData()
736{
737 // Writes the CTP (trigger) DDL raw data
738 // Details of the format are given in the
739 // trigger TDR - pages 134 and 135.
740 AliCTPRawData writer;
741 writer.RawData();
a5a091ce 742
007155cc 743 return kTRUE;
744}
a5a091ce 745
746//_____________________________________________________________________________
95601147 747Bool_t AliSimulation::RunSimulation(Int_t nEvents)
85a5290f 748{
749// run the generation and simulation
750
4df28b43 751 TStopwatch stopwatch;
752 stopwatch.Start();
753
95601147 754 if (!gAlice) {
ac1671c0 755 AliError("no gAlice object. Restart aliroot and try again.");
95601147 756 return kFALSE;
757 }
758 if (gAlice->Modules()->GetEntries() > 0) {
ac1671c0 759 AliError("gAlice was already run. Restart aliroot and try again.");
95601147 760 return kFALSE;
761 }
762
ac1671c0 763 AliInfo(Form("initializing gAlice with config file %s",
764 fConfigFileName.Data()));
815c2b38 765 StdoutToAliInfo(StderrToAliError(
766 gAlice->Init(fConfigFileName.Data());
767 ););
995ad051 768
087426de 769 // Get the trigger descriptor string
770 // Either from AliSimulation or from
771 // gAlice
772 if (fMakeTrigger.IsNull()) {
773 if (gAlice->GetTriggerDescriptor() != "")
774 fMakeTrigger = gAlice->GetTriggerDescriptor();
775 }
776 else
777 gAlice->SetTriggerDescriptor(fMakeTrigger.Data());
778
6bae477a 779 // Set run number in CDBManager
024cf675 780 AliCDBManager::Instance()->SetRun(gAlice->GetRunNumber());
781 AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
782
783 AliRunLoader* runLoader = gAlice->GetRunLoader();
784 if (!runLoader) {
785 AliError(Form("gAlice has no run loader object. "
786 "Check your config file: %s", fConfigFileName.Data()));
787 return kFALSE;
788 }
789 SetGAliceFile(runLoader->GetFileName());
790
bea6ea16 791 // Export ideal geometry
792 if (gGeoManager) gGeoManager->Export("geometry.root");
024cf675 793
6bae477a 794 // Misalign geometry
e198134a 795// if (!MisalignGeometry(runLoader)) {
796// delete runLoader;
797// return kFALSE;
798// }
799 MisalignGeometry(runLoader);
995ad051 800
6bae477a 801 // Export (mis)aligned geometry
802 if (gGeoManager) gGeoManager->Export("misaligned_geometry.root");
803
024cf675 804// AliRunLoader* runLoader = gAlice->GetRunLoader();
805// if (!runLoader) {
806// AliError(Form("gAlice has no run loader object. "
807// "Check your config file: %s", fConfigFileName.Data()));
808// return kFALSE;
809// }
810// SetGAliceFile(runLoader->GetFileName());
85a5290f 811
04bae0a0 812 if (!gAlice->Generator()) {
ac1671c0 813 AliError(Form("gAlice has no generator object. "
814 "Check your config file: %s", fConfigFileName.Data()));
04bae0a0 815 return kFALSE;
816 }
05526d44 817 if (nEvents <= 0) nEvents = fNEvents;
04bae0a0 818
819 // get vertex from background file in case of merging
820 if (fUseBkgrdVertex &&
821 fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
05526d44 822 Int_t signalPerBkgrd = GetNSignalPerBkgrd(nEvents);
04bae0a0 823 const char* fileName = ((TObjString*)
824 (fBkgrdFileNames->At(0)))->GetName();
ac1671c0 825 AliInfo(Form("The vertex will be taken from the background "
826 "file %s with nSignalPerBackground = %d",
827 fileName, signalPerBkgrd));
04bae0a0 828 AliVertexGenFile* vtxGen = new AliVertexGenFile(fileName, signalPerBkgrd);
829 gAlice->Generator()->SetVertexGenerator(vtxGen);
830 }
831
85a5290f 832 if (!fRunSimulation) {
85a5290f 833 gAlice->Generator()->SetTrackingFlag(0);
834 }
835
18b43626 836 // set the number of events per file for given detectors and data types
837 for (Int_t i = 0; i < fEventsPerFile.GetEntriesFast(); i++) {
838 if (!fEventsPerFile[i]) continue;
839 const char* detName = fEventsPerFile[i]->GetName();
840 const char* typeName = fEventsPerFile[i]->GetTitle();
841 TString loaderName(detName);
842 loaderName += "Loader";
843 AliLoader* loader = runLoader->GetLoader(loaderName);
844 if (!loader) {
ac1671c0 845 AliError(Form("RunSimulation", "no loader for %s found\n"
846 "Number of events per file not set for %s %s",
847 detName, typeName, detName));
18b43626 848 continue;
849 }
850 AliDataLoader* dataLoader =
851 loader->GetDataLoader(typeName);
852 if (!dataLoader) {
ac1671c0 853 AliError(Form("no data loader for %s found\n"
854 "Number of events per file not set for %s %s",
855 typeName, detName, typeName));
18b43626 856 continue;
857 }
858 dataLoader->SetNumberOfEventsPerFile(fEventsPerFile[i]->GetUniqueID());
ac1671c0 859 AliDebug(1, Form("number of events per file set to %d for %s %s",
860 fEventsPerFile[i]->GetUniqueID(), detName, typeName));
18b43626 861 }
862
ac1671c0 863 AliInfo("running gAlice");
815c2b38 864 StdoutToAliInfo(StderrToAliError(
865 gAlice->Run(nEvents);
866 ););
95601147 867
868 delete runLoader;
85a5290f 869
5f8272e1 870 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
871 stopwatch.RealTime(),stopwatch.CpuTime()));
4df28b43 872
85a5290f 873 return kTRUE;
874}
875
876//_____________________________________________________________________________
95601147 877Bool_t AliSimulation::RunSDigitization(const char* detectors)
85a5290f 878{
879// run the digitization and produce summable digits
880
4df28b43 881 TStopwatch stopwatch;
882 stopwatch.Start();
883
95601147 884 AliRunLoader* runLoader = LoadRun();
885 if (!runLoader) return kFALSE;
886
85a5290f 887 TString detStr = detectors;
95601147 888 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 889 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
890 AliModule* det = (AliModule*) detArray->At(iDet);
891 if (!det || !det->IsActive()) continue;
892 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 893 AliInfo(Form("creating summable digits for %s", det->GetName()));
de76655b 894 TStopwatch stopwatchDet;
895 stopwatchDet.Start();
85a5290f 896 det->Hits2SDigits();
5f8272e1 897 AliInfo(Form("Execution time for %s: R:%.2fs C:%.2fs",
898 det->GetName(),stopwatchDet.RealTime(),stopwatchDet.CpuTime()));
85a5290f 899 }
900 }
901
902 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 903 AliError(Form("the following detectors were not found: %s",
904 detStr.Data()));
85a5290f 905 if (fStopOnError) return kFALSE;
906 }
907
95601147 908 delete runLoader;
909
5f8272e1 910 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
911 stopwatch.RealTime(),stopwatch.CpuTime()));
4df28b43 912
85a5290f 913 return kTRUE;
914}
915
916
917//_____________________________________________________________________________
95601147 918Bool_t AliSimulation::RunDigitization(const char* detectors,
919 const char* excludeDetectors)
85a5290f 920{
921// run the digitization and produce digits from sdigits
922
4df28b43 923 TStopwatch stopwatch;
924 stopwatch.Start();
925
95601147 926 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
927 if (gAlice) delete gAlice;
928 gAlice = NULL;
929
930 Int_t nStreams = 1;
05526d44 931 if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
932 Int_t signalPerBkgrd = GetNSignalPerBkgrd();
85a5290f 933 AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
ca30848f 934 // manager->SetEmbeddingFlag(fEmbeddingFlag);
85a5290f 935 manager->SetInputStream(0, fGAliceFileName.Data());
936 for (Int_t iStream = 1; iStream < nStreams; iStream++) {
937 const char* fileName = ((TObjString*)
938 (fBkgrdFileNames->At(iStream-1)))->GetName();
939 manager->SetInputStream(iStream, fileName);
940 }
941
942 TString detStr = detectors;
943 TString detExcl = excludeDetectors;
95601147 944 manager->GetInputStream(0)->ImportgAlice();
945 AliRunLoader* runLoader =
946 AliRunLoader::GetRunLoader(manager->GetInputStream(0)->GetFolderName());
947 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 948 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
949 AliModule* det = (AliModule*) detArray->At(iDet);
950 if (!det || !det->IsActive()) continue;
951 if (IsSelected(det->GetName(), detStr) &&
952 !IsSelected(det->GetName(), detExcl)) {
4df28b43 953 AliDigitizer* digitizer = det->CreateDigitizer(manager);
ca30848f 954
4df28b43 955 if (!digitizer) {
ac1671c0 956 AliError(Form("no digitizer for %s", det->GetName()));
85a5290f 957 if (fStopOnError) return kFALSE;
4df28b43 958 } else {
959 digitizer->SetRegionOfInterest(fRegionOfInterest);
85a5290f 960 }
961 }
962 }
963
964 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 965 AliError(Form("the following detectors were not found: %s",
966 detStr.Data()));
85a5290f 967 if (fStopOnError) return kFALSE;
968 }
969
970 if (!manager->GetListOfTasks()->IsEmpty()) {
ac1671c0 971 AliInfo("executing digitization");
85a5290f 972 manager->Exec("");
973 }
95601147 974
85a5290f 975 delete manager;
976
5f8272e1 977 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
978 stopwatch.RealTime(),stopwatch.CpuTime()));
979
85a5290f 980 return kTRUE;
981}
982
983//_____________________________________________________________________________
95601147 984Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
85a5290f 985{
986// run the digitization and produce digits from hits
987
4df28b43 988 TStopwatch stopwatch;
989 stopwatch.Start();
990
591f51bb 991 AliRunLoader* runLoader = LoadRun("READ");
95601147 992 if (!runLoader) return kFALSE;
993
85a5290f 994 TString detStr = detectors;
95601147 995 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 996 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
997 AliModule* det = (AliModule*) detArray->At(iDet);
998 if (!det || !det->IsActive()) continue;
999 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 1000 AliInfo(Form("creating digits from hits for %s", det->GetName()));
85a5290f 1001 det->Hits2Digits();
1002 }
1003 }
1004
1005 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 1006 AliError(Form("the following detectors were not found: %s",
1007 detStr.Data()));
85a5290f 1008 if (fStopOnError) return kFALSE;
1009 }
1010
b17cac0f 1011 delete runLoader;
591f51bb 1012 //PH Temporary fix to avoid interference with the PHOS loder/getter
1013 //PH The problem has to be solved in more general way 09/06/05
95601147 1014
5f8272e1 1015 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
1016 stopwatch.RealTime(),stopwatch.CpuTime()));
4df28b43 1017
85a5290f 1018 return kTRUE;
1019}
1020
0421c3d1 1021//_____________________________________________________________________________
06c7e392 1022Bool_t AliSimulation::WriteRawData(const char* detectors,
1023 const char* fileName,
1024 Bool_t deleteIntermediateFiles)
0421c3d1 1025{
1026// convert the digits to raw data
06c7e392 1027// First DDL raw data files for the given detectors are created.
1028// If a file name is given, the DDL files are then converted to a DATE file.
1029// If deleteIntermediateFiles is true, the DDL raw files are deleted
1030// afterwards.
1031// If the file name has the extension ".root", the DATE file is converted
1032// to a root file.
1033// If deleteIntermediateFiles is true, the DATE file is deleted afterwards.
0421c3d1 1034
1035 TStopwatch stopwatch;
1036 stopwatch.Start();
1037
06c7e392 1038 if (!WriteRawFiles(detectors)) {
1039 if (fStopOnError) return kFALSE;
1040 }
1041
1042 TString dateFileName(fileName);
1043 if (!dateFileName.IsNull()) {
1044 Bool_t rootOutput = dateFileName.EndsWith(".root");
1045 if (rootOutput) dateFileName += ".date";
1046 if (!ConvertRawFilesToDate(dateFileName)) {
1047 if (fStopOnError) return kFALSE;
1048 }
1049 if (deleteIntermediateFiles) {
1050 AliRunLoader* runLoader = LoadRun("READ");
1051 if (runLoader) for (Int_t iEvent = 0;
1052 iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1053 char command[256];
1054 sprintf(command, "rm -r raw%d", iEvent);
1055 gSystem->Exec(command);
1056 }
1057 }
1058
1059 if (rootOutput) {
1060 if (!ConvertDateToRoot(dateFileName, fileName)) {
1061 if (fStopOnError) return kFALSE;
1062 }
1063 if (deleteIntermediateFiles) {
1064 gSystem->Unlink(dateFileName);
1065 }
1066 }
1067 }
1068
5f8272e1 1069 AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
1070 stopwatch.RealTime(),stopwatch.CpuTime()));
06c7e392 1071
1072 return kTRUE;
1073}
1074
1075//_____________________________________________________________________________
1076Bool_t AliSimulation::WriteRawFiles(const char* detectors)
1077{
1078// convert the digits to raw data DDL files
1079
1080 AliRunLoader* runLoader = LoadRun("READ");
0421c3d1 1081 if (!runLoader) return kFALSE;
1082
06c7e392 1083 // write raw data to DDL files
0421c3d1 1084 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
ac1671c0 1085 AliInfo(Form("processing event %d", iEvent));
0421c3d1 1086 runLoader->GetEvent(iEvent);
1087 TString baseDir = gSystem->WorkingDirectory();
1088 char dirName[256];
1089 sprintf(dirName, "raw%d", iEvent);
1090 gSystem->MakeDirectory(dirName);
1091 if (!gSystem->ChangeDirectory(dirName)) {
ac1671c0 1092 AliError(Form("couldn't change to directory %s", dirName));
0421c3d1 1093 if (fStopOnError) return kFALSE; else continue;
1094 }
1095
1096 TString detStr = detectors;
1097 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1098 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1099 AliModule* det = (AliModule*) detArray->At(iDet);
1100 if (!det || !det->IsActive()) continue;
1101 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 1102 AliInfo(Form("creating raw data from digits for %s", det->GetName()));
0421c3d1 1103 det->Digits2Raw();
1104 }
1105 }
1106
007155cc 1107 if (!WriteTriggerRawData())
1108 if (fStopOnError) return kFALSE;
1109
0421c3d1 1110 gSystem->ChangeDirectory(baseDir);
1111 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 1112 AliError(Form("the following detectors were not found: %s",
1113 detStr.Data()));
0421c3d1 1114 if (fStopOnError) return kFALSE;
1115 }
1116 }
1117
1118 delete runLoader;
06c7e392 1119 return kTRUE;
1120}
0421c3d1 1121
06c7e392 1122//_____________________________________________________________________________
1123Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName)
1124{
1125// convert raw data DDL files to a DATE file with the program "dateStream"
1126
06c7e392 1127 char* path = gSystem->Which(gSystem->Getenv("PATH"), "dateStream");
1128 if (!path) {
ac1671c0 1129 AliError("the program dateStream was not found");
06c7e392 1130 if (fStopOnError) return kFALSE;
1131 } else {
1132 delete[] path;
1133 }
0421c3d1 1134
06c7e392 1135 AliRunLoader* runLoader = LoadRun("READ");
1136 if (!runLoader) return kFALSE;
1137
ac1671c0 1138 AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
06c7e392 1139 char command[256];
f28a8d2d 1140 // Note the option -s. It is used in order to avoid
1141 // the generation of SOR/EOR events.
1142 sprintf(command, "dateStream -s -D -o %s -# %d -C",
06c7e392 1143 dateFileName, runLoader->GetNumberOfEvents());
1144 FILE* pipe = gSystem->OpenPipe(command, "w");
1145
1146 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1147 fprintf(pipe, "GDC\n");
1148 Float_t ldc = 0;
1149 Int_t prevLDC = -1;
1150
1151 // loop over detectors and DDLs
362c9d61 1152 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1153 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
06c7e392 1154
362c9d61 1155 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
06c7e392 1156 Int_t ldcID = Int_t(ldc + 0.0001);
362c9d61 1157 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
06c7e392 1158
1159 char rawFileName[256];
362c9d61 1160 sprintf(rawFileName, "raw%d/%s",
1161 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
06c7e392 1162
1163 // check existence and size of raw data file
1164 FILE* file = fopen(rawFileName, "rb");
1165 if (!file) continue;
1166 fseek(file, 0, SEEK_END);
1167 unsigned long size = ftell(file);
aa04f81b 1168 fclose(file);
1169 if (!size) continue;
06c7e392 1170
1171 if (ldcID != prevLDC) {
1172 fprintf(pipe, " LDC Id %d\n", ldcID);
1173 prevLDC = ldcID;
1174 }
1175 fprintf(pipe, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1176 }
1177 }
1178 }
1179
1180 Int_t result = gSystem->ClosePipe(pipe);
1181
1182 delete runLoader;
1183 return (result == 0);
1184}
1185
1186//_____________________________________________________________________________
1187Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
1188 const char* rootFileName)
1189{
1190// convert a DATE file to a root file with the program "alimdc"
1191
1192 // ALIMDC setup
f28a8d2d 1193 const Int_t kDBSize = 2000000000;
c83eb8f4 1194 const Int_t kTagDBSize = 1000000000;
06c7e392 1195 const Bool_t kFilter = kFALSE;
f28a8d2d 1196 const Int_t kCompression = 0;
06c7e392 1197
1198 char* path = gSystem->Which(gSystem->Getenv("PATH"), "alimdc");
1199 if (!path) {
ac1671c0 1200 AliError("the program alimdc was not found");
06c7e392 1201 if (fStopOnError) return kFALSE;
1202 } else {
1203 delete[] path;
1204 }
1205
ac1671c0 1206 AliInfo(Form("converting DATE file %s to root file %s",
1207 dateFileName, rootFileName));
06c7e392 1208
f732bd7d 1209 const char* rawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
1210 const char* tagDBFS = "/tmp/mdc1/tags";
1211 const char* runDBFS = "/tmp/mdc1/meta";
1212
1213 // User defined file system locations
1214 if (gSystem->Getenv("ALIMDC_RAWDB1"))
1215 rawDBFS[0] = gSystem->Getenv("ALIMDC_RAWDB1");
1216 if (gSystem->Getenv("ALIMDC_RAWDB2"))
1217 rawDBFS[1] = gSystem->Getenv("ALIMDC_RAWDB2");
1218 if (gSystem->Getenv("ALIMDC_TAGDB"))
1219 tagDBFS = gSystem->Getenv("ALIMDC_TAGDB");
1220 if (gSystem->Getenv("ALIMDC_RUNDB"))
1221 runDBFS = gSystem->Getenv("ALIMDC_RUNDB");
1222
1223 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1224 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1225 gSystem->Exec(Form("rm -rf %s",tagDBFS));
1226 gSystem->Exec(Form("rm -rf %s",runDBFS));
1227
1228 gSystem->Exec(Form("mkdir %s",rawDBFS[0]));
1229 gSystem->Exec(Form("mkdir %s",rawDBFS[1]));
1230 gSystem->Exec(Form("mkdir %s",tagDBFS));
1231 gSystem->Exec(Form("mkdir %s",runDBFS));
1232
1233 Int_t result = gSystem->Exec(Form("alimdc %d %d %d %d %s",
1234 kDBSize, kTagDBSize, kFilter, kCompression, dateFileName));
1235 gSystem->Exec(Form("mv %s/*.root %s", rawDBFS[0], rootFileName));
1236
1237 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1238 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1239 gSystem->Exec(Form("rm -rf %s",tagDBFS));
1240 gSystem->Exec(Form("rm -rf %s",runDBFS));
06c7e392 1241
1242 return (result == 0);
0421c3d1 1243}
1244
85a5290f 1245
1246//_____________________________________________________________________________
06c7e392 1247AliRunLoader* AliSimulation::LoadRun(const char* mode) const
95601147 1248{
1249// delete existing run loaders, open a new one and load gAlice
1250
1251 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
1252 AliRunLoader* runLoader =
1253 AliRunLoader::Open(fGAliceFileName.Data(),
06c7e392 1254 AliConfig::GetDefaultEventFolderName(), mode);
95601147 1255 if (!runLoader) {
ac1671c0 1256 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
95601147 1257 return NULL;
1258 }
1259 runLoader->LoadgAlice();
1260 gAlice = runLoader->GetAliRun();
1261 if (!gAlice) {
ac1671c0 1262 AliError(Form("no gAlice object found in file %s",
1263 fGAliceFileName.Data()));
95601147 1264 return NULL;
1265 }
1266 return runLoader;
1267}
1268
1269//_____________________________________________________________________________
05526d44 1270Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
1271{
1272// get or calculate the number of signal events per background event
1273
1274 if (!fBkgrdFileNames) return 1;
1275 Int_t nBkgrdFiles = fBkgrdFileNames->GetEntriesFast();
1276 if (nBkgrdFiles == 0) return 1;
1277
1278 // get the number of signal events
1279 if (nEvents <= 0) {
1280 AliRunLoader* runLoader =
ca30848f 1281 AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
05526d44 1282 if (!runLoader) return 1;
ca30848f 1283
05526d44 1284 nEvents = runLoader->GetNumberOfEvents();
1285 delete runLoader;
1286 }
1287
1288 Int_t result = 0;
1289 for (Int_t iBkgrdFile = 0; iBkgrdFile < nBkgrdFiles; iBkgrdFile++) {
1290 // get the number of background events
1291 const char* fileName = ((TObjString*)
1292 (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
ca30848f 1293 AliRunLoader* runLoader =
05526d44 1294 AliRunLoader::Open(fileName, "BKGRD");
1295 if (!runLoader) continue;
1296 Int_t nBkgrdEvents = runLoader->GetNumberOfEvents();
1297 delete runLoader;
ca30848f 1298
05526d44 1299 // get or calculate the number of signal per background events
1300 Int_t nSignalPerBkgrd = fBkgrdFileNames->At(iBkgrdFile)->GetUniqueID();
1301 if (nSignalPerBkgrd <= 0) {
1302 nSignalPerBkgrd = (nEvents-1) / nBkgrdEvents + 1;
1303 } else if (result && (result != nSignalPerBkgrd)) {
ac1671c0 1304 AliInfo(Form("the number of signal events per background event "
1305 "will be changed from %d to %d for stream %d",
1306 nSignalPerBkgrd, result, iBkgrdFile+1));
05526d44 1307 nSignalPerBkgrd = result;
1308 }
1309
1310 if (!result) result = nSignalPerBkgrd;
1311 if (nSignalPerBkgrd * nBkgrdEvents < nEvents) {
ac1671c0 1312 AliWarning(Form("not enough background events (%d) for %d signal events "
1313 "using %d signal per background events for stream %d",
1314 nBkgrdEvents, nEvents, nSignalPerBkgrd, iBkgrdFile+1));
05526d44 1315 }
1316 }
1317
1318 return result;
1319}
1320
1321//_____________________________________________________________________________
85a5290f 1322Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
1323{
1324// check whether detName is contained in detectors
1325// if yes, it is removed from detectors
1326
1327 // check if all detectors are selected
1328 if ((detectors.CompareTo("ALL") == 0) ||
1329 detectors.BeginsWith("ALL ") ||
1330 detectors.EndsWith(" ALL") ||
1331 detectors.Contains(" ALL ")) {
1332 detectors = "ALL";
1333 return kTRUE;
1334 }
1335
1336 // search for the given detector
1337 Bool_t result = kFALSE;
1338 if ((detectors.CompareTo(detName) == 0) ||
1339 detectors.BeginsWith(detName+" ") ||
1340 detectors.EndsWith(" "+detName) ||
1341 detectors.Contains(" "+detName+" ")) {
1342 detectors.ReplaceAll(detName, "");
1343 result = kTRUE;
1344 }
1345
1346 // clean up the detectors string
1347 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
1348 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
1349 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
1350
1351 return result;
1352}
ca30848f 1353
1354Bool_t AliSimulation::ConvertRaw2SDigits(const char* rawDirectory, const char* esdFileName)
1355{
1356//
1357// Steering routine to convert raw data in directory rawDirectory/ to fake SDigits.
1358// These can be used for embedding of MC tracks into RAW data using the standard
1359// merging procedure.
1360//
1361// If an ESD file is given the reconstructed vertex is taken from it and stored in the event header.
1362//
1363 if (!gAlice) {
1364 AliError("no gAlice object. Restart aliroot and try again.");
1365 return kFALSE;
1366 }
1367 if (gAlice->Modules()->GetEntries() > 0) {
1368 AliError("gAlice was already run. Restart aliroot and try again.");
1369 return kFALSE;
1370 }
1371
1372 AliInfo(Form("initializing gAlice with config file %s",fConfigFileName.Data()));
1373 StdoutToAliInfo(StderrToAliError(gAlice->Init(fConfigFileName.Data());););
1374//
1375// Initialize CDB
1376 InitCDBStorage();
1377 AliCDBManager* man = AliCDBManager::Instance();
1378 man->SetRun(0); // Should this come from rawdata header ?
1379
1380 Int_t iDet;
1381 //
1382 // Get the runloader
1383 AliRunLoader* runLoader = gAlice->GetRunLoader();
1384 //
1385 // Open esd file if available
1386 TFile* esdFile = TFile::Open(esdFileName);
1387 Bool_t esdOK = (esdFile != 0);
1388 AliESD* esd = new AliESD;
1389 TTree* treeESD = 0;
1390 if (esdOK) {
1391 treeESD = (TTree*) esdFile->Get("esdTree");
1392 if (!treeESD) {
1393 AliWarning("No ESD tree found");
1394 esdOK = kFALSE;
1395 } else {
1396 treeESD->SetBranchAddress("ESD", &esd);
1397 }
1398 }
1399 //
1400 // Create the RawReader
1401 AliRawReaderFile* rawReader = new AliRawReaderFile(rawDirectory);
1402 //
1403 // Get list of detectors
1404 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1405 //
1406 // Get Header
1407 AliHeader* header = runLoader->GetHeader();
1408 //
1409 // Event loop
1410 Int_t nev = 0;
1411 while(kTRUE) {
1412 if (!(rawReader->NextEvent())) break;
1413 //
1414 // Detector loop
1415 for (iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1416 AliModule* det = (AliModule*) detArray->At(iDet);
1417 AliInfo(Form("Calling Raw2SDigits for %s\n", det->GetName()));
1418 det->Raw2SDigits(rawReader);
1419 rawReader->Reset();
1420 } // detectors
1421
1422 //
1423 // If ESD information available obtain reconstructed vertex and store in header.
1424 if (esdOK) {
1425 treeESD->GetEvent(nev);
1426 const AliESDVertex* esdVertex = esd->GetPrimaryVertex();
1427 Double_t position[3];
1428 esdVertex->GetXYZ(position);
1429 AliGenEventHeader* mcHeader = new AliGenEventHeader("ESD");
1430 TArrayF mcV;
1431 mcV.Set(3);
1432 for (Int_t i = 0; i < 3; i++) mcV[i] = position[i];
1433 mcHeader->SetPrimaryVertex(mcV);
1434 header->Reset(0,nev);
1435 header->SetGenEventHeader(mcHeader);
1436 printf("***** Saved vertex %f %f %f \n", position[0], position[1], position[2]);
1437 }
1438 nev++;
1439//
1440// Finish the event
1441 runLoader->TreeE()->Fill();
1442 runLoader->SetNextEvent();
1443 } // events
1444
1445 delete rawReader;
1446//
1447// Finish the run
1448 runLoader->CdGAFile();
1449 runLoader->WriteHeader("OVERWRITE");
1450 runLoader->WriteRunLoader();
1451
1452 return kTRUE;
1453}