EMCAL cells and PHOS cells separated.
[u/mrichter/AliRoot.git] / STEER / AliSimulation.cxx
CommitLineData
85a5290f 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/* $Id$ */
17
18///////////////////////////////////////////////////////////////////////////////
19// //
20// class for running generation, simulation and digitization //
21// //
22// Hits, sdigits and digits are created for all detectors by typing: //
23// //
24// AliSimulation sim; //
25// sim.Run(); //
26// //
27// The Run method returns kTRUE in case of successful execution. //
28// The number of events can be given as argument to the Run method or it //
29// can be set by //
30// //
31// sim.SetNumberOfEvents(n); //
32// //
95601147 33// The name of the configuration file can be passed as argument to the //
34// AliSimulation constructor or can be specified by //
85a5290f 35// //
36// sim.SetConfigFile("..."); //
37// //
38// The generation of particles and the simulation of detector hits can be //
39// switched on or off by //
40// //
41// sim.SetRunGeneration(kTRUE); // generation of primary particles //
42// sim.SetRunSimulation(kFALSE); // but no tracking //
43// //
44// For which detectors sdigits and digits will be created, can be steered //
45// by //
46// //
47// sim.SetMakeSDigits("ALL"); // make sdigits for all detectors //
48// sim.SetMakeDigits("ITS TPC"); // make digits only for ITS and TPC //
49// //
50// The argument is a (case sensitive) string with the names of the //
51// detectors separated by a space. An empty string ("") can be used to //
52// disable the creation of sdigits or digits. The special string "ALL" //
53// selects all available detectors. This is the default. //
54// //
55// The creation of digits from hits instead of from sdigits can be selected //
56// by //
57// //
58// sim.SetMakeDigitsFromHits("TRD"); //
59// //
60// The argument is again a string with the selected detectors. Be aware that //
61// this feature is not available for all detectors and that merging is not //
62// possible, when digits are created directly from hits. //
63// //
05526d44 64// Background events can be merged by calling //
85a5290f 65// //
66// sim.MergeWith("background/galice.root", 2); //
67// //
68// The first argument is the file name of the background galice file. The //
69// second argument is the number of signal events per background event. //
05526d44 70// By default this number is calculated from the number of available //
71// background events. MergeWith can be called several times to merge more //
72// than two event streams. It is assumed that the sdigits were already //
73// produced for the background events. //
85a5290f 74// //
0421c3d1 75// The output of raw data can be switched on by calling //
76// //
77// sim.SetWriteRawData("MUON"); // write raw data for MUON //
78// //
06c7e392 79// The default output format of the raw data are DDL files. They are //
80// converted to a DATE file, if a file name is given as second argument. //
81// For this conversion the program "dateStream" is required. If the file //
82// name has the extension ".root", the DATE file is converted to a root //
83// file. The program "alimdc" is used for this purpose. For the conversion //
84// to DATE and root format the two conversion programs have to be installed. //
85// Only the raw data in the final format is kept if the third argument is //
86// kTRUE. //
87// //
0421c3d1 88// The methods RunSimulation, RunSDigitization, RunDigitization, //
89// RunHitsDigitization and WriteRawData can be used to run only parts of //
06c7e392 90// the full simulation chain. The creation of raw data DDL files and their //
91// conversion to the DATE or root format can be run directly by calling //
92// the methods WriteRawFiles, ConvertRawFilesToDate and ConvertDateToRoot. //
95601147 93// //
18b43626 94// The default number of events per file, which is usually set in the //
95// config file, can be changed for individual detectors and data types //
96// by calling //
97// //
98// sim.SetEventsPerFile("PHOS", "Reconstructed Points", 3); //
99// //
100// The first argument is the detector, the second one the data type and the //
101// last one the number of events per file. Valid data types are "Hits", //
102// "Summable Digits", "Digits", "Reconstructed Points" and "Tracks". //
103// The number of events per file has to be set before the simulation of //
104// hits. Otherwise it has no effect. //
105// //
85a5290f 106///////////////////////////////////////////////////////////////////////////////
107
b856a511 108#include <TVirtualMCApplication.h>
e8d02863 109#include <TGeoManager.h>
85a5290f 110#include <TObjString.h>
af7ba10c 111#include <TSystem.h>
bfbd5665 112#include <TFile.h>
85a5290f 113
87932dab 114#include "AliCodeTimer.h"
e8d02863 115#include "AliCDBStorage.h"
116#include "AliCDBEntry.h"
117#include "AliCDBManager.h"
25be1e5c 118#include "AliGeomManager.h"
e8d02863 119#include "AliAlignObj.h"
120#include "AliCentralTrigger.h"
362c9d61 121#include "AliDAQ.h"
af7ba10c 122#include "AliDigitizer.h"
123#include "AliGenerator.h"
e8d02863 124#include "AliLog.h"
af7ba10c 125#include "AliModule.h"
126#include "AliRun.h"
127#include "AliRunDigitizer.h"
128#include "AliRunLoader.h"
129#include "AliSimulation.h"
130#include "AliVertexGenFile.h"
a5a091ce 131#include "AliCentralTrigger.h"
007155cc 132#include "AliCTPRawData.h"
ca30848f 133#include "AliRawReaderFile.h"
8b31e840 134#include "AliRawReaderRoot.h"
135#include "AliRawReaderDate.h"
ca30848f 136#include "AliESD.h"
137#include "AliHeader.h"
138#include "AliGenEventHeader.h"
b856a511 139#include "AliMC.h"
cae21299 140#include "AliHLTSimulation.h"
c65c502a 141#include "AliQADataMakerSteer.h"
85a5290f 142
143ClassImp(AliSimulation)
144
b856a511 145AliSimulation *AliSimulation::fgInstance = 0;
5b188f2f 146const char* AliSimulation::fgkDetectorName[AliSimulation::fgkNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE", "HLT"};
85a5290f 147
148//_____________________________________________________________________________
795e4a22 149AliSimulation::AliSimulation(const char* configFileName,
95601147 150 const char* name, const char* title) :
151 TNamed(name, title),
152
153 fRunGeneration(kTRUE),
154 fRunSimulation(kTRUE),
024cf675 155 fLoadAlignFromCDB(kTRUE),
25be1e5c 156 fLoadAlObjsListOfDets("ALL"),
95601147 157 fMakeSDigits("ALL"),
158 fMakeDigits("ALL"),
a5a091ce 159 fMakeTrigger(""),
95601147 160 fMakeDigitsFromHits(""),
0421c3d1 161 fWriteRawData(""),
06c7e392 162 fRawDataFileName(""),
163 fDeleteIntermediateFiles(kFALSE),
95601147 164 fStopOnError(kFALSE),
165
166 fNEvents(1),
167 fConfigFileName(configFileName),
168 fGAliceFileName("galice.root"),
18b43626 169 fEventsPerFile(),
95601147 170 fBkgrdFileNames(NULL),
995ad051 171 fAlignObjArray(NULL),
04bae0a0 172 fUseBkgrdVertex(kTRUE),
024cf675 173 fRegionOfInterest(kFALSE),
795e4a22 174 fCDBUri(""),
ca30848f 175 fSpecCDBUri(),
795e4a22 176 fRun(-1),
177 fSeed(0),
178 fInitCDBCalled(kFALSE),
179 fInitRunNumberCalled(kFALSE),
180 fSetRunNumberFromDataCalled(kFALSE),
cae21299 181 fEmbeddingFlag(kFALSE),
759c1df1 182 fRunQA(kTRUE),
cae21299 183 fRunHLT("default")
85a5290f 184{
185// create simulation object with default parameters
b856a511 186 fgInstance = this;
0421c3d1 187 SetGAliceFile("galice.root");
5b188f2f 188
189// for QA
190 for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++)
191 fQACycles[iDet] = 999999;
85a5290f 192}
193
194//_____________________________________________________________________________
195AliSimulation::AliSimulation(const AliSimulation& sim) :
95601147 196 TNamed(sim),
197
198 fRunGeneration(sim.fRunGeneration),
199 fRunSimulation(sim.fRunSimulation),
024cf675 200 fLoadAlignFromCDB(sim.fLoadAlignFromCDB),
25be1e5c 201 fLoadAlObjsListOfDets(sim.fLoadAlObjsListOfDets),
95601147 202 fMakeSDigits(sim.fMakeSDigits),
203 fMakeDigits(sim.fMakeDigits),
a5a091ce 204 fMakeTrigger(sim.fMakeTrigger),
95601147 205 fMakeDigitsFromHits(sim.fMakeDigitsFromHits),
0421c3d1 206 fWriteRawData(sim.fWriteRawData),
06c7e392 207 fRawDataFileName(""),
208 fDeleteIntermediateFiles(kFALSE),
95601147 209 fStopOnError(sim.fStopOnError),
210
211 fNEvents(sim.fNEvents),
212 fConfigFileName(sim.fConfigFileName),
213 fGAliceFileName(sim.fGAliceFileName),
18b43626 214 fEventsPerFile(),
95601147 215 fBkgrdFileNames(NULL),
995ad051 216 fAlignObjArray(NULL),
04bae0a0 217 fUseBkgrdVertex(sim.fUseBkgrdVertex),
024cf675 218 fRegionOfInterest(sim.fRegionOfInterest),
ec92bee0 219 fCDBUri(sim.fCDBUri),
ca30848f 220 fSpecCDBUri(),
795e4a22 221 fRun(-1),
222 fSeed(0),
223 fInitCDBCalled(sim.fInitCDBCalled),
224 fInitRunNumberCalled(sim.fInitRunNumberCalled),
225 fSetRunNumberFromDataCalled(sim.fSetRunNumberFromDataCalled),
c65c502a 226 fEmbeddingFlag(sim.fEmbeddingFlag),
759c1df1 227 fRunQA(kTRUE),
c65c502a 228 fRunHLT(sim.fRunHLT)
85a5290f 229{
230// copy constructor
231
18b43626 232 for (Int_t i = 0; i < sim.fEventsPerFile.GetEntriesFast(); i++) {
233 if (!sim.fEventsPerFile[i]) continue;
234 fEventsPerFile.Add(sim.fEventsPerFile[i]->Clone());
235 }
236
85a5290f 237 fBkgrdFileNames = new TObjArray;
238 for (Int_t i = 0; i < sim.fBkgrdFileNames->GetEntriesFast(); i++) {
239 if (!sim.fBkgrdFileNames->At(i)) continue;
240 fBkgrdFileNames->Add(sim.fBkgrdFileNames->At(i)->Clone());
241 }
ec92bee0 242
243 for (Int_t i = 0; i < sim.fSpecCDBUri.GetEntriesFast(); i++) {
244 if (sim.fSpecCDBUri[i]) fSpecCDBUri.Add(sim.fSpecCDBUri[i]->Clone());
245 }
b856a511 246 fgInstance = this;
5b188f2f 247
248// for QA
249 for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++)
250 fQACycles[iDet] = sim.fQACycles[iDet];
85a5290f 251}
252
253//_____________________________________________________________________________
254AliSimulation& AliSimulation::operator = (const AliSimulation& sim)
255{
256// assignment operator
257
258 this->~AliSimulation();
259 new(this) AliSimulation(sim);
260 return *this;
261}
262
263//_____________________________________________________________________________
264AliSimulation::~AliSimulation()
265{
266// clean up
267
18b43626 268 fEventsPerFile.Delete();
024cf675 269// if(fAlignObjArray) fAlignObjArray->Delete(); // fAlignObjArray->RemoveAll() ???
270// delete fAlignObjArray; fAlignObjArray=0;
18b43626 271
95601147 272 if (fBkgrdFileNames) {
273 fBkgrdFileNames->Delete();
274 delete fBkgrdFileNames;
275 }
ec92bee0 276
277 fSpecCDBUri.Delete();
b856a511 278 if (fgInstance==this) fgInstance = 0;
87932dab 279
280 AliCodeTimer::Instance()->Print();
85a5290f 281}
282
283
284//_____________________________________________________________________________
285void AliSimulation::SetNumberOfEvents(Int_t nEvents)
286{
287// set the number of events for one run
288
289 fNEvents = nEvents;
290}
291
292//_____________________________________________________________________________
795e4a22 293void AliSimulation::InitCDB()
024cf675 294{
295// activate a default CDB storage
296// First check if we have any CDB storage set, because it is used
297// to retrieve the calibration and alignment constants
298
795e4a22 299 if (fInitCDBCalled) return;
300 fInitCDBCalled = kTRUE;
301
024cf675 302 AliCDBManager* man = AliCDBManager::Instance();
ec92bee0 303 if (man->IsDefaultStorageSet())
024cf675 304 {
ec92bee0 305 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
306 AliWarning("Default CDB storage has been already set !");
ced249e6 307 AliWarning(Form("Ignoring the default storage declared in AliSimulation: %s",fCDBUri.Data()));
ec92bee0 308 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
795e4a22 309 fCDBUri = man->GetDefaultStorage()->GetURI();
ec92bee0 310 }
311 else {
795e4a22 312 if (fCDBUri.Length() > 0)
313 {
314 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
315 AliDebug(2, Form("Default CDB storage is set to: %s", fCDBUri.Data()));
316 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
317 } else {
318 fCDBUri="local://$ALICE_ROOT";
319 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
320 AliWarning("Default CDB storage not yet set !!!!");
321 AliWarning(Form("Setting it now to: %s", fCDBUri.Data()));
322 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
323
324 }
ec92bee0 325 man->SetDefaultStorage(fCDBUri);
326 }
327
328 // Now activate the detector specific CDB storage locations
329 for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
330 TObject* obj = fSpecCDBUri[i];
331 if (!obj) continue;
b8ec52f6 332 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
333 AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
334 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
c3a7b59a 335 man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
ec92bee0 336 }
795e4a22 337
024cf675 338}
339
340//_____________________________________________________________________________
795e4a22 341void AliSimulation::InitRunNumber(){
342// check run number. If not set, set it to 0 !!!!
343
344 if (fInitRunNumberCalled) return;
345 fInitRunNumberCalled = kTRUE;
346
347 AliCDBManager* man = AliCDBManager::Instance();
348 if (man->GetRun() >= 0)
349 {
8c8fbdcd 350 AliFatal(Form("Run number cannot be set in AliCDBManager before start of simulation: "
351 "Use external variable DC_RUN or AliSimulation::SetRun()!"));
795e4a22 352 }
8c8fbdcd 353
354 if(fRun >= 0) {
795e4a22 355 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
356 AliDebug(2, Form("Setting CDB run number to: %d",fRun));
357 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
8c8fbdcd 358 } else {
795e4a22 359 fRun=0;
360 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
361 AliWarning("Run number not yet set !!!!");
362 AliWarning(Form("Setting it now to: %d", fRun));
363 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
364
795e4a22 365 }
8c8fbdcd 366 man->SetRun(fRun);
024cf675 367
795e4a22 368 man->Print();
024cf675 369
370}
371
372//_____________________________________________________________________________
795e4a22 373void AliSimulation::SetCDBLock() {
374 // Set CDB lock: from now on it is forbidden to reset the run number
375 // or the default storage or to activate any further storage!
376
377 AliCDBManager::Instance()->SetLock(1);
378}
379
380//_____________________________________________________________________________
381void AliSimulation::SetDefaultStorage(const char* uri) {
382// Store the desired default CDB storage location
00aa02d5 383// Activate it later within the Run() method
00aa02d5 384
795e4a22 385 fCDBUri = uri;
00aa02d5 386
387}
388
389//_____________________________________________________________________________
c3a7b59a 390void AliSimulation::SetSpecificStorage(const char* calibType, const char* uri) {
ec92bee0 391// Store a detector-specific CDB storage location
392// Activate it later within the Run() method
024cf675 393
c3a7b59a 394 AliCDBPath aPath(calibType);
395 if(!aPath.IsValid()){
396 AliError(Form("Not a valid path: %s", calibType));
397 return;
398 }
399
400 TObject* obj = fSpecCDBUri.FindObject(calibType);
ec92bee0 401 if (obj) fSpecCDBUri.Remove(obj);
c3a7b59a 402 fSpecCDBUri.Add(new TNamed(calibType, uri));
024cf675 403
404}
405
406//_____________________________________________________________________________
795e4a22 407void AliSimulation::SetRunNumber(Int_t run)
408{
409// sets run number
410// Activate it later within the Run() method
411
412 fRun = run;
413}
414
415//_____________________________________________________________________________
416void AliSimulation::SetSeed(Int_t seed)
417{
418// sets seed number
419// Activate it later within the Run() method
420
421 fSeed = seed;
422}
423
424//_____________________________________________________________________________
425Bool_t AliSimulation::SetRunNumberFromData()
426{
427 // Set the CDB manager run number
428 // The run number is retrieved from gAlice
429
430 if (fSetRunNumberFromDataCalled) return kTRUE;
431 fSetRunNumberFromDataCalled = kTRUE;
432
433 AliCDBManager* man = AliCDBManager::Instance();
434 Int_t runData = -1, runCDB = -1;
435
436 AliRunLoader* runLoader = LoadRun("READ");
437 if (!runLoader) return kFALSE;
438 else {
439 runData = runLoader->GetAliRun()->GetHeader()->GetRun();
440 delete runLoader;
441 }
442
443 runCDB = man->GetRun();
444 if(runCDB >= 0) {
445 if (runCDB != runData) {
446 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
447 AliWarning(Form("A run number was previously set in AliCDBManager: %d !", runCDB));
448 AliWarning(Form("It will be replaced with the run number got from run header: %d !", runData));
449 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
450 }
451
452 }
453
454 man->SetRun(runData);
455 fRun = runData;
456
457 if(man->GetRun() < 0) {
458 AliError("Run number not properly initalized!");
459 return kFALSE;
460 }
461
462 man->Print();
463
464 return kTRUE;
465}
466
467//_____________________________________________________________________________
85a5290f 468void AliSimulation::SetConfigFile(const char* fileName)
469{
470// set the name of the config file
471
472 fConfigFileName = fileName;
473}
474
475//_____________________________________________________________________________
0421c3d1 476void AliSimulation::SetGAliceFile(const char* fileName)
477{
478// set the name of the galice file
479// the path is converted to an absolute one if it is relative
480
481 fGAliceFileName = fileName;
482 if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
483 char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
484 fGAliceFileName);
485 fGAliceFileName = absFileName;
486 delete[] absFileName;
487 }
ac1671c0 488
489 AliDebug(2, Form("galice file name set to %s", fileName));
0421c3d1 490}
491
492//_____________________________________________________________________________
18b43626 493void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
494 Int_t nEvents)
495{
496// set the number of events per file for the given detector and data type
497// ("Hits", "Summable Digits", "Digits", "Reconstructed Points" or "Tracks")
498
499 TNamed* obj = new TNamed(detector, type);
500 obj->SetUniqueID(nEvents);
501 fEventsPerFile.Add(obj);
502}
503
504//_____________________________________________________________________________
6bae477a 505Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
506{
507 // Read the alignment objects from CDB.
508 // Each detector is supposed to have the
509 // alignment objects in DET/Align/Data CDB path.
510 // All the detector objects are then collected,
511 // sorted by geometry level (starting from ALIC) and
512 // then applied to the TGeo geometry.
513 // Finally an overlaps check is performed.
514
74ae4297 515 if (!AliGeomManager::GetGeometry() || !AliGeomManager::GetGeometry()->IsClosed()) {
516 AliError("Can't apply the misalignment! Geometry is not loaded or it is still opened!");
0717eed2 517 return kFALSE;
518 }
795e4a22 519
520 // initialize CDB storage, run number, set CDB lock
521 InitCDB();
522// if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
523 SetCDBLock();
524
6bae477a 525 Bool_t delRunLoader = kFALSE;
526 if (!runLoader) {
527 runLoader = LoadRun("READ");
528 if (!runLoader) return kFALSE;
529 delRunLoader = kTRUE;
530 }
795e4a22 531
b856a511 532 // Export ideal geometry
ced249e6 533 if(!gAlice->IsRootGeometry()) AliGeomManager::GetGeometry()->Export("geometry.root");
b856a511 534
25be1e5c 535 // Load alignment data from CDB and apply to geometry through AliGeomManager
6bae477a 536 if(fLoadAlignFromCDB){
25be1e5c 537
538 TString detStr = fLoadAlObjsListOfDets;
539 TString loadAlObjsListOfDets = "";
540
541 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
542 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
543 AliModule* det = (AliModule*) detArray->At(iDet);
544 if (!det || !det->IsActive()) continue;
545 if (IsSelected(det->GetName(), detStr)) {
0717eed2 546 //add det to list of dets to be aligned from CDB
547 loadAlObjsListOfDets += det->GetName();
548 loadAlObjsListOfDets += " ";
25be1e5c 549 }
550 } // end loop over detectors
53dd3c3d 551 loadAlObjsListOfDets.Prepend("GRP "); //add alignment objects for non-sensitive modules
45daac75 552 AliGeomManager::ApplyAlignObjsFromCDB(loadAlObjsListOfDets.Data());
25be1e5c 553 }else{
554 // Check if the array with alignment objects was
555 // provided by the user. If yes, apply the objects
556 // to the present TGeo geometry
557 if (fAlignObjArray) {
45daac75 558 if (AliGeomManager::ApplyAlignObjsToGeom(*fAlignObjArray) == kFALSE) {
0717eed2 559 AliError("The misalignment of one or more volumes failed!"
560 "Compare the list of simulated detectors and the list of detector alignment data!");
561 if (delRunLoader) delete runLoader;
562 return kFALSE;
6bae477a 563 }
564 }
6bae477a 565 }
566
ef7ff1f9 567 // Update the internal geometry of modules (ITS needs it)
25be1e5c 568 TString detStr = fLoadAlObjsListOfDets;
ef7ff1f9 569 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
570 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
571
572 AliModule* det = (AliModule*) detArray->At(iDet);
573 if (!det || !det->IsActive()) continue;
574 if (IsSelected(det->GetName(), detStr)) {
575 det->UpdateInternalGeometry();
576 }
577 } // end loop over detectors
578
579
6bae477a 580 if (delRunLoader) delete runLoader;
581
582 return kTRUE;
583}
584
6bae477a 585//_____________________________________________________________________________
85a5290f 586void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
587{
588// add a file with background events for merging
589
590 TObjString* fileNameStr = new TObjString(fileName);
591 fileNameStr->SetUniqueID(nSignalPerBkgrd);
95601147 592 if (!fBkgrdFileNames) fBkgrdFileNames = new TObjArray;
85a5290f 593 fBkgrdFileNames->Add(fileNameStr);
594}
595
ca30848f 596void AliSimulation::EmbedInto(const char* fileName, Int_t nSignalPerBkgrd)
597{
598// add a file with background events for embeddin
599 MergeWith(fileName, nSignalPerBkgrd);
600 fEmbeddingFlag = kTRUE;
601}
85a5290f 602
603//_____________________________________________________________________________
604Bool_t AliSimulation::Run(Int_t nEvents)
605{
606// run the generation, simulation and digitization
607
c65c502a 608
795e4a22 609 AliCodeTimerAuto("")
87932dab 610
795e4a22 611 // Load run number and seed from environmental vars
612 ProcessEnvironmentVars();
b0314964 613
795e4a22 614 gRandom->SetSeed(fSeed);
615
85a5290f 616 if (nEvents > 0) fNEvents = nEvents;
617
618 // generation and simulation -> hits
619 if (fRunGeneration) {
85a5290f 620 if (!RunSimulation()) if (fStopOnError) return kFALSE;
621 }
795e4a22 622
623 // initialize CDB storage from external environment
624 // (either CDB manager or AliSimulation setters),
625 // if not already done in RunSimulation()
626 InitCDB();
627
628 // Set run number in CDBManager from data
629 // From this point on the run number must be always loaded from data!
630 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
631
632 // Set CDB lock: from now on it is forbidden to reset the run number
633 // or the default storage or to activate any further storage!
634 SetCDBLock();
6bae477a 635
25be1e5c 636 // If RunSimulation was not called, load the geometry and misalign it
74ae4297 637 if (!AliGeomManager::GetGeometry()) {
638 // Initialize the geometry manager
45daac75 639 AliGeomManager::LoadGeometry("geometry.root");
74ae4297 640 if (!AliGeomManager::GetGeometry()) if (fStopOnError) return kFALSE;
641 // Misalign geometry
25be1e5c 642 if(!MisalignGeometry()) if (fStopOnError) return kFALSE;
6bae477a 643 }
25be1e5c 644
85a5290f 645 // hits -> summable digits
646 if (!fMakeSDigits.IsNull()) {
647 if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
a1b90a73 648
85a5290f 649 }
c65c502a 650
85a5290f 651
795e4a22 652
653 // summable digits -> digits
85a5290f 654 if (!fMakeDigits.IsNull()) {
655 if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
656 if (fStopOnError) return kFALSE;
657 }
a1b90a73 658 }
85a5290f 659
795e4a22 660
661
85a5290f 662 // hits -> digits
663 if (!fMakeDigitsFromHits.IsNull()) {
95601147 664 if (fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
ac1671c0 665 AliWarning(Form("Merging and direct creation of digits from hits "
666 "was selected for some detectors. "
667 "No merging will be done for the following detectors: %s",
668 fMakeDigitsFromHits.Data()));
85a5290f 669 }
670 if (!RunHitsDigitization(fMakeDigitsFromHits)) {
671 if (fStopOnError) return kFALSE;
672 }
673 }
674
795e4a22 675
676
a5a091ce 677 // digits -> trigger
087426de 678 if (!RunTrigger(fMakeTrigger)) {
679 if (fStopOnError) return kFALSE;
a5a091ce 680 }
681
795e4a22 682
683
0421c3d1 684 // digits -> raw data
685 if (!fWriteRawData.IsNull()) {
06c7e392 686 if (!WriteRawData(fWriteRawData, fRawDataFileName,
687 fDeleteIntermediateFiles)) {
0421c3d1 688 if (fStopOnError) return kFALSE;
689 }
690 }
691
795e4a22 692
693
cae21299 694 // run HLT simulation
695 if (!fRunHLT.IsNull()) {
696 if (!RunHLT()) {
697 if (fStopOnError) return kFALSE;
698 }
699 }
795e4a22 700
a1b90a73 701 // //QA
702// if (fRunQA) {
703// Bool_t rv = RunQA() ;
704// if (!rv)
705// if (fStopOnError)
706// return kFALSE ;
707// }
795e4a22 708
709 // Cleanup of CDB manager: cache and active storages!
710 AliCDBManager::Instance()->ClearCache();
711
85a5290f 712 return kTRUE;
713}
714
715//_____________________________________________________________________________
a5a091ce 716Bool_t AliSimulation::RunTrigger(const char* descriptors)
717{
718 // run the trigger
719
87932dab 720 AliCodeTimerAuto("")
a5a091ce 721
795e4a22 722 // initialize CDB storage from external environment
723 // (either CDB manager or AliSimulation setters),
724 // if not already done in RunSimulation()
725 InitCDB();
726
727 // Set run number in CDBManager from data
728 // From this point on the run number must be always loaded from data!
729 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
730
731 // Set CDB lock: from now on it is forbidden to reset the run number
732 // or the default storage or to activate any further storage!
733 SetCDBLock();
734
bacbe0fd 735 AliRunLoader* runLoader = LoadRun("READ");
736 if (!runLoader) return kFALSE;
737 TString des = descriptors;
738
087426de 739 if (des.IsNull()) {
740 if (gAlice->GetTriggerDescriptor() != "") {
741 des = gAlice->GetTriggerDescriptor();
742 }
743 else {
744 AliWarning("No trigger descriptor is specified. Skipping the trigger simulation...");
745 return kTRUE;
746 }
747 }
748
0f46f5fa 749 runLoader->MakeTree( "GG" );
bacbe0fd 750 AliCentralTrigger* aCTP = runLoader->GetTrigger();
a5a091ce 751 // Load Descriptors
bacbe0fd 752 aCTP->LoadDescriptor( des );
a5a091ce 753
754 // digits -> trigger
bacbe0fd 755 if( !aCTP->RunTrigger( runLoader ) ) {
a5a091ce 756 if (fStopOnError) {
bacbe0fd 757 // delete aCTP;
a5a091ce 758 return kFALSE;
759 }
bacbe0fd 760 }
a5a091ce 761
bacbe0fd 762 delete runLoader;
a5a091ce 763
bacbe0fd 764 return kTRUE;
a5a091ce 765}
766
007155cc 767//_____________________________________________________________________________
768Bool_t AliSimulation::WriteTriggerRawData()
769{
770 // Writes the CTP (trigger) DDL raw data
771 // Details of the format are given in the
772 // trigger TDR - pages 134 and 135.
773 AliCTPRawData writer;
774 writer.RawData();
a5a091ce 775
007155cc 776 return kTRUE;
777}
a5a091ce 778
779//_____________________________________________________________________________
95601147 780Bool_t AliSimulation::RunSimulation(Int_t nEvents)
85a5290f 781{
782// run the generation and simulation
783
87932dab 784 AliCodeTimerAuto("")
4df28b43 785
795e4a22 786 // initialize CDB storage and run number from external environment
787 // (either CDB manager or AliSimulation setters)
788 InitCDB();
789 InitRunNumber();
790 SetCDBLock();
791
95601147 792 if (!gAlice) {
ac1671c0 793 AliError("no gAlice object. Restart aliroot and try again.");
95601147 794 return kFALSE;
795 }
796 if (gAlice->Modules()->GetEntries() > 0) {
ac1671c0 797 AliError("gAlice was already run. Restart aliroot and try again.");
95601147 798 return kFALSE;
799 }
800
ac1671c0 801 AliInfo(Form("initializing gAlice with config file %s",
802 fConfigFileName.Data()));
815c2b38 803 StdoutToAliInfo(StderrToAliError(
804 gAlice->Init(fConfigFileName.Data());
805 ););
795e4a22 806
087426de 807 // Get the trigger descriptor string
808 // Either from AliSimulation or from
809 // gAlice
810 if (fMakeTrigger.IsNull()) {
811 if (gAlice->GetTriggerDescriptor() != "")
812 fMakeTrigger = gAlice->GetTriggerDescriptor();
813 }
814 else
815 gAlice->SetTriggerDescriptor(fMakeTrigger.Data());
816
6bae477a 817 // Set run number in CDBManager
024cf675 818 AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
819
820 AliRunLoader* runLoader = gAlice->GetRunLoader();
821 if (!runLoader) {
822 AliError(Form("gAlice has no run loader object. "
823 "Check your config file: %s", fConfigFileName.Data()));
824 return kFALSE;
825 }
826 SetGAliceFile(runLoader->GetFileName());
795e4a22 827
6bae477a 828 // Misalign geometry
caf9c464 829#if ROOT_VERSION_CODE < 331527
74ae4297 830 AliGeomManager::SetGeometry(gGeoManager);
caf9c464 831 MisalignGeometry(runLoader);
832#endif
995ad051 833
024cf675 834// AliRunLoader* runLoader = gAlice->GetRunLoader();
835// if (!runLoader) {
836// AliError(Form("gAlice has no run loader object. "
837// "Check your config file: %s", fConfigFileName.Data()));
838// return kFALSE;
839// }
840// SetGAliceFile(runLoader->GetFileName());
85a5290f 841
04bae0a0 842 if (!gAlice->Generator()) {
ac1671c0 843 AliError(Form("gAlice has no generator object. "
844 "Check your config file: %s", fConfigFileName.Data()));
04bae0a0 845 return kFALSE;
846 }
05526d44 847 if (nEvents <= 0) nEvents = fNEvents;
04bae0a0 848
849 // get vertex from background file in case of merging
850 if (fUseBkgrdVertex &&
851 fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
05526d44 852 Int_t signalPerBkgrd = GetNSignalPerBkgrd(nEvents);
04bae0a0 853 const char* fileName = ((TObjString*)
854 (fBkgrdFileNames->At(0)))->GetName();
ac1671c0 855 AliInfo(Form("The vertex will be taken from the background "
856 "file %s with nSignalPerBackground = %d",
857 fileName, signalPerBkgrd));
04bae0a0 858 AliVertexGenFile* vtxGen = new AliVertexGenFile(fileName, signalPerBkgrd);
859 gAlice->Generator()->SetVertexGenerator(vtxGen);
860 }
861
85a5290f 862 if (!fRunSimulation) {
85a5290f 863 gAlice->Generator()->SetTrackingFlag(0);
864 }
865
18b43626 866 // set the number of events per file for given detectors and data types
867 for (Int_t i = 0; i < fEventsPerFile.GetEntriesFast(); i++) {
868 if (!fEventsPerFile[i]) continue;
869 const char* detName = fEventsPerFile[i]->GetName();
870 const char* typeName = fEventsPerFile[i]->GetTitle();
871 TString loaderName(detName);
872 loaderName += "Loader";
873 AliLoader* loader = runLoader->GetLoader(loaderName);
874 if (!loader) {
ac1671c0 875 AliError(Form("RunSimulation", "no loader for %s found\n"
876 "Number of events per file not set for %s %s",
877 detName, typeName, detName));
18b43626 878 continue;
879 }
880 AliDataLoader* dataLoader =
881 loader->GetDataLoader(typeName);
882 if (!dataLoader) {
ac1671c0 883 AliError(Form("no data loader for %s found\n"
884 "Number of events per file not set for %s %s",
885 typeName, detName, typeName));
18b43626 886 continue;
887 }
888 dataLoader->SetNumberOfEventsPerFile(fEventsPerFile[i]->GetUniqueID());
ac1671c0 889 AliDebug(1, Form("number of events per file set to %d for %s %s",
890 fEventsPerFile[i]->GetUniqueID(), detName, typeName));
18b43626 891 }
892
ac1671c0 893 AliInfo("running gAlice");
815c2b38 894 StdoutToAliInfo(StderrToAliError(
895 gAlice->Run(nEvents);
896 ););
95601147 897
898 delete runLoader;
85a5290f 899
900 return kTRUE;
901}
902
903//_____________________________________________________________________________
95601147 904Bool_t AliSimulation::RunSDigitization(const char* detectors)
85a5290f 905{
906// run the digitization and produce summable digits
907
87932dab 908 AliCodeTimerAuto("")
4df28b43 909
795e4a22 910 // initialize CDB storage, run number, set CDB lock
911 InitCDB();
912 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
913 SetCDBLock();
914
95601147 915 AliRunLoader* runLoader = LoadRun();
916 if (!runLoader) return kFALSE;
917
85a5290f 918 TString detStr = detectors;
95601147 919 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 920 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
921 AliModule* det = (AliModule*) detArray->At(iDet);
922 if (!det || !det->IsActive()) continue;
923 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 924 AliInfo(Form("creating summable digits for %s", det->GetName()));
87932dab 925 AliCodeTimerAuto(Form("creating summable digits for %s", det->GetName()));
5b188f2f 926
85a5290f 927 det->Hits2SDigits();
928 }
929 }
930
931 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 932 AliError(Form("the following detectors were not found: %s",
933 detStr.Data()));
85a5290f 934 if (fStopOnError) return kFALSE;
935 }
936
95601147 937 delete runLoader;
938
85a5290f 939 return kTRUE;
940}
941
942
943//_____________________________________________________________________________
95601147 944Bool_t AliSimulation::RunDigitization(const char* detectors,
945 const char* excludeDetectors)
85a5290f 946{
947// run the digitization and produce digits from sdigits
948
87932dab 949 AliCodeTimerAuto("")
4df28b43 950
795e4a22 951 // initialize CDB storage, run number, set CDB lock
952 InitCDB();
953 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
954 SetCDBLock();
955
95601147 956 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
957 if (gAlice) delete gAlice;
958 gAlice = NULL;
959
960 Int_t nStreams = 1;
05526d44 961 if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
962 Int_t signalPerBkgrd = GetNSignalPerBkgrd();
85a5290f 963 AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
ca30848f 964 // manager->SetEmbeddingFlag(fEmbeddingFlag);
85a5290f 965 manager->SetInputStream(0, fGAliceFileName.Data());
966 for (Int_t iStream = 1; iStream < nStreams; iStream++) {
967 const char* fileName = ((TObjString*)
968 (fBkgrdFileNames->At(iStream-1)))->GetName();
969 manager->SetInputStream(iStream, fileName);
970 }
971
972 TString detStr = detectors;
973 TString detExcl = excludeDetectors;
95601147 974 manager->GetInputStream(0)->ImportgAlice();
975 AliRunLoader* runLoader =
976 AliRunLoader::GetRunLoader(manager->GetInputStream(0)->GetFolderName());
977 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 978 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
979 AliModule* det = (AliModule*) detArray->At(iDet);
980 if (!det || !det->IsActive()) continue;
981 if (IsSelected(det->GetName(), detStr) &&
982 !IsSelected(det->GetName(), detExcl)) {
4df28b43 983 AliDigitizer* digitizer = det->CreateDigitizer(manager);
ca30848f 984
4df28b43 985 if (!digitizer) {
ac1671c0 986 AliError(Form("no digitizer for %s", det->GetName()));
85a5290f 987 if (fStopOnError) return kFALSE;
4df28b43 988 } else {
989 digitizer->SetRegionOfInterest(fRegionOfInterest);
85a5290f 990 }
991 }
992 }
993
994 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 995 AliError(Form("the following detectors were not found: %s",
996 detStr.Data()));
85a5290f 997 if (fStopOnError) return kFALSE;
998 }
999
1000 if (!manager->GetListOfTasks()->IsEmpty()) {
ac1671c0 1001 AliInfo("executing digitization");
85a5290f 1002 manager->Exec("");
1003 }
95601147 1004
85a5290f 1005 delete manager;
1006
1007 return kTRUE;
1008}
1009
1010//_____________________________________________________________________________
95601147 1011Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
85a5290f 1012{
1013// run the digitization and produce digits from hits
1014
87932dab 1015 AliCodeTimerAuto("")
4df28b43 1016
795e4a22 1017 // initialize CDB storage, run number, set CDB lock
1018 InitCDB();
1019 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1020 SetCDBLock();
1021
591f51bb 1022 AliRunLoader* runLoader = LoadRun("READ");
95601147 1023 if (!runLoader) return kFALSE;
1024
85a5290f 1025 TString detStr = detectors;
95601147 1026 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 1027 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1028 AliModule* det = (AliModule*) detArray->At(iDet);
1029 if (!det || !det->IsActive()) continue;
1030 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 1031 AliInfo(Form("creating digits from hits for %s", det->GetName()));
85a5290f 1032 det->Hits2Digits();
1033 }
1034 }
1035
1036 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 1037 AliError(Form("the following detectors were not found: %s",
1038 detStr.Data()));
85a5290f 1039 if (fStopOnError) return kFALSE;
1040 }
1041
b17cac0f 1042 delete runLoader;
591f51bb 1043 //PH Temporary fix to avoid interference with the PHOS loder/getter
1044 //PH The problem has to be solved in more general way 09/06/05
95601147 1045
85a5290f 1046 return kTRUE;
1047}
1048
0421c3d1 1049//_____________________________________________________________________________
06c7e392 1050Bool_t AliSimulation::WriteRawData(const char* detectors,
1051 const char* fileName,
1052 Bool_t deleteIntermediateFiles)
0421c3d1 1053{
1054// convert the digits to raw data
06c7e392 1055// First DDL raw data files for the given detectors are created.
1056// If a file name is given, the DDL files are then converted to a DATE file.
1057// If deleteIntermediateFiles is true, the DDL raw files are deleted
1058// afterwards.
1059// If the file name has the extension ".root", the DATE file is converted
1060// to a root file.
1061// If deleteIntermediateFiles is true, the DATE file is deleted afterwards.
0421c3d1 1062
87932dab 1063 AliCodeTimerAuto("")
0421c3d1 1064
06c7e392 1065 if (!WriteRawFiles(detectors)) {
1066 if (fStopOnError) return kFALSE;
1067 }
1068
1069 TString dateFileName(fileName);
1070 if (!dateFileName.IsNull()) {
1071 Bool_t rootOutput = dateFileName.EndsWith(".root");
1072 if (rootOutput) dateFileName += ".date";
1073 if (!ConvertRawFilesToDate(dateFileName)) {
1074 if (fStopOnError) return kFALSE;
1075 }
1076 if (deleteIntermediateFiles) {
1077 AliRunLoader* runLoader = LoadRun("READ");
1078 if (runLoader) for (Int_t iEvent = 0;
1079 iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1080 char command[256];
1081 sprintf(command, "rm -r raw%d", iEvent);
1082 gSystem->Exec(command);
1083 }
1084 }
1085
1086 if (rootOutput) {
1087 if (!ConvertDateToRoot(dateFileName, fileName)) {
1088 if (fStopOnError) return kFALSE;
1089 }
1090 if (deleteIntermediateFiles) {
1091 gSystem->Unlink(dateFileName);
1092 }
1093 }
1094 }
1095
06c7e392 1096 return kTRUE;
1097}
1098
1099//_____________________________________________________________________________
1100Bool_t AliSimulation::WriteRawFiles(const char* detectors)
1101{
1102// convert the digits to raw data DDL files
1103
87932dab 1104 AliCodeTimerAuto("")
1105
06c7e392 1106 AliRunLoader* runLoader = LoadRun("READ");
0421c3d1 1107 if (!runLoader) return kFALSE;
1108
06c7e392 1109 // write raw data to DDL files
0421c3d1 1110 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
ac1671c0 1111 AliInfo(Form("processing event %d", iEvent));
0421c3d1 1112 runLoader->GetEvent(iEvent);
1113 TString baseDir = gSystem->WorkingDirectory();
1114 char dirName[256];
1115 sprintf(dirName, "raw%d", iEvent);
1116 gSystem->MakeDirectory(dirName);
1117 if (!gSystem->ChangeDirectory(dirName)) {
ac1671c0 1118 AliError(Form("couldn't change to directory %s", dirName));
0421c3d1 1119 if (fStopOnError) return kFALSE; else continue;
1120 }
1121
1122 TString detStr = detectors;
1123 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1124 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1125 AliModule* det = (AliModule*) detArray->At(iDet);
1126 if (!det || !det->IsActive()) continue;
1127 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 1128 AliInfo(Form("creating raw data from digits for %s", det->GetName()));
0421c3d1 1129 det->Digits2Raw();
1130 }
1131 }
1132
007155cc 1133 if (!WriteTriggerRawData())
1134 if (fStopOnError) return kFALSE;
1135
0421c3d1 1136 gSystem->ChangeDirectory(baseDir);
1137 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 1138 AliError(Form("the following detectors were not found: %s",
1139 detStr.Data()));
0421c3d1 1140 if (fStopOnError) return kFALSE;
1141 }
1142 }
1143
1144 delete runLoader;
87932dab 1145
06c7e392 1146 return kTRUE;
1147}
0421c3d1 1148
06c7e392 1149//_____________________________________________________________________________
1150Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName)
1151{
1152// convert raw data DDL files to a DATE file with the program "dateStream"
1153
87932dab 1154 AliCodeTimerAuto("")
1155
06c7e392 1156 char* path = gSystem->Which(gSystem->Getenv("PATH"), "dateStream");
1157 if (!path) {
ac1671c0 1158 AliError("the program dateStream was not found");
06c7e392 1159 if (fStopOnError) return kFALSE;
1160 } else {
1161 delete[] path;
1162 }
0421c3d1 1163
06c7e392 1164 AliRunLoader* runLoader = LoadRun("READ");
1165 if (!runLoader) return kFALSE;
1166
ac1671c0 1167 AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
06c7e392 1168 char command[256];
f28a8d2d 1169 // Note the option -s. It is used in order to avoid
1170 // the generation of SOR/EOR events.
1171 sprintf(command, "dateStream -s -D -o %s -# %d -C",
06c7e392 1172 dateFileName, runLoader->GetNumberOfEvents());
1173 FILE* pipe = gSystem->OpenPipe(command, "w");
1174
1175 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1176 fprintf(pipe, "GDC\n");
1177 Float_t ldc = 0;
1178 Int_t prevLDC = -1;
1179
1180 // loop over detectors and DDLs
362c9d61 1181 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1182 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
06c7e392 1183
362c9d61 1184 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
06c7e392 1185 Int_t ldcID = Int_t(ldc + 0.0001);
362c9d61 1186 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
06c7e392 1187
1188 char rawFileName[256];
362c9d61 1189 sprintf(rawFileName, "raw%d/%s",
1190 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
06c7e392 1191
1192 // check existence and size of raw data file
1193 FILE* file = fopen(rawFileName, "rb");
1194 if (!file) continue;
1195 fseek(file, 0, SEEK_END);
1196 unsigned long size = ftell(file);
aa04f81b 1197 fclose(file);
1198 if (!size) continue;
06c7e392 1199
1200 if (ldcID != prevLDC) {
1201 fprintf(pipe, " LDC Id %d\n", ldcID);
1202 prevLDC = ldcID;
1203 }
1204 fprintf(pipe, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1205 }
1206 }
1207 }
1208
1209 Int_t result = gSystem->ClosePipe(pipe);
1210
1211 delete runLoader;
1212 return (result == 0);
1213}
1214
1215//_____________________________________________________________________________
1216Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
1217 const char* rootFileName)
1218{
1219// convert a DATE file to a root file with the program "alimdc"
1220
1221 // ALIMDC setup
f28a8d2d 1222 const Int_t kDBSize = 2000000000;
c83eb8f4 1223 const Int_t kTagDBSize = 1000000000;
06c7e392 1224 const Bool_t kFilter = kFALSE;
8ec1d12b 1225 const Int_t kCompression = 1;
06c7e392 1226
1227 char* path = gSystem->Which(gSystem->Getenv("PATH"), "alimdc");
1228 if (!path) {
ac1671c0 1229 AliError("the program alimdc was not found");
06c7e392 1230 if (fStopOnError) return kFALSE;
1231 } else {
1232 delete[] path;
1233 }
1234
ac1671c0 1235 AliInfo(Form("converting DATE file %s to root file %s",
1236 dateFileName, rootFileName));
06c7e392 1237
f732bd7d 1238 const char* rawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
1239 const char* tagDBFS = "/tmp/mdc1/tags";
f732bd7d 1240
1241 // User defined file system locations
1242 if (gSystem->Getenv("ALIMDC_RAWDB1"))
1243 rawDBFS[0] = gSystem->Getenv("ALIMDC_RAWDB1");
1244 if (gSystem->Getenv("ALIMDC_RAWDB2"))
1245 rawDBFS[1] = gSystem->Getenv("ALIMDC_RAWDB2");
1246 if (gSystem->Getenv("ALIMDC_TAGDB"))
1247 tagDBFS = gSystem->Getenv("ALIMDC_TAGDB");
f732bd7d 1248
1249 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1250 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1251 gSystem->Exec(Form("rm -rf %s",tagDBFS));
f732bd7d 1252
1253 gSystem->Exec(Form("mkdir %s",rawDBFS[0]));
1254 gSystem->Exec(Form("mkdir %s",rawDBFS[1]));
1255 gSystem->Exec(Form("mkdir %s",tagDBFS));
f732bd7d 1256
1257 Int_t result = gSystem->Exec(Form("alimdc %d %d %d %d %s",
1258 kDBSize, kTagDBSize, kFilter, kCompression, dateFileName));
1259 gSystem->Exec(Form("mv %s/*.root %s", rawDBFS[0], rootFileName));
1260
1261 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1262 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1263 gSystem->Exec(Form("rm -rf %s",tagDBFS));
06c7e392 1264
1265 return (result == 0);
0421c3d1 1266}
1267
85a5290f 1268
1269//_____________________________________________________________________________
06c7e392 1270AliRunLoader* AliSimulation::LoadRun(const char* mode) const
95601147 1271{
1272// delete existing run loaders, open a new one and load gAlice
1273
1274 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
1275 AliRunLoader* runLoader =
1276 AliRunLoader::Open(fGAliceFileName.Data(),
06c7e392 1277 AliConfig::GetDefaultEventFolderName(), mode);
95601147 1278 if (!runLoader) {
ac1671c0 1279 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
95601147 1280 return NULL;
1281 }
1282 runLoader->LoadgAlice();
795e4a22 1283 runLoader->LoadHeader();
95601147 1284 gAlice = runLoader->GetAliRun();
1285 if (!gAlice) {
ac1671c0 1286 AliError(Form("no gAlice object found in file %s",
1287 fGAliceFileName.Data()));
95601147 1288 return NULL;
1289 }
1290 return runLoader;
1291}
1292
1293//_____________________________________________________________________________
05526d44 1294Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
1295{
1296// get or calculate the number of signal events per background event
1297
1298 if (!fBkgrdFileNames) return 1;
1299 Int_t nBkgrdFiles = fBkgrdFileNames->GetEntriesFast();
1300 if (nBkgrdFiles == 0) return 1;
1301
1302 // get the number of signal events
1303 if (nEvents <= 0) {
1304 AliRunLoader* runLoader =
ca30848f 1305 AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
05526d44 1306 if (!runLoader) return 1;
ca30848f 1307
05526d44 1308 nEvents = runLoader->GetNumberOfEvents();
1309 delete runLoader;
1310 }
1311
1312 Int_t result = 0;
1313 for (Int_t iBkgrdFile = 0; iBkgrdFile < nBkgrdFiles; iBkgrdFile++) {
1314 // get the number of background events
1315 const char* fileName = ((TObjString*)
1316 (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
ca30848f 1317 AliRunLoader* runLoader =
05526d44 1318 AliRunLoader::Open(fileName, "BKGRD");
1319 if (!runLoader) continue;
1320 Int_t nBkgrdEvents = runLoader->GetNumberOfEvents();
1321 delete runLoader;
ca30848f 1322
05526d44 1323 // get or calculate the number of signal per background events
1324 Int_t nSignalPerBkgrd = fBkgrdFileNames->At(iBkgrdFile)->GetUniqueID();
1325 if (nSignalPerBkgrd <= 0) {
1326 nSignalPerBkgrd = (nEvents-1) / nBkgrdEvents + 1;
1327 } else if (result && (result != nSignalPerBkgrd)) {
ac1671c0 1328 AliInfo(Form("the number of signal events per background event "
1329 "will be changed from %d to %d for stream %d",
1330 nSignalPerBkgrd, result, iBkgrdFile+1));
05526d44 1331 nSignalPerBkgrd = result;
1332 }
1333
1334 if (!result) result = nSignalPerBkgrd;
1335 if (nSignalPerBkgrd * nBkgrdEvents < nEvents) {
ac1671c0 1336 AliWarning(Form("not enough background events (%d) for %d signal events "
1337 "using %d signal per background events for stream %d",
1338 nBkgrdEvents, nEvents, nSignalPerBkgrd, iBkgrdFile+1));
05526d44 1339 }
1340 }
1341
1342 return result;
1343}
1344
1345//_____________________________________________________________________________
85a5290f 1346Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
1347{
1348// check whether detName is contained in detectors
1349// if yes, it is removed from detectors
1350
1351 // check if all detectors are selected
1352 if ((detectors.CompareTo("ALL") == 0) ||
1353 detectors.BeginsWith("ALL ") ||
1354 detectors.EndsWith(" ALL") ||
1355 detectors.Contains(" ALL ")) {
1356 detectors = "ALL";
1357 return kTRUE;
1358 }
1359
1360 // search for the given detector
1361 Bool_t result = kFALSE;
1362 if ((detectors.CompareTo(detName) == 0) ||
1363 detectors.BeginsWith(detName+" ") ||
1364 detectors.EndsWith(" "+detName) ||
1365 detectors.Contains(" "+detName+" ")) {
1366 detectors.ReplaceAll(detName, "");
1367 result = kTRUE;
1368 }
1369
1370 // clean up the detectors string
1371 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
1372 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
1373 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
1374
1375 return result;
1376}
ca30848f 1377
795e4a22 1378//_____________________________________________________________________________
ca30848f 1379Bool_t AliSimulation::ConvertRaw2SDigits(const char* rawDirectory, const char* esdFileName)
1380{
1381//
1382// Steering routine to convert raw data in directory rawDirectory/ to fake SDigits.
1383// These can be used for embedding of MC tracks into RAW data using the standard
1384// merging procedure.
1385//
1386// If an ESD file is given the reconstructed vertex is taken from it and stored in the event header.
1387//
1388 if (!gAlice) {
1389 AliError("no gAlice object. Restart aliroot and try again.");
1390 return kFALSE;
1391 }
1392 if (gAlice->Modules()->GetEntries() > 0) {
1393 AliError("gAlice was already run. Restart aliroot and try again.");
1394 return kFALSE;
1395 }
1396
1397 AliInfo(Form("initializing gAlice with config file %s",fConfigFileName.Data()));
1398 StdoutToAliInfo(StderrToAliError(gAlice->Init(fConfigFileName.Data());););
1399//
1400// Initialize CDB
795e4a22 1401 InitCDB();
1402 //AliCDBManager* man = AliCDBManager::Instance();
1403 //man->SetRun(0); // Should this come from rawdata header ?
ca30848f 1404
1405 Int_t iDet;
1406 //
1407 // Get the runloader
1408 AliRunLoader* runLoader = gAlice->GetRunLoader();
1409 //
1410 // Open esd file if available
1411 TFile* esdFile = TFile::Open(esdFileName);
1412 Bool_t esdOK = (esdFile != 0);
1413 AliESD* esd = new AliESD;
1414 TTree* treeESD = 0;
1415 if (esdOK) {
1416 treeESD = (TTree*) esdFile->Get("esdTree");
1417 if (!treeESD) {
1418 AliWarning("No ESD tree found");
1419 esdOK = kFALSE;
1420 } else {
1421 treeESD->SetBranchAddress("ESD", &esd);
1422 }
1423 }
1424 //
1425 // Create the RawReader
8b31e840 1426 TString fileName(rawDirectory);
1427 AliRawReader* rawReader = 0x0;
1428 if (fileName.EndsWith("/")) {
1429 rawReader = new AliRawReaderFile(fileName);
1430 } else if (fileName.EndsWith(".root")) {
1431 rawReader = new AliRawReaderRoot(fileName);
1432 } else if (!fileName.IsNull()) {
1433 rawReader = new AliRawReaderDate(fileName);
1434 rawReader->SelectEvents(7);
1435 }
1436// if (!fEquipIdMap.IsNull() && fRawReader)
1437// fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
ca30848f 1438 //
1439 // Get list of detectors
1440 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1441 //
1442 // Get Header
1443 AliHeader* header = runLoader->GetHeader();
1444 //
8b31e840 1445 TString detStr = fMakeSDigits;
ca30848f 1446 // Event loop
1447 Int_t nev = 0;
1448 while(kTRUE) {
1449 if (!(rawReader->NextEvent())) break;
1450 //
1451 // Detector loop
1452 for (iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1453 AliModule* det = (AliModule*) detArray->At(iDet);
8b31e840 1454 if (!det || !det->IsActive()) continue;
1455 if (IsSelected(det->GetName(), detStr)) {
1456 AliInfo(Form("Calling Raw2SDigits for %s\n", det->GetName()));
1457 det->Raw2SDigits(rawReader);
1458 rawReader->Reset();
1459 }
ca30848f 1460 } // detectors
1461
5b188f2f 1462
ca30848f 1463 //
1464 // If ESD information available obtain reconstructed vertex and store in header.
1465 if (esdOK) {
1466 treeESD->GetEvent(nev);
1467 const AliESDVertex* esdVertex = esd->GetPrimaryVertex();
1468 Double_t position[3];
1469 esdVertex->GetXYZ(position);
1470 AliGenEventHeader* mcHeader = new AliGenEventHeader("ESD");
1471 TArrayF mcV;
1472 mcV.Set(3);
1473 for (Int_t i = 0; i < 3; i++) mcV[i] = position[i];
1474 mcHeader->SetPrimaryVertex(mcV);
1475 header->Reset(0,nev);
1476 header->SetGenEventHeader(mcHeader);
1477 printf("***** Saved vertex %f %f %f \n", position[0], position[1], position[2]);
1478 }
1479 nev++;
1480//
1481// Finish the event
1482 runLoader->TreeE()->Fill();
1483 runLoader->SetNextEvent();
1484 } // events
1485
1486 delete rawReader;
1487//
1488// Finish the run
1489 runLoader->CdGAFile();
1490 runLoader->WriteHeader("OVERWRITE");
1491 runLoader->WriteRunLoader();
1492
1493 return kTRUE;
1494}
5b188f2f 1495
1496//_____________________________________________________________________________
1497Int_t AliSimulation::GetDetIndex(const char* detector)
1498{
1499 // return the detector index corresponding to detector
1500 Int_t index = -1 ;
1501 for (index = 0; index < fgkNDetectors ; index++) {
1502 if ( strcmp(detector, fgkDetectorName[index]) == 0 )
1503 break ;
1504 }
1505 return index ;
1506}
cae21299 1507
1508//_____________________________________________________________________________
1509Bool_t AliSimulation::RunHLT()
1510{
1511 // Run the HLT simulation
1512 // HLT simulation is implemented in HLT/sim/AliHLTSimulation
1513 // Disabled if fRunHLT is empty, default vaule is "default".
1514 // AliSimulation::SetRunHLT can be used to set the options for HLT simulation
1515 // The default simulation depends on the HLT component libraries and their
1516 // corresponding agents which define components and chains to run. See
1517 // http://web.ift.uib.no/~kjeks/doc/alice-hlt/
1518 // http://web.ift.uib.no/~kjeks/doc/alice-hlt/classAliHLTModuleAgent.html
1519 //
1520 // The libraries to be loaded can be specified as an option.
1521 // <pre>
1522 // AliSimulation sim;
1523 // sim.SetRunHLT("libAliHLTSample.so");
1524 // </pre>
1525 // will only load <tt>libAliHLTSample.so</tt>
1526
1527 // Other available options:
1528 // \li loglevel=<i>level</i> <br>
1529 // logging level for this processing
1530 // \li alilog=off
1531 // disable redirection of log messages to AliLog class
1532 // \li config=<i>macro</i>
1533 // configuration macro
1534 // \li localrec=<i>configuration</i>
1535 // comma separated list of configurations to be run during simulation
1536
1537 int iResult=0;
1538 AliRunLoader* pRunLoader = LoadRun("READ");
1539 if (!pRunLoader) return kFALSE;
1540
795e4a22 1541 // initialize CDB storage, run number, set CDB lock
1542 InitCDB();
1543 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1544 SetCDBLock();
1545
cae21299 1546 // load the library dynamically
1547 gSystem->Load(ALIHLTSIMULATION_LIBRARY);
1548
1549 // check for the library version
1550 AliHLTSimulationGetLibraryVersion_t fctVersion=(AliHLTSimulationGetLibraryVersion_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_GET_LIBRARY_VERSION));
1551 if (!fctVersion) {
1552 AliError(Form("can not load library %s", ALIHLTSIMULATION_LIBRARY));
1553 return kFALSE;
1554 }
1555 if (fctVersion()!= ALIHLTSIMULATION_LIBRARY_VERSION) {
1556 AliError(Form("%s version does not match: compiled for version %d, loaded %d", ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_LIBRARY_VERSION, fctVersion()));
1557 return kFALSE;
1558 }
1559
1560 // print compile info
1561 typedef void (*CompileInfo)( char*& date, char*& time);
1562 CompileInfo fctInfo=(CompileInfo)gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, "CompileInfo");
1563 if (fctInfo) {
1564 char* date="";
1565 char* time="";
1566 (*fctInfo)(date, time);
1567 if (!date) date="unknown";
1568 if (!time) time="unknown";
1569 AliInfo(Form("%s build on %s (%s)", ALIHLTSIMULATION_LIBRARY, date, time));
1570 } else {
1571 AliInfo(Form("no build info available for %s", ALIHLTSIMULATION_LIBRARY));
1572 }
1573
1574 // create instance of the HLT simulation
1575 AliHLTSimulationCreateInstance_t fctCreate=(AliHLTSimulationCreateInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_CREATE_INSTANCE));
1576 AliHLTSimulation* pHLT=NULL;
1577 if (fctCreate==NULL || (pHLT=(fctCreate()))==NULL) {
1578 AliError(Form("can not create instance of HLT simulation (creator %p)", fctCreate));
1579 return kFALSE;
1580 }
1581
1582 // init the HLT simulation
1583 if (fRunHLT.CompareTo("default")==0) fRunHLT="";
1584 AliHLTSimulationInit_t fctInit=(AliHLTSimulationInit_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_INIT));
1585 if (fctInit==NULL || (iResult=(fctInit(pHLT, pRunLoader, fRunHLT.Data())))<0) {
1586 AliError(Form("can not init HLT simulation: error %d (init %p)", iResult, fctInit));
1587 } else {
1588 // run the HLT simulation
1589 AliHLTSimulationRun_t fctRun=(AliHLTSimulationRun_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_RUN));
1590 if (fctRun==NULL || (iResult=(fctRun(pHLT, pRunLoader)))<0) {
1591 AliError(Form("can not run HLT simulation: error %d (run %p)", iResult, fctRun));
1592 }
1593 }
1594
1595 // delete the instance
1596 AliHLTSimulationDeleteInstance_t fctDelete=(AliHLTSimulationDeleteInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_DELETE_INSTANCE));
1597 if (fctDelete==NULL || fctDelete(pHLT)<0) {
1598 AliError(Form("can not delete instance of HLT simulation (creator %p)", fctDelete));
1599 }
1600 pHLT=NULL;
1601
1602 return iResult>=0?kTRUE:kFALSE;
1603}
cae17938 1604
1605//_____________________________________________________________________________
a1b90a73 1606Bool_t AliSimulation::RunQA()
cae17938 1607{
a1b90a73 1608 // run the QA on summable hits, digits or digits
1609
1610 AliQADataMakerSteer qas ;
1611 qas.SetRunLoader(gAlice->GetRunLoader()) ;
1612
1613 Bool_t rv = qas.Run("ALL", AliQA::kHITS) ;
1614// qas.Reset() ;
1615 rv *= qas.Run(fMakeSDigits.Data(), AliQA::kSDIGITS) ;
1616// qas.Reset() ;
1617 rv *= qas.Run(fMakeDigits.Data(), AliQA::kDIGITS) ;
1618// qas.Reset() ;
1619 rv *= qas.Run(fMakeDigitsFromHits.Data(), AliQA::kDIGITS) ;
cae17938 1620
a1b90a73 1621 return rv ;
cae17938 1622}
1623
795e4a22 1624//_____________________________________________________________________________
1625void AliSimulation::ProcessEnvironmentVars()
1626{
1627// Extract run number and random generator seed from env variables
1628
1629 AliInfo("Processing environment variables");
1630
1631 // Random Number seed
1632
1633 // first check that seed is not already set
1634 if (fSeed == 0) {
1635 if (gSystem->Getenv("CONFIG_SEED")) {
1636 fSeed = atoi(gSystem->Getenv("CONFIG_SEED"));
1637 }
1638 } else {
1639 if (gSystem->Getenv("CONFIG_SEED")) {
1640 AliInfo(Form("Seed for random number generation already set (%d)"
1641 ": CONFIG_SEED variable ignored!", fSeed));
1642 }
1643 }
1644
1645 AliInfo(Form("Seed for random number generation = %d ", fSeed));
1646
1647 // Run Number
1648
1649 // first check that run number is not already set
1650 if(fRun < 0) {
1651 if (gSystem->Getenv("DC_RUN")) {
1652 fRun = atoi(gSystem->Getenv("DC_RUN"));
1653 }
1654 } else {
1655 if (gSystem->Getenv("DC_RUN")) {
1656 AliInfo(Form("Run number already set (%d): DC_RUN variable ignored!", fRun));
1657 }
1658 }
1659
1660 AliInfo(Form("Run number = %d", fRun));
1661}