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 passed as argument to the //
34 // AliSimulation constructor or can be specified by //
36 // sim.SetConfigFile("..."); //
38 // The generation of particles and the simulation of detector hits can be //
39 // switched on or off by //
41 // sim.SetRunGeneration(kTRUE); // generation of primary particles //
42 // sim.SetRunSimulation(kFALSE); // but no tracking //
44 // For which detectors sdigits and digits will be created, can be steered //
47 // sim.SetMakeSDigits("ALL"); // make sdigits for all detectors //
48 // sim.SetMakeDigits("ITS TPC"); // make digits only for ITS and TPC //
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. //
55 // The creation of digits from hits instead of from sdigits can be selected //
58 // sim.SetMakeDigitsFromHits("TRD"); //
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. //
64 // Background events can be merged by calling //
66 // sim.MergeWith("background/galice.root", 2); //
68 // The first argument is the file name of the background galice file. The //
69 // second argument is the number of signal events per background event. //
70 // 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. //
75 // The output of raw data can be switched on by calling //
77 // sim.SetWriteRawData("MUON"); // write raw data for MUON //
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 //
88 // The methods RunSimulation, RunSDigitization, RunDigitization, //
89 // RunHitsDigitization and WriteRawData can be used to run only parts of //
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. //
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 //
98 // sim.SetEventsPerFile("PHOS", "Reconstructed Points", 3); //
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. //
106 ///////////////////////////////////////////////////////////////////////////////
108 #include <TVirtualMCApplication.h>
109 #include <TGeoManager.h>
110 #include <TObjString.h>
115 #include "AliCodeTimer.h"
116 #include "AliCDBStorage.h"
117 #include "AliCDBEntry.h"
118 #include "AliCDBManager.h"
119 #include "AliGeomManager.h"
120 #include "AliAlignObj.h"
121 #include "AliCentralTrigger.h"
123 #include "AliDigitizer.h"
124 #include "AliGenerator.h"
126 #include "AliModule.h"
128 #include "AliRunDigitizer.h"
129 #include "AliRunLoader.h"
130 #include "AliSimulation.h"
131 #include "AliVertexGenFile.h"
132 #include "AliCentralTrigger.h"
133 #include "AliCTPRawData.h"
134 #include "AliRawReaderFile.h"
135 #include "AliRawReaderRoot.h"
136 #include "AliRawReaderDate.h"
138 #include "AliHeader.h"
139 #include "AliGenEventHeader.h"
141 #include "AliHLTSimulation.h"
142 #include "AliSysInfo.h"
144 #include "AliGRPObject.h"
146 ClassImp(AliSimulation)
148 AliSimulation *AliSimulation::fgInstance = 0;
149 const char* AliSimulation::fgkDetectorName[AliSimulation::fgkNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE", "HLT"};
151 //_____________________________________________________________________________
152 AliSimulation::AliSimulation(const char* configFileName,
153 const char* name, const char* title) :
156 fRunGeneration(kTRUE),
157 fRunSimulation(kTRUE),
158 fLoadAlignFromCDB(kTRUE),
159 fLoadAlObjsListOfDets("ALL"),
163 fMakeDigitsFromHits(""),
165 fRawDataFileName(""),
166 fDeleteIntermediateFiles(kFALSE),
167 fWriteSelRawData(kFALSE),
168 fStopOnError(kFALSE),
171 fConfigFileName(configFileName),
172 fGAliceFileName("galice.root"),
174 fBkgrdFileNames(NULL),
175 fAlignObjArray(NULL),
176 fUseBkgrdVertex(kTRUE),
177 fRegionOfInterest(kFALSE),
182 fInitCDBCalled(kFALSE),
183 fInitRunNumberCalled(kFALSE),
184 fSetRunNumberFromDataCalled(kFALSE),
185 fEmbeddingFlag(kFALSE),
191 fWriteGRPEntry(kTRUE)
193 // create simulation object with default parameters
195 SetGAliceFile("galice.root");
198 fQASteer = new AliQADataMakerSteer("sim") ;
199 fQASteer->SetActiveDetectors(fQADetectors) ;
200 fQATasks = Form("%d %d %d", AliQA::kHITS, AliQA::kSDIGITS, AliQA::kDIGITS) ;
201 fQASteer->SetTasks(fQATasks) ;
204 //_____________________________________________________________________________
205 AliSimulation::AliSimulation(const AliSimulation& sim) :
208 fRunGeneration(sim.fRunGeneration),
209 fRunSimulation(sim.fRunSimulation),
210 fLoadAlignFromCDB(sim.fLoadAlignFromCDB),
211 fLoadAlObjsListOfDets(sim.fLoadAlObjsListOfDets),
212 fMakeSDigits(sim.fMakeSDigits),
213 fMakeDigits(sim.fMakeDigits),
214 fMakeTrigger(sim.fMakeTrigger),
215 fMakeDigitsFromHits(sim.fMakeDigitsFromHits),
216 fWriteRawData(sim.fWriteRawData),
217 fRawDataFileName(""),
218 fDeleteIntermediateFiles(kFALSE),
219 fWriteSelRawData(kFALSE),
220 fStopOnError(sim.fStopOnError),
222 fNEvents(sim.fNEvents),
223 fConfigFileName(sim.fConfigFileName),
224 fGAliceFileName(sim.fGAliceFileName),
226 fBkgrdFileNames(NULL),
227 fAlignObjArray(NULL),
228 fUseBkgrdVertex(sim.fUseBkgrdVertex),
229 fRegionOfInterest(sim.fRegionOfInterest),
230 fCDBUri(sim.fCDBUri),
234 fInitCDBCalled(sim.fInitCDBCalled),
235 fInitRunNumberCalled(sim.fInitRunNumberCalled),
236 fSetRunNumberFromDataCalled(sim.fSetRunNumberFromDataCalled),
237 fEmbeddingFlag(sim.fEmbeddingFlag),
238 fQADetectors(sim.fQADetectors),
239 fQATasks(sim.fQATasks),
240 fQASteer(sim.fQASteer),
242 fRunHLT(sim.fRunHLT),
243 fWriteGRPEntry(sim.fWriteGRPEntry)
247 for (Int_t i = 0; i < sim.fEventsPerFile.GetEntriesFast(); i++) {
248 if (!sim.fEventsPerFile[i]) continue;
249 fEventsPerFile.Add(sim.fEventsPerFile[i]->Clone());
252 fBkgrdFileNames = new TObjArray;
253 for (Int_t i = 0; i < sim.fBkgrdFileNames->GetEntriesFast(); i++) {
254 if (!sim.fBkgrdFileNames->At(i)) continue;
255 fBkgrdFileNames->Add(sim.fBkgrdFileNames->At(i)->Clone());
258 for (Int_t i = 0; i < sim.fSpecCDBUri.GetEntriesFast(); i++) {
259 if (sim.fSpecCDBUri[i]) fSpecCDBUri.Add(sim.fSpecCDBUri[i]->Clone());
264 //_____________________________________________________________________________
265 AliSimulation& AliSimulation::operator = (const AliSimulation& sim)
267 // assignment operator
269 this->~AliSimulation();
270 new(this) AliSimulation(sim);
274 //_____________________________________________________________________________
275 AliSimulation::~AliSimulation()
279 fEventsPerFile.Delete();
280 // if(fAlignObjArray) fAlignObjArray->Delete(); // fAlignObjArray->RemoveAll() ???
281 // delete fAlignObjArray; fAlignObjArray=0;
283 if (fBkgrdFileNames) {
284 fBkgrdFileNames->Delete();
285 delete fBkgrdFileNames;
288 fSpecCDBUri.Delete();
289 if (fgInstance==this) fgInstance = 0;
293 AliCodeTimer::Instance()->Print();
297 //_____________________________________________________________________________
298 void AliSimulation::SetNumberOfEvents(Int_t nEvents)
300 // set the number of events for one run
305 //_____________________________________________________________________________
306 void AliSimulation::InitCDB()
308 // activate a default CDB storage
309 // First check if we have any CDB storage set, because it is used
310 // to retrieve the calibration and alignment constants
312 if (fInitCDBCalled) return;
313 fInitCDBCalled = kTRUE;
315 AliCDBManager* man = AliCDBManager::Instance();
316 if (man->IsDefaultStorageSet())
318 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
319 AliWarning("Default CDB storage has been already set !");
320 AliWarning(Form("Ignoring the default storage declared in AliSimulation: %s",fCDBUri.Data()));
321 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
322 fCDBUri = man->GetDefaultStorage()->GetURI();
325 if (fCDBUri.Length() > 0)
327 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
328 AliDebug(2, Form("Default CDB storage is set to: %s", fCDBUri.Data()));
329 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
331 fCDBUri="local://$ALICE_ROOT";
332 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
333 AliWarning("Default CDB storage not yet set !!!!");
334 AliWarning(Form("Setting it now to: %s", fCDBUri.Data()));
335 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
338 man->SetDefaultStorage(fCDBUri);
341 // Now activate the detector specific CDB storage locations
342 for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
343 TObject* obj = fSpecCDBUri[i];
345 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
346 AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
347 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
348 man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
353 //_____________________________________________________________________________
354 void AliSimulation::InitRunNumber(){
355 // check run number. If not set, set it to 0 !!!!
357 if (fInitRunNumberCalled) return;
358 fInitRunNumberCalled = kTRUE;
360 AliCDBManager* man = AliCDBManager::Instance();
361 if (man->GetRun() >= 0)
363 AliFatal(Form("Run number cannot be set in AliCDBManager before start of simulation: "
364 "Use external variable DC_RUN or AliSimulation::SetRun()!"));
368 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
369 AliDebug(2, Form("Setting CDB run number to: %d",fRun));
370 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
373 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
374 AliWarning("Run number not yet set !!!!");
375 AliWarning(Form("Setting it now to: %d", fRun));
376 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
385 //_____________________________________________________________________________
386 void AliSimulation::SetCDBLock() {
387 // Set CDB lock: from now on it is forbidden to reset the run number
388 // or the default storage or to activate any further storage!
390 AliCDBManager::Instance()->SetLock(1);
393 //_____________________________________________________________________________
394 void AliSimulation::SetDefaultStorage(const char* uri) {
395 // Store the desired default CDB storage location
396 // Activate it later within the Run() method
402 //_____________________________________________________________________________
403 void AliSimulation::SetSpecificStorage(const char* calibType, const char* uri) {
404 // Store a detector-specific CDB storage location
405 // Activate it later within the Run() method
407 AliCDBPath aPath(calibType);
408 if(!aPath.IsValid()){
409 AliError(Form("Not a valid path: %s", calibType));
413 TObject* obj = fSpecCDBUri.FindObject(calibType);
414 if (obj) fSpecCDBUri.Remove(obj);
415 fSpecCDBUri.Add(new TNamed(calibType, uri));
419 //_____________________________________________________________________________
420 void AliSimulation::SetRunNumber(Int_t run)
423 // Activate it later within the Run() method
428 //_____________________________________________________________________________
429 void AliSimulation::SetSeed(Int_t seed)
432 // Activate it later within the Run() method
437 //_____________________________________________________________________________
438 Bool_t AliSimulation::SetRunNumberFromData()
440 // Set the CDB manager run number
441 // The run number is retrieved from gAlice
443 if (fSetRunNumberFromDataCalled) return kTRUE;
444 fSetRunNumberFromDataCalled = kTRUE;
446 AliCDBManager* man = AliCDBManager::Instance();
447 Int_t runData = -1, runCDB = -1;
449 AliRunLoader* runLoader = LoadRun("READ");
450 if (!runLoader) return kFALSE;
452 runData = runLoader->GetAliRun()->GetHeader()->GetRun();
456 runCDB = man->GetRun();
458 if (runCDB != runData) {
459 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
460 AliWarning(Form("A run number was previously set in AliCDBManager: %d !", runCDB));
461 AliWarning(Form("It will be replaced with the run number got from run header: %d !", runData));
462 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
467 man->SetRun(runData);
470 if(man->GetRun() < 0) {
471 AliError("Run number not properly initalized!");
480 //_____________________________________________________________________________
481 void AliSimulation::SetConfigFile(const char* fileName)
483 // set the name of the config file
485 fConfigFileName = fileName;
488 //_____________________________________________________________________________
489 void AliSimulation::SetGAliceFile(const char* fileName)
491 // set the name of the galice file
492 // the path is converted to an absolute one if it is relative
494 fGAliceFileName = fileName;
495 if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
496 char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
498 fGAliceFileName = absFileName;
499 delete[] absFileName;
502 AliDebug(2, Form("galice file name set to %s", fileName));
505 //_____________________________________________________________________________
506 void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
509 // set the number of events per file for the given detector and data type
510 // ("Hits", "Summable Digits", "Digits", "Reconstructed Points" or "Tracks")
512 TNamed* obj = new TNamed(detector, type);
513 obj->SetUniqueID(nEvents);
514 fEventsPerFile.Add(obj);
517 //_____________________________________________________________________________
518 Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
520 // Read the alignment objects from CDB.
521 // Each detector is supposed to have the
522 // alignment objects in DET/Align/Data CDB path.
523 // All the detector objects are then collected,
524 // sorted by geometry level (starting from ALIC) and
525 // then applied to the TGeo geometry.
526 // Finally an overlaps check is performed.
528 if (!AliGeomManager::GetGeometry() || !AliGeomManager::GetGeometry()->IsClosed()) {
529 AliError("Can't apply the misalignment! Geometry is not loaded or it is still opened!");
533 // initialize CDB storage, run number, set CDB lock
535 // if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
538 Bool_t delRunLoader = kFALSE;
540 runLoader = LoadRun("READ");
541 if (!runLoader) return kFALSE;
542 delRunLoader = kTRUE;
545 // Export ideal geometry
546 if(!gAlice->IsRootGeometry()) AliGeomManager::GetGeometry()->Export("geometry.root");
548 // Load alignment data from CDB and apply to geometry through AliGeomManager
549 if(fLoadAlignFromCDB){
551 TString detStr = fLoadAlObjsListOfDets;
552 TString loadAlObjsListOfDets = "";
554 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
555 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
556 AliModule* det = (AliModule*) detArray->At(iDet);
557 if (!det || !det->IsActive()) continue;
558 if (IsSelected(det->GetName(), detStr)) {
559 //add det to list of dets to be aligned from CDB
560 loadAlObjsListOfDets += det->GetName();
561 loadAlObjsListOfDets += " ";
563 } // end loop over detectors
564 loadAlObjsListOfDets.Prepend("GRP "); //add alignment objects for non-sensitive modules
565 AliGeomManager::ApplyAlignObjsFromCDB(loadAlObjsListOfDets.Data());
567 // Check if the array with alignment objects was
568 // provided by the user. If yes, apply the objects
569 // to the present TGeo geometry
570 if (fAlignObjArray) {
571 if (AliGeomManager::ApplyAlignObjsToGeom(*fAlignObjArray) == kFALSE) {
572 AliError("The misalignment of one or more volumes failed!"
573 "Compare the list of simulated detectors and the list of detector alignment data!");
574 if (delRunLoader) delete runLoader;
580 // Update the internal geometry of modules (ITS needs it)
581 TString detStr = fLoadAlObjsListOfDets;
582 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
583 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
585 AliModule* det = (AliModule*) detArray->At(iDet);
586 if (!det || !det->IsActive()) continue;
587 if (IsSelected(det->GetName(), detStr)) {
588 det->UpdateInternalGeometry();
590 } // end loop over detectors
593 if (delRunLoader) delete runLoader;
598 //_____________________________________________________________________________
599 void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
601 // add a file with background events for merging
603 TObjString* fileNameStr = new TObjString(fileName);
604 fileNameStr->SetUniqueID(nSignalPerBkgrd);
605 if (!fBkgrdFileNames) fBkgrdFileNames = new TObjArray;
606 fBkgrdFileNames->Add(fileNameStr);
609 void AliSimulation::EmbedInto(const char* fileName, Int_t nSignalPerBkgrd)
611 // add a file with background events for embeddin
612 MergeWith(fileName, nSignalPerBkgrd);
613 fEmbeddingFlag = kTRUE;
616 //_____________________________________________________________________________
617 Bool_t AliSimulation::Run(Int_t nEvents)
619 // run the generation, simulation and digitization
624 // Load run number and seed from environmental vars
625 ProcessEnvironmentVars();
627 gRandom->SetSeed(fSeed);
629 if (nEvents > 0) fNEvents = nEvents;
631 // generation and simulation -> hits
632 if (fRunGeneration) {
633 if (!RunSimulation()) if (fStopOnError) return kFALSE;
636 // initialize CDB storage from external environment
637 // (either CDB manager or AliSimulation setters),
638 // if not already done in RunSimulation()
641 // Set run number in CDBManager from data
642 // From this point on the run number must be always loaded from data!
643 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
645 // Set CDB lock: from now on it is forbidden to reset the run number
646 // or the default storage or to activate any further storage!
649 // If RunSimulation was not called, load the geometry and misalign it
650 if (!AliGeomManager::GetGeometry()) {
651 // Initialize the geometry manager
652 AliGeomManager::LoadGeometry("geometry.root");
654 // // Check that the consistency of symbolic names for the activated subdetectors
655 // // in the geometry loaded by AliGeomManager
656 // AliRunLoader* runLoader = LoadRun("READ");
657 // if (!runLoader) return kFALSE;
659 // TString detsToBeChecked = "";
660 // TObjArray* detArray = runLoader->GetAliRun()->Detectors();
661 // for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
662 // AliModule* det = (AliModule*) detArray->At(iDet);
663 // if (!det || !det->IsActive()) continue;
664 // detsToBeChecked += det->GetName();
665 // detsToBeChecked += " ";
666 // } // end loop over detectors
667 // if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
668 if(!AliGeomManager::CheckSymNamesLUT("ALL"))
669 AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
671 if (!AliGeomManager::GetGeometry()) if (fStopOnError) return kFALSE;
673 if(!MisalignGeometry()) if (fStopOnError) return kFALSE;
677 // hits -> summable digits
678 AliSysInfo::AddStamp("Start_sdigitization");
679 if (!fMakeSDigits.IsNull()) {
680 if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
683 AliSysInfo::AddStamp("Stop_sdigitization");
685 AliSysInfo::AddStamp("Start_digitization");
686 // summable digits -> digits
687 if (!fMakeDigits.IsNull()) {
688 if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
689 if (fStopOnError) return kFALSE;
692 AliSysInfo::AddStamp("Stop_digitization");
697 if (!fMakeDigitsFromHits.IsNull()) {
698 if (fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
699 AliWarning(Form("Merging and direct creation of digits from hits "
700 "was selected for some detectors. "
701 "No merging will be done for the following detectors: %s",
702 fMakeDigitsFromHits.Data()));
704 if (!RunHitsDigitization(fMakeDigitsFromHits)) {
705 if (fStopOnError) return kFALSE;
712 if (!RunTrigger(fMakeTrigger,fMakeDigits)) {
713 if (fStopOnError) return kFALSE;
718 // digits -> raw data
719 if (!fWriteRawData.IsNull()) {
720 if (!WriteRawData(fWriteRawData, fRawDataFileName,
721 fDeleteIntermediateFiles,fWriteSelRawData)) {
722 if (fStopOnError) return kFALSE;
728 // run HLT simulation
729 if (!fRunHLT.IsNull()) {
731 if (fStopOnError) return kFALSE;
737 Bool_t rv = RunQA() ;
743 // Cleanup of CDB manager: cache and active storages!
744 AliCDBManager::Instance()->ClearCache();
749 //_____________________________________________________________________________
750 Bool_t AliSimulation::RunTrigger(const char* config, const char* detectors)
756 // initialize CDB storage from external environment
757 // (either CDB manager or AliSimulation setters),
758 // if not already done in RunSimulation()
761 // Set run number in CDBManager from data
762 // From this point on the run number must be always loaded from data!
763 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
765 // Set CDB lock: from now on it is forbidden to reset the run number
766 // or the default storage or to activate any further storage!
769 AliRunLoader* runLoader = LoadRun("READ");
770 if (!runLoader) return kFALSE;
771 TString trconfiguration = config;
773 if (trconfiguration.IsNull()) {
774 if (gAlice->GetTriggerDescriptor() != "") {
775 trconfiguration = gAlice->GetTriggerDescriptor();
778 AliWarning("No trigger descriptor is specified. Loading the one that is in the CDB.");
781 runLoader->MakeTree( "GG" );
782 AliCentralTrigger* aCTP = runLoader->GetTrigger();
783 // Load Configuration
784 if (!aCTP->LoadConfiguration( trconfiguration ))
788 if( !aCTP->RunTrigger( runLoader , detectors ) ) {
800 //_____________________________________________________________________________
801 Bool_t AliSimulation::WriteTriggerRawData()
803 // Writes the CTP (trigger) DDL raw data
804 // Details of the format are given in the
805 // trigger TDR - pages 134 and 135.
806 AliCTPRawData writer;
812 //_____________________________________________________________________________
813 Bool_t AliSimulation::RunSimulation(Int_t nEvents)
815 // run the generation and simulation
819 // initialize CDB storage and run number from external environment
820 // (either CDB manager or AliSimulation setters)
826 AliError("no gAlice object. Restart aliroot and try again.");
829 if (gAlice->Modules()->GetEntries() > 0) {
830 AliError("gAlice was already run. Restart aliroot and try again.");
834 AliInfo(Form("initializing gAlice with config file %s",
835 fConfigFileName.Data()));
836 StdoutToAliInfo(StderrToAliError(
837 gAlice->Init(fConfigFileName.Data());
840 // Get the trigger descriptor string
841 // Either from AliSimulation or from
843 if (fMakeTrigger.IsNull()) {
844 if (gAlice->GetTriggerDescriptor() != "")
845 fMakeTrigger = gAlice->GetTriggerDescriptor();
848 gAlice->SetTriggerDescriptor(fMakeTrigger.Data());
850 // Set run number in CDBManager
851 AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
853 AliRunLoader* runLoader = gAlice->GetRunLoader();
855 AliError(Form("gAlice has no run loader object. "
856 "Check your config file: %s", fConfigFileName.Data()));
859 SetGAliceFile(runLoader->GetFileName());
862 #if ROOT_VERSION_CODE < 331527
863 AliGeomManager::SetGeometry(gGeoManager);
865 // Check that the consistency of symbolic names for the activated subdetectors
866 // in the geometry loaded by AliGeomManager
867 TString detsToBeChecked = "";
868 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
869 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
870 AliModule* det = (AliModule*) detArray->At(iDet);
871 if (!det || !det->IsActive()) continue;
872 detsToBeChecked += det->GetName();
873 detsToBeChecked += " ";
874 } // end loop over detectors
875 if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
876 AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
877 MisalignGeometry(runLoader);
880 // AliRunLoader* runLoader = gAlice->GetRunLoader();
882 // AliError(Form("gAlice has no run loader object. "
883 // "Check your config file: %s", fConfigFileName.Data()));
886 // SetGAliceFile(runLoader->GetFileName());
888 if (!gAlice->Generator()) {
889 AliError(Form("gAlice has no generator object. "
890 "Check your config file: %s", fConfigFileName.Data()));
894 // Write GRP entry corresponding to the setting found in Cofig.C
898 if (nEvents <= 0) nEvents = fNEvents;
900 // get vertex from background file in case of merging
901 if (fUseBkgrdVertex &&
902 fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
903 Int_t signalPerBkgrd = GetNSignalPerBkgrd(nEvents);
904 const char* fileName = ((TObjString*)
905 (fBkgrdFileNames->At(0)))->GetName();
906 AliInfo(Form("The vertex will be taken from the background "
907 "file %s with nSignalPerBackground = %d",
908 fileName, signalPerBkgrd));
909 AliVertexGenFile* vtxGen = new AliVertexGenFile(fileName, signalPerBkgrd);
910 gAlice->Generator()->SetVertexGenerator(vtxGen);
913 if (!fRunSimulation) {
914 gAlice->Generator()->SetTrackingFlag(0);
917 // set the number of events per file for given detectors and data types
918 for (Int_t i = 0; i < fEventsPerFile.GetEntriesFast(); i++) {
919 if (!fEventsPerFile[i]) continue;
920 const char* detName = fEventsPerFile[i]->GetName();
921 const char* typeName = fEventsPerFile[i]->GetTitle();
922 TString loaderName(detName);
923 loaderName += "Loader";
924 AliLoader* loader = runLoader->GetLoader(loaderName);
926 AliError(Form("RunSimulation", "no loader for %s found\n"
927 "Number of events per file not set for %s %s",
928 detName, typeName, detName));
931 AliDataLoader* dataLoader =
932 loader->GetDataLoader(typeName);
934 AliError(Form("no data loader for %s found\n"
935 "Number of events per file not set for %s %s",
936 typeName, detName, typeName));
939 dataLoader->SetNumberOfEventsPerFile(fEventsPerFile[i]->GetUniqueID());
940 AliDebug(1, Form("number of events per file set to %d for %s %s",
941 fEventsPerFile[i]->GetUniqueID(), detName, typeName));
944 AliInfo("running gAlice");
945 AliSysInfo::AddStamp("Start_simulation");
946 StdoutToAliInfo(StderrToAliError(
947 gAlice->Run(nEvents);
949 AliSysInfo::AddStamp("Stop_simulation");
955 //_____________________________________________________________________________
956 Bool_t AliSimulation::RunSDigitization(const char* detectors)
958 // run the digitization and produce summable digits
959 static Int_t eventNr=0;
962 // initialize CDB storage, run number, set CDB lock
964 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
967 AliRunLoader* runLoader = LoadRun();
968 if (!runLoader) return kFALSE;
970 TString detStr = detectors;
971 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
972 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
973 AliModule* det = (AliModule*) detArray->At(iDet);
974 if (!det || !det->IsActive()) continue;
975 if (IsSelected(det->GetName(), detStr)) {
976 AliInfo(Form("creating summable digits for %s", det->GetName()));
977 AliCodeTimerAuto(Form("creating summable digits for %s", det->GetName()));
979 AliSysInfo::AddStamp(Form("Digit_%s_%d",det->GetName(),eventNr), 0,1, eventNr);
983 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
984 AliError(Form("the following detectors were not found: %s",
986 if (fStopOnError) return kFALSE;
995 //_____________________________________________________________________________
996 Bool_t AliSimulation::RunDigitization(const char* detectors,
997 const char* excludeDetectors)
999 // run the digitization and produce digits from sdigits
1001 AliCodeTimerAuto("")
1003 // initialize CDB storage, run number, set CDB lock
1005 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1008 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
1009 if (gAlice) delete gAlice;
1013 if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
1014 Int_t signalPerBkgrd = GetNSignalPerBkgrd();
1015 AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
1016 // manager->SetEmbeddingFlag(fEmbeddingFlag);
1017 manager->SetInputStream(0, fGAliceFileName.Data());
1018 for (Int_t iStream = 1; iStream < nStreams; iStream++) {
1019 const char* fileName = ((TObjString*)
1020 (fBkgrdFileNames->At(iStream-1)))->GetName();
1021 manager->SetInputStream(iStream, fileName);
1024 TString detStr = detectors;
1025 TString detExcl = excludeDetectors;
1026 manager->GetInputStream(0)->ImportgAlice();
1027 AliRunLoader* runLoader =
1028 AliRunLoader::GetRunLoader(manager->GetInputStream(0)->GetFolderName());
1029 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1030 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1031 AliModule* det = (AliModule*) detArray->At(iDet);
1032 if (!det || !det->IsActive()) continue;
1033 if (IsSelected(det->GetName(), detStr) &&
1034 !IsSelected(det->GetName(), detExcl)) {
1035 AliDigitizer* digitizer = det->CreateDigitizer(manager);
1038 AliError(Form("no digitizer for %s", det->GetName()));
1039 if (fStopOnError) return kFALSE;
1041 digitizer->SetRegionOfInterest(fRegionOfInterest);
1046 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1047 AliError(Form("the following detectors were not found: %s",
1049 if (fStopOnError) return kFALSE;
1052 if (!manager->GetListOfTasks()->IsEmpty()) {
1053 AliInfo("executing digitization");
1062 //_____________________________________________________________________________
1063 Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
1065 // run the digitization and produce digits from hits
1067 AliCodeTimerAuto("")
1069 // initialize CDB storage, run number, set CDB lock
1071 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1074 AliRunLoader* runLoader = LoadRun("READ");
1075 if (!runLoader) return kFALSE;
1077 TString detStr = detectors;
1078 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1079 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1080 AliModule* det = (AliModule*) detArray->At(iDet);
1081 if (!det || !det->IsActive()) continue;
1082 if (IsSelected(det->GetName(), detStr)) {
1083 AliInfo(Form("creating digits from hits for %s", det->GetName()));
1088 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1089 AliError(Form("the following detectors were not found: %s",
1091 if (fStopOnError) return kFALSE;
1095 //PH Temporary fix to avoid interference with the PHOS loder/getter
1096 //PH The problem has to be solved in more general way 09/06/05
1101 //_____________________________________________________________________________
1102 Bool_t AliSimulation::WriteRawData(const char* detectors,
1103 const char* fileName,
1104 Bool_t deleteIntermediateFiles,
1107 // convert the digits to raw data
1108 // First DDL raw data files for the given detectors are created.
1109 // If a file name is given, the DDL files are then converted to a DATE file.
1110 // If deleteIntermediateFiles is true, the DDL raw files are deleted
1112 // If the file name has the extension ".root", the DATE file is converted
1114 // If deleteIntermediateFiles is true, the DATE file is deleted afterwards.
1115 // 'selrawdata' flag can be used to enable writing of detectors raw data
1116 // accoring to the trigger cluster.
1118 AliCodeTimerAuto("")
1120 TString detStr = detectors;
1121 if (IsSelected("HLT", detStr))
1123 // Do nothing. "HLT" will be removed from detStr because the HLT raw
1124 // data files are generated in RunHLT.
1127 if (!WriteRawFiles(detStr.Data())) {
1128 if (fStopOnError) return kFALSE;
1131 TString dateFileName(fileName);
1132 if (!dateFileName.IsNull()) {
1133 Bool_t rootOutput = dateFileName.EndsWith(".root");
1134 if (rootOutput) dateFileName += ".date";
1135 TString selDateFileName;
1137 selDateFileName = "selected.";
1138 selDateFileName+= dateFileName;
1140 if (!ConvertRawFilesToDate(dateFileName,selDateFileName)) {
1141 if (fStopOnError) return kFALSE;
1143 if (deleteIntermediateFiles) {
1144 AliRunLoader* runLoader = LoadRun("READ");
1145 if (runLoader) for (Int_t iEvent = 0;
1146 iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1148 sprintf(command, "rm -r raw%d", iEvent);
1149 gSystem->Exec(command);
1154 if (!ConvertDateToRoot(dateFileName, fileName)) {
1155 if (fStopOnError) return kFALSE;
1157 if (deleteIntermediateFiles) {
1158 gSystem->Unlink(dateFileName);
1161 TString selFileName = "selected.";
1162 selFileName += fileName;
1163 if (!ConvertDateToRoot(selDateFileName, selFileName)) {
1164 if (fStopOnError) return kFALSE;
1166 if (deleteIntermediateFiles) {
1167 gSystem->Unlink(selDateFileName);
1176 //_____________________________________________________________________________
1177 Bool_t AliSimulation::WriteRawFiles(const char* detectors)
1179 // convert the digits to raw data DDL files
1181 AliCodeTimerAuto("")
1183 AliRunLoader* runLoader = LoadRun("READ");
1184 if (!runLoader) return kFALSE;
1186 // write raw data to DDL files
1187 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1188 AliInfo(Form("processing event %d", iEvent));
1189 runLoader->GetEvent(iEvent);
1190 TString baseDir = gSystem->WorkingDirectory();
1192 sprintf(dirName, "raw%d", iEvent);
1193 gSystem->MakeDirectory(dirName);
1194 if (!gSystem->ChangeDirectory(dirName)) {
1195 AliError(Form("couldn't change to directory %s", dirName));
1196 if (fStopOnError) return kFALSE; else continue;
1199 ofstream runNbFile(Form("run%u",runLoader->GetHeader()->GetRun()));
1202 TString detStr = detectors;
1203 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1204 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1205 AliModule* det = (AliModule*) detArray->At(iDet);
1206 if (!det || !det->IsActive()) continue;
1207 if (IsSelected(det->GetName(), detStr)) {
1208 AliInfo(Form("creating raw data from digits for %s", det->GetName()));
1213 if (!WriteTriggerRawData())
1214 if (fStopOnError) return kFALSE;
1216 gSystem->ChangeDirectory(baseDir);
1217 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1218 AliError(Form("the following detectors were not found: %s",
1220 if (fStopOnError) return kFALSE;
1229 //_____________________________________________________________________________
1230 Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName,
1231 const char* selDateFileName)
1233 // convert raw data DDL files to a DATE file with the program "dateStream"
1234 // The second argument is not empty when the user decides to write
1235 // the detectors raw data according to the trigger cluster.
1237 AliCodeTimerAuto("")
1239 char* path = gSystem->Which(gSystem->Getenv("PATH"), "dateStream");
1241 AliError("the program dateStream was not found");
1242 if (fStopOnError) return kFALSE;
1247 AliRunLoader* runLoader = LoadRun("READ");
1248 if (!runLoader) return kFALSE;
1250 AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
1251 Bool_t selrawdata = kFALSE;
1252 if (strcmp(selDateFileName,"") != 0) selrawdata = kTRUE;
1255 // Note the option -s. It is used in order to avoid
1256 // the generation of SOR/EOR events.
1257 sprintf(command, "dateStream -c -s -D -o %s -# %d -C -run %d",
1258 dateFileName, runLoader->GetNumberOfEvents(),runLoader->GetHeader()->GetRun());
1259 FILE* pipe = gSystem->OpenPipe(command, "w");
1261 Int_t selEvents = 0;
1262 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1264 UInt_t detectorPattern = 0;
1265 runLoader->GetEvent(iEvent);
1266 if (!runLoader->LoadTrigger()) {
1267 AliCentralTrigger *aCTP = runLoader->GetTrigger();
1268 detectorPattern = aCTP->GetClusterMask();
1269 // Check if the event was triggered by CTP
1271 if (aCTP->GetClassMask()) selEvents++;
1275 AliWarning("No trigger can be loaded! Some fields in the event header will be empty !");
1277 AliWarning("No trigger can be loaded! Writing of selected raw data is abandoned !");
1278 selrawdata = kFALSE;
1282 fprintf(pipe, "GDC DetectorPattern %u\n", detectorPattern);
1286 // loop over detectors and DDLs
1287 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1288 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
1290 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
1291 Int_t ldcID = Int_t(ldc + 0.0001);
1292 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
1294 char rawFileName[256];
1295 sprintf(rawFileName, "raw%d/%s",
1296 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
1298 // check existence and size of raw data file
1299 FILE* file = fopen(rawFileName, "rb");
1300 if (!file) continue;
1301 fseek(file, 0, SEEK_END);
1302 unsigned long size = ftell(file);
1304 if (!size) continue;
1306 if (ldcID != prevLDC) {
1307 fprintf(pipe, " LDC Id %d\n", ldcID);
1310 fprintf(pipe, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1315 Int_t result = gSystem->ClosePipe(pipe);
1317 if (!(selrawdata && selEvents > 0)) {
1319 return (result == 0);
1322 AliInfo(Form("converting selected by trigger cluster raw data DDL files to DATE file %s", selDateFileName));
1324 sprintf(command, "dateStream -c -s -D -o %s -# %d -C -run %d",
1325 selDateFileName,selEvents,runLoader->GetHeader()->GetRun());
1326 FILE* pipe2 = gSystem->OpenPipe(command, "w");
1328 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1330 // Get the trigger decision and cluster
1331 UInt_t detectorPattern = 0;
1333 runLoader->GetEvent(iEvent);
1334 if (!runLoader->LoadTrigger()) {
1335 AliCentralTrigger *aCTP = runLoader->GetTrigger();
1336 if (aCTP->GetClassMask() == 0) continue;
1337 detectorPattern = aCTP->GetClusterMask();
1338 detClust = AliDAQ::ListOfTriggeredDetectors(detectorPattern);
1339 AliInfo(Form("List of detectors to be read out: %s",detClust.Data()));
1342 fprintf(pipe2, "GDC DetectorPattern %u\n", detectorPattern);
1346 // loop over detectors and DDLs
1347 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1348 // Write only raw data from detectors that
1349 // are contained in the trigger cluster(s)
1350 if (!IsSelected(AliDAQ::DetectorName(iDet),detClust)) continue;
1352 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
1354 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
1355 Int_t ldcID = Int_t(ldc + 0.0001);
1356 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
1358 char rawFileName[256];
1359 sprintf(rawFileName, "raw%d/%s",
1360 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
1362 // check existence and size of raw data file
1363 FILE* file = fopen(rawFileName, "rb");
1364 if (!file) continue;
1365 fseek(file, 0, SEEK_END);
1366 unsigned long size = ftell(file);
1368 if (!size) continue;
1370 if (ldcID != prevLDC) {
1371 fprintf(pipe2, " LDC Id %d\n", ldcID);
1374 fprintf(pipe2, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1379 Int_t result2 = gSystem->ClosePipe(pipe2);
1382 return ((result == 0) && (result2 == 0));
1385 //_____________________________________________________________________________
1386 Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
1387 const char* rootFileName)
1389 // convert a DATE file to a root file with the program "alimdc"
1392 const Int_t kDBSize = 2000000000;
1393 const Int_t kTagDBSize = 1000000000;
1394 const Bool_t kFilter = kFALSE;
1395 const Int_t kCompression = 1;
1397 char* path = gSystem->Which(gSystem->Getenv("PATH"), "alimdc");
1399 AliError("the program alimdc was not found");
1400 if (fStopOnError) return kFALSE;
1405 AliInfo(Form("converting DATE file %s to root file %s",
1406 dateFileName, rootFileName));
1408 const char* rawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
1409 const char* tagDBFS = "/tmp/mdc1/tags";
1411 // User defined file system locations
1412 if (gSystem->Getenv("ALIMDC_RAWDB1"))
1413 rawDBFS[0] = gSystem->Getenv("ALIMDC_RAWDB1");
1414 if (gSystem->Getenv("ALIMDC_RAWDB2"))
1415 rawDBFS[1] = gSystem->Getenv("ALIMDC_RAWDB2");
1416 if (gSystem->Getenv("ALIMDC_TAGDB"))
1417 tagDBFS = gSystem->Getenv("ALIMDC_TAGDB");
1419 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1420 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1421 gSystem->Exec(Form("rm -rf %s",tagDBFS));
1423 gSystem->Exec(Form("mkdir %s",rawDBFS[0]));
1424 gSystem->Exec(Form("mkdir %s",rawDBFS[1]));
1425 gSystem->Exec(Form("mkdir %s",tagDBFS));
1427 Int_t result = gSystem->Exec(Form("alimdc %d %d %d %d %s",
1428 kDBSize, kTagDBSize, kFilter, kCompression, dateFileName));
1429 gSystem->Exec(Form("mv %s/*.root %s", rawDBFS[0], rootFileName));
1431 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1432 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1433 gSystem->Exec(Form("rm -rf %s",tagDBFS));
1435 return (result == 0);
1439 //_____________________________________________________________________________
1440 AliRunLoader* AliSimulation::LoadRun(const char* mode) const
1442 // delete existing run loaders, open a new one and load gAlice
1444 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
1445 AliRunLoader* runLoader =
1446 AliRunLoader::Open(fGAliceFileName.Data(),
1447 AliConfig::GetDefaultEventFolderName(), mode);
1449 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
1452 runLoader->LoadgAlice();
1453 runLoader->LoadHeader();
1454 gAlice = runLoader->GetAliRun();
1456 AliError(Form("no gAlice object found in file %s",
1457 fGAliceFileName.Data()));
1463 //_____________________________________________________________________________
1464 Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
1466 // get or calculate the number of signal events per background event
1468 if (!fBkgrdFileNames) return 1;
1469 Int_t nBkgrdFiles = fBkgrdFileNames->GetEntriesFast();
1470 if (nBkgrdFiles == 0) return 1;
1472 // get the number of signal events
1474 AliRunLoader* runLoader =
1475 AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
1476 if (!runLoader) return 1;
1478 nEvents = runLoader->GetNumberOfEvents();
1483 for (Int_t iBkgrdFile = 0; iBkgrdFile < nBkgrdFiles; iBkgrdFile++) {
1484 // get the number of background events
1485 const char* fileName = ((TObjString*)
1486 (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
1487 AliRunLoader* runLoader =
1488 AliRunLoader::Open(fileName, "BKGRD");
1489 if (!runLoader) continue;
1490 Int_t nBkgrdEvents = runLoader->GetNumberOfEvents();
1493 // get or calculate the number of signal per background events
1494 Int_t nSignalPerBkgrd = fBkgrdFileNames->At(iBkgrdFile)->GetUniqueID();
1495 if (nSignalPerBkgrd <= 0) {
1496 nSignalPerBkgrd = (nEvents-1) / nBkgrdEvents + 1;
1497 } else if (result && (result != nSignalPerBkgrd)) {
1498 AliInfo(Form("the number of signal events per background event "
1499 "will be changed from %d to %d for stream %d",
1500 nSignalPerBkgrd, result, iBkgrdFile+1));
1501 nSignalPerBkgrd = result;
1504 if (!result) result = nSignalPerBkgrd;
1505 if (nSignalPerBkgrd * nBkgrdEvents < nEvents) {
1506 AliWarning(Form("not enough background events (%d) for %d signal events "
1507 "using %d signal per background events for stream %d",
1508 nBkgrdEvents, nEvents, nSignalPerBkgrd, iBkgrdFile+1));
1515 //_____________________________________________________________________________
1516 Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
1518 // check whether detName is contained in detectors
1519 // if yes, it is removed from detectors
1521 // check if all detectors are selected
1522 if ((detectors.CompareTo("ALL") == 0) ||
1523 detectors.BeginsWith("ALL ") ||
1524 detectors.EndsWith(" ALL") ||
1525 detectors.Contains(" ALL ")) {
1530 // search for the given detector
1531 Bool_t result = kFALSE;
1532 if ((detectors.CompareTo(detName) == 0) ||
1533 detectors.BeginsWith(detName+" ") ||
1534 detectors.EndsWith(" "+detName) ||
1535 detectors.Contains(" "+detName+" ")) {
1536 detectors.ReplaceAll(detName, "");
1540 // clean up the detectors string
1541 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
1542 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
1543 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
1548 //_____________________________________________________________________________
1549 Bool_t AliSimulation::ConvertRaw2SDigits(const char* rawDirectory, const char* esdFileName)
1552 // Steering routine to convert raw data in directory rawDirectory/ to fake SDigits.
1553 // These can be used for embedding of MC tracks into RAW data using the standard
1554 // merging procedure.
1556 // If an ESD file is given the reconstructed vertex is taken from it and stored in the event header.
1559 AliError("no gAlice object. Restart aliroot and try again.");
1562 if (gAlice->Modules()->GetEntries() > 0) {
1563 AliError("gAlice was already run. Restart aliroot and try again.");
1567 AliInfo(Form("initializing gAlice with config file %s",fConfigFileName.Data()));
1568 StdoutToAliInfo(StderrToAliError(gAlice->Init(fConfigFileName.Data());););
1572 //AliCDBManager* man = AliCDBManager::Instance();
1573 //man->SetRun(0); // Should this come from rawdata header ?
1577 // Get the runloader
1578 AliRunLoader* runLoader = gAlice->GetRunLoader();
1580 // Open esd file if available
1581 TFile* esdFile = TFile::Open(esdFileName);
1582 Bool_t esdOK = (esdFile != 0);
1583 AliESD* esd = new AliESD;
1586 treeESD = (TTree*) esdFile->Get("esdTree");
1588 AliWarning("No ESD tree found");
1591 treeESD->SetBranchAddress("ESD", &esd);
1595 // Create the RawReader
1596 TString fileName(rawDirectory);
1597 AliRawReader* rawReader = 0x0;
1598 if (fileName.EndsWith("/")) {
1599 rawReader = new AliRawReaderFile(fileName);
1600 } else if (fileName.EndsWith(".root")) {
1601 rawReader = new AliRawReaderRoot(fileName);
1602 } else if (!fileName.IsNull()) {
1603 rawReader = new AliRawReaderDate(fileName);
1605 // if (!fEquipIdMap.IsNull() && fRawReader)
1606 // fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
1608 // Get list of detectors
1609 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1612 AliHeader* header = runLoader->GetHeader();
1614 TString detStr = fMakeSDigits;
1618 if (!(rawReader->NextEvent())) break;
1621 for (iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1622 AliModule* det = (AliModule*) detArray->At(iDet);
1623 if (!det || !det->IsActive()) continue;
1624 if (IsSelected(det->GetName(), detStr)) {
1625 AliInfo(Form("Calling Raw2SDigits for %s\n", det->GetName()));
1626 det->Raw2SDigits(rawReader);
1633 // If ESD information available obtain reconstructed vertex and store in header.
1635 treeESD->GetEvent(nev);
1636 const AliESDVertex* esdVertex = esd->GetPrimaryVertex();
1637 Double_t position[3];
1638 esdVertex->GetXYZ(position);
1639 AliGenEventHeader* mcHeader = new AliGenEventHeader("ESD");
1642 for (Int_t i = 0; i < 3; i++) mcV[i] = position[i];
1643 mcHeader->SetPrimaryVertex(mcV);
1644 header->Reset(0,nev);
1645 header->SetGenEventHeader(mcHeader);
1646 printf("***** Saved vertex %f %f %f \n", position[0], position[1], position[2]);
1651 runLoader->TreeE()->Fill();
1652 runLoader->SetNextEvent();
1658 runLoader->CdGAFile();
1659 runLoader->WriteHeader("OVERWRITE");
1660 runLoader->WriteRunLoader();
1665 //_____________________________________________________________________________
1666 Int_t AliSimulation::GetDetIndex(const char* detector)
1668 // return the detector index corresponding to detector
1670 for (index = 0; index < fgkNDetectors ; index++) {
1671 if ( strcmp(detector, fgkDetectorName[index]) == 0 )
1677 //_____________________________________________________________________________
1678 Bool_t AliSimulation::RunHLT()
1680 // Run the HLT simulation
1681 // HLT simulation is implemented in HLT/sim/AliHLTSimulation
1682 // Disabled if fRunHLT is empty, default vaule is "default".
1683 // AliSimulation::SetRunHLT can be used to set the options for HLT simulation
1684 // The default simulation depends on the HLT component libraries and their
1685 // corresponding agents which define components and chains to run. See
1686 // http://web.ift.uib.no/~kjeks/doc/alice-hlt/
1687 // http://web.ift.uib.no/~kjeks/doc/alice-hlt/classAliHLTModuleAgent.html
1689 // The libraries to be loaded can be specified as an option.
1691 // AliSimulation sim;
1692 // sim.SetRunHLT("libAliHLTSample.so");
1694 // will only load <tt>libAliHLTSample.so</tt>
1696 // Other available options:
1697 // \li loglevel=<i>level</i> <br>
1698 // logging level for this processing
1700 // disable redirection of log messages to AliLog class
1701 // \li config=<i>macro</i>
1702 // configuration macro
1703 // \li localrec=<i>configuration</i>
1704 // comma separated list of configurations to be run during simulation
1707 AliRunLoader* pRunLoader = LoadRun("READ");
1708 if (!pRunLoader) return kFALSE;
1710 // initialize CDB storage, run number, set CDB lock
1712 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1715 // load the library dynamically
1716 gSystem->Load(ALIHLTSIMULATION_LIBRARY);
1718 // check for the library version
1719 AliHLTSimulationGetLibraryVersion_t fctVersion=(AliHLTSimulationGetLibraryVersion_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_GET_LIBRARY_VERSION));
1721 AliError(Form("can not load library %s", ALIHLTSIMULATION_LIBRARY));
1724 if (fctVersion()!= ALIHLTSIMULATION_LIBRARY_VERSION) {
1725 AliError(Form("%s version does not match: compiled for version %d, loaded %d", ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_LIBRARY_VERSION, fctVersion()));
1729 // print compile info
1730 typedef void (*CompileInfo)( char*& date, char*& time);
1731 CompileInfo fctInfo=(CompileInfo)gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, "CompileInfo");
1735 (*fctInfo)(date, time);
1736 if (!date) date="unknown";
1737 if (!time) time="unknown";
1738 AliInfo(Form("%s build on %s (%s)", ALIHLTSIMULATION_LIBRARY, date, time));
1740 AliInfo(Form("no build info available for %s", ALIHLTSIMULATION_LIBRARY));
1743 // create instance of the HLT simulation
1744 AliHLTSimulationCreateInstance_t fctCreate=(AliHLTSimulationCreateInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_CREATE_INSTANCE));
1745 AliHLTSimulation* pHLT=NULL;
1746 if (fctCreate==NULL || (pHLT=(fctCreate()))==NULL) {
1747 AliError(Form("can not create instance of HLT simulation (creator %p)", fctCreate));
1751 // init the HLT simulation
1753 if (fRunHLT.CompareTo("default")!=0) options=fRunHLT;
1754 if (!IsSelected("HLT", fWriteRawData)) {
1755 options+=" writerawfiles=";
1757 options+=" writerawfiles=HLT";
1759 AliHLTSimulationInit_t fctInit=(AliHLTSimulationInit_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_INIT));
1760 if (fctInit==NULL || (iResult=(fctInit(pHLT, pRunLoader, options.Data())))<0) {
1761 AliError(Form("can not init HLT simulation: error %d (init %p)", iResult, fctInit));
1763 // run the HLT simulation
1764 AliHLTSimulationRun_t fctRun=(AliHLTSimulationRun_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_RUN));
1765 if (fctRun==NULL || (iResult=(fctRun(pHLT, pRunLoader)))<0) {
1766 AliError(Form("can not run HLT simulation: error %d (run %p)", iResult, fctRun));
1770 // delete the instance
1771 AliHLTSimulationDeleteInstance_t fctDelete=(AliHLTSimulationDeleteInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_DELETE_INSTANCE));
1772 if (fctDelete==NULL || fctDelete(pHLT)<0) {
1773 AliError(Form("can not delete instance of HLT simulation (creator %p)", fctDelete));
1777 return iResult>=0?kTRUE:kFALSE;
1780 //_____________________________________________________________________________
1781 Bool_t AliSimulation::RunQA()
1783 // run the QA on summable hits, digits or digits
1785 fQASteer->SetRunLoader(gAlice->GetRunLoader()) ;
1787 TString detectorsw("") ;
1789 detectorsw = fQASteer->Run(fQADetectors.Data()) ;
1790 if ( detectorsw.IsNull() )
1795 //_____________________________________________________________________________
1796 Bool_t AliSimulation::SetRunQA(TString detAndAction)
1798 // Allows to run QA for a selected set of detectors
1799 // and a selected set of tasks among HITS, SDIGITS and DIGITS
1800 // all selected detectors run the same selected tasks
1802 if (!detAndAction.Contains(":")) {
1803 AliError( Form("%s is a wrong syntax, use \"DetectorList:ActionList\" \n", detAndAction.Data()) ) ;
1807 Int_t colon = detAndAction.Index(":") ;
1808 fQADetectors = detAndAction(0, colon) ;
1809 if (fQADetectors.Contains("ALL") )
1810 fQADetectors = Form("%s %s", fMakeDigits.Data(), fMakeDigitsFromHits.Data()) ;
1811 fQATasks = detAndAction(colon+1, detAndAction.Sizeof() ) ;
1812 if (fQATasks.Contains("ALL") ) {
1813 fQATasks = Form("%d %d %d", AliQA::kHITS, AliQA::kSDIGITS, AliQA::kDIGITS) ;
1815 fQATasks.ToUpper() ;
1817 if ( fQATasks.Contains("HIT") )
1818 tempo = Form("%d ", AliQA::kHITS) ;
1819 if ( fQATasks.Contains("SDIGIT") )
1820 tempo += Form("%d ", AliQA::kSDIGITS) ;
1821 if ( fQATasks.Contains("DIGIT") )
1822 tempo += Form("%d ", AliQA::kDIGITS) ;
1824 if (fQATasks.IsNull()) {
1825 AliInfo("No QA requested\n") ;
1830 TString tempo(fQATasks) ;
1831 tempo.ReplaceAll(Form("%d", AliQA::kHITS), AliQA::GetTaskName(AliQA::kHITS)) ;
1832 tempo.ReplaceAll(Form("%d", AliQA::kSDIGITS), AliQA::GetTaskName(AliQA::kSDIGITS)) ;
1833 tempo.ReplaceAll(Form("%d", AliQA::kDIGITS), AliQA::GetTaskName(AliQA::kDIGITS)) ;
1834 AliInfo( Form("QA will be done on \"%s\" for \"%s\"\n", fQADetectors.Data(), tempo.Data()) ) ;
1836 fQASteer->SetActiveDetectors(fQADetectors) ;
1837 fQASteer->SetTasks(fQATasks) ;
1841 //_____________________________________________________________________________
1842 void AliSimulation::ProcessEnvironmentVars()
1844 // Extract run number and random generator seed from env variables
1846 AliInfo("Processing environment variables");
1848 // Random Number seed
1850 // first check that seed is not already set
1852 if (gSystem->Getenv("CONFIG_SEED")) {
1853 fSeed = atoi(gSystem->Getenv("CONFIG_SEED"));
1856 if (gSystem->Getenv("CONFIG_SEED")) {
1857 AliInfo(Form("Seed for random number generation already set (%d)"
1858 ": CONFIG_SEED variable ignored!", fSeed));
1862 AliInfo(Form("Seed for random number generation = %d ", fSeed));
1866 // first check that run number is not already set
1868 if (gSystem->Getenv("DC_RUN")) {
1869 fRun = atoi(gSystem->Getenv("DC_RUN"));
1872 if (gSystem->Getenv("DC_RUN")) {
1873 AliInfo(Form("Run number already set (%d): DC_RUN variable ignored!", fRun));
1877 AliInfo(Form("Run number = %d", fRun));
1880 //---------------------------------------------------------------------
1882 void AliSimulation::WriteGRPEntry()
1884 // Get the necessary information from galice (generator, trigger etc) and
1885 // write a GRP entry corresponding to the settings in the Config.C used
1886 // note that Hall probes and Cavern and Surface Atmos pressures are not simulated.
1889 AliInfo("Writing global run parameters entry into the OCDB");
1891 AliGRPObject* grpObj = new AliGRPObject();
1893 grpObj->SetRunType("PHYSICS");
1894 grpObj->SetTimeStart(0);
1895 grpObj->SetTimeEnd(9999);
1897 const AliGenerator *gen = gAlice->Generator();
1899 grpObj->SetBeamEnergy(gen->GetEnergyCMS());
1902 gen->GetProjectile(projectile,a,z);
1904 gen->GetTarget(target,a,z);
1905 TString beamType = projectile + "-" + target;
1906 if (!beamType.CompareTo("-")) {
1908 grpObj->SetBeamType("UNKNOWN");
1911 grpObj->SetBeamType(beamType);
1915 AliWarning("Unknown beam type and energy! Setting energy to 0");
1916 grpObj->SetBeamEnergy(0);
1917 grpObj->SetBeamType("UNKNOWN");
1920 UInt_t detectorPattern = 0;
1922 TObjArray *detArray = gAlice->Detectors();
1923 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors-1; iDet++) {
1924 if (detArray->FindObject(AliDAQ::OfflineModuleName(iDet))) {
1925 detectorPattern |= (1 << iDet);
1930 if (!fRunHLT.IsNull())
1931 detectorPattern |= (1 << AliDAQ::kHLTId);
1933 grpObj->SetNumberOfDetectors((Char_t)nDets);
1934 grpObj->SetDetectorMask((Int_t)detectorPattern);
1935 grpObj->SetLHCPeriod("LHC08c");
1936 grpObj->SetLHCState("STABLE_BEAMS");
1937 grpObj->SetLHCLuminosity(0,(AliGRPObject::Stats)0);
1938 grpObj->SetBeamIntensity(0,(AliGRPObject::Stats)0);
1940 AliMagF *field = gAlice->Field();
1941 Float_t solenoidField = TMath::Abs(field->SolenoidField());
1942 Float_t factor = field->Factor();
1943 Float_t l3current = TMath::Abs(factor)*solenoidField*30000./5.;
1944 grpObj->SetL3Current(l3current,(AliGRPObject::Stats)0);
1947 grpObj->SetL3Polarity(0);
1948 grpObj->SetDipolePolarity(0);
1951 grpObj->SetL3Polarity(1);
1952 grpObj->SetDipolePolarity(1);
1955 if (TMath::Abs(factor) != 0)
1956 grpObj->SetDipoleCurrent(6000,(AliGRPObject::Stats)0);
1958 grpObj->SetDipoleCurrent(0,(AliGRPObject::Stats)0);
1960 grpObj->SetCavernTemperature(0,(AliGRPObject::Stats)0);
1962 //grpMap->Add(new TObjString("fCavernPressure"),new TObjString("0")); ---> not inserted in simulation with the new object, since it is now an AliDCSSensor
1964 // Now store the entry in OCDB
1965 AliCDBManager* man = AliCDBManager::Instance();
1967 AliCDBId id("GRP/GRP/Data", man->GetRun(), man->GetRun());
1968 AliCDBMetaData *metadata= new AliCDBMetaData();
1970 metadata->SetResponsible("alice-off@cern.ch");
1971 metadata->SetComment("Automatically produced GRP entry for Monte Carlo");
1973 man->Put(grpObj,id,metadata);