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"
145 ClassImp(AliSimulation)
147 AliSimulation *AliSimulation::fgInstance = 0;
148 const char* AliSimulation::fgkDetectorName[AliSimulation::fgkNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE", "HLT"};
150 //_____________________________________________________________________________
151 AliSimulation::AliSimulation(const char* configFileName,
152 const char* name, const char* title) :
155 fRunGeneration(kTRUE),
156 fRunSimulation(kTRUE),
157 fLoadAlignFromCDB(kTRUE),
158 fLoadAlObjsListOfDets("ALL"),
162 fMakeDigitsFromHits(""),
164 fRawDataFileName(""),
165 fDeleteIntermediateFiles(kFALSE),
166 fWriteSelRawData(kFALSE),
167 fStopOnError(kFALSE),
170 fConfigFileName(configFileName),
171 fGAliceFileName("galice.root"),
173 fBkgrdFileNames(NULL),
174 fAlignObjArray(NULL),
175 fUseBkgrdVertex(kTRUE),
176 fRegionOfInterest(kFALSE),
181 fInitCDBCalled(kFALSE),
182 fInitRunNumberCalled(kFALSE),
183 fSetRunNumberFromDataCalled(kFALSE),
184 fEmbeddingFlag(kFALSE),
190 fWriteGRPEntry(kTRUE)
192 // create simulation object with default parameters
194 SetGAliceFile("galice.root");
197 fQASteer = new AliQADataMakerSteer("sim") ;
198 fQASteer->SetActiveDetectors(fQADetectors) ;
199 fQATasks = Form("%d %d %d", AliQA::kHITS, AliQA::kSDIGITS, AliQA::kDIGITS) ;
200 fQASteer->SetTasks(fQATasks) ;
203 //_____________________________________________________________________________
204 AliSimulation::AliSimulation(const AliSimulation& sim) :
207 fRunGeneration(sim.fRunGeneration),
208 fRunSimulation(sim.fRunSimulation),
209 fLoadAlignFromCDB(sim.fLoadAlignFromCDB),
210 fLoadAlObjsListOfDets(sim.fLoadAlObjsListOfDets),
211 fMakeSDigits(sim.fMakeSDigits),
212 fMakeDigits(sim.fMakeDigits),
213 fMakeTrigger(sim.fMakeTrigger),
214 fMakeDigitsFromHits(sim.fMakeDigitsFromHits),
215 fWriteRawData(sim.fWriteRawData),
216 fRawDataFileName(""),
217 fDeleteIntermediateFiles(kFALSE),
218 fWriteSelRawData(kFALSE),
219 fStopOnError(sim.fStopOnError),
221 fNEvents(sim.fNEvents),
222 fConfigFileName(sim.fConfigFileName),
223 fGAliceFileName(sim.fGAliceFileName),
225 fBkgrdFileNames(NULL),
226 fAlignObjArray(NULL),
227 fUseBkgrdVertex(sim.fUseBkgrdVertex),
228 fRegionOfInterest(sim.fRegionOfInterest),
229 fCDBUri(sim.fCDBUri),
233 fInitCDBCalled(sim.fInitCDBCalled),
234 fInitRunNumberCalled(sim.fInitRunNumberCalled),
235 fSetRunNumberFromDataCalled(sim.fSetRunNumberFromDataCalled),
236 fEmbeddingFlag(sim.fEmbeddingFlag),
237 fQADetectors(sim.fQADetectors),
238 fQATasks(sim.fQATasks),
239 fQASteer(sim.fQASteer),
241 fRunHLT(sim.fRunHLT),
242 fWriteGRPEntry(sim.fWriteGRPEntry)
246 for (Int_t i = 0; i < sim.fEventsPerFile.GetEntriesFast(); i++) {
247 if (!sim.fEventsPerFile[i]) continue;
248 fEventsPerFile.Add(sim.fEventsPerFile[i]->Clone());
251 fBkgrdFileNames = new TObjArray;
252 for (Int_t i = 0; i < sim.fBkgrdFileNames->GetEntriesFast(); i++) {
253 if (!sim.fBkgrdFileNames->At(i)) continue;
254 fBkgrdFileNames->Add(sim.fBkgrdFileNames->At(i)->Clone());
257 for (Int_t i = 0; i < sim.fSpecCDBUri.GetEntriesFast(); i++) {
258 if (sim.fSpecCDBUri[i]) fSpecCDBUri.Add(sim.fSpecCDBUri[i]->Clone());
263 //_____________________________________________________________________________
264 AliSimulation& AliSimulation::operator = (const AliSimulation& sim)
266 // assignment operator
268 this->~AliSimulation();
269 new(this) AliSimulation(sim);
273 //_____________________________________________________________________________
274 AliSimulation::~AliSimulation()
278 fEventsPerFile.Delete();
279 // if(fAlignObjArray) fAlignObjArray->Delete(); // fAlignObjArray->RemoveAll() ???
280 // delete fAlignObjArray; fAlignObjArray=0;
282 if (fBkgrdFileNames) {
283 fBkgrdFileNames->Delete();
284 delete fBkgrdFileNames;
287 fSpecCDBUri.Delete();
288 if (fgInstance==this) fgInstance = 0;
292 AliCodeTimer::Instance()->Print();
296 //_____________________________________________________________________________
297 void AliSimulation::SetNumberOfEvents(Int_t nEvents)
299 // set the number of events for one run
304 //_____________________________________________________________________________
305 void AliSimulation::InitCDB()
307 // activate a default CDB storage
308 // First check if we have any CDB storage set, because it is used
309 // to retrieve the calibration and alignment constants
311 if (fInitCDBCalled) return;
312 fInitCDBCalled = kTRUE;
314 AliCDBManager* man = AliCDBManager::Instance();
315 if (man->IsDefaultStorageSet())
317 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
318 AliWarning("Default CDB storage has been already set !");
319 AliWarning(Form("Ignoring the default storage declared in AliSimulation: %s",fCDBUri.Data()));
320 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
321 fCDBUri = man->GetDefaultStorage()->GetURI();
324 if (fCDBUri.Length() > 0)
326 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
327 AliDebug(2, Form("Default CDB storage is set to: %s", fCDBUri.Data()));
328 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
330 fCDBUri="local://$ALICE_ROOT";
331 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
332 AliWarning("Default CDB storage not yet set !!!!");
333 AliWarning(Form("Setting it now to: %s", fCDBUri.Data()));
334 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
337 man->SetDefaultStorage(fCDBUri);
340 // Now activate the detector specific CDB storage locations
341 for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
342 TObject* obj = fSpecCDBUri[i];
344 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
345 AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
346 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
347 man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
352 //_____________________________________________________________________________
353 void AliSimulation::InitRunNumber(){
354 // check run number. If not set, set it to 0 !!!!
356 if (fInitRunNumberCalled) return;
357 fInitRunNumberCalled = kTRUE;
359 AliCDBManager* man = AliCDBManager::Instance();
360 if (man->GetRun() >= 0)
362 AliFatal(Form("Run number cannot be set in AliCDBManager before start of simulation: "
363 "Use external variable DC_RUN or AliSimulation::SetRun()!"));
367 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
368 AliDebug(2, Form("Setting CDB run number to: %d",fRun));
369 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
372 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
373 AliWarning("Run number not yet set !!!!");
374 AliWarning(Form("Setting it now to: %d", fRun));
375 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
384 //_____________________________________________________________________________
385 void AliSimulation::SetCDBLock() {
386 // Set CDB lock: from now on it is forbidden to reset the run number
387 // or the default storage or to activate any further storage!
389 AliCDBManager::Instance()->SetLock(1);
392 //_____________________________________________________________________________
393 void AliSimulation::SetDefaultStorage(const char* uri) {
394 // Store the desired default CDB storage location
395 // Activate it later within the Run() method
401 //_____________________________________________________________________________
402 void AliSimulation::SetSpecificStorage(const char* calibType, const char* uri) {
403 // Store a detector-specific CDB storage location
404 // Activate it later within the Run() method
406 AliCDBPath aPath(calibType);
407 if(!aPath.IsValid()){
408 AliError(Form("Not a valid path: %s", calibType));
412 TObject* obj = fSpecCDBUri.FindObject(calibType);
413 if (obj) fSpecCDBUri.Remove(obj);
414 fSpecCDBUri.Add(new TNamed(calibType, uri));
418 //_____________________________________________________________________________
419 void AliSimulation::SetRunNumber(Int_t run)
422 // Activate it later within the Run() method
427 //_____________________________________________________________________________
428 void AliSimulation::SetSeed(Int_t seed)
431 // Activate it later within the Run() method
436 //_____________________________________________________________________________
437 Bool_t AliSimulation::SetRunNumberFromData()
439 // Set the CDB manager run number
440 // The run number is retrieved from gAlice
442 if (fSetRunNumberFromDataCalled) return kTRUE;
443 fSetRunNumberFromDataCalled = kTRUE;
445 AliCDBManager* man = AliCDBManager::Instance();
446 Int_t runData = -1, runCDB = -1;
448 AliRunLoader* runLoader = LoadRun("READ");
449 if (!runLoader) return kFALSE;
451 runData = runLoader->GetAliRun()->GetHeader()->GetRun();
455 runCDB = man->GetRun();
457 if (runCDB != runData) {
458 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
459 AliWarning(Form("A run number was previously set in AliCDBManager: %d !", runCDB));
460 AliWarning(Form("It will be replaced with the run number got from run header: %d !", runData));
461 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
466 man->SetRun(runData);
469 if(man->GetRun() < 0) {
470 AliError("Run number not properly initalized!");
479 //_____________________________________________________________________________
480 void AliSimulation::SetConfigFile(const char* fileName)
482 // set the name of the config file
484 fConfigFileName = fileName;
487 //_____________________________________________________________________________
488 void AliSimulation::SetGAliceFile(const char* fileName)
490 // set the name of the galice file
491 // the path is converted to an absolute one if it is relative
493 fGAliceFileName = fileName;
494 if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
495 char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
497 fGAliceFileName = absFileName;
498 delete[] absFileName;
501 AliDebug(2, Form("galice file name set to %s", fileName));
504 //_____________________________________________________________________________
505 void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
508 // set the number of events per file for the given detector and data type
509 // ("Hits", "Summable Digits", "Digits", "Reconstructed Points" or "Tracks")
511 TNamed* obj = new TNamed(detector, type);
512 obj->SetUniqueID(nEvents);
513 fEventsPerFile.Add(obj);
516 //_____________________________________________________________________________
517 Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
519 // Read the alignment objects from CDB.
520 // Each detector is supposed to have the
521 // alignment objects in DET/Align/Data CDB path.
522 // All the detector objects are then collected,
523 // sorted by geometry level (starting from ALIC) and
524 // then applied to the TGeo geometry.
525 // Finally an overlaps check is performed.
527 if (!AliGeomManager::GetGeometry() || !AliGeomManager::GetGeometry()->IsClosed()) {
528 AliError("Can't apply the misalignment! Geometry is not loaded or it is still opened!");
532 // initialize CDB storage, run number, set CDB lock
534 // if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
537 Bool_t delRunLoader = kFALSE;
539 runLoader = LoadRun("READ");
540 if (!runLoader) return kFALSE;
541 delRunLoader = kTRUE;
544 // Export ideal geometry
545 if(!gAlice->IsRootGeometry()) AliGeomManager::GetGeometry()->Export("geometry.root");
547 // Load alignment data from CDB and apply to geometry through AliGeomManager
548 if(fLoadAlignFromCDB){
550 TString detStr = fLoadAlObjsListOfDets;
551 TString loadAlObjsListOfDets = "";
553 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
554 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
555 AliModule* det = (AliModule*) detArray->At(iDet);
556 if (!det || !det->IsActive()) continue;
557 if (IsSelected(det->GetName(), detStr)) {
558 //add det to list of dets to be aligned from CDB
559 loadAlObjsListOfDets += det->GetName();
560 loadAlObjsListOfDets += " ";
562 } // end loop over detectors
563 loadAlObjsListOfDets.Prepend("GRP "); //add alignment objects for non-sensitive modules
564 AliGeomManager::ApplyAlignObjsFromCDB(loadAlObjsListOfDets.Data());
566 // Check if the array with alignment objects was
567 // provided by the user. If yes, apply the objects
568 // to the present TGeo geometry
569 if (fAlignObjArray) {
570 if (AliGeomManager::ApplyAlignObjsToGeom(*fAlignObjArray) == kFALSE) {
571 AliError("The misalignment of one or more volumes failed!"
572 "Compare the list of simulated detectors and the list of detector alignment data!");
573 if (delRunLoader) delete runLoader;
579 // Update the internal geometry of modules (ITS needs it)
580 TString detStr = fLoadAlObjsListOfDets;
581 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
582 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
584 AliModule* det = (AliModule*) detArray->At(iDet);
585 if (!det || !det->IsActive()) continue;
586 if (IsSelected(det->GetName(), detStr)) {
587 det->UpdateInternalGeometry();
589 } // end loop over detectors
592 if (delRunLoader) delete runLoader;
597 //_____________________________________________________________________________
598 void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
600 // add a file with background events for merging
602 TObjString* fileNameStr = new TObjString(fileName);
603 fileNameStr->SetUniqueID(nSignalPerBkgrd);
604 if (!fBkgrdFileNames) fBkgrdFileNames = new TObjArray;
605 fBkgrdFileNames->Add(fileNameStr);
608 void AliSimulation::EmbedInto(const char* fileName, Int_t nSignalPerBkgrd)
610 // add a file with background events for embeddin
611 MergeWith(fileName, nSignalPerBkgrd);
612 fEmbeddingFlag = kTRUE;
615 //_____________________________________________________________________________
616 Bool_t AliSimulation::Run(Int_t nEvents)
618 // run the generation, simulation and digitization
623 // Load run number and seed from environmental vars
624 ProcessEnvironmentVars();
626 gRandom->SetSeed(fSeed);
628 if (nEvents > 0) fNEvents = nEvents;
630 // generation and simulation -> hits
631 if (fRunGeneration) {
632 if (!RunSimulation()) if (fStopOnError) return kFALSE;
635 // initialize CDB storage from external environment
636 // (either CDB manager or AliSimulation setters),
637 // if not already done in RunSimulation()
640 // Set run number in CDBManager from data
641 // From this point on the run number must be always loaded from data!
642 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
644 // Set CDB lock: from now on it is forbidden to reset the run number
645 // or the default storage or to activate any further storage!
648 // If RunSimulation was not called, load the geometry and misalign it
649 if (!AliGeomManager::GetGeometry()) {
650 // Initialize the geometry manager
651 AliGeomManager::LoadGeometry("geometry.root");
653 // // Check that the consistency of symbolic names for the activated subdetectors
654 // // in the geometry loaded by AliGeomManager
655 // AliRunLoader* runLoader = LoadRun("READ");
656 // if (!runLoader) return kFALSE;
658 // TString detsToBeChecked = "";
659 // TObjArray* detArray = runLoader->GetAliRun()->Detectors();
660 // for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
661 // AliModule* det = (AliModule*) detArray->At(iDet);
662 // if (!det || !det->IsActive()) continue;
663 // detsToBeChecked += det->GetName();
664 // detsToBeChecked += " ";
665 // } // end loop over detectors
666 // if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
667 if(!AliGeomManager::CheckSymNamesLUT("ALL"))
668 AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
670 if (!AliGeomManager::GetGeometry()) if (fStopOnError) return kFALSE;
672 if(!MisalignGeometry()) if (fStopOnError) return kFALSE;
676 // hits -> summable digits
677 AliSysInfo::AddStamp("Start_sdigitization");
678 if (!fMakeSDigits.IsNull()) {
679 if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
682 AliSysInfo::AddStamp("Stop_sdigitization");
684 AliSysInfo::AddStamp("Start_digitization");
685 // summable digits -> digits
686 if (!fMakeDigits.IsNull()) {
687 if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
688 if (fStopOnError) return kFALSE;
691 AliSysInfo::AddStamp("Stop_digitization");
696 if (!fMakeDigitsFromHits.IsNull()) {
697 if (fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
698 AliWarning(Form("Merging and direct creation of digits from hits "
699 "was selected for some detectors. "
700 "No merging will be done for the following detectors: %s",
701 fMakeDigitsFromHits.Data()));
703 if (!RunHitsDigitization(fMakeDigitsFromHits)) {
704 if (fStopOnError) return kFALSE;
711 if (!RunTrigger(fMakeTrigger,fMakeDigits)) {
712 if (fStopOnError) return kFALSE;
717 // digits -> raw data
718 if (!fWriteRawData.IsNull()) {
719 if (!WriteRawData(fWriteRawData, fRawDataFileName,
720 fDeleteIntermediateFiles,fWriteSelRawData)) {
721 if (fStopOnError) return kFALSE;
727 // run HLT simulation
728 if (!fRunHLT.IsNull()) {
730 if (fStopOnError) return kFALSE;
736 Bool_t rv = RunQA() ;
742 // Cleanup of CDB manager: cache and active storages!
743 AliCDBManager::Instance()->ClearCache();
748 //_____________________________________________________________________________
749 Bool_t AliSimulation::RunTrigger(const char* config, const char* detectors)
755 // initialize CDB storage from external environment
756 // (either CDB manager or AliSimulation setters),
757 // if not already done in RunSimulation()
760 // Set run number in CDBManager from data
761 // From this point on the run number must be always loaded from data!
762 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
764 // Set CDB lock: from now on it is forbidden to reset the run number
765 // or the default storage or to activate any further storage!
768 AliRunLoader* runLoader = LoadRun("READ");
769 if (!runLoader) return kFALSE;
770 TString trconfiguration = config;
772 if (trconfiguration.IsNull()) {
773 if (gAlice->GetTriggerDescriptor() != "") {
774 trconfiguration = gAlice->GetTriggerDescriptor();
777 AliWarning("No trigger descriptor is specified. Loading the one that is in the CDB.");
780 runLoader->MakeTree( "GG" );
781 AliCentralTrigger* aCTP = runLoader->GetTrigger();
782 // Load Configuration
783 if (!aCTP->LoadConfiguration( trconfiguration ))
787 if( !aCTP->RunTrigger( runLoader , detectors ) ) {
799 //_____________________________________________________________________________
800 Bool_t AliSimulation::WriteTriggerRawData()
802 // Writes the CTP (trigger) DDL raw data
803 // Details of the format are given in the
804 // trigger TDR - pages 134 and 135.
805 AliCTPRawData writer;
811 //_____________________________________________________________________________
812 Bool_t AliSimulation::RunSimulation(Int_t nEvents)
814 // run the generation and simulation
818 // initialize CDB storage and run number from external environment
819 // (either CDB manager or AliSimulation setters)
825 AliError("no gAlice object. Restart aliroot and try again.");
828 if (gAlice->Modules()->GetEntries() > 0) {
829 AliError("gAlice was already run. Restart aliroot and try again.");
833 AliInfo(Form("initializing gAlice with config file %s",
834 fConfigFileName.Data()));
835 StdoutToAliInfo(StderrToAliError(
836 gAlice->Init(fConfigFileName.Data());
839 // Get the trigger descriptor string
840 // Either from AliSimulation or from
842 if (fMakeTrigger.IsNull()) {
843 if (gAlice->GetTriggerDescriptor() != "")
844 fMakeTrigger = gAlice->GetTriggerDescriptor();
847 gAlice->SetTriggerDescriptor(fMakeTrigger.Data());
849 // Set run number in CDBManager
850 AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
852 AliRunLoader* runLoader = gAlice->GetRunLoader();
854 AliError(Form("gAlice has no run loader object. "
855 "Check your config file: %s", fConfigFileName.Data()));
858 SetGAliceFile(runLoader->GetFileName());
861 #if ROOT_VERSION_CODE < 331527
862 AliGeomManager::SetGeometry(gGeoManager);
864 // Check that the consistency of symbolic names for the activated subdetectors
865 // in the geometry loaded by AliGeomManager
866 TString detsToBeChecked = "";
867 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
868 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
869 AliModule* det = (AliModule*) detArray->At(iDet);
870 if (!det || !det->IsActive()) continue;
871 detsToBeChecked += det->GetName();
872 detsToBeChecked += " ";
873 } // end loop over detectors
874 if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
875 AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
876 MisalignGeometry(runLoader);
879 // AliRunLoader* runLoader = gAlice->GetRunLoader();
881 // AliError(Form("gAlice has no run loader object. "
882 // "Check your config file: %s", fConfigFileName.Data()));
885 // SetGAliceFile(runLoader->GetFileName());
887 if (!gAlice->Generator()) {
888 AliError(Form("gAlice has no generator object. "
889 "Check your config file: %s", fConfigFileName.Data()));
893 // Write GRP entry corresponding to the setting found in Cofig.C
897 if (nEvents <= 0) nEvents = fNEvents;
899 // get vertex from background file in case of merging
900 if (fUseBkgrdVertex &&
901 fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
902 Int_t signalPerBkgrd = GetNSignalPerBkgrd(nEvents);
903 const char* fileName = ((TObjString*)
904 (fBkgrdFileNames->At(0)))->GetName();
905 AliInfo(Form("The vertex will be taken from the background "
906 "file %s with nSignalPerBackground = %d",
907 fileName, signalPerBkgrd));
908 AliVertexGenFile* vtxGen = new AliVertexGenFile(fileName, signalPerBkgrd);
909 gAlice->Generator()->SetVertexGenerator(vtxGen);
912 if (!fRunSimulation) {
913 gAlice->Generator()->SetTrackingFlag(0);
916 // set the number of events per file for given detectors and data types
917 for (Int_t i = 0; i < fEventsPerFile.GetEntriesFast(); i++) {
918 if (!fEventsPerFile[i]) continue;
919 const char* detName = fEventsPerFile[i]->GetName();
920 const char* typeName = fEventsPerFile[i]->GetTitle();
921 TString loaderName(detName);
922 loaderName += "Loader";
923 AliLoader* loader = runLoader->GetLoader(loaderName);
925 AliError(Form("RunSimulation", "no loader for %s found\n"
926 "Number of events per file not set for %s %s",
927 detName, typeName, detName));
930 AliDataLoader* dataLoader =
931 loader->GetDataLoader(typeName);
933 AliError(Form("no data loader for %s found\n"
934 "Number of events per file not set for %s %s",
935 typeName, detName, typeName));
938 dataLoader->SetNumberOfEventsPerFile(fEventsPerFile[i]->GetUniqueID());
939 AliDebug(1, Form("number of events per file set to %d for %s %s",
940 fEventsPerFile[i]->GetUniqueID(), detName, typeName));
943 AliInfo("running gAlice");
944 AliSysInfo::AddStamp("Start_simulation");
945 StdoutToAliInfo(StderrToAliError(
946 gAlice->Run(nEvents);
948 AliSysInfo::AddStamp("Stop_simulation");
954 //_____________________________________________________________________________
955 Bool_t AliSimulation::RunSDigitization(const char* detectors)
957 // run the digitization and produce summable digits
958 static Int_t eventNr=0;
961 // initialize CDB storage, run number, set CDB lock
963 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
966 AliRunLoader* runLoader = LoadRun();
967 if (!runLoader) return kFALSE;
969 TString detStr = detectors;
970 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
971 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
972 AliModule* det = (AliModule*) detArray->At(iDet);
973 if (!det || !det->IsActive()) continue;
974 if (IsSelected(det->GetName(), detStr)) {
975 AliInfo(Form("creating summable digits for %s", det->GetName()));
976 AliCodeTimerAuto(Form("creating summable digits for %s", det->GetName()));
978 AliSysInfo::AddStamp(Form("Digit_%s_%d",det->GetName(),eventNr), 0,1, eventNr);
982 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
983 AliError(Form("the following detectors were not found: %s",
985 if (fStopOnError) return kFALSE;
994 //_____________________________________________________________________________
995 Bool_t AliSimulation::RunDigitization(const char* detectors,
996 const char* excludeDetectors)
998 // run the digitization and produce digits from sdigits
1000 AliCodeTimerAuto("")
1002 // initialize CDB storage, run number, set CDB lock
1004 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1007 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
1008 if (gAlice) delete gAlice;
1012 if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
1013 Int_t signalPerBkgrd = GetNSignalPerBkgrd();
1014 AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
1015 // manager->SetEmbeddingFlag(fEmbeddingFlag);
1016 manager->SetInputStream(0, fGAliceFileName.Data());
1017 for (Int_t iStream = 1; iStream < nStreams; iStream++) {
1018 const char* fileName = ((TObjString*)
1019 (fBkgrdFileNames->At(iStream-1)))->GetName();
1020 manager->SetInputStream(iStream, fileName);
1023 TString detStr = detectors;
1024 TString detExcl = excludeDetectors;
1025 manager->GetInputStream(0)->ImportgAlice();
1026 AliRunLoader* runLoader =
1027 AliRunLoader::GetRunLoader(manager->GetInputStream(0)->GetFolderName());
1028 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1029 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1030 AliModule* det = (AliModule*) detArray->At(iDet);
1031 if (!det || !det->IsActive()) continue;
1032 if (IsSelected(det->GetName(), detStr) &&
1033 !IsSelected(det->GetName(), detExcl)) {
1034 AliDigitizer* digitizer = det->CreateDigitizer(manager);
1037 AliError(Form("no digitizer for %s", det->GetName()));
1038 if (fStopOnError) return kFALSE;
1040 digitizer->SetRegionOfInterest(fRegionOfInterest);
1045 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1046 AliError(Form("the following detectors were not found: %s",
1048 if (fStopOnError) return kFALSE;
1051 if (!manager->GetListOfTasks()->IsEmpty()) {
1052 AliInfo("executing digitization");
1061 //_____________________________________________________________________________
1062 Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
1064 // run the digitization and produce digits from hits
1066 AliCodeTimerAuto("")
1068 // initialize CDB storage, run number, set CDB lock
1070 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1073 AliRunLoader* runLoader = LoadRun("READ");
1074 if (!runLoader) return kFALSE;
1076 TString detStr = detectors;
1077 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1078 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1079 AliModule* det = (AliModule*) detArray->At(iDet);
1080 if (!det || !det->IsActive()) continue;
1081 if (IsSelected(det->GetName(), detStr)) {
1082 AliInfo(Form("creating digits from hits for %s", det->GetName()));
1087 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1088 AliError(Form("the following detectors were not found: %s",
1090 if (fStopOnError) return kFALSE;
1094 //PH Temporary fix to avoid interference with the PHOS loder/getter
1095 //PH The problem has to be solved in more general way 09/06/05
1100 //_____________________________________________________________________________
1101 Bool_t AliSimulation::WriteRawData(const char* detectors,
1102 const char* fileName,
1103 Bool_t deleteIntermediateFiles,
1106 // convert the digits to raw data
1107 // First DDL raw data files for the given detectors are created.
1108 // If a file name is given, the DDL files are then converted to a DATE file.
1109 // If deleteIntermediateFiles is true, the DDL raw files are deleted
1111 // If the file name has the extension ".root", the DATE file is converted
1113 // If deleteIntermediateFiles is true, the DATE file is deleted afterwards.
1114 // 'selrawdata' flag can be used to enable writing of detectors raw data
1115 // accoring to the trigger cluster.
1117 AliCodeTimerAuto("")
1119 TString detStr = detectors;
1120 if (IsSelected("HLT", detStr))
1122 // Do nothing. "HLT" will be removed from detStr because the HLT raw
1123 // data files are generated in RunHLT.
1126 if (!WriteRawFiles(detStr.Data())) {
1127 if (fStopOnError) return kFALSE;
1130 TString dateFileName(fileName);
1131 if (!dateFileName.IsNull()) {
1132 Bool_t rootOutput = dateFileName.EndsWith(".root");
1133 if (rootOutput) dateFileName += ".date";
1134 TString selDateFileName;
1136 selDateFileName = "selected.";
1137 selDateFileName+= dateFileName;
1139 if (!ConvertRawFilesToDate(dateFileName,selDateFileName)) {
1140 if (fStopOnError) return kFALSE;
1142 if (deleteIntermediateFiles) {
1143 AliRunLoader* runLoader = LoadRun("READ");
1144 if (runLoader) for (Int_t iEvent = 0;
1145 iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1147 sprintf(command, "rm -r raw%d", iEvent);
1148 gSystem->Exec(command);
1153 if (!ConvertDateToRoot(dateFileName, fileName)) {
1154 if (fStopOnError) return kFALSE;
1156 if (deleteIntermediateFiles) {
1157 gSystem->Unlink(dateFileName);
1160 TString selFileName = "selected.";
1161 selFileName += fileName;
1162 if (!ConvertDateToRoot(selDateFileName, selFileName)) {
1163 if (fStopOnError) return kFALSE;
1165 if (deleteIntermediateFiles) {
1166 gSystem->Unlink(selDateFileName);
1175 //_____________________________________________________________________________
1176 Bool_t AliSimulation::WriteRawFiles(const char* detectors)
1178 // convert the digits to raw data DDL files
1180 AliCodeTimerAuto("")
1182 AliRunLoader* runLoader = LoadRun("READ");
1183 if (!runLoader) return kFALSE;
1185 // write raw data to DDL files
1186 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1187 AliInfo(Form("processing event %d", iEvent));
1188 runLoader->GetEvent(iEvent);
1189 TString baseDir = gSystem->WorkingDirectory();
1191 sprintf(dirName, "raw%d", iEvent);
1192 gSystem->MakeDirectory(dirName);
1193 if (!gSystem->ChangeDirectory(dirName)) {
1194 AliError(Form("couldn't change to directory %s", dirName));
1195 if (fStopOnError) return kFALSE; else continue;
1198 ofstream runNbFile(Form("run%u",runLoader->GetHeader()->GetRun()));
1201 TString detStr = detectors;
1202 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1203 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1204 AliModule* det = (AliModule*) detArray->At(iDet);
1205 if (!det || !det->IsActive()) continue;
1206 if (IsSelected(det->GetName(), detStr)) {
1207 AliInfo(Form("creating raw data from digits for %s", det->GetName()));
1212 if (!WriteTriggerRawData())
1213 if (fStopOnError) return kFALSE;
1215 gSystem->ChangeDirectory(baseDir);
1216 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1217 AliError(Form("the following detectors were not found: %s",
1219 if (fStopOnError) return kFALSE;
1228 //_____________________________________________________________________________
1229 Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName,
1230 const char* selDateFileName)
1232 // convert raw data DDL files to a DATE file with the program "dateStream"
1233 // The second argument is not empty when the user decides to write
1234 // the detectors raw data according to the trigger cluster.
1236 AliCodeTimerAuto("")
1238 char* path = gSystem->Which(gSystem->Getenv("PATH"), "dateStream");
1240 AliError("the program dateStream was not found");
1241 if (fStopOnError) return kFALSE;
1246 AliRunLoader* runLoader = LoadRun("READ");
1247 if (!runLoader) return kFALSE;
1249 AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
1250 Bool_t selrawdata = kFALSE;
1251 if (strcmp(selDateFileName,"") != 0) selrawdata = kTRUE;
1254 // Note the option -s. It is used in order to avoid
1255 // the generation of SOR/EOR events.
1256 sprintf(command, "dateStream -c -s -D -o %s -# %d -C -run %d",
1257 dateFileName, runLoader->GetNumberOfEvents(),runLoader->GetHeader()->GetRun());
1258 FILE* pipe = gSystem->OpenPipe(command, "w");
1260 Int_t selEvents = 0;
1261 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1263 UInt_t detectorPattern = 0;
1264 runLoader->GetEvent(iEvent);
1265 if (!runLoader->LoadTrigger()) {
1266 AliCentralTrigger *aCTP = runLoader->GetTrigger();
1267 detectorPattern = aCTP->GetClusterMask();
1268 // Check if the event was triggered by CTP
1270 if (aCTP->GetClassMask()) selEvents++;
1274 AliWarning("No trigger can be loaded! Some fields in the event header will be empty !");
1276 AliWarning("No trigger can be loaded! Writing of selected raw data is abandoned !");
1277 selrawdata = kFALSE;
1281 fprintf(pipe, "GDC DetectorPattern %u\n", detectorPattern);
1285 // loop over detectors and DDLs
1286 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1287 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
1289 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
1290 Int_t ldcID = Int_t(ldc + 0.0001);
1291 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
1293 char rawFileName[256];
1294 sprintf(rawFileName, "raw%d/%s",
1295 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
1297 // check existence and size of raw data file
1298 FILE* file = fopen(rawFileName, "rb");
1299 if (!file) continue;
1300 fseek(file, 0, SEEK_END);
1301 unsigned long size = ftell(file);
1303 if (!size) continue;
1305 if (ldcID != prevLDC) {
1306 fprintf(pipe, " LDC Id %d\n", ldcID);
1309 fprintf(pipe, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1314 Int_t result = gSystem->ClosePipe(pipe);
1316 if (!(selrawdata && selEvents > 0)) {
1318 return (result == 0);
1321 AliInfo(Form("converting selected by trigger cluster raw data DDL files to DATE file %s", selDateFileName));
1323 sprintf(command, "dateStream -c -s -D -o %s -# %d -C -run %d",
1324 selDateFileName,selEvents,runLoader->GetHeader()->GetRun());
1325 FILE* pipe2 = gSystem->OpenPipe(command, "w");
1327 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1329 // Get the trigger decision and cluster
1330 UInt_t detectorPattern = 0;
1332 runLoader->GetEvent(iEvent);
1333 if (!runLoader->LoadTrigger()) {
1334 AliCentralTrigger *aCTP = runLoader->GetTrigger();
1335 if (aCTP->GetClassMask() == 0) continue;
1336 detectorPattern = aCTP->GetClusterMask();
1337 detClust = AliDAQ::ListOfTriggeredDetectors(detectorPattern);
1338 AliInfo(Form("List of detectors to be read out: %s",detClust.Data()));
1341 fprintf(pipe2, "GDC DetectorPattern %u\n", detectorPattern);
1345 // loop over detectors and DDLs
1346 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1347 // Write only raw data from detectors that
1348 // are contained in the trigger cluster(s)
1349 if (!IsSelected(AliDAQ::DetectorName(iDet),detClust)) continue;
1351 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
1353 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
1354 Int_t ldcID = Int_t(ldc + 0.0001);
1355 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
1357 char rawFileName[256];
1358 sprintf(rawFileName, "raw%d/%s",
1359 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
1361 // check existence and size of raw data file
1362 FILE* file = fopen(rawFileName, "rb");
1363 if (!file) continue;
1364 fseek(file, 0, SEEK_END);
1365 unsigned long size = ftell(file);
1367 if (!size) continue;
1369 if (ldcID != prevLDC) {
1370 fprintf(pipe2, " LDC Id %d\n", ldcID);
1373 fprintf(pipe2, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1378 Int_t result2 = gSystem->ClosePipe(pipe2);
1381 return ((result == 0) && (result2 == 0));
1384 //_____________________________________________________________________________
1385 Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
1386 const char* rootFileName)
1388 // convert a DATE file to a root file with the program "alimdc"
1391 const Int_t kDBSize = 2000000000;
1392 const Int_t kTagDBSize = 1000000000;
1393 const Bool_t kFilter = kFALSE;
1394 const Int_t kCompression = 1;
1396 char* path = gSystem->Which(gSystem->Getenv("PATH"), "alimdc");
1398 AliError("the program alimdc was not found");
1399 if (fStopOnError) return kFALSE;
1404 AliInfo(Form("converting DATE file %s to root file %s",
1405 dateFileName, rootFileName));
1407 const char* rawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
1408 const char* tagDBFS = "/tmp/mdc1/tags";
1410 // User defined file system locations
1411 if (gSystem->Getenv("ALIMDC_RAWDB1"))
1412 rawDBFS[0] = gSystem->Getenv("ALIMDC_RAWDB1");
1413 if (gSystem->Getenv("ALIMDC_RAWDB2"))
1414 rawDBFS[1] = gSystem->Getenv("ALIMDC_RAWDB2");
1415 if (gSystem->Getenv("ALIMDC_TAGDB"))
1416 tagDBFS = gSystem->Getenv("ALIMDC_TAGDB");
1418 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1419 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1420 gSystem->Exec(Form("rm -rf %s",tagDBFS));
1422 gSystem->Exec(Form("mkdir %s",rawDBFS[0]));
1423 gSystem->Exec(Form("mkdir %s",rawDBFS[1]));
1424 gSystem->Exec(Form("mkdir %s",tagDBFS));
1426 Int_t result = gSystem->Exec(Form("alimdc %d %d %d %d %s",
1427 kDBSize, kTagDBSize, kFilter, kCompression, dateFileName));
1428 gSystem->Exec(Form("mv %s/*.root %s", rawDBFS[0], rootFileName));
1430 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1431 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1432 gSystem->Exec(Form("rm -rf %s",tagDBFS));
1434 return (result == 0);
1438 //_____________________________________________________________________________
1439 AliRunLoader* AliSimulation::LoadRun(const char* mode) const
1441 // delete existing run loaders, open a new one and load gAlice
1443 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
1444 AliRunLoader* runLoader =
1445 AliRunLoader::Open(fGAliceFileName.Data(),
1446 AliConfig::GetDefaultEventFolderName(), mode);
1448 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
1451 runLoader->LoadgAlice();
1452 runLoader->LoadHeader();
1453 gAlice = runLoader->GetAliRun();
1455 AliError(Form("no gAlice object found in file %s",
1456 fGAliceFileName.Data()));
1462 //_____________________________________________________________________________
1463 Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
1465 // get or calculate the number of signal events per background event
1467 if (!fBkgrdFileNames) return 1;
1468 Int_t nBkgrdFiles = fBkgrdFileNames->GetEntriesFast();
1469 if (nBkgrdFiles == 0) return 1;
1471 // get the number of signal events
1473 AliRunLoader* runLoader =
1474 AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
1475 if (!runLoader) return 1;
1477 nEvents = runLoader->GetNumberOfEvents();
1482 for (Int_t iBkgrdFile = 0; iBkgrdFile < nBkgrdFiles; iBkgrdFile++) {
1483 // get the number of background events
1484 const char* fileName = ((TObjString*)
1485 (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
1486 AliRunLoader* runLoader =
1487 AliRunLoader::Open(fileName, "BKGRD");
1488 if (!runLoader) continue;
1489 Int_t nBkgrdEvents = runLoader->GetNumberOfEvents();
1492 // get or calculate the number of signal per background events
1493 Int_t nSignalPerBkgrd = fBkgrdFileNames->At(iBkgrdFile)->GetUniqueID();
1494 if (nSignalPerBkgrd <= 0) {
1495 nSignalPerBkgrd = (nEvents-1) / nBkgrdEvents + 1;
1496 } else if (result && (result != nSignalPerBkgrd)) {
1497 AliInfo(Form("the number of signal events per background event "
1498 "will be changed from %d to %d for stream %d",
1499 nSignalPerBkgrd, result, iBkgrdFile+1));
1500 nSignalPerBkgrd = result;
1503 if (!result) result = nSignalPerBkgrd;
1504 if (nSignalPerBkgrd * nBkgrdEvents < nEvents) {
1505 AliWarning(Form("not enough background events (%d) for %d signal events "
1506 "using %d signal per background events for stream %d",
1507 nBkgrdEvents, nEvents, nSignalPerBkgrd, iBkgrdFile+1));
1514 //_____________________________________________________________________________
1515 Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
1517 // check whether detName is contained in detectors
1518 // if yes, it is removed from detectors
1520 // check if all detectors are selected
1521 if ((detectors.CompareTo("ALL") == 0) ||
1522 detectors.BeginsWith("ALL ") ||
1523 detectors.EndsWith(" ALL") ||
1524 detectors.Contains(" ALL ")) {
1529 // search for the given detector
1530 Bool_t result = kFALSE;
1531 if ((detectors.CompareTo(detName) == 0) ||
1532 detectors.BeginsWith(detName+" ") ||
1533 detectors.EndsWith(" "+detName) ||
1534 detectors.Contains(" "+detName+" ")) {
1535 detectors.ReplaceAll(detName, "");
1539 // clean up the detectors string
1540 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
1541 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
1542 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
1547 //_____________________________________________________________________________
1548 Bool_t AliSimulation::ConvertRaw2SDigits(const char* rawDirectory, const char* esdFileName)
1551 // Steering routine to convert raw data in directory rawDirectory/ to fake SDigits.
1552 // These can be used for embedding of MC tracks into RAW data using the standard
1553 // merging procedure.
1555 // If an ESD file is given the reconstructed vertex is taken from it and stored in the event header.
1558 AliError("no gAlice object. Restart aliroot and try again.");
1561 if (gAlice->Modules()->GetEntries() > 0) {
1562 AliError("gAlice was already run. Restart aliroot and try again.");
1566 AliInfo(Form("initializing gAlice with config file %s",fConfigFileName.Data()));
1567 StdoutToAliInfo(StderrToAliError(gAlice->Init(fConfigFileName.Data());););
1571 //AliCDBManager* man = AliCDBManager::Instance();
1572 //man->SetRun(0); // Should this come from rawdata header ?
1576 // Get the runloader
1577 AliRunLoader* runLoader = gAlice->GetRunLoader();
1579 // Open esd file if available
1580 TFile* esdFile = TFile::Open(esdFileName);
1581 Bool_t esdOK = (esdFile != 0);
1582 AliESD* esd = new AliESD;
1585 treeESD = (TTree*) esdFile->Get("esdTree");
1587 AliWarning("No ESD tree found");
1590 treeESD->SetBranchAddress("ESD", &esd);
1594 // Create the RawReader
1595 TString fileName(rawDirectory);
1596 AliRawReader* rawReader = 0x0;
1597 if (fileName.EndsWith("/")) {
1598 rawReader = new AliRawReaderFile(fileName);
1599 } else if (fileName.EndsWith(".root")) {
1600 rawReader = new AliRawReaderRoot(fileName);
1601 } else if (!fileName.IsNull()) {
1602 rawReader = new AliRawReaderDate(fileName);
1604 // if (!fEquipIdMap.IsNull() && fRawReader)
1605 // fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
1607 // Get list of detectors
1608 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1611 AliHeader* header = runLoader->GetHeader();
1613 TString detStr = fMakeSDigits;
1617 if (!(rawReader->NextEvent())) break;
1620 for (iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1621 AliModule* det = (AliModule*) detArray->At(iDet);
1622 if (!det || !det->IsActive()) continue;
1623 if (IsSelected(det->GetName(), detStr)) {
1624 AliInfo(Form("Calling Raw2SDigits for %s\n", det->GetName()));
1625 det->Raw2SDigits(rawReader);
1632 // If ESD information available obtain reconstructed vertex and store in header.
1634 treeESD->GetEvent(nev);
1635 const AliESDVertex* esdVertex = esd->GetPrimaryVertex();
1636 Double_t position[3];
1637 esdVertex->GetXYZ(position);
1638 AliGenEventHeader* mcHeader = new AliGenEventHeader("ESD");
1641 for (Int_t i = 0; i < 3; i++) mcV[i] = position[i];
1642 mcHeader->SetPrimaryVertex(mcV);
1643 header->Reset(0,nev);
1644 header->SetGenEventHeader(mcHeader);
1645 printf("***** Saved vertex %f %f %f \n", position[0], position[1], position[2]);
1650 runLoader->TreeE()->Fill();
1651 runLoader->SetNextEvent();
1657 runLoader->CdGAFile();
1658 runLoader->WriteHeader("OVERWRITE");
1659 runLoader->WriteRunLoader();
1664 //_____________________________________________________________________________
1665 Int_t AliSimulation::GetDetIndex(const char* detector)
1667 // return the detector index corresponding to detector
1669 for (index = 0; index < fgkNDetectors ; index++) {
1670 if ( strcmp(detector, fgkDetectorName[index]) == 0 )
1676 //_____________________________________________________________________________
1677 Bool_t AliSimulation::RunHLT()
1679 // Run the HLT simulation
1680 // HLT simulation is implemented in HLT/sim/AliHLTSimulation
1681 // Disabled if fRunHLT is empty, default vaule is "default".
1682 // AliSimulation::SetRunHLT can be used to set the options for HLT simulation
1683 // The default simulation depends on the HLT component libraries and their
1684 // corresponding agents which define components and chains to run. See
1685 // http://web.ift.uib.no/~kjeks/doc/alice-hlt/
1686 // http://web.ift.uib.no/~kjeks/doc/alice-hlt/classAliHLTModuleAgent.html
1688 // The libraries to be loaded can be specified as an option.
1690 // AliSimulation sim;
1691 // sim.SetRunHLT("libAliHLTSample.so");
1693 // will only load <tt>libAliHLTSample.so</tt>
1695 // Other available options:
1696 // \li loglevel=<i>level</i> <br>
1697 // logging level for this processing
1699 // disable redirection of log messages to AliLog class
1700 // \li config=<i>macro</i>
1701 // configuration macro
1702 // \li localrec=<i>configuration</i>
1703 // comma separated list of configurations to be run during simulation
1706 AliRunLoader* pRunLoader = LoadRun("READ");
1707 if (!pRunLoader) return kFALSE;
1709 // initialize CDB storage, run number, set CDB lock
1711 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1714 // load the library dynamically
1715 gSystem->Load(ALIHLTSIMULATION_LIBRARY);
1717 // check for the library version
1718 AliHLTSimulationGetLibraryVersion_t fctVersion=(AliHLTSimulationGetLibraryVersion_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_GET_LIBRARY_VERSION));
1720 AliError(Form("can not load library %s", ALIHLTSIMULATION_LIBRARY));
1723 if (fctVersion()!= ALIHLTSIMULATION_LIBRARY_VERSION) {
1724 AliError(Form("%s version does not match: compiled for version %d, loaded %d", ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_LIBRARY_VERSION, fctVersion()));
1728 // print compile info
1729 typedef void (*CompileInfo)( char*& date, char*& time);
1730 CompileInfo fctInfo=(CompileInfo)gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, "CompileInfo");
1734 (*fctInfo)(date, time);
1735 if (!date) date="unknown";
1736 if (!time) time="unknown";
1737 AliInfo(Form("%s build on %s (%s)", ALIHLTSIMULATION_LIBRARY, date, time));
1739 AliInfo(Form("no build info available for %s", ALIHLTSIMULATION_LIBRARY));
1742 // create instance of the HLT simulation
1743 AliHLTSimulationCreateInstance_t fctCreate=(AliHLTSimulationCreateInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_CREATE_INSTANCE));
1744 AliHLTSimulation* pHLT=NULL;
1745 if (fctCreate==NULL || (pHLT=(fctCreate()))==NULL) {
1746 AliError(Form("can not create instance of HLT simulation (creator %p)", fctCreate));
1750 // init the HLT simulation
1752 if (fRunHLT.CompareTo("default")!=0) options=fRunHLT;
1753 if (!IsSelected("HLT", fWriteRawData)) {
1754 options+=" writerawfiles=";
1756 options+=" writerawfiles=HLT";
1758 AliHLTSimulationInit_t fctInit=(AliHLTSimulationInit_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_INIT));
1759 if (fctInit==NULL || (iResult=(fctInit(pHLT, pRunLoader, options.Data())))<0) {
1760 AliError(Form("can not init HLT simulation: error %d (init %p)", iResult, fctInit));
1762 // run the HLT simulation
1763 AliHLTSimulationRun_t fctRun=(AliHLTSimulationRun_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_RUN));
1764 if (fctRun==NULL || (iResult=(fctRun(pHLT, pRunLoader)))<0) {
1765 AliError(Form("can not run HLT simulation: error %d (run %p)", iResult, fctRun));
1769 // delete the instance
1770 AliHLTSimulationDeleteInstance_t fctDelete=(AliHLTSimulationDeleteInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_DELETE_INSTANCE));
1771 if (fctDelete==NULL || fctDelete(pHLT)<0) {
1772 AliError(Form("can not delete instance of HLT simulation (creator %p)", fctDelete));
1776 return iResult>=0?kTRUE:kFALSE;
1779 //_____________________________________________________________________________
1780 Bool_t AliSimulation::RunQA()
1782 // run the QA on summable hits, digits or digits
1784 fQASteer->SetRunLoader(gAlice->GetRunLoader()) ;
1786 TString detectorsw("") ;
1788 detectorsw = fQASteer->Run(fQADetectors.Data()) ;
1789 if ( detectorsw.IsNull() )
1794 //_____________________________________________________________________________
1795 Bool_t AliSimulation::SetRunQA(TString detAndAction)
1797 // Allows to run QA for a selected set of detectors
1798 // and a selected set of tasks among HITS, SDIGITS and DIGITS
1799 // all selected detectors run the same selected tasks
1801 if (!detAndAction.Contains(":")) {
1802 AliError( Form("%s is a wrong syntax, use \"DetectorList:ActionList\" \n", detAndAction.Data()) ) ;
1806 Int_t colon = detAndAction.Index(":") ;
1807 fQADetectors = detAndAction(0, colon) ;
1808 if (fQADetectors.Contains("ALL") )
1809 fQADetectors = Form("%s %s", fMakeDigits.Data(), fMakeDigitsFromHits.Data()) ;
1810 fQATasks = detAndAction(colon+1, detAndAction.Sizeof() ) ;
1811 if (fQATasks.Contains("ALL") ) {
1812 fQATasks = Form("%d %d %d", AliQA::kHITS, AliQA::kSDIGITS, AliQA::kDIGITS) ;
1814 fQATasks.ToUpper() ;
1816 if ( fQATasks.Contains("HIT") )
1817 tempo = Form("%d ", AliQA::kHITS) ;
1818 if ( fQATasks.Contains("SDIGIT") )
1819 tempo += Form("%d ", AliQA::kSDIGITS) ;
1820 if ( fQATasks.Contains("DIGIT") )
1821 tempo += Form("%d ", AliQA::kDIGITS) ;
1823 if (fQATasks.IsNull()) {
1824 AliInfo("No QA requested\n") ;
1829 TString tempo(fQATasks) ;
1830 tempo.ReplaceAll(Form("%d", AliQA::kHITS), AliQA::GetTaskName(AliQA::kHITS)) ;
1831 tempo.ReplaceAll(Form("%d", AliQA::kSDIGITS), AliQA::GetTaskName(AliQA::kSDIGITS)) ;
1832 tempo.ReplaceAll(Form("%d", AliQA::kDIGITS), AliQA::GetTaskName(AliQA::kDIGITS)) ;
1833 AliInfo( Form("QA will be done on \"%s\" for \"%s\"\n", fQADetectors.Data(), tempo.Data()) ) ;
1835 fQASteer->SetActiveDetectors(fQADetectors) ;
1836 fQASteer->SetTasks(fQATasks) ;
1840 //_____________________________________________________________________________
1841 void AliSimulation::ProcessEnvironmentVars()
1843 // Extract run number and random generator seed from env variables
1845 AliInfo("Processing environment variables");
1847 // Random Number seed
1849 // first check that seed is not already set
1851 if (gSystem->Getenv("CONFIG_SEED")) {
1852 fSeed = atoi(gSystem->Getenv("CONFIG_SEED"));
1855 if (gSystem->Getenv("CONFIG_SEED")) {
1856 AliInfo(Form("Seed for random number generation already set (%d)"
1857 ": CONFIG_SEED variable ignored!", fSeed));
1861 AliInfo(Form("Seed for random number generation = %d ", fSeed));
1865 // first check that run number is not already set
1867 if (gSystem->Getenv("DC_RUN")) {
1868 fRun = atoi(gSystem->Getenv("DC_RUN"));
1871 if (gSystem->Getenv("DC_RUN")) {
1872 AliInfo(Form("Run number already set (%d): DC_RUN variable ignored!", fRun));
1876 AliInfo(Form("Run number = %d", fRun));
1879 //_____________________________________________________________________________
1880 void AliSimulation::WriteGRPEntry()
1882 // Get the necessary information from galice (generator, trigger etc) and
1883 // write a GRP entry corresponding to the settings in the Config.C used
1884 AliInfo("Writing global run parameters entry into the OCDB");
1886 TMap *grpMap = new TMap();
1887 grpMap->SetName("MONTECARLO");
1889 grpMap->Add(new TObjString("fRunType"),new TObjString("PHYSICS"));
1890 grpMap->Add(new TObjString("fAliceStartTime"),new TObjString("0"));
1891 grpMap->Add(new TObjString("fAliceStopTime"),new TObjString("9999"));
1893 const AliGenerator *gen = gAlice->Generator();
1895 grpMap->Add(new TObjString("fAliceBeamEnergy"),new TObjString(Form("%f",gen->GetEnergyCMS())));
1898 gen->GetProjectile(projectile,a,z);
1900 gen->GetTarget(target,a,z);
1901 TString beamType = projectile + "-" + target;
1902 if (!beamType.CompareTo("-")) {
1903 grpMap->Add(new TObjString("fAliceBeamType"),new TObjString("UNKNOWN"));
1906 grpMap->Add(new TObjString("fAliceBeamType"),new TObjString(beamType.Data()));
1910 AliWarning("Unknown beam type and energy!");
1911 grpMap->Add(new TObjString("fAliceBeamEnergy"),new TObjString("UNKNOWN"));
1912 grpMap->Add(new TObjString("fAliceBeamType"),new TObjString("0"));
1915 UInt_t detectorPattern = 0;
1917 TObjArray *detArray = gAlice->Detectors();
1918 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors-1; iDet++) {
1919 if (detArray->FindObject(AliDAQ::OfflineModuleName(iDet))) {
1920 detectorPattern |= (1 << iDet);
1925 if (!fRunHLT.IsNull())
1926 detectorPattern |= (1 << AliDAQ::kHLTId);
1928 grpMap->Add(new TObjString("fNumberOfDetectors"),new TObjString(Form("%d",nDets)));
1929 grpMap->Add(new TObjString("fDetectorMask"),new TObjString(Form("%u",detectorPattern)));
1931 grpMap->Add(new TObjString("fLHCPeriod"),new TObjString("LHC08c"));
1933 grpMap->Add(new TObjString("fLHCState"),new TObjString("STABLE BEAMS"));
1934 grpMap->Add(new TObjString("fLHCCondition"),new TObjString("0"));
1935 grpMap->Add(new TObjString("fLHCLuminosity"),new TObjString("0"));
1936 grpMap->Add(new TObjString("fBeamIntensity"),new TObjString("0"));
1938 AliMagF *field = gAlice->Field();
1939 Float_t solenoidField = TMath::Abs(field->SolenoidField());
1940 Float_t factor = field->Factor();
1941 Float_t l3current = TMath::Abs(factor)*solenoidField*30000./5.;
1942 grpMap->Add(new TObjString("fL3Current"),new TObjString(Form("%f",l3current)));
1945 grpMap->Add(new TObjString("fL3Polarity"),new TObjString("0"));
1946 grpMap->Add(new TObjString("fDipolePolarity"),new TObjString("0"));
1949 grpMap->Add(new TObjString("fL3Polarity"),new TObjString("1"));
1950 grpMap->Add(new TObjString("fDipolePolarity"),new TObjString("1"));
1953 if (TMath::Abs(factor) != 0)
1954 grpMap->Add(new TObjString("fDipoleCurrent"),new TObjString("6000"));
1956 grpMap->Add(new TObjString("fDipoleCurrent"),new TObjString("0"));
1958 grpMap->Add(new TObjString("fCavernTemperature"),new TObjString("0"));
1959 grpMap->Add(new TObjString("fCavernPressure"),new TObjString("0"));
1961 // Now store the entry in OCDB
1962 AliCDBManager* man = AliCDBManager::Instance();
1964 AliCDBId id("GRP/GRP/Data", man->GetRun(), man->GetRun());
1965 AliCDBMetaData *metadata= new AliCDBMetaData();
1967 metadata->SetResponsible("alice-off@cern.ch");
1968 metadata->SetComment("Automatically produced GRP entry for Monte Carlo");
1970 man->Put(grpMap,id,metadata);