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