]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEER/AliSimulation.cxx
Fixing coverity 17919
[u/mrichter/AliRoot.git] / STEER / STEER / AliSimulation.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // class for running generation, simulation and digitization                 //
21 //                                                                           //
22 // Hits, sdigits and digits are created for all detectors by typing:         //
23 //                                                                           //
24 //   AliSimulation sim;                                                      //
25 //   sim.Run();                                                              //
26 //                                                                           //
27 // The Run method returns kTRUE in case of successful execution.             //
28 // The number of events can be given as argument to the Run method or it     //
29 // can be set by                                                             //
30 //                                                                           //
31 //   sim.SetNumberOfEvents(n);                                               //
32 //                                                                           //
33 // The name of the configuration file can be passed as argument to the       //
34 // AliSimulation constructor or can be specified by                          //
35 //                                                                           //
36 //   sim.SetConfigFile("...");                                               //
37 //                                                                           //
38 // The generation of particles and the simulation of detector hits can be    //
39 // switched on or off by                                                     //
40 //                                                                           //
41 //   sim.SetRunGeneration(kTRUE);   // generation of primary particles       //
42 //   sim.SetRunSimulation(kFALSE);  // but no tracking                       //
43 //                                                                           //
44 // For which detectors sdigits and digits will be created, can be steered    //
45 // by                                                                        //
46 //                                                                           //
47 //   sim.SetMakeSDigits("ALL");     // make sdigits for all detectors        //
48 //   sim.SetMakeDigits("ITS TPC");  // make digits only for ITS and TPC      //
49 //                                                                           //
50 // The argument is a (case sensitive) string with the names of the           //
51 // detectors separated by a space. An empty string ("") can be used to       //
52 // disable the creation of sdigits or digits. The special string "ALL"       //
53 // selects all available detectors. This is the default.                     //
54 //                                                                           //
55 // The creation of digits from hits instead of from sdigits can be selected  //
56 // by                                                                        //
57 //                                                                           //
58 //   sim.SetMakeDigitsFromHits("TRD");                                       //
59 //                                                                           //
60 // The argument is again a string with the selected detectors. Be aware that //
61 // this feature is not available for all detectors and that merging is not   //
62 // possible, when digits are created directly from hits.                     //
63 //                                                                           //
64 // Background events can be merged by calling                                //
65 //                                                                           //
66 //   sim.MergeWith("background/galice.root", 2);                             //
67 //                                                                           //
68 // The first argument is the file name of the background galice file. The    //
69 // second argument is the number of signal events per background event.      //
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.                                       //
74 //                                                                           //
75 // The output of raw data can be switched on by calling                      //
76 //                                                                           //
77 //   sim.SetWriteRawData("MUON");   // write raw data for MUON               //
78 //                                                                           //
79 // The default output format of the raw data are DDL files. They are         //
80 // converted to a DATE file, if a file name is given as second argument.     //
81 // For this conversion the program "dateStream" is required. If the file     //
82 // name has the extension ".root", the DATE file is converted to a root      //
83 // file. The program "alimdc" is used for this purpose. For the conversion   //
84 // to DATE and root format the two conversion programs have to be installed. //
85 // Only the raw data in the final format is kept if the third argument is    //
86 // kTRUE.                                                                    //
87 //                                                                           //
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.   //
93 //                                                                           //
94 // The default number of events per file, which is usually set in the        //
95 // config file, can be changed for individual detectors and data types       //
96 // by calling                                                                //
97 //                                                                           //
98 //   sim.SetEventsPerFile("PHOS", "Reconstructed Points", 3);                //
99 //                                                                           //
100 // The first argument is the detector, the second one the data type and the  //
101 // last one the number of events per file. Valid data types are "Hits",      //
102 // "Summable Digits", "Digits", "Reconstructed Points" and "Tracks".         //
103 // The number of events per file has to be set before the simulation of      //
104 // hits. Otherwise it has no effect.                                         //
105 //                                                                           //
106 ///////////////////////////////////////////////////////////////////////////////
107
108 #include <TCint.h>
109 #include <TFile.h>
110 #include <TGeoGlobalMagField.h>
111 #include <TGeoManager.h>
112 #include <TObjString.h>
113 #include <TROOT.h>
114 #include <TSystem.h>
115 #include <TVirtualMC.h>
116 #include <TVirtualMCApplication.h>
117 #include <TDatime.h>
118
119 #include "AliAlignObj.h"
120 #include "AliCDBEntry.h"
121 #include "AliCDBManager.h"
122 #include "AliGRPManager.h"
123 #include "AliCDBStorage.h"
124 #include "AliCTPRawData.h"
125 #include "AliCentralTrigger.h"
126 #include "AliCentralTrigger.h"
127 #include "AliCodeTimer.h"
128 #include "AliDAQ.h"
129 #include "AliDigitizer.h"
130 #include "AliESDEvent.h"
131 #include "AliGRPObject.h"
132 #include "AliGenEventHeader.h"
133 #include "AliGenerator.h"
134 #include "AliGeomManager.h"
135 #include "AliHLTSimulation.h"
136 #include "AliHeader.h"
137 #include "AliLego.h"
138 #include "AliLegoGenerator.h"
139 #include "AliLog.h"
140 #include "AliMC.h"
141 #include "AliMagF.h"
142 #include "AliModule.h"
143 #include "AliPDG.h"
144 #include "AliRawReaderDate.h"
145 #include "AliRawReaderFile.h"
146 #include "AliRawReaderRoot.h"
147 #include "AliRun.h"
148 #include "AliDigitizationInput.h"
149 #include "AliRunLoader.h"
150 #include "AliSimulation.h"
151 #include "AliSysInfo.h"
152 #include "AliVertexGenFile.h"
153
154 ClassImp(AliSimulation)
155
156 AliSimulation *AliSimulation::fgInstance = 0;
157 const char* AliSimulation::fgkDetectorName[AliSimulation::fgkNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE"
158 // #ifdef MFT_UPGRADE
159 //                                                                             ,"MFT"
160 // #endif 
161                                                                             ,"MFT"    // AU
162                                                                             ,"HLT"
163 };
164
165 //_____________________________________________________________________________
166 AliSimulation::AliSimulation(const char* configFileName,
167                              const char* name, const char* title) :
168   TNamed(name, title),
169
170   fRunGeneration(kTRUE),
171   fRunSimulation(kTRUE),
172   fLoadAlignFromCDB(kTRUE),
173   fLoadAlObjsListOfDets("ALL"),
174   fMakeSDigits("ALL"),
175   fMakeDigits("ALL"),
176   fTriggerConfig(""),
177   fMakeDigitsFromHits(""),
178   fWriteRawData(""),
179   fRawDataFileName(""),
180   fDeleteIntermediateFiles(kFALSE),
181   fWriteSelRawData(kFALSE),
182   fStopOnError(kFALSE),
183   fNEvents(1),
184   fConfigFileName(configFileName),
185   fGAliceFileName("galice.root"),
186   fEventsPerFile(),
187   fBkgrdFileNames(NULL),
188   fAlignObjArray(NULL),
189   fUseBkgrdVertex(kTRUE),
190   fRegionOfInterest(kFALSE),
191   fCDBUri(""),
192   fQARefUri(""), 
193   fSpecCDBUri(),
194   fRun(-1),
195   fSeed(0),
196   fInitCDBCalled(kFALSE),
197   fInitRunNumberCalled(kFALSE),
198   fSetRunNumberFromDataCalled(kFALSE),
199   fEmbeddingFlag(kFALSE),
200   fLego(NULL),
201   fKey(0),
202   fUseVertexFromCDB(0),
203   fUseMagFieldFromGRP(0),
204   fGRPWriteLocation(Form("local://%s", gSystem->pwd())),
205   fUseTimeStampFromCDB(0),
206   fTimeStart(0),
207   fTimeEnd(0),
208   fQADetectors("ALL"),                  
209   fQATasks("ALL"),      
210   fRunQA(kTRUE), 
211   fEventSpecie(AliRecoParam::kDefault),
212   fWriteQAExpertData(kTRUE), 
213   fGeometryFile(),
214   fRunHLT("default"),
215   fpHLT(NULL),
216   fWriteGRPEntry(kTRUE)
217 {
218 // create simulation object with default parameters
219   fgInstance = this;
220   SetGAliceFile("galice.root");
221   
222 // for QA
223         AliQAManager * qam = AliQAManager::QAManager(AliQAv1::kSIMMODE) ; 
224         qam->SetActiveDetectors(fQADetectors) ; 
225         fQATasks = Form("%d %d %d", AliQAv1::kHITS, AliQAv1::kSDIGITS, AliQAv1::kDIGITS) ; 
226         qam->SetTasks(fQATasks) ;       
227 }
228
229 //_____________________________________________________________________________
230 AliSimulation::~AliSimulation()
231 {
232 // clean up
233
234   fEventsPerFile.Delete();
235 //  if(fAlignObjArray) fAlignObjArray->Delete(); // fAlignObjArray->RemoveAll() ???
236 //  delete fAlignObjArray; fAlignObjArray=0;
237
238   if (fBkgrdFileNames) {
239     fBkgrdFileNames->Delete();
240     delete fBkgrdFileNames;
241   }
242
243   fSpecCDBUri.Delete();
244   if (fgInstance==this) fgInstance = 0;
245
246   AliQAManager::QAManager()->ShowQA() ; 
247   AliQAManager::Destroy() ;     
248   AliCodeTimer::Instance()->Print();
249 }
250
251
252 //_____________________________________________________________________________
253 void AliSimulation::SetNumberOfEvents(Int_t nEvents)
254 {
255 // set the number of events for one run
256
257   fNEvents = nEvents;
258 }
259
260 //_____________________________________________________________________________
261 void AliSimulation::InitQA()
262 {
263   // activate a default CDB storage
264   // First check if we have any CDB storage set, because it is used 
265   // to retrieve the calibration and alignment constants
266   
267   if (fInitCDBCalled) return;
268   fInitCDBCalled = kTRUE;
269
270   AliQAManager * qam = AliQAManager::QAManager(AliQAv1::kSIMMODE) ; 
271   qam->SetActiveDetectors(fQADetectors) ; 
272   fQATasks = Form("%d %d %d", AliQAv1::kHITS, AliQAv1::kSDIGITS, AliQAv1::kDIGITS) ; 
273   qam->SetTasks(fQATasks) ;
274         if (fWriteQAExpertData)
275     qam->SetWriteExpert() ; 
276   
277   if (qam->IsDefaultStorageSet()) {
278     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
279     AliWarning("Default QA reference storage has been already set !");
280     AliWarning(Form("Ignoring the default storage declared in AliSimulation: %s",fQARefUri.Data()));
281     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
282     fQARefUri = qam->GetDefaultStorage()->GetURI();
283   } else {
284       if (fQARefUri.Length() > 0) {
285         AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
286         AliDebug(2, Form("Default QA reference storage is set to: %s", fQARefUri.Data()));
287         AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
288       } else {
289         fQARefUri="local://$ALICE_ROOT/QARef";
290         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
291         AliWarning("Default QA reference storage not yet set !!!!");
292         AliWarning(Form("Setting it now to: %s", fQARefUri.Data()));
293         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
294       }
295     qam->SetDefaultStorage(fQARefUri);
296   }
297 }
298
299 //_____________________________________________________________________________
300 void AliSimulation::InitCDB()
301 {
302 // activate a default CDB storage
303 // First check if we have any CDB storage set, because it is used 
304 // to retrieve the calibration and alignment constants
305
306   if (fInitCDBCalled) return;
307   fInitCDBCalled = kTRUE;
308
309   AliCDBManager* man = AliCDBManager::Instance();
310   if (man->IsDefaultStorageSet())
311   {
312     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
313     AliWarning("Default CDB storage has been already set !");
314     AliWarning(Form("Ignoring the default storage declared in AliSimulation: %s",fCDBUri.Data()));
315     AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
316     fCDBUri = man->GetDefaultStorage()->GetURI();
317   }
318   else {
319     if (fCDBUri.Length() > 0) 
320     {
321         AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
322         AliDebug(2, Form("Default CDB storage is set to: %s", fCDBUri.Data()));
323         AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
324     } else {
325         fCDBUri="local://$ALICE_ROOT/OCDB";
326         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
327         AliWarning("Default CDB storage not yet set !!!!");
328         AliWarning(Form("Setting it now to: %s", fCDBUri.Data()));
329         AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
330                 
331     }
332     man->SetDefaultStorage(fCDBUri);
333   }
334
335   // Now activate the detector specific CDB storage locations
336   for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
337     TObject* obj = fSpecCDBUri[i];
338     if (!obj) continue;
339     AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
340     AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
341     AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
342     man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
343   }
344       
345 }
346
347 //_____________________________________________________________________________
348 void AliSimulation::InitRunNumber(){
349 // check run number. If not set, set it to 0 !!!!
350   
351   if (fInitRunNumberCalled) return;
352   fInitRunNumberCalled = kTRUE;
353   
354   AliCDBManager* man = AliCDBManager::Instance();
355   if (man->GetRun() >= 0)
356   {
357         AliFatal(Form("Run number cannot be set in AliCDBManager before start of simulation: "
358                         "Use external variable DC_RUN or AliSimulation::SetRun()!"));
359   }
360     
361   if(fRun >= 0) {
362         AliDebug(2, Form("Setting CDB run number to: %d",fRun));
363   } else {
364         fRun=0;
365         AliWarning(Form("Run number not yet set !!!! Setting it now to: %d",
366                         fRun));
367   }
368   man->SetRun(fRun);
369
370   man->Print();
371
372 }
373
374 //_____________________________________________________________________________
375 void AliSimulation::SetCDBLock() {
376   // Set CDB lock: from now on it is forbidden to reset the run number
377   // or the default storage or to activate any further storage!
378   
379   ULong_t key = AliCDBManager::Instance()->SetLock(1);
380   if (key) fKey = key;
381 }
382
383 //_____________________________________________________________________________
384 void AliSimulation::SetDefaultStorage(const char* uri) {
385   // Store the desired default CDB storage location
386   // Activate it later within the Run() method
387   
388   fCDBUri = uri;
389   
390 }
391
392 //_____________________________________________________________________________
393 void AliSimulation::SetQARefDefaultStorage(const char* uri) {
394   // Store the desired default CDB storage location
395   // Activate it later within the Run() method
396   
397   fQARefUri = uri;
398   AliQAv1::SetQARefStorage(fQARefUri.Data()) ;
399 }
400
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
405
406   AliCDBPath aPath(calibType);
407   if(!aPath.IsValid()){
408         AliError(Form("Not a valid path: %s", calibType));
409         return;
410   }
411
412   TObject* obj = fSpecCDBUri.FindObject(calibType);
413   if (obj) fSpecCDBUri.Remove(obj);
414   fSpecCDBUri.Add(new TNamed(calibType, uri));
415
416 }
417
418 //_____________________________________________________________________________
419 void AliSimulation::SetRunNumber(Int_t run)
420 {
421 // sets run number
422 // Activate it later within the Run() method
423
424         fRun = run;
425 }
426
427 //_____________________________________________________________________________
428 void AliSimulation::SetSeed(Int_t seed)
429 {
430 // sets seed number
431 // Activate it later within the Run() method
432
433         fSeed = seed;
434 }
435
436 //_____________________________________________________________________________
437 Bool_t AliSimulation::SetRunNumberFromData()
438 {
439   // Set the CDB manager run number
440   // The run number is retrieved from gAlice
441
442     if (fSetRunNumberFromDataCalled) return kTRUE;
443     fSetRunNumberFromDataCalled = kTRUE;    
444   
445     AliCDBManager* man = AliCDBManager::Instance();
446     Int_t runData = -1, runCDB = -1;
447   
448     AliRunLoader* runLoader = LoadRun("READ");
449     if (!runLoader) return kFALSE;
450     else {
451         runData = runLoader->GetHeader()->GetRun();
452         delete runLoader;
453     }
454   
455     runCDB = man->GetRun();
456     if(runCDB >= 0) {
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("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");    
462         }
463         
464     }
465       
466     man->SetRun(runData);
467     fRun = runData;
468     
469     if(man->GetRun() < 0) {
470         AliError("Run number not properly initalized!");
471         return kFALSE;
472     }
473   
474     man->Print();
475     
476     return kTRUE;
477 }
478
479 //_____________________________________________________________________________
480 void AliSimulation::SetConfigFile(const char* fileName)
481 {
482 // set the name of the config file
483
484   fConfigFileName = fileName;
485 }
486
487 //_____________________________________________________________________________
488 void AliSimulation::SetGAliceFile(const char* fileName)
489 {
490 // set the name of the galice file
491 // the path is converted to an absolute one if it is relative
492
493   fGAliceFileName = fileName;
494   if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
495     char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
496                                                 fGAliceFileName);
497     fGAliceFileName = absFileName;
498     delete[] absFileName;
499   }
500
501   AliDebug(2, Form("galice file name set to %s", fileName));
502 }
503
504 //_____________________________________________________________________________
505 void AliSimulation::SetEventsPerFile(const char* detector, const char* type, 
506                                      Int_t nEvents)
507 {
508 // set the number of events per file for the given detector and data type
509 // ("Hits", "Summable Digits", "Digits", "Reconstructed Points" or "Tracks")
510
511   TNamed* obj = new TNamed(detector, type);
512   obj->SetUniqueID(nEvents);
513   fEventsPerFile.Add(obj);
514 }
515
516 //_____________________________________________________________________________
517 Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
518 {
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.
526
527   if (!AliGeomManager::GetGeometry() || !AliGeomManager::GetGeometry()->IsClosed()) {
528     AliError("Can't apply the misalignment! Geometry is not loaded or it is still opened!");
529     return kFALSE;
530   }  
531   
532   // initialize CDB storage, run number, set CDB lock
533   InitCDB();
534 //  if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
535   SetCDBLock();
536     
537   Bool_t delRunLoader = kFALSE;
538   if (!runLoader) {
539     runLoader = LoadRun("READ");
540     if (!runLoader) return kFALSE;
541     delRunLoader = kTRUE;
542   }
543   
544   // Export ideal geometry 
545   if(!IsGeometryFromFile()) AliGeomManager::GetGeometry()->Export("geometry.root");
546
547   // Load alignment data from CDB and apply to geometry through AliGeomManager
548   if(fLoadAlignFromCDB){
549     
550     TString detStr = fLoadAlObjsListOfDets;
551     TString loadAlObjsListOfDets = "";
552     
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 += " ";
561       }
562     } // end loop over detectors
563     loadAlObjsListOfDets.Prepend("GRP "); //add alignment objects for non-sensitive modules
564     AliGeomManager::ApplyAlignObjsFromCDB(loadAlObjsListOfDets.Data());
565   }else{
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;
574         return kFALSE;
575       }
576     }
577   }
578
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++) {
583
584     AliModule* det = (AliModule*) detArray->At(iDet);
585     if (!det || !det->IsActive()) continue;
586     if (IsSelected(det->GetName(), detStr)) {
587       det->UpdateInternalGeometry();
588     }
589   } // end loop over detectors
590
591
592   if (delRunLoader) delete runLoader;
593
594   return kTRUE;
595 }
596
597 //_____________________________________________________________________________
598 void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
599 {
600 // add a file with background events for merging
601
602   TObjString* fileNameStr = new TObjString(fileName);
603   fileNameStr->SetUniqueID(nSignalPerBkgrd);
604   if (!fBkgrdFileNames) fBkgrdFileNames = new TObjArray;
605   fBkgrdFileNames->Add(fileNameStr);
606 }
607
608 void AliSimulation::EmbedInto(const char* fileName, Int_t nSignalPerBkgrd)
609 {
610 // add a file with background events for embeddin
611   MergeWith(fileName, nSignalPerBkgrd);
612   fEmbeddingFlag = kTRUE;
613 }
614
615 //_____________________________________________________________________________
616 Bool_t AliSimulation::Run(Int_t nEvents)
617 {
618 // run the generation, simulation and digitization
619
620  
621   AliCodeTimerAuto("",0)
622   AliSysInfo::AddStamp("Start_Run");
623   
624   // Load run number and seed from environmental vars
625   ProcessEnvironmentVars();
626   AliSysInfo::AddStamp("ProcessEnvironmentVars");
627
628   gRandom->SetSeed(fSeed);
629    
630   if (nEvents > 0) fNEvents = nEvents;
631
632   // create and setup the HLT instance
633   if (!fRunHLT.IsNull() && !CreateHLT()) {
634     if (fStopOnError) return kFALSE;
635     // disable HLT
636     fRunHLT="";
637   }
638   
639   // generation and simulation -> hits
640   if (fRunGeneration) {
641     if (!RunSimulation()) if (fStopOnError) return kFALSE;
642   }
643   AliSysInfo::AddStamp("RunSimulation");
644            
645   // initialize CDB storage from external environment
646   // (either CDB manager or AliSimulation setters),
647   // if not already done in RunSimulation()
648   InitCDB();
649   AliSysInfo::AddStamp("InitCDB");
650   
651   // Set run number in CDBManager from data 
652   // From this point on the run number must be always loaded from data!
653   if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
654   
655   // Set CDB lock: from now on it is forbidden to reset the run number
656   // or the default storage or to activate any further storage!
657   SetCDBLock();
658
659   // If RunSimulation was not called, load the geometry and misalign it
660   if (!AliGeomManager::GetGeometry()) {
661     // Initialize the geometry manager
662     AliGeomManager::LoadGeometry("geometry.root");
663     AliSysInfo::AddStamp("GetGeometry");
664 //    // Check that the consistency of symbolic names for the activated subdetectors
665 //    // in the geometry loaded by AliGeomManager
666 //    AliRunLoader* runLoader = LoadRun("READ");
667 //    if (!runLoader) return kFALSE;
668 //
669 //    TString detsToBeChecked = "";
670 //    TObjArray* detArray = runLoader->GetAliRun()->Detectors();
671 //    for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
672 //      AliModule* det = (AliModule*) detArray->At(iDet);
673 //      if (!det || !det->IsActive()) continue;
674 //      detsToBeChecked += det->GetName();
675 //      detsToBeChecked += " ";
676 //    } // end loop over detectors
677 //    if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
678     if(!AliGeomManager::CheckSymNamesLUT("ALL"))
679         AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
680         
681     if (!AliGeomManager::GetGeometry()) if (fStopOnError) return kFALSE;
682     // Misalign geometry
683     if(!MisalignGeometry()) if (fStopOnError) return kFALSE;
684   }
685   AliSysInfo::AddStamp("MissalignGeometry");
686
687
688   // hits -> summable digits
689   AliSysInfo::AddStamp("Start_sdigitization");
690   if (!fMakeSDigits.IsNull()) {
691     if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
692  
693   }
694   AliSysInfo::AddStamp("Stop_sdigitization");
695   
696   AliSysInfo::AddStamp("Start_digitization");  
697   // summable digits -> digits  
698   if (!fMakeDigits.IsNull()) {
699     if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
700       if (fStopOnError) return kFALSE;
701     }
702    }
703   AliSysInfo::AddStamp("Stop_digitization");
704
705   
706   
707   // hits -> digits
708   if (!fMakeDigitsFromHits.IsNull()) {
709     if (fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
710       AliWarning(Form("Merging and direct creation of digits from hits " 
711                  "was selected for some detectors. "
712                  "No merging will be done for the following detectors: %s",
713                  fMakeDigitsFromHits.Data()));
714     }
715     if (!RunHitsDigitization(fMakeDigitsFromHits)) {
716       if (fStopOnError) return kFALSE;
717     }
718   }
719
720   AliSysInfo::AddStamp("Hits2Digits");
721   
722   
723   // digits -> trigger
724   if (!fTriggerConfig.IsNull() && !RunTrigger(fTriggerConfig,fMakeDigits)) {
725     if (fStopOnError) return kFALSE;
726   }
727
728   AliSysInfo::AddStamp("RunTrigger");
729   
730   
731   // digits -> raw data
732   if (!fWriteRawData.IsNull()) {
733     if (!WriteRawData(fWriteRawData, fRawDataFileName, 
734                       fDeleteIntermediateFiles,fWriteSelRawData)) {
735       if (fStopOnError) return kFALSE;
736     }
737   }
738
739   AliSysInfo::AddStamp("WriteRaw");
740   
741   // run HLT simulation on simulated digit data if raw data is not
742   // simulated, otherwise its called as part of WriteRawData
743   if (!fRunHLT.IsNull() && fWriteRawData.IsNull()) {
744     if (!RunHLT()) {
745       if (fStopOnError) return kFALSE;
746     }
747   }
748
749   AliSysInfo::AddStamp("RunHLT");
750   
751   //QA
752         if (fRunQA) {
753                 Bool_t rv = RunQA() ; 
754                 if (!rv)
755                         if (fStopOnError) 
756                                 return kFALSE ;         
757         }
758
759   AliSysInfo::AddStamp("RunQA");
760
761   // Cleanup of CDB manager: cache and active storages!
762   AliCDBManager::Instance()->ClearCache();
763
764   return kTRUE;
765 }
766
767 //_______________________________________________________________________
768 Bool_t AliSimulation::RunLego(const char *setup, Int_t nc1, Float_t c1min,
769                      Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
770                      Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener, Int_t nev)
771 {
772   //
773   // Generates lego plots of:
774   //    - radiation length map phi vs theta
775   //    - radiation length map phi vs eta
776   //    - interaction length map
777   //    - g/cm2 length map
778   //
779   //  ntheta    bins in theta, eta
780   //  themin    minimum angle in theta (degrees)
781   //  themax    maximum angle in theta (degrees)
782   //  nphi      bins in phi
783   //  phimin    minimum angle in phi (degrees)
784   //  phimax    maximum angle in phi (degrees)
785   //  rmin      minimum radius
786   //  rmax      maximum radius
787   //  
788   //
789   //  The number of events generated = ntheta*nphi
790   //  run input parameters in macro setup (default="Config.C")
791   //
792   //  Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
793   //Begin_Html
794   /*
795     <img src="picts/AliRunLego1.gif">
796   */
797   //End_Html
798   //Begin_Html
799   /*
800     <img src="picts/AliRunLego2.gif">
801   */
802   //End_Html
803   //Begin_Html
804   /*
805     <img src="picts/AliRunLego3.gif">
806   */
807   //End_Html
808   //
809
810 // run the generation and simulation
811
812   AliCodeTimerAuto("",0)
813
814   // initialize CDB storage and run number from external environment
815   // (either CDB manager or AliSimulation setters)
816   InitCDB();
817   InitRunNumber();
818   SetCDBLock();
819   
820   if (!gAlice) {
821     AliError("no gAlice object. Restart aliroot and try again.");
822     return kFALSE;
823   }
824   if (gAlice->Modules()->GetEntries() > 0) {
825     AliError("gAlice was already run. Restart aliroot and try again.");
826     return kFALSE;
827   }
828
829   AliInfo(Form("initializing gAlice with config file %s",
830           fConfigFileName.Data()));
831
832   // Number of events 
833     if (nev == -1) nev  = nc1 * nc2;
834     
835   // check if initialisation has been done
836   // If runloader has been initialized, set the number of events per file to nc1 * nc2
837     
838   // Set new generator
839   if (!gener) gener  = new AliLegoGenerator();
840   //
841   // Configure Generator
842
843   gener->SetRadiusRange(rmin, rmax);
844   gener->SetZMax(zmax);
845   gener->SetCoor1Range(nc1, c1min, c1max);
846   gener->SetCoor2Range(nc2, c2min, c2max);
847   
848   
849   //Create Lego object  
850   fLego = new AliLego("lego",gener);
851
852   //__________________________________________________________________________
853
854   gAlice->Announce();
855
856   gROOT->LoadMacro(setup);
857   gInterpreter->ProcessLine(gAlice->GetConfigFunction());
858
859   if(AliCDBManager::Instance()->GetRun() >= 0) { 
860     SetRunNumber(AliCDBManager::Instance()->GetRun());
861   } else {
862     AliWarning("Run number not initialized!!");
863   }
864
865   AliRunLoader::Instance()->CdGAFile();
866   
867   AliPDG::AddParticlesToPdgDataBase();  
868   
869   gMC->SetMagField(TGeoGlobalMagField::Instance()->GetField());
870
871   gAlice->GetMCApp()->Init();
872   
873   
874   //Must be here because some MCs (G4) adds detectors here and not in Config.C
875   gAlice->InitLoaders();
876   AliRunLoader::Instance()->MakeTree("E");
877   
878   //
879   // Save stuff at the beginning of the file to avoid file corruption
880   AliRunLoader::Instance()->CdGAFile();
881   gAlice->Write();
882
883   //Save current generator
884   AliGenerator *gen=gAlice->GetMCApp()->Generator();
885   gAlice->GetMCApp()->ResetGenerator(gener);
886   //Prepare MC for Lego Run
887   gMC->InitLego();
888   
889   //Run Lego Object
890   
891   
892   AliRunLoader::Instance()->SetNumberOfEventsPerFile(nev);
893   gMC->ProcessRun(nev);
894   
895   // End of this run, close files
896   FinishRun();
897   // Restore current generator
898   gAlice->GetMCApp()->ResetGenerator(gen);
899   // Delete Lego Object
900   delete fLego;
901
902   return kTRUE;
903 }
904
905 //_____________________________________________________________________________
906 Bool_t AliSimulation::RunTrigger(const char* config, const char* detectors)
907 {
908   // run the trigger
909
910   AliCodeTimerAuto("",0)
911
912   // initialize CDB storage from external environment
913   // (either CDB manager or AliSimulation setters),
914   // if not already done in RunSimulation()
915   InitCDB();
916   
917   // Set run number in CDBManager from data 
918   // From this point on the run number must be always loaded from data!
919   if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
920   
921   // Set CDB lock: from now on it is forbidden to reset the run number
922   // or the default storage or to activate any further storage!
923   SetCDBLock();
924    
925    AliRunLoader* runLoader = LoadRun("READ");
926    if (!runLoader) return kFALSE;
927    TString trconfiguration = config;
928
929    if (trconfiguration.IsNull()) {
930      if(!fTriggerConfig.IsNull()) {
931        trconfiguration = fTriggerConfig;
932      }
933      else
934        AliWarning("No trigger descriptor is specified. Loading the one that is in the CDB.");
935    }
936
937    runLoader->MakeTree( "GG" );
938    AliCentralTrigger* aCTP = runLoader->GetTrigger();
939    // Load Configuration
940    if (!aCTP->LoadConfiguration( trconfiguration ))
941      return kFALSE;
942
943    // digits -> trigger
944    if( !aCTP->RunTrigger( runLoader , detectors ) ) {
945       if (fStopOnError) {
946         //  delete aCTP;
947         return kFALSE;
948       }
949    }
950
951    delete runLoader;
952
953    return kTRUE;
954 }
955
956 //_____________________________________________________________________________
957 Bool_t AliSimulation::WriteTriggerRawData()
958 {
959   // Writes the CTP (trigger) DDL raw data
960   // Details of the format are given in the
961   // trigger TDR - pages 134 and 135.
962   AliCTPRawData writer;
963   writer.RawData();
964
965   return kTRUE;
966 }
967
968 //_____________________________________________________________________________
969 Bool_t AliSimulation::RunSimulation(Int_t nEvents)
970 {
971 // run the generation and simulation
972
973   AliCodeTimerAuto("",0)
974
975   // initialize CDB storage and run number from external environment
976   // (either CDB manager or AliSimulation setters)
977   AliSysInfo::AddStamp("RunSimulation_Begin");
978   InitCDB();
979   AliSysInfo::AddStamp("RunSimulation_InitCDB");
980   InitRunNumber();
981   SetCDBLock();
982   AliSysInfo::AddStamp("RunSimulation_SetCDBLock");
983   
984   if (!gAlice) {
985     AliError("no gAlice object. Restart aliroot and try again.");
986     return kFALSE;
987   }
988   if (gAlice->Modules()->GetEntries() > 0) {
989     AliError("gAlice was already run. Restart aliroot and try again.");
990     return kFALSE;
991   }
992
993   AliInfo(Form("initializing gAlice with config file %s",
994           fConfigFileName.Data()));
995
996   //
997   // Initialize ALICE Simulation run
998   //
999   gAlice->Announce();
1000
1001   //
1002   // If requested set the mag. field from the GRP entry.
1003   // After this the field is loccked and cannot be changed by Config.C
1004   if (fUseMagFieldFromGRP) {
1005       AliGRPManager grpM;
1006       grpM.ReadGRPEntry();
1007       grpM.SetMagField();
1008       AliInfo("Field is locked now. It cannot be changed in Config.C");
1009   }
1010 //
1011 // Execute Config.C
1012   gROOT->LoadMacro(fConfigFileName.Data());
1013   gInterpreter->ProcessLine(gAlice->GetConfigFunction());
1014   AliSysInfo::AddStamp("RunSimulation_Config");
1015
1016 //
1017 // If requested obtain the vertex position and vertex sigma_z from the CDB
1018 // This overwrites the settings from the Config.C  
1019   if (fUseVertexFromCDB) {
1020       Double_t vtxPos[3] = {0., 0., 0.}; 
1021       Double_t vtxSig[3] = {0., 0., 0.};
1022       AliCDBEntry* entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertex");
1023       if (entry) {
1024           AliESDVertex* vertex = dynamic_cast<AliESDVertex*> (entry->GetObject());
1025           if (vertex) {
1026               if(vertex->GetXRes()>2.8) { // > pipe radius --> it's a dummy object, don't use it 
1027                   entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertexSPD");
1028                   if (entry) vertex = dynamic_cast<AliESDVertex*> (entry->GetObject());
1029               }
1030           }
1031           if (vertex) {
1032               vertex->GetXYZ(vtxPos);
1033               vertex->GetSigmaXYZ(vtxSig);
1034               AliInfo("Overwriting Config.C vertex settings !");
1035               AliInfo(Form("Vertex position from OCDB entry: x = %13.3f, y = %13.3f, z = %13.3f (sigma = %13.3f)\n",
1036                            vtxPos[0], vtxPos[1], vtxPos[2], vtxSig[2]));
1037               
1038               AliGenerator *gen = gAlice->GetMCApp()->Generator();
1039               gen->SetOrigin(vtxPos[0], vtxPos[1], vtxPos[2]);   // vertex position
1040               gen->SetSigmaZ(vtxSig[2]);
1041           }
1042       }
1043   }
1044
1045   // If requested we take the SOR and EOR time-stamps from the GRP and use them
1046   // in order to generate the event time-stamps
1047   if (fUseTimeStampFromCDB) {
1048     AliGRPManager grpM;
1049     grpM.ReadGRPEntry();
1050     const AliGRPObject *grpObj = grpM.GetGRPData();
1051     if (!grpObj || (grpObj->GetTimeEnd() <= grpObj->GetTimeStart())) {
1052       AliError("Missing GRP or bad SOR/EOR time-stamps! Switching off the time-stamp generation from GRP!");
1053       fTimeStart = fTimeEnd = 0;
1054       fUseTimeStampFromCDB = kFALSE;
1055     }
1056     else {
1057       fTimeStart = grpObj->GetTimeStart();
1058       fTimeEnd = grpObj->GetTimeEnd();
1059     }
1060   }
1061   
1062   if(AliCDBManager::Instance()->GetRun() >= 0) { 
1063     AliRunLoader::Instance()->SetRunNumber(AliCDBManager::Instance()->GetRun());
1064     AliRunLoader::Instance()->SetNumberOfEventsPerRun(fNEvents);
1065   } else {
1066         AliWarning("Run number not initialized!!");
1067   }
1068   
1069    AliRunLoader::Instance()->CdGAFile();
1070    
1071
1072    AliPDG::AddParticlesToPdgDataBase();  
1073
1074    gMC->SetMagField(TGeoGlobalMagField::Instance()->GetField());
1075    AliSysInfo::AddStamp("RunSimulation_GetField");
1076    
1077    gAlice->GetMCApp()->Init();
1078    AliSysInfo::AddStamp("RunSimulation_InitMCApp");
1079
1080    //Must be here because some MCs (G4) adds detectors here and not in Config.C
1081    gAlice->InitLoaders();
1082    AliRunLoader::Instance()->MakeTree("E");
1083    AliRunLoader::Instance()->LoadKinematics("RECREATE");
1084    AliRunLoader::Instance()->LoadTrackRefs("RECREATE");
1085    AliRunLoader::Instance()->LoadHits("all","RECREATE");
1086    //
1087    // Save stuff at the beginning of the file to avoid file corruption
1088    AliRunLoader::Instance()->CdGAFile();
1089    gAlice->Write();
1090    gAlice->SetEventNrInRun(-1); //important - we start Begin event from increasing current number in run
1091    AliSysInfo::AddStamp("RunSimulation_InitLoaders");
1092   //___________________________________________________________________________________________
1093   
1094   AliSysInfo::AddStamp("RunSimulation_TriggerDescriptor");
1095
1096   // Set run number in CDBManager
1097   AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
1098
1099   AliRunLoader* runLoader = AliRunLoader::Instance();
1100   if (!runLoader) {
1101              AliError(Form("gAlice has no run loader object. "
1102                              "Check your config file: %s", fConfigFileName.Data()));
1103              return kFALSE;
1104   }
1105   SetGAliceFile(runLoader->GetFileName());
1106       
1107   // Misalign geometry
1108 #if ROOT_VERSION_CODE < 331527
1109   AliGeomManager::SetGeometry(gGeoManager);
1110   
1111   // Check that the consistency of symbolic names for the activated subdetectors
1112   // in the geometry loaded by AliGeomManager
1113   TString detsToBeChecked = "";
1114   TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1115   for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1116     AliModule* det = (AliModule*) detArray->At(iDet);
1117     if (!det || !det->IsActive()) continue;
1118     detsToBeChecked += det->GetName();
1119     detsToBeChecked += " ";
1120   } // end loop over detectors
1121   if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
1122     AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
1123   MisalignGeometry(runLoader);
1124   AliSysInfo::AddStamp("RunSimulation_MisalignGeometry");
1125 #endif
1126
1127 //   AliRunLoader* runLoader = AliRunLoader::Instance();
1128 //   if (!runLoader) {
1129 //     AliError(Form("gAlice has no run loader object. "
1130 //                   "Check your config file: %s", fConfigFileName.Data()));
1131 //     return kFALSE;
1132 //   }
1133 //   SetGAliceFile(runLoader->GetFileName());
1134
1135   if (!gAlice->GetMCApp()->Generator()) {
1136     AliError(Form("gAlice has no generator object. "
1137                   "Check your config file: %s", fConfigFileName.Data()));
1138     return kFALSE;
1139   }
1140
1141   // Write GRP entry corresponding to the setting found in Cofig.C
1142   if (fWriteGRPEntry)
1143     WriteGRPEntry();
1144   AliSysInfo::AddStamp("RunSimulation_WriteGRP");
1145
1146   if (nEvents <= 0) nEvents = fNEvents;
1147
1148   // get vertex from background file in case of merging
1149   if (fUseBkgrdVertex &&
1150       fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
1151     Int_t signalPerBkgrd = GetNSignalPerBkgrd(nEvents);
1152     const char* fileName = ((TObjString*)
1153                             (fBkgrdFileNames->At(0)))->GetName();
1154     AliInfo(Form("The vertex will be taken from the background "
1155                  "file %s with nSignalPerBackground = %d", 
1156                  fileName, signalPerBkgrd));
1157     AliVertexGenFile* vtxGen = new AliVertexGenFile(fileName, signalPerBkgrd);
1158     gAlice->GetMCApp()->Generator()->SetVertexGenerator(vtxGen);
1159   }
1160
1161   if (!fRunSimulation) {
1162     gAlice->GetMCApp()->Generator()->SetTrackingFlag(0);
1163   }
1164
1165   // set the number of events per file for given detectors and data types
1166   for (Int_t i = 0; i < fEventsPerFile.GetEntriesFast(); i++) {
1167     if (!fEventsPerFile[i]) continue;
1168     const char* detName = fEventsPerFile[i]->GetName();
1169     const char* typeName = fEventsPerFile[i]->GetTitle();
1170     TString loaderName(detName);
1171     loaderName += "Loader";
1172     AliLoader* loader = runLoader->GetLoader(loaderName);
1173     if (!loader) {
1174       AliError(Form("RunSimulation no loader for %s found\n Number of events per file not set for %s %s", 
1175                     detName, typeName, detName));
1176       continue;
1177     }
1178     AliDataLoader* dataLoader = 
1179       loader->GetDataLoader(typeName);
1180     if (!dataLoader) {
1181       AliError(Form("no data loader for %s found\n"
1182                     "Number of events per file not set for %s %s", 
1183                     typeName, detName, typeName));
1184       continue;
1185     }
1186     dataLoader->SetNumberOfEventsPerFile(fEventsPerFile[i]->GetUniqueID());
1187     AliDebug(1, Form("number of events per file set to %d for %s %s",
1188                      fEventsPerFile[i]->GetUniqueID(), detName, typeName));
1189   }
1190
1191   AliInfo("running gAlice");
1192   AliSysInfo::AddStamp("Start_ProcessRun");
1193
1194   // Create the Root Tree with one branch per detector
1195   //Hits moved to begin event -> now we are crating separate tree for each event
1196
1197   gMC->ProcessRun(nEvents);
1198
1199   // End of this run, close files
1200   if(nEvents>0) FinishRun();
1201
1202   AliSysInfo::AddStamp("Stop_ProcessRun");
1203   delete runLoader;
1204
1205   return kTRUE;
1206 }
1207
1208 //_____________________________________________________________________________
1209 Bool_t AliSimulation::RunSDigitization(const char* detectors)
1210 {
1211 // run the digitization and produce summable digits
1212   static Int_t eventNr=0;
1213   AliCodeTimerAuto("",0) ;
1214
1215   // initialize CDB storage, run number, set CDB lock
1216   InitCDB();
1217   if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1218   SetCDBLock();
1219   
1220   AliRunLoader* runLoader = LoadRun();
1221   if (!runLoader) return kFALSE;
1222
1223   TString detStr = detectors;
1224   TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1225   for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1226     AliModule* det = (AliModule*) detArray->At(iDet);
1227     if (!det || !det->IsActive()) continue;
1228     if (IsSelected(det->GetName(), detStr)) {
1229       AliInfo(Form("creating summable digits for %s", det->GetName()));
1230       AliCodeTimerStart(Form("creating summable digits for %s", det->GetName()));
1231       det->Hits2SDigits();
1232       AliCodeTimerStop(Form("creating summable digits for %s", det->GetName()));
1233       AliSysInfo::AddStamp(Form("Digit_%s_%d",det->GetName(),eventNr), 0,1, eventNr);
1234     }
1235   }
1236
1237   if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1238     AliError(Form("the following detectors were not found: %s",
1239                   detStr.Data()));
1240     if (fStopOnError) return kFALSE;
1241   }
1242   eventNr++;
1243   delete runLoader;
1244
1245   return kTRUE;
1246 }
1247
1248
1249 //_____________________________________________________________________________
1250 Bool_t AliSimulation::RunDigitization(const char* detectors, 
1251                                       const char* excludeDetectors)
1252 {
1253 // run the digitization and produce digits from sdigits
1254
1255   AliCodeTimerAuto("",0)
1256
1257   // initialize CDB storage, run number, set CDB lock
1258   InitCDB();
1259   if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1260   SetCDBLock();
1261   
1262   delete AliRunLoader::Instance();
1263   delete gAlice;
1264   gAlice = NULL;
1265
1266   Int_t nStreams = 1;
1267   if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
1268   Int_t signalPerBkgrd = GetNSignalPerBkgrd();
1269   AliDigitizationInput digInp(nStreams, signalPerBkgrd);
1270   // digInp.SetEmbeddingFlag(fEmbeddingFlag);
1271   digInp.SetRegionOfInterest(fRegionOfInterest);
1272   digInp.SetInputStream(0, fGAliceFileName.Data());
1273   for (Int_t iStream = 1; iStream < nStreams; iStream++) {
1274     const char* fileName = ((TObjString*)(fBkgrdFileNames->At(iStream-1)))->GetName();
1275     digInp.SetInputStream(iStream, fileName);
1276   }
1277   TObjArray detArr;
1278   detArr.SetOwner(kTRUE);
1279   TString detStr = detectors;
1280   TString detExcl = excludeDetectors;
1281   if (!static_cast<AliStream*>(digInp.GetInputStream(0))->ImportgAlice()) {
1282     AliError("Error occured while getting gAlice from Input 0");
1283     return kFALSE;
1284   }
1285   AliRunLoader* runLoader = AliRunLoader::GetRunLoader(digInp.GetInputStream(0)->GetFolderName());
1286   TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1287   for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1288     AliModule* det = (AliModule*) detArray->At(iDet);
1289     if (!det || !det->IsActive()) continue;
1290     if (!IsSelected(det->GetName(), detStr) || IsSelected(det->GetName(), detExcl)) continue;
1291     AliDigitizer* digitizer = det->CreateDigitizer(&digInp);
1292     if (!digitizer || !digitizer->Init()) {
1293       AliError(Form("no digitizer for %s", det->GetName()));
1294       if (fStopOnError) return kFALSE;
1295       else continue;
1296     }
1297     detArr.AddLast(digitizer);    
1298     AliInfo(Form("Created digitizer from SDigits -> Digits for %s", det->GetName()));    
1299   }
1300   //
1301   if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1302     AliError(Form("the following detectors were not found: %s", detStr.Data()));
1303     if (fStopOnError) return kFALSE;
1304   }
1305   //
1306   Int_t ndigs = detArr.GetEntriesFast();
1307   Int_t eventsCreated = 0;
1308   AliRunLoader* outRl =  digInp.GetOutRunLoader();
1309   while ((eventsCreated++ < fNEvents) || (fNEvents < 0)) {
1310     if (!digInp.ConnectInputTrees()) break;
1311     digInp.InitEvent(); //this must be after call of Connect Input tress.
1312     if (outRl) outRl->SetEventNumber(eventsCreated-1);
1313     static_cast<AliStream*>(digInp.GetInputStream(0))->ImportgAlice(); // use gAlice of the first input stream
1314     for (int id=0;id<ndigs;id++) ((AliDigitizer*)detArr[id])->Digitize("");
1315     digInp.FinishEvent();
1316   };
1317   digInp.FinishGlobal();
1318   //
1319   return kTRUE;
1320 }
1321
1322 //_____________________________________________________________________________
1323 Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
1324 {
1325 // run the digitization and produce digits from hits
1326
1327   AliCodeTimerAuto("",0)
1328
1329   // initialize CDB storage, run number, set CDB lock
1330   InitCDB();
1331   if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1332   SetCDBLock();
1333   
1334   AliRunLoader* runLoader = LoadRun("READ");
1335   if (!runLoader) return kFALSE;
1336
1337   TString detStr = detectors;
1338   TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1339   for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1340     AliModule* det = (AliModule*) detArray->At(iDet);
1341     if (!det || !det->IsActive()) continue;
1342     if (IsSelected(det->GetName(), detStr)) {
1343       AliInfo(Form("creating digits from hits for %s", det->GetName()));
1344       det->Hits2Digits();
1345     }
1346   }
1347
1348   if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1349     AliError(Form("the following detectors were not found: %s", 
1350                   detStr.Data()));
1351     if (fStopOnError) return kFALSE;
1352   }
1353
1354   return kTRUE;
1355 }
1356
1357 //_____________________________________________________________________________
1358 Bool_t AliSimulation::WriteRawData(const char* detectors, 
1359                                    const char* fileName,
1360                                    Bool_t deleteIntermediateFiles,
1361                                    Bool_t selrawdata)
1362 {
1363 // convert the digits to raw data
1364 // First DDL raw data files for the given detectors are created.
1365 // If a file name is given, the DDL files are then converted to a DATE file.
1366 // If deleteIntermediateFiles is true, the DDL raw files are deleted 
1367 // afterwards.
1368 // If the file name has the extension ".root", the DATE file is converted
1369 // to a root file.
1370 // If deleteIntermediateFiles is true, the DATE file is deleted afterwards.
1371 // 'selrawdata' flag can be used to enable writing of detectors raw data
1372 // accoring to the trigger cluster.
1373
1374   AliCodeTimerAuto("",0)
1375   AliSysInfo::AddStamp("WriteRawData_Start");
1376   
1377   TString detStr = detectors;
1378   if (!WriteRawFiles(detStr.Data())) {
1379     if (fStopOnError) return kFALSE;
1380   }
1381   AliSysInfo::AddStamp("WriteRawFiles");
1382
1383   // run HLT simulation on simulated DDL raw files
1384   // and produce HLT ddl raw files to be included in date/root file
1385   // bugfix 2009-06-26: the decision whether to write HLT raw data
1386   // is taken in RunHLT. Here HLT always needs to be run in order to
1387   // create HLT digits, unless its switched off. This is due to the
1388   // special placement of the HLT between the generation of DDL files
1389   // and conversion to DATE/Root file.
1390   detStr.ReplaceAll("HLT", "");
1391   if (!fRunHLT.IsNull()) {
1392     if (!RunHLT()) {
1393       if (fStopOnError) return kFALSE;
1394     }
1395   }
1396   AliSysInfo::AddStamp("WriteRawData_RunHLT");
1397
1398   TString dateFileName(fileName);
1399   if (!dateFileName.IsNull()) {
1400     Bool_t rootOutput = dateFileName.EndsWith(".root");
1401     if (rootOutput) dateFileName += ".date";
1402     TString selDateFileName;
1403     if (selrawdata) {
1404       selDateFileName = "selected.";
1405       selDateFileName+= dateFileName;
1406     }
1407     if (!ConvertRawFilesToDate(dateFileName,selDateFileName)) {
1408       if (fStopOnError) return kFALSE;
1409     }
1410     AliSysInfo::AddStamp("ConvertRawFilesToDate");
1411     if (deleteIntermediateFiles) {
1412       AliRunLoader* runLoader = LoadRun("READ");
1413       if (runLoader) for (Int_t iEvent = 0; 
1414                           iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1415         char command[256];
1416         snprintf(command, 256, "rm -r raw%d", iEvent);
1417         gSystem->Exec(command);
1418       }
1419       delete runLoader;
1420     }
1421
1422     if (rootOutput) {
1423       if (!ConvertDateToRoot(dateFileName, fileName)) {
1424         if (fStopOnError) return kFALSE;
1425       }
1426       AliSysInfo::AddStamp("ConvertDateToRoot");
1427       if (deleteIntermediateFiles) {
1428         gSystem->Unlink(dateFileName);
1429       }
1430       if (selrawdata) {
1431         TString selFileName = "selected.";
1432         selFileName        += fileName;
1433         if (!ConvertDateToRoot(selDateFileName, selFileName)) {
1434           if (fStopOnError) return kFALSE;
1435         }
1436         if (deleteIntermediateFiles) {
1437           gSystem->Unlink(selDateFileName);
1438         }
1439       }
1440     }
1441   }
1442
1443   return kTRUE;
1444 }
1445
1446 //_____________________________________________________________________________
1447 Bool_t AliSimulation::WriteRawFiles(const char* detectors)
1448 {
1449 // convert the digits to raw data DDL files
1450
1451   AliCodeTimerAuto("",0)
1452   
1453   AliRunLoader* runLoader = LoadRun("READ");
1454   if (!runLoader) return kFALSE;
1455
1456   // write raw data to DDL files
1457   for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1458     AliInfo(Form("processing event %d", iEvent));
1459     runLoader->GetEvent(iEvent);
1460     TString baseDir = gSystem->WorkingDirectory();
1461     char dirName[256];
1462     snprintf(dirName, 256, "raw%d", iEvent);
1463     gSystem->MakeDirectory(dirName);
1464     if (!gSystem->ChangeDirectory(dirName)) {
1465       AliError(Form("couldn't change to directory %s", dirName));
1466       if (fStopOnError) return kFALSE; else continue;
1467     }
1468
1469     ofstream runNbFile(Form("run%u",runLoader->GetHeader()->GetRun()));
1470     runNbFile.close();
1471
1472     TString detStr = detectors;
1473     if (IsSelected("HLT", detStr)) {
1474       // Do nothing. "HLT" will be removed from detStr and HLT raw
1475       // data files are generated in RunHLT.
1476     }
1477
1478     TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1479     for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1480       AliModule* det = (AliModule*) detArray->At(iDet);
1481       if (!det || !det->IsActive()) continue;
1482       if (IsSelected(det->GetName(), detStr)) {
1483         AliInfo(Form("creating raw data from digits for %s", det->GetName()));
1484         det->Digits2Raw();
1485       }
1486     }
1487
1488     if (!WriteTriggerRawData())
1489       if (fStopOnError) return kFALSE;
1490
1491     gSystem->ChangeDirectory(baseDir);
1492     if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
1493       AliError(Form("the following detectors were not found: %s", 
1494                     detStr.Data()));
1495       if (fStopOnError) return kFALSE;
1496     }
1497   }
1498
1499   delete runLoader;
1500   
1501   return kTRUE;
1502 }
1503
1504 //_____________________________________________________________________________
1505 Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName,
1506                                             const char* selDateFileName)
1507 {
1508 // convert raw data DDL files to a DATE file with the program "dateStream"
1509 // The second argument is not empty when the user decides to write
1510 // the detectors raw data according to the trigger cluster.
1511
1512   AliCodeTimerAuto("",0)
1513   
1514   char* path = gSystem->Which(gSystem->Getenv("PATH"), "dateStream");
1515   if (!path) {
1516     AliError("the program dateStream was not found");
1517     if (fStopOnError) return kFALSE;
1518   } else {
1519     delete[] path;
1520   }
1521
1522   AliRunLoader* runLoader = LoadRun("READ");
1523   if (!runLoader) return kFALSE;
1524
1525   AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
1526   Bool_t selrawdata = kFALSE;
1527   if (strcmp(selDateFileName,"") != 0) selrawdata = kTRUE;
1528
1529   char command[256];
1530   // Note the option -s. It is used in order to avoid
1531   // the generation of SOR/EOR events.
1532   snprintf(command, 256, "dateStream -c -s -D -o %s -# %d -C -run %d", 
1533           dateFileName, runLoader->GetNumberOfEvents(),runLoader->GetHeader()->GetRun());
1534   FILE* pipe = gSystem->OpenPipe(command, "w");
1535
1536   if (!pipe) {
1537     AliError(Form("Cannot execute command: %s",command));
1538     return kFALSE;
1539   }
1540
1541   Int_t selEvents = 0;
1542   for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1543
1544     UInt_t detectorPattern = 0;
1545     runLoader->GetEvent(iEvent);
1546     if (!runLoader->LoadTrigger()) {
1547       AliCentralTrigger *aCTP = runLoader->GetTrigger();
1548       detectorPattern = aCTP->GetClusterMask();
1549       // Check if the event was triggered by CTP
1550       if (selrawdata) {
1551         if (aCTP->GetClassMask()) selEvents++;
1552       }
1553     }
1554     else {
1555       AliWarning("No trigger can be loaded! Some fields in the event header will be empty !");
1556       if (selrawdata) {
1557         AliWarning("No trigger can be loaded! Writing of selected raw data is abandoned !");
1558         selrawdata = kFALSE;
1559       }
1560     }
1561
1562     fprintf(pipe, "GDC DetectorPattern %u Timestamp %ld\n", detectorPattern, runLoader->GetHeader()->GetTimeStamp());
1563     Float_t ldc = 0;
1564     Int_t prevLDC = -1;
1565
1566     // loop over detectors and DDLs
1567     for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1568       for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
1569
1570         Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
1571         Int_t ldcID = Int_t(ldc + 0.0001);
1572         ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
1573
1574         char rawFileName[256];
1575         snprintf(rawFileName, 256, "raw%d/%s", 
1576                 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
1577
1578         // check existence and size of raw data file
1579         FILE* file = fopen(rawFileName, "rb");
1580         if (!file) continue;
1581         fseek(file, 0, SEEK_END);
1582         unsigned long size = ftell(file);
1583         fclose(file);
1584         if (!size) continue;
1585
1586         if (ldcID != prevLDC) {
1587           fprintf(pipe, " LDC Id %d\n", ldcID);
1588           prevLDC = ldcID;
1589         }
1590         fprintf(pipe, "  Equipment Id %d Payload %s\n", ddlID, rawFileName);
1591       }
1592     }
1593   }
1594
1595   Int_t result = gSystem->ClosePipe(pipe);
1596
1597   if (!(selrawdata && selEvents > 0)) {
1598     delete runLoader;
1599     return (result == 0);
1600   }
1601
1602   AliInfo(Form("converting selected by trigger cluster raw data DDL files to DATE file %s", selDateFileName));
1603   
1604   snprintf(command, 256, "dateStream -c -s -D -o %s -# %d -C -run %d", 
1605           selDateFileName,selEvents,runLoader->GetHeader()->GetRun());
1606   FILE* pipe2 = gSystem->OpenPipe(command, "w");
1607
1608   for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1609
1610     // Get the trigger decision and cluster
1611     UInt_t detectorPattern = 0;
1612     TString detClust;
1613     runLoader->GetEvent(iEvent);
1614     if (!runLoader->LoadTrigger()) {
1615       AliCentralTrigger *aCTP = runLoader->GetTrigger();
1616       if (aCTP->GetClassMask() == 0) continue;
1617       detectorPattern = aCTP->GetClusterMask();
1618       detClust = AliDAQ::ListOfTriggeredDetectors(detectorPattern);
1619       AliInfo(Form("List of detectors to be read out: %s",detClust.Data()));
1620     }
1621
1622     fprintf(pipe2, "GDC DetectorPattern %u Timestamp %ld\n", detectorPattern, runLoader->GetHeader()->GetTimeStamp());
1623     Float_t ldc = 0;
1624     Int_t prevLDC = -1;
1625
1626     // loop over detectors and DDLs
1627     for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1628       // Write only raw data from detectors that
1629       // are contained in the trigger cluster(s)
1630       if (!IsSelected(AliDAQ::DetectorName(iDet),detClust)) continue;
1631
1632       for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
1633
1634         Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
1635         Int_t ldcID = Int_t(ldc + 0.0001);
1636         ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
1637
1638         char rawFileName[256];
1639         snprintf(rawFileName, 256, "raw%d/%s", 
1640                 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
1641
1642         // check existence and size of raw data file
1643         FILE* file = fopen(rawFileName, "rb");
1644         if (!file) continue;
1645         fseek(file, 0, SEEK_END);
1646         unsigned long size = ftell(file);
1647         fclose(file);
1648         if (!size) continue;
1649
1650         if (ldcID != prevLDC) {
1651           fprintf(pipe2, " LDC Id %d\n", ldcID);
1652           prevLDC = ldcID;
1653         }
1654         fprintf(pipe2, "  Equipment Id %d Payload %s\n", ddlID, rawFileName);
1655       }
1656     }
1657   }
1658
1659   Int_t result2 = gSystem->ClosePipe(pipe2);
1660
1661   delete runLoader;
1662   return ((result == 0) && (result2 == 0));
1663 }
1664
1665 //_____________________________________________________________________________
1666 Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
1667                                         const char* rootFileName)
1668 {
1669 // convert a DATE file to a root file with the program "alimdc"
1670
1671   // ALIMDC setup
1672   const Int_t kDBSize = 2000000000;
1673   const Int_t kTagDBSize = 1000000000;
1674   const Bool_t kFilter = kFALSE;
1675   const Int_t kCompression = 1;
1676
1677   char* path = gSystem->Which(gSystem->Getenv("PATH"), "alimdc");
1678   if (!path) {
1679     AliError("the program alimdc was not found");
1680     if (fStopOnError) return kFALSE;
1681   } else {
1682     delete[] path;
1683   }
1684
1685   AliInfo(Form("converting DATE file %s to root file %s", 
1686                dateFileName, rootFileName));
1687
1688   const char* rawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
1689   const char* tagDBFS    = "/tmp/mdc1/tags";
1690
1691   // User defined file system locations
1692   if (gSystem->Getenv("ALIMDC_RAWDB1")) 
1693     rawDBFS[0] = gSystem->Getenv("ALIMDC_RAWDB1");
1694   if (gSystem->Getenv("ALIMDC_RAWDB2")) 
1695     rawDBFS[1] = gSystem->Getenv("ALIMDC_RAWDB2");
1696   if (gSystem->Getenv("ALIMDC_TAGDB")) 
1697     tagDBFS = gSystem->Getenv("ALIMDC_TAGDB");
1698
1699   gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1700   gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1701   gSystem->Exec(Form("rm -rf %s",tagDBFS));
1702
1703   gSystem->Exec(Form("mkdir %s",rawDBFS[0]));
1704   gSystem->Exec(Form("mkdir %s",rawDBFS[1]));
1705   gSystem->Exec(Form("mkdir %s",tagDBFS));
1706
1707   Int_t result = gSystem->Exec(Form("alimdc %d %d %d %d %s", 
1708                                     kDBSize, kTagDBSize, kFilter, kCompression, dateFileName));
1709   gSystem->Exec(Form("mv %s/*.root %s", rawDBFS[0], rootFileName));
1710
1711   gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1712   gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1713   gSystem->Exec(Form("rm -rf %s",tagDBFS));
1714
1715   return (result == 0);
1716 }
1717
1718
1719 //_____________________________________________________________________________
1720 AliRunLoader* AliSimulation::LoadRun(const char* mode) const
1721 {
1722 // delete existing run loaders, open a new one and load gAlice
1723
1724   delete AliRunLoader::Instance();
1725   AliRunLoader* runLoader = 
1726     AliRunLoader::Open(fGAliceFileName.Data(), 
1727                        AliConfig::GetDefaultEventFolderName(), mode);
1728   if (!runLoader) {
1729     AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
1730     return NULL;
1731   }
1732   runLoader->LoadgAlice();
1733   runLoader->LoadHeader();
1734   gAlice = runLoader->GetAliRun();
1735   if (!gAlice) {
1736     AliError(Form("no gAlice object found in file %s", 
1737                   fGAliceFileName.Data()));
1738     return NULL;
1739   }
1740   return runLoader;
1741 }
1742
1743 //_____________________________________________________________________________
1744 Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
1745 {
1746 // get or calculate the number of signal events per background event
1747
1748   if (!fBkgrdFileNames) return 1;
1749   Int_t nBkgrdFiles = fBkgrdFileNames->GetEntriesFast();
1750   if (nBkgrdFiles == 0) return 1;
1751
1752   // get the number of signal events
1753   if (nEvents <= 0) {
1754     AliRunLoader* runLoader = 
1755         AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
1756     if (!runLoader) return 1;
1757     
1758     nEvents = runLoader->GetNumberOfEvents();
1759     delete runLoader;
1760   }
1761
1762   Int_t result = 0;
1763   for (Int_t iBkgrdFile = 0; iBkgrdFile < nBkgrdFiles; iBkgrdFile++) {
1764     // get the number of background events
1765     const char* fileName = ((TObjString*)
1766                             (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
1767     AliRunLoader* runLoader =
1768       AliRunLoader::Open(fileName, "BKGRD");
1769     if (!runLoader) continue;
1770     Int_t nBkgrdEvents = runLoader->GetNumberOfEvents();
1771     delete runLoader;
1772   
1773     // get or calculate the number of signal per background events
1774     Int_t nSignalPerBkgrd = fBkgrdFileNames->At(iBkgrdFile)->GetUniqueID();
1775     if (nSignalPerBkgrd <= 0) {
1776       nSignalPerBkgrd = (nEvents-1) / nBkgrdEvents + 1;
1777     } else if (result && (result != nSignalPerBkgrd)) {
1778       AliInfo(Form("the number of signal events per background event "
1779                    "will be changed from %d to %d for stream %d", 
1780                    nSignalPerBkgrd, result, iBkgrdFile+1));
1781       nSignalPerBkgrd = result;
1782     }
1783
1784     if (!result) result = nSignalPerBkgrd;
1785     if (nSignalPerBkgrd * nBkgrdEvents < nEvents) {
1786       AliWarning(Form("not enough background events (%d) for %d signal events "
1787                       "using %d signal per background events for stream %d",
1788                       nBkgrdEvents, nEvents, nSignalPerBkgrd, iBkgrdFile+1));
1789     }
1790   }
1791
1792   return result;
1793 }
1794
1795 //_____________________________________________________________________________
1796 Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
1797 {
1798 // check whether detName is contained in detectors
1799 // if yes, it is removed from detectors
1800
1801   // check if all detectors are selected
1802   if ((detectors.CompareTo("ALL") == 0) ||
1803       detectors.BeginsWith("ALL ") ||
1804       detectors.EndsWith(" ALL") ||
1805       detectors.Contains(" ALL ")) {
1806     detectors = "ALL";
1807     return kTRUE;
1808   }
1809
1810   // search for the given detector
1811   Bool_t result = kFALSE;
1812   if ((detectors.CompareTo(detName) == 0) ||
1813       detectors.BeginsWith(detName+" ") ||
1814       detectors.EndsWith(" "+detName) ||
1815       detectors.Contains(" "+detName+" ")) {
1816     detectors.ReplaceAll(detName, "");
1817     result = kTRUE;
1818   }
1819
1820   // clean up the detectors string
1821   while (detectors.Contains("  ")) detectors.ReplaceAll("  ", " ");
1822   while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
1823   while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
1824
1825   return result;
1826 }
1827
1828 //_____________________________________________________________________________
1829 Bool_t AliSimulation::ConvertRaw2SDigits(const char* rawDirectory, const char* esdFileName, Int_t N) 
1830 {
1831 //
1832 // Steering routine  to convert raw data in directory rawDirectory/ to fake SDigits. 
1833 // These can be used for embedding of MC tracks into RAW data using the standard 
1834 // merging procedure.
1835 //
1836 // If an ESD file is given the reconstructed vertex is taken from it and stored in the event header.
1837 //
1838   if (!gAlice) {
1839     AliError("no gAlice object. Restart aliroot and try again.");
1840     return kFALSE;
1841   }
1842   if (gAlice->Modules()->GetEntries() > 0) {
1843     AliError("gAlice was already run. Restart aliroot and try again.");
1844     return kFALSE;
1845   }
1846   
1847   AliInfo(Form("initializing gAlice with config file %s",fConfigFileName.Data()));
1848   
1849   gAlice->Announce();
1850   
1851   gROOT->LoadMacro(fConfigFileName.Data());
1852   gInterpreter->ProcessLine(gAlice->GetConfigFunction());
1853   
1854   if(AliCDBManager::Instance()->GetRun() >= 0) { 
1855         SetRunNumber(AliCDBManager::Instance()->GetRun());
1856   } else {
1857         AliWarning("Run number not initialized!!");
1858   }
1859   
1860    AliRunLoader::Instance()->CdGAFile();
1861     
1862    AliPDG::AddParticlesToPdgDataBase();  
1863
1864    gMC->SetMagField(TGeoGlobalMagField::Instance()->GetField());
1865    
1866    gAlice->GetMCApp()->Init();
1867
1868    //Must be here because some MCs (G4) adds detectors here and not in Config.C
1869    gAlice->InitLoaders();
1870    AliRunLoader::Instance()->MakeTree("E");
1871    AliRunLoader::Instance()->LoadKinematics("RECREATE");
1872    AliRunLoader::Instance()->LoadTrackRefs("RECREATE");
1873    AliRunLoader::Instance()->LoadHits("all","RECREATE");
1874
1875    //
1876    // Save stuff at the beginning of the file to avoid file corruption
1877    AliRunLoader::Instance()->CdGAFile();
1878    gAlice->Write();
1879 //
1880 //  Initialize CDB     
1881     InitCDB();
1882     //AliCDBManager* man = AliCDBManager::Instance();
1883     //man->SetRun(0); // Should this come from rawdata header ?
1884     
1885     Int_t iDet;
1886     //
1887     // Get the runloader
1888     AliRunLoader* runLoader = AliRunLoader::Instance();
1889     //
1890     // Open esd file if available
1891     TFile* esdFile = 0;
1892     TTree* treeESD = 0;
1893     AliESDEvent* esd = 0;
1894     if (esdFileName && (strlen(esdFileName)>0)) {
1895       esdFile = TFile::Open(esdFileName);
1896       if (esdFile) {
1897         esd = new AliESDEvent();
1898         esdFile->GetObject("esdTree", treeESD);
1899         if (treeESD) esd->ReadFromTree(treeESD);
1900       }
1901     }
1902
1903     //
1904     // Create the RawReader
1905     TString fileName(rawDirectory);
1906     AliRawReader* rawReader = 0x0;
1907     if (fileName.EndsWith("/")) {
1908       rawReader = new AliRawReaderFile(fileName);
1909     } else if (fileName.EndsWith(".root")) {
1910       rawReader = new AliRawReaderRoot(fileName);
1911     } else if (!fileName.IsNull()) {
1912       rawReader = new AliRawReaderDate(fileName);
1913     }
1914     if (!rawReader) return (kFALSE);
1915     
1916 //     if (!fEquipIdMap.IsNull() && fRawReader)
1917 //       fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
1918     //
1919     // Get list of detectors
1920     TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1921     //
1922     // Get Header
1923     AliHeader* header = runLoader->GetHeader();
1924     // Event loop
1925     Int_t nev = 0;
1926     while(kTRUE) {
1927         if (!(rawReader->NextEvent())) break;
1928         runLoader->SetEventNumber(nev);
1929         runLoader->GetHeader()->Reset(rawReader->GetRunNumber(), 
1930                                       nev, nev);
1931         runLoader->GetEvent(nev);
1932         AliInfo(Form("We are at event %d",nev));
1933         //
1934         // Detector loop
1935         TString detStr = fMakeSDigits;
1936         for (iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1937             AliModule* det = (AliModule*) detArray->At(iDet);
1938             if (!det || !det->IsActive()) continue;
1939             if (IsSelected(det->GetName(), detStr)) {
1940               AliInfo(Form("Calling Raw2SDigits for %s", det->GetName()));
1941               det->Raw2SDigits(rawReader);
1942               rawReader->Reset();
1943             }
1944         } // detectors
1945         
1946
1947         //
1948         //  If ESD information available obtain reconstructed vertex and store in header.
1949         if (treeESD) {
1950             treeESD->GetEvent(nev);
1951             const AliESDVertex* esdVertex = esd->GetPrimaryVertex();
1952             Double_t position[3];
1953             esdVertex->GetXYZ(position);
1954             AliGenEventHeader* mcHeader = new  AliGenEventHeader("ESD");
1955             TArrayF mcV;
1956             mcV.Set(3);
1957             for (Int_t i = 0; i < 3; i++) mcV[i] = position[i];
1958             mcHeader->SetPrimaryVertex(mcV);
1959             header->Reset(0,nev);
1960             header->SetGenEventHeader(mcHeader);
1961             AliInfo(Form("***** Saved vertex %f %f %f \n", position[0], position[1], position[2]));
1962         }
1963 //
1964 //      Finish the event
1965         runLoader->TreeE()->Fill();
1966         AliInfo(Form("Finished event %d",nev));
1967         nev++;
1968         if (N>0&&nev>=N)
1969           break;
1970     } // events
1971  
1972     delete rawReader;
1973 //
1974 //  Finish the run 
1975     runLoader->CdGAFile();
1976     runLoader->WriteHeader("OVERWRITE");
1977     runLoader->WriteRunLoader();
1978
1979     return kTRUE;
1980 }
1981
1982 //_____________________________________________________________________________
1983 void AliSimulation::FinishRun()
1984 {
1985   //
1986   // Called at the end of the run.
1987   //
1988
1989   if(IsLegoRun()) 
1990    {
1991     AliDebug(1, "Finish Lego");
1992     AliRunLoader::Instance()->CdGAFile();
1993     fLego->FinishRun();
1994    }
1995   
1996   // Clean detector information
1997   TIter next(gAlice->Modules());
1998   AliModule *detector;
1999   while((detector = dynamic_cast<AliModule*>(next()))) {
2000     AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
2001     detector->FinishRun();
2002   }
2003   
2004   AliDebug(1, "AliRunLoader::Instance()->WriteHeader(OVERWRITE)");
2005   AliRunLoader::Instance()->WriteHeader("OVERWRITE");
2006
2007   // Write AliRun info and all detectors parameters
2008   AliRunLoader::Instance()->CdGAFile();
2009   gAlice->Write(0,TObject::kOverwrite);//write AliRun
2010   AliRunLoader::Instance()->Write(0,TObject::kOverwrite);//write RunLoader itself
2011   
2012   if(gAlice->GetMCApp()) gAlice->GetMCApp()->FinishRun();  
2013   AliRunLoader::Instance()->Synchronize();
2014 }
2015
2016 //_____________________________________________________________________________
2017 Int_t AliSimulation::GetDetIndex(const char* detector)
2018 {
2019   // return the detector index corresponding to detector
2020   Int_t index = -1 ; 
2021   for (index = 0; index < fgkNDetectors ; index++) {
2022     if ( strcmp(detector, fgkDetectorName[index]) == 0 )
2023           break ; 
2024   }     
2025   return index ; 
2026 }
2027
2028 //_____________________________________________________________________________
2029 Bool_t AliSimulation::CreateHLT()
2030 {
2031   // Init the HLT simulation.
2032   // The function  loads the library and creates the instance of AliHLTSimulation.
2033   // the main reason for the decoupled creation is to set the transient OCDB
2034   // objects before the OCDB is locked
2035
2036   // load the library dynamically
2037   gSystem->Load(ALIHLTSIMULATION_LIBRARY);
2038
2039   // check for the library version
2040   AliHLTSimulationGetLibraryVersion_t fctVersion=(AliHLTSimulationGetLibraryVersion_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_GET_LIBRARY_VERSION));
2041   if (!fctVersion) {
2042     AliError(Form("can not load library %s", ALIHLTSIMULATION_LIBRARY));
2043     return kFALSE;
2044   }
2045   if (fctVersion()!= ALIHLTSIMULATION_LIBRARY_VERSION) {
2046     AliWarning(Form("%s version does not match: compiled for version %d, loaded %d", ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_LIBRARY_VERSION, fctVersion()));
2047   }
2048
2049   // print compile info
2050   typedef void (*CompileInfo)( const char*& date, const char*& time);
2051   CompileInfo fctInfo=(CompileInfo)gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, "CompileInfo");
2052   if (fctInfo) {
2053     const char* date="";
2054     const char* time="";
2055     (*fctInfo)(date, time);
2056     if (!date) date="unknown";
2057     if (!time) time="unknown";
2058     AliInfo(Form("%s build on %s (%s)", ALIHLTSIMULATION_LIBRARY, date, time));
2059   } else {
2060     AliInfo(Form("no build info available for %s", ALIHLTSIMULATION_LIBRARY));
2061   }
2062
2063   // create instance of the HLT simulation
2064   AliHLTSimulationCreateInstance_t fctCreate=(AliHLTSimulationCreateInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_CREATE_INSTANCE));
2065   if (fctCreate==NULL || (fpHLT=(fctCreate()))==NULL) {
2066     AliError(Form("can not create instance of HLT simulation (creator %p)", fctCreate));
2067     return kFALSE;    
2068   }
2069
2070   TString specObjects;
2071   for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
2072     if (specObjects.Length()>0) specObjects+=" ";
2073     specObjects+=fSpecCDBUri[i]->GetName();
2074   }
2075
2076   AliHLTSimulationSetup_t fctSetup=(AliHLTSimulationSetup_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_SETUP));
2077   if (fctSetup==NULL || fctSetup(fpHLT, this, specObjects.Data())<0) {
2078     AliWarning(Form("failed to setup HLT simulation (function %p)", fctSetup));
2079   }
2080
2081   return kTRUE;
2082 }
2083
2084 //_____________________________________________________________________________
2085 Bool_t AliSimulation::RunHLT()
2086 {
2087   // Run the HLT simulation
2088   // HLT simulation is implemented in HLT/sim/AliHLTSimulation
2089   // Disabled if fRunHLT is empty, default vaule is "default".
2090   // AliSimulation::SetRunHLT can be used to set the options for HLT simulation
2091   // The default simulation depends on the HLT component libraries and their
2092   // corresponding agents which define components and chains to run. See
2093   // http://web.ift.uib.no/~kjeks/doc/alice-hlt-current/
2094   // http://web.ift.uib.no/~kjeks/doc/alice-hlt-current/classAliHLTModuleAgent.html
2095   //
2096   // The libraries to be loaded can be specified as an option.
2097   // <pre>
2098   // AliSimulation sim;
2099   // sim.SetRunHLT("libAliHLTSample.so");
2100   // </pre>
2101   // will only load <tt>libAliHLTSample.so</tt>
2102
2103   // Other available options:
2104   // \li loglevel=<i>level</i> <br>
2105   //     logging level for this processing
2106   // \li alilog=off
2107   //     disable redirection of log messages to AliLog class
2108   // \li config=<i>macro</i>
2109   //     configuration macro
2110   // \li chains=<i>configuration</i>
2111   //     comma separated list of configurations to be run during simulation
2112   // \li rawfile=<i>file</i>
2113   //     source for the RawReader to be created, the default is <i>./</i> if
2114   //     raw data is simulated
2115
2116   int iResult=0;
2117
2118   if (!fpHLT && !CreateHLT()) {
2119     return kFALSE;
2120   }
2121   AliHLTSimulation* pHLT=fpHLT;
2122
2123   AliRunLoader* pRunLoader = LoadRun("READ");
2124   if (!pRunLoader) return kFALSE;
2125
2126   // initialize CDB storage, run number, set CDB lock
2127   // thats for the case of running HLT simulation without all the other steps
2128   // multiple calls are handled by the function, so we can just call
2129   InitCDB();
2130   if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
2131   SetCDBLock();
2132   
2133   // init the HLT simulation
2134   TString options;
2135   if (fRunHLT.CompareTo("default")!=0) options=fRunHLT;
2136   TString detStr = fWriteRawData;
2137   if (!IsSelected("HLT", detStr)) {
2138     options+=" writerawfiles=";
2139   } else {
2140     options+=" writerawfiles=HLT";
2141   }
2142
2143   if (!detStr.IsNull() && !options.Contains("rawfile=")) {
2144     // as a matter of fact, HLT will run reconstruction and needs the RawReader
2145     // in order to get detector data. By default, RawReaderFile is used to read
2146     // the already simulated ddl files. Date and Root files from the raw data
2147     // are generated after the HLT simulation.
2148     options+=" rawfile=./";
2149   }
2150
2151   AliHLTSimulationInit_t fctInit=(AliHLTSimulationInit_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_INIT));
2152   if (fctInit==NULL || (iResult=(fctInit(pHLT, pRunLoader, options.Data())))<0) {
2153     AliError(Form("can not init HLT simulation: error %d (init %p)", iResult, fctInit));
2154   } else {
2155     // run the HLT simulation
2156     AliHLTSimulationRun_t fctRun=(AliHLTSimulationRun_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_RUN));
2157     if (fctRun==NULL || (iResult=(fctRun(pHLT, pRunLoader)))<0) {
2158       AliError(Form("can not run HLT simulation: error %d (run %p)", iResult, fctRun));
2159     }
2160   }
2161
2162   // delete the instance
2163   AliHLTSimulationDeleteInstance_t fctDelete=(AliHLTSimulationDeleteInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_DELETE_INSTANCE));
2164   if (fctDelete==NULL || fctDelete(pHLT)<0) {
2165     AliError(Form("can not delete instance of HLT simulation (creator %p)", fctDelete));
2166   }
2167   pHLT=NULL;
2168
2169   return iResult>=0?kTRUE:kFALSE;
2170 }
2171
2172 //_____________________________________________________________________________
2173 Bool_t AliSimulation::RunQA()
2174 {
2175         // run the QA on summable hits, digits or digits
2176         
2177     //if(!gAlice) return kFALSE;
2178         AliQAManager::QAManager()->SetRunLoader(AliRunLoader::Instance()) ;
2179
2180         TString detectorsw("") ;  
2181         Bool_t rv = kTRUE ; 
2182   AliQAManager::QAManager()->SetEventSpecie(fEventSpecie) ;
2183         detectorsw = AliQAManager::QAManager()->Run(fQADetectors.Data()) ; 
2184         if ( detectorsw.IsNull() ) 
2185                 rv = kFALSE ; 
2186         return rv ; 
2187 }
2188
2189 //_____________________________________________________________________________
2190 Bool_t AliSimulation::SetRunQA(TString detAndAction) 
2191 {
2192         // Allows to run QA for a selected set of detectors
2193         // and a selected set of tasks among HITS, SDIGITS and DIGITS
2194         // all selected detectors run the same selected tasks
2195         
2196         if (!detAndAction.Contains(":")) {
2197                 AliError( Form("%s is a wrong syntax, use \"DetectorList:ActionList\" \n", detAndAction.Data()) ) ;
2198                 fRunQA = kFALSE ;
2199                 return kFALSE ;                 
2200         }
2201         Int_t colon = detAndAction.Index(":") ; 
2202         fQADetectors = detAndAction(0, colon) ; 
2203         if (fQADetectors.Contains("ALL") ){
2204     TString tmp = Form("%s %s", fMakeDigits.Data(), fMakeDigitsFromHits.Data()) ; 
2205     Int_t minus = fQADetectors.Last('-') ; 
2206     TString toKeep = Form("%s %s", fMakeDigits.Data(), fMakeDigitsFromHits.Data()) ; 
2207     TString toRemove("") ;
2208     while (minus >= 0) {
2209       toRemove = fQADetectors(minus+1, fQADetectors.Length()) ; 
2210       toRemove = toRemove.Strip() ; 
2211       toKeep.ReplaceAll(toRemove, "") ; 
2212       fQADetectors.ReplaceAll(Form("-%s", toRemove.Data()), "") ; 
2213       minus = fQADetectors.Last('-') ; 
2214     }
2215     fQADetectors = toKeep ; 
2216   }
2217   fQATasks   = detAndAction(colon+1, detAndAction.Sizeof() ) ; 
2218         if (fQATasks.Contains("ALL") ) {
2219                 fQATasks = Form("%d %d %d", AliQAv1::kHITS, AliQAv1::kSDIGITS, AliQAv1::kDIGITS) ; 
2220         } else {
2221                 fQATasks.ToUpper() ; 
2222                 TString tempo("") ; 
2223                 if ( fQATasks.Contains("HIT") ) 
2224                         tempo = Form("%d ", AliQAv1::kHITS) ; 
2225                 if ( fQATasks.Contains("SDIGIT") ) 
2226                         tempo += Form("%d ", AliQAv1::kSDIGITS) ; 
2227                 if ( fQATasks.Contains("DIGIT") ) 
2228                         tempo += Form("%d ", AliQAv1::kDIGITS) ; 
2229                 fQATasks = tempo ; 
2230                 if (fQATasks.IsNull()) {
2231                         AliInfo("No QA requested\n")  ;
2232                         fRunQA = kFALSE ;
2233                         return kTRUE ; 
2234                 }
2235         }       
2236         TString tempo(fQATasks) ; 
2237     tempo.ReplaceAll(Form("%d", AliQAv1::kHITS), AliQAv1::GetTaskName(AliQAv1::kHITS))  ;
2238     tempo.ReplaceAll(Form("%d", AliQAv1::kSDIGITS), AliQAv1::GetTaskName(AliQAv1::kSDIGITS)) ;  
2239     tempo.ReplaceAll(Form("%d", AliQAv1::kDIGITS), AliQAv1::GetTaskName(AliQAv1::kDIGITS)) ;    
2240         AliInfo( Form("QA will be done on \"%s\" for \"%s\"\n", fQADetectors.Data(), tempo.Data()) ) ;  
2241         fRunQA = kTRUE ;
2242         AliQAManager::QAManager()->SetActiveDetectors(fQADetectors) ; 
2243         AliQAManager::QAManager()->SetTasks(fQATasks) ; 
2244   for (Int_t det = 0 ; det < AliQAv1::kNDET ; det++) 
2245     AliQAManager::QAManager()->SetWriteExpert(AliQAv1::DETECTORINDEX_t(det)) ;
2246   
2247         return kTRUE; 
2248
2249
2250 //_____________________________________________________________________________
2251 void AliSimulation::ProcessEnvironmentVars()
2252 {
2253 // Extract run number and random generator seed from env variables
2254
2255     AliInfo("Processing environment variables");
2256     
2257     // Random Number seed
2258     
2259     // first check that seed is not already set
2260     if (fSeed == 0) {
2261         if (gSystem->Getenv("CONFIG_SEED")) {
2262                 fSeed = atoi(gSystem->Getenv("CONFIG_SEED"));
2263         }
2264     } else {
2265         if (gSystem->Getenv("CONFIG_SEED")) {
2266                 AliInfo(Form("Seed for random number generation already set (%d)"
2267                              ": CONFIG_SEED variable ignored!", fSeed));
2268         }
2269     }
2270    
2271     AliInfo(Form("Seed for random number generation = %d ", fSeed)); 
2272
2273     // Run Number
2274     
2275     // first check that run number is not already set
2276     if(fRun < 0) {    
2277         if (gSystem->Getenv("DC_RUN")) {
2278                 fRun = atoi(gSystem->Getenv("DC_RUN"));
2279         }
2280     } else {
2281         if (gSystem->Getenv("DC_RUN")) {
2282                 AliInfo(Form("Run number already set (%d): DC_RUN variable ignored!", fRun));
2283         }
2284     }
2285     
2286     AliInfo(Form("Run number = %d", fRun)); 
2287 }
2288
2289 //---------------------------------------------------------------------
2290 void AliSimulation::WriteGRPEntry()
2291 {
2292   // Get the necessary information from galice (generator, trigger etc) and
2293   // write a GRP entry corresponding to the settings in the Config.C used
2294   // note that Hall probes and Cavern and Surface Atmos pressures are not simulated.
2295
2296
2297   AliInfo("Writing global run parameters entry into the OCDB");
2298
2299   AliGRPObject* grpObj = new AliGRPObject();
2300
2301   grpObj->SetRunType("PHYSICS");
2302   grpObj->SetTimeStart(fTimeStart);
2303   grpObj->SetTimeEnd(fTimeEnd); 
2304   grpObj->SetBeamEnergyIsSqrtSHalfGeV(); // new format of GRP: store sqrt(s)/2 in GeV
2305
2306   const AliGenerator *gen = gAlice->GetMCApp()->Generator();
2307   Int_t a = 0;
2308   Int_t z = 0;
2309
2310   if (gen) {
2311     TString projectile;
2312     gen->GetProjectile(projectile,a,z);
2313     TString target;
2314     gen->GetTarget(target,a,z);
2315     TString beamType = projectile + "-" + target;
2316     beamType.ReplaceAll(" ","");
2317     if (!beamType.CompareTo("-")) {
2318       grpObj->SetBeamType("UNKNOWN");
2319       grpObj->SetBeamEnergy(gen->GetEnergyCMS()/2);
2320     }
2321     else {
2322       grpObj->SetBeamType(beamType);
2323       if (z != 0) {
2324           grpObj->SetBeamEnergy(gen->GetEnergyCMS()/2 * a / z);
2325       } else {
2326           grpObj->SetBeamEnergy(gen->GetEnergyCMS()/2 );
2327       }
2328       // Heavy ion run, the event specie is set to kHighMult
2329       fEventSpecie = AliRecoParam::kHighMult;
2330       if ((strcmp(beamType,"p-p") == 0) ||
2331           (strcmp(beamType,"p-")  == 0) ||
2332           (strcmp(beamType,"-p")  == 0) ||
2333           (strcmp(beamType,"P-P") == 0) ||
2334           (strcmp(beamType,"P-")  == 0) ||
2335           (strcmp(beamType,"-P")  == 0)) {
2336         // Proton run, the event specie is set to kLowMult
2337         fEventSpecie = AliRecoParam::kLowMult;
2338       } 
2339     }
2340   } else {
2341     AliWarning("Unknown beam type and energy! Setting energy to 0");
2342     grpObj->SetBeamEnergy(0);
2343     grpObj->SetBeamType("UNKNOWN");
2344   }
2345
2346   UInt_t detectorPattern  = 0;
2347   Int_t nDets = 0;
2348   TObjArray *detArray = gAlice->Detectors();
2349   for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors-1; iDet++) {
2350     if (detArray->FindObject(AliDAQ::OfflineModuleName(iDet))) {
2351       AliDebug(1, Form("Detector #%d found: %s", iDet, AliDAQ::OfflineModuleName(iDet)));
2352       detectorPattern |= (1 << iDet);
2353       nDets++;
2354     }
2355   }
2356   // CTP
2357   if (!fTriggerConfig.IsNull())
2358     detectorPattern |= (1 << AliDAQ::DetectorID("TRG"));
2359
2360   // HLT
2361   if (!fRunHLT.IsNull())
2362     detectorPattern |= (1 << AliDAQ::kHLTId);
2363
2364   grpObj->SetNumberOfDetectors((Char_t)nDets);
2365   grpObj->SetDetectorMask((Int_t)detectorPattern);
2366   grpObj->SetLHCPeriod("LHC08c");
2367   grpObj->SetLHCState("STABLE_BEAMS");
2368   //
2369   AliMagF *field = (AliMagF*)TGeoGlobalMagField::Instance()->GetField();
2370   Float_t solenoidField = field ? TMath::Abs(field->SolenoidField()) : 0;
2371
2372   Float_t factorSol     = field ? field->GetFactorSol() : 0;
2373   Float_t currentSol    = TMath::Abs(factorSol)>1E-6 ? 
2374     TMath::Nint(TMath::Abs(solenoidField/factorSol))/5.*30000.*TMath::Abs(factorSol) : 0;
2375   //
2376   Float_t factorDip     = field ? field->GetFactorDip() : 0;
2377   Float_t currentDip    = 6000.*TMath::Abs(factorDip);
2378   //
2379   grpObj->SetL3Current(currentSol,(AliGRPObject::Stats)0);
2380   grpObj->SetDipoleCurrent(currentDip,(AliGRPObject::Stats)0);  
2381   grpObj->SetL3Polarity(factorSol>0 ? 0:1);  
2382   grpObj->SetDipolePolarity(factorDip>0 ? 0:1);
2383   if (field) grpObj->SetUniformBMap(field->IsUniform()); // for special MC with k5kGUniform map
2384   grpObj->SetPolarityConventionLHC();                    // LHC convention +/+ current -> -/- field main components
2385   //
2386   grpObj->SetCavernTemperature(0,(AliGRPObject::Stats)0);
2387   
2388   //grpMap->Add(new TObjString("fCavernPressure"),new TObjString("0")); ---> not inserted in simulation with the new object, since it is now an AliDCSSensor
2389
2390   // Now store the entry in OCDB
2391   AliCDBManager* man = AliCDBManager::Instance();
2392   
2393   man->SetLock(0, fKey);
2394   
2395   AliCDBStorage* sto = man->GetStorage(fGRPWriteLocation.Data());
2396   
2397
2398   AliCDBId id("GRP/GRP/Data", man->GetRun(), man->GetRun(), 1, 1);
2399   AliCDBMetaData *metadata= new AliCDBMetaData();
2400
2401   metadata->SetResponsible("alice-off@cern.ch");
2402   metadata->SetComment("Automatically produced GRP entry for Monte Carlo");
2403  
2404   sto->Put(grpObj,id,metadata);
2405   man->SetLock(1, fKey);
2406 }
2407
2408 //_____________________________________________________________________________
2409 time_t AliSimulation::GenerateTimeStamp() const
2410 {
2411   // Generate event time-stamp according to
2412   // SOR/EOR time from GRP
2413   if (fUseTimeStampFromCDB)
2414     return fTimeStart + gRandom->Integer(fTimeEnd-fTimeStart);
2415   else
2416     return 0;
2417 }