added forgotten files for new libraries
[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"
85a5290f 140
141ClassImp(AliSimulation)
142
b856a511 143AliSimulation *AliSimulation::fgInstance = 0;
5b188f2f 144const char* AliSimulation::fgkDetectorName[AliSimulation::fgkNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE", "HLT"};
85a5290f 145
146//_____________________________________________________________________________
024cf675 147AliSimulation::AliSimulation(const char* configFileName, const char* cdbUri,
95601147 148 const char* name, const char* title) :
149 TNamed(name, title),
150
151 fRunGeneration(kTRUE),
152 fRunSimulation(kTRUE),
024cf675 153 fLoadAlignFromCDB(kTRUE),
25be1e5c 154 fLoadAlObjsListOfDets("ALL"),
95601147 155 fMakeSDigits("ALL"),
156 fMakeDigits("ALL"),
a5a091ce 157 fMakeTrigger(""),
95601147 158 fMakeDigitsFromHits(""),
0421c3d1 159 fWriteRawData(""),
06c7e392 160 fRawDataFileName(""),
161 fDeleteIntermediateFiles(kFALSE),
95601147 162 fStopOnError(kFALSE),
163
164 fNEvents(1),
165 fConfigFileName(configFileName),
166 fGAliceFileName("galice.root"),
18b43626 167 fEventsPerFile(),
95601147 168 fBkgrdFileNames(NULL),
995ad051 169 fAlignObjArray(NULL),
04bae0a0 170 fUseBkgrdVertex(kTRUE),
024cf675 171 fRegionOfInterest(kFALSE),
ec92bee0 172 fCDBUri(cdbUri),
00aa02d5 173 fRemoteCDBUri(""),
ca30848f 174 fSpecCDBUri(),
175 fEmbeddingFlag(kFALSE)
85a5290f 176{
177// create simulation object with default parameters
b856a511 178 fgInstance = this;
0421c3d1 179 SetGAliceFile("galice.root");
5b188f2f 180
181// for QA
182 for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++)
183 fQACycles[iDet] = 999999;
85a5290f 184}
185
186//_____________________________________________________________________________
187AliSimulation::AliSimulation(const AliSimulation& sim) :
95601147 188 TNamed(sim),
189
190 fRunGeneration(sim.fRunGeneration),
191 fRunSimulation(sim.fRunSimulation),
024cf675 192 fLoadAlignFromCDB(sim.fLoadAlignFromCDB),
25be1e5c 193 fLoadAlObjsListOfDets(sim.fLoadAlObjsListOfDets),
95601147 194 fMakeSDigits(sim.fMakeSDigits),
195 fMakeDigits(sim.fMakeDigits),
a5a091ce 196 fMakeTrigger(sim.fMakeTrigger),
95601147 197 fMakeDigitsFromHits(sim.fMakeDigitsFromHits),
0421c3d1 198 fWriteRawData(sim.fWriteRawData),
06c7e392 199 fRawDataFileName(""),
200 fDeleteIntermediateFiles(kFALSE),
95601147 201 fStopOnError(sim.fStopOnError),
202
203 fNEvents(sim.fNEvents),
204 fConfigFileName(sim.fConfigFileName),
205 fGAliceFileName(sim.fGAliceFileName),
18b43626 206 fEventsPerFile(),
95601147 207 fBkgrdFileNames(NULL),
995ad051 208 fAlignObjArray(NULL),
04bae0a0 209 fUseBkgrdVertex(sim.fUseBkgrdVertex),
024cf675 210 fRegionOfInterest(sim.fRegionOfInterest),
ec92bee0 211 fCDBUri(sim.fCDBUri),
00aa02d5 212 fRemoteCDBUri(sim.fRemoteCDBUri),
ca30848f 213 fSpecCDBUri(),
214 fEmbeddingFlag(sim.fEmbeddingFlag)
85a5290f 215{
216// copy constructor
217
18b43626 218 for (Int_t i = 0; i < sim.fEventsPerFile.GetEntriesFast(); i++) {
219 if (!sim.fEventsPerFile[i]) continue;
220 fEventsPerFile.Add(sim.fEventsPerFile[i]->Clone());
221 }
222
85a5290f 223 fBkgrdFileNames = new TObjArray;
224 for (Int_t i = 0; i < sim.fBkgrdFileNames->GetEntriesFast(); i++) {
225 if (!sim.fBkgrdFileNames->At(i)) continue;
226 fBkgrdFileNames->Add(sim.fBkgrdFileNames->At(i)->Clone());
227 }
ec92bee0 228
229 for (Int_t i = 0; i < sim.fSpecCDBUri.GetEntriesFast(); i++) {
230 if (sim.fSpecCDBUri[i]) fSpecCDBUri.Add(sim.fSpecCDBUri[i]->Clone());
231 }
b856a511 232 fgInstance = this;
5b188f2f 233
234// for QA
235 for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++)
236 fQACycles[iDet] = sim.fQACycles[iDet];
85a5290f 237}
238
239//_____________________________________________________________________________
240AliSimulation& AliSimulation::operator = (const AliSimulation& sim)
241{
242// assignment operator
243
244 this->~AliSimulation();
245 new(this) AliSimulation(sim);
246 return *this;
247}
248
249//_____________________________________________________________________________
250AliSimulation::~AliSimulation()
251{
252// clean up
253
18b43626 254 fEventsPerFile.Delete();
024cf675 255// if(fAlignObjArray) fAlignObjArray->Delete(); // fAlignObjArray->RemoveAll() ???
256// delete fAlignObjArray; fAlignObjArray=0;
18b43626 257
95601147 258 if (fBkgrdFileNames) {
259 fBkgrdFileNames->Delete();
260 delete fBkgrdFileNames;
261 }
ec92bee0 262
263 fSpecCDBUri.Delete();
b856a511 264 if (fgInstance==this) fgInstance = 0;
87932dab 265
266 AliCodeTimer::Instance()->Print();
85a5290f 267}
268
269
270//_____________________________________________________________________________
271void AliSimulation::SetNumberOfEvents(Int_t nEvents)
272{
273// set the number of events for one run
274
275 fNEvents = nEvents;
276}
277
278//_____________________________________________________________________________
ec92bee0 279void AliSimulation::InitCDBStorage()
024cf675 280{
281// activate a default CDB storage
282// First check if we have any CDB storage set, because it is used
283// to retrieve the calibration and alignment constants
284
285 AliCDBManager* man = AliCDBManager::Instance();
ec92bee0 286 if (man->IsDefaultStorageSet())
024cf675 287 {
ec92bee0 288 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
289 AliWarning("Default CDB storage has been already set !");
ced249e6 290 AliWarning(Form("Ignoring the default storage declared in AliSimulation: %s",fCDBUri.Data()));
ec92bee0 291 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
292 fCDBUri = "";
293 }
294 else {
b8ec52f6 295 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
296 AliDebug(2, Form("Default CDB storage is set to: %s",fCDBUri.Data()));
297 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
ec92bee0 298 man->SetDefaultStorage(fCDBUri);
299 }
300
00aa02d5 301 // Remote storage (the Grid storage) is used if it is activated
302 // and if the object is not found in the default storage
c75fabed 303 // OBSOLETE: Removed
304 // if (man->IsRemoteStorageSet())
305 // {
306 // AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
307 // AliWarning("Remote CDB storage has been already set !");
308 // AliWarning(Form("Ignoring the remote storage declared in AliSimulation: %s",fRemoteCDBUri.Data()));
309 // AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
310 // fRemoteCDBUri = "";
311 // }
312 // else {
313 // AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
314 // AliDebug(2, Form("Remote CDB storage is set to: %s",fRemoteCDBUri.Data()));
315 // AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
316 // man->SetRemoteStorage(fRemoteCDBUri);
317 // }
00aa02d5 318
ec92bee0 319 // Now activate the detector specific CDB storage locations
320 for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
321 TObject* obj = fSpecCDBUri[i];
322 if (!obj) continue;
b8ec52f6 323 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
324 AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
325 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
c3a7b59a 326 man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
ec92bee0 327 }
727d0766 328 man->Print();
024cf675 329}
330
331//_____________________________________________________________________________
332void AliSimulation::SetDefaultStorage(const char* uri) {
ec92bee0 333// Store the desired default CDB storage location
334// Activate it later within the Run() method
024cf675 335
ec92bee0 336 fCDBUri = uri;
024cf675 337
338}
339
340//_____________________________________________________________________________
00aa02d5 341void AliSimulation::SetRemoteStorage(const char* uri) {
342// Store the desired remote CDB storage location
343// Activate it later within the Run() method
344// Remote storage (the Grid storage) is used if it is activated
345// and if the object is not found in the default storage (the local cache)
346
347 fRemoteCDBUri = uri;
348
349}
350
351//_____________________________________________________________________________
c3a7b59a 352void AliSimulation::SetSpecificStorage(const char* calibType, const char* uri) {
ec92bee0 353// Store a detector-specific CDB storage location
354// Activate it later within the Run() method
024cf675 355
c3a7b59a 356 AliCDBPath aPath(calibType);
357 if(!aPath.IsValid()){
358 AliError(Form("Not a valid path: %s", calibType));
359 return;
360 }
361
362 TObject* obj = fSpecCDBUri.FindObject(calibType);
ec92bee0 363 if (obj) fSpecCDBUri.Remove(obj);
c3a7b59a 364 fSpecCDBUri.Add(new TNamed(calibType, uri));
024cf675 365
366}
367
368//_____________________________________________________________________________
85a5290f 369void AliSimulation::SetConfigFile(const char* fileName)
370{
371// set the name of the config file
372
373 fConfigFileName = fileName;
374}
375
376//_____________________________________________________________________________
0421c3d1 377void AliSimulation::SetGAliceFile(const char* fileName)
378{
379// set the name of the galice file
380// the path is converted to an absolute one if it is relative
381
382 fGAliceFileName = fileName;
383 if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
384 char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
385 fGAliceFileName);
386 fGAliceFileName = absFileName;
387 delete[] absFileName;
388 }
ac1671c0 389
390 AliDebug(2, Form("galice file name set to %s", fileName));
0421c3d1 391}
392
393//_____________________________________________________________________________
18b43626 394void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
395 Int_t nEvents)
396{
397// set the number of events per file for the given detector and data type
398// ("Hits", "Summable Digits", "Digits", "Reconstructed Points" or "Tracks")
399
400 TNamed* obj = new TNamed(detector, type);
401 obj->SetUniqueID(nEvents);
402 fEventsPerFile.Add(obj);
403}
404
405//_____________________________________________________________________________
6bae477a 406Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
407{
408 // Read the alignment objects from CDB.
409 // Each detector is supposed to have the
410 // alignment objects in DET/Align/Data CDB path.
411 // All the detector objects are then collected,
412 // sorted by geometry level (starting from ALIC) and
413 // then applied to the TGeo geometry.
414 // Finally an overlaps check is performed.
415
74ae4297 416 if (!AliGeomManager::GetGeometry() || !AliGeomManager::GetGeometry()->IsClosed()) {
417 AliError("Can't apply the misalignment! Geometry is not loaded or it is still opened!");
0717eed2 418 return kFALSE;
419 }
6bae477a 420 Bool_t delRunLoader = kFALSE;
421 if (!runLoader) {
422 runLoader = LoadRun("READ");
423 if (!runLoader) return kFALSE;
424 delRunLoader = kTRUE;
425 }
426
b856a511 427 // Export ideal geometry
ced249e6 428 if(!gAlice->IsRootGeometry()) AliGeomManager::GetGeometry()->Export("geometry.root");
b856a511 429
25be1e5c 430 // Load alignment data from CDB and apply to geometry through AliGeomManager
6bae477a 431 if(fLoadAlignFromCDB){
25be1e5c 432
433 TString detStr = fLoadAlObjsListOfDets;
434 TString loadAlObjsListOfDets = "";
435
436 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
437 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
438 AliModule* det = (AliModule*) detArray->At(iDet);
439 if (!det || !det->IsActive()) continue;
440 if (IsSelected(det->GetName(), detStr)) {
0717eed2 441 //add det to list of dets to be aligned from CDB
442 loadAlObjsListOfDets += det->GetName();
443 loadAlObjsListOfDets += " ";
25be1e5c 444 }
445 } // end loop over detectors
53dd3c3d 446 loadAlObjsListOfDets.Prepend("GRP "); //add alignment objects for non-sensitive modules
45daac75 447 AliGeomManager::ApplyAlignObjsFromCDB(loadAlObjsListOfDets.Data());
25be1e5c 448 }else{
449 // Check if the array with alignment objects was
450 // provided by the user. If yes, apply the objects
451 // to the present TGeo geometry
452 if (fAlignObjArray) {
45daac75 453 if (AliGeomManager::ApplyAlignObjsToGeom(*fAlignObjArray) == kFALSE) {
0717eed2 454 AliError("The misalignment of one or more volumes failed!"
455 "Compare the list of simulated detectors and the list of detector alignment data!");
456 if (delRunLoader) delete runLoader;
457 return kFALSE;
6bae477a 458 }
459 }
6bae477a 460 }
461
ef7ff1f9 462 // Update the internal geometry of modules (ITS needs it)
25be1e5c 463 TString detStr = fLoadAlObjsListOfDets;
ef7ff1f9 464 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
465 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
466
467 AliModule* det = (AliModule*) detArray->At(iDet);
468 if (!det || !det->IsActive()) continue;
469 if (IsSelected(det->GetName(), detStr)) {
470 det->UpdateInternalGeometry();
471 }
472 } // end loop over detectors
473
474
6bae477a 475 if (delRunLoader) delete runLoader;
476
477 return kTRUE;
478}
479
480
481//_____________________________________________________________________________
482Bool_t AliSimulation::SetRunNumber()
483{
484 // Set the CDB manager run number
485 // The run number is retrieved from gAlice
486
487 if(AliCDBManager::Instance()->GetRun() < 0) {
488 AliRunLoader* runLoader = LoadRun("READ");
489 if (!runLoader) return kFALSE;
490 else {
491 AliCDBManager::Instance()->SetRun(runLoader->GetAliRun()->GetRunNumber());
492 AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
493 delete runLoader;
494 }
495 }
496 return kTRUE;
497}
498
499//_____________________________________________________________________________
85a5290f 500void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
501{
502// add a file with background events for merging
503
504 TObjString* fileNameStr = new TObjString(fileName);
505 fileNameStr->SetUniqueID(nSignalPerBkgrd);
95601147 506 if (!fBkgrdFileNames) fBkgrdFileNames = new TObjArray;
85a5290f 507 fBkgrdFileNames->Add(fileNameStr);
508}
509
ca30848f 510void AliSimulation::EmbedInto(const char* fileName, Int_t nSignalPerBkgrd)
511{
512// add a file with background events for embeddin
513 MergeWith(fileName, nSignalPerBkgrd);
514 fEmbeddingFlag = kTRUE;
515}
85a5290f 516
517//_____________________________________________________________________________
518Bool_t AliSimulation::Run(Int_t nEvents)
519{
520// run the generation, simulation and digitization
521
87932dab 522 AliCodeTimerAuto("")
523
ec92bee0 524 InitCDBStorage();
b0314964 525
85a5290f 526 if (nEvents > 0) fNEvents = nEvents;
527
528 // generation and simulation -> hits
529 if (fRunGeneration) {
85a5290f 530 if (!RunSimulation()) if (fStopOnError) return kFALSE;
531 }
532
6bae477a 533 // Set run number in CDBManager (if it is not already set in RunSimulation)
534 if (!SetRunNumber()) if (fStopOnError) return kFALSE;
535
25be1e5c 536 // If RunSimulation was not called, load the geometry and misalign it
74ae4297 537 if (!AliGeomManager::GetGeometry()) {
538 // Initialize the geometry manager
45daac75 539 AliGeomManager::LoadGeometry("geometry.root");
74ae4297 540 if (!AliGeomManager::GetGeometry()) if (fStopOnError) return kFALSE;
541 // Misalign geometry
25be1e5c 542 if(!MisalignGeometry()) if (fStopOnError) return kFALSE;
6bae477a 543 }
25be1e5c 544
85a5290f 545 // hits -> summable digits
546 if (!fMakeSDigits.IsNull()) {
547 if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
548 }
549
550 // summable digits -> digits
551 if (!fMakeDigits.IsNull()) {
552 if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
553 if (fStopOnError) return kFALSE;
554 }
555 }
556
557 // hits -> digits
558 if (!fMakeDigitsFromHits.IsNull()) {
95601147 559 if (fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
ac1671c0 560 AliWarning(Form("Merging and direct creation of digits from hits "
561 "was selected for some detectors. "
562 "No merging will be done for the following detectors: %s",
563 fMakeDigitsFromHits.Data()));
85a5290f 564 }
565 if (!RunHitsDigitization(fMakeDigitsFromHits)) {
566 if (fStopOnError) return kFALSE;
567 }
568 }
569
a5a091ce 570 // digits -> trigger
087426de 571 if (!RunTrigger(fMakeTrigger)) {
572 if (fStopOnError) return kFALSE;
a5a091ce 573 }
574
0421c3d1 575 // digits -> raw data
576 if (!fWriteRawData.IsNull()) {
06c7e392 577 if (!WriteRawData(fWriteRawData, fRawDataFileName,
578 fDeleteIntermediateFiles)) {
0421c3d1 579 if (fStopOnError) return kFALSE;
580 }
581 }
582
85a5290f 583 return kTRUE;
584}
585
586//_____________________________________________________________________________
a5a091ce 587Bool_t AliSimulation::RunTrigger(const char* descriptors)
588{
589 // run the trigger
590
87932dab 591 AliCodeTimerAuto("")
a5a091ce 592
bacbe0fd 593 AliRunLoader* runLoader = LoadRun("READ");
594 if (!runLoader) return kFALSE;
595 TString des = descriptors;
596
087426de 597 if (des.IsNull()) {
598 if (gAlice->GetTriggerDescriptor() != "") {
599 des = gAlice->GetTriggerDescriptor();
600 }
601 else {
602 AliWarning("No trigger descriptor is specified. Skipping the trigger simulation...");
603 return kTRUE;
604 }
605 }
606
0f46f5fa 607 runLoader->MakeTree( "GG" );
bacbe0fd 608 AliCentralTrigger* aCTP = runLoader->GetTrigger();
a5a091ce 609 // Load Descriptors
bacbe0fd 610 aCTP->LoadDescriptor( des );
a5a091ce 611
612 // digits -> trigger
bacbe0fd 613 if( !aCTP->RunTrigger( runLoader ) ) {
a5a091ce 614 if (fStopOnError) {
bacbe0fd 615 // delete aCTP;
a5a091ce 616 return kFALSE;
617 }
bacbe0fd 618 }
a5a091ce 619
bacbe0fd 620 delete runLoader;
a5a091ce 621
bacbe0fd 622 return kTRUE;
a5a091ce 623}
624
007155cc 625//_____________________________________________________________________________
626Bool_t AliSimulation::WriteTriggerRawData()
627{
628 // Writes the CTP (trigger) DDL raw data
629 // Details of the format are given in the
630 // trigger TDR - pages 134 and 135.
631 AliCTPRawData writer;
632 writer.RawData();
a5a091ce 633
007155cc 634 return kTRUE;
635}
a5a091ce 636
637//_____________________________________________________________________________
95601147 638Bool_t AliSimulation::RunSimulation(Int_t nEvents)
85a5290f 639{
640// run the generation and simulation
641
87932dab 642 AliCodeTimerAuto("")
4df28b43 643
95601147 644 if (!gAlice) {
ac1671c0 645 AliError("no gAlice object. Restart aliroot and try again.");
95601147 646 return kFALSE;
647 }
648 if (gAlice->Modules()->GetEntries() > 0) {
ac1671c0 649 AliError("gAlice was already run. Restart aliroot and try again.");
95601147 650 return kFALSE;
651 }
652
ac1671c0 653 AliInfo(Form("initializing gAlice with config file %s",
654 fConfigFileName.Data()));
815c2b38 655 StdoutToAliInfo(StderrToAliError(
656 gAlice->Init(fConfigFileName.Data());
657 ););
995ad051 658
087426de 659 // Get the trigger descriptor string
660 // Either from AliSimulation or from
661 // gAlice
662 if (fMakeTrigger.IsNull()) {
663 if (gAlice->GetTriggerDescriptor() != "")
664 fMakeTrigger = gAlice->GetTriggerDescriptor();
665 }
666 else
667 gAlice->SetTriggerDescriptor(fMakeTrigger.Data());
668
6bae477a 669 // Set run number in CDBManager
024cf675 670 AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
671
672 AliRunLoader* runLoader = gAlice->GetRunLoader();
673 if (!runLoader) {
674 AliError(Form("gAlice has no run loader object. "
675 "Check your config file: %s", fConfigFileName.Data()));
676 return kFALSE;
677 }
678 SetGAliceFile(runLoader->GetFileName());
679
6bae477a 680 // Misalign geometry
caf9c464 681#if ROOT_VERSION_CODE < 331527
74ae4297 682 AliGeomManager::SetGeometry(gGeoManager);
caf9c464 683 MisalignGeometry(runLoader);
684#endif
995ad051 685
024cf675 686// AliRunLoader* runLoader = gAlice->GetRunLoader();
687// if (!runLoader) {
688// AliError(Form("gAlice has no run loader object. "
689// "Check your config file: %s", fConfigFileName.Data()));
690// return kFALSE;
691// }
692// SetGAliceFile(runLoader->GetFileName());
85a5290f 693
04bae0a0 694 if (!gAlice->Generator()) {
ac1671c0 695 AliError(Form("gAlice has no generator object. "
696 "Check your config file: %s", fConfigFileName.Data()));
04bae0a0 697 return kFALSE;
698 }
05526d44 699 if (nEvents <= 0) nEvents = fNEvents;
04bae0a0 700
701 // get vertex from background file in case of merging
702 if (fUseBkgrdVertex &&
703 fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
05526d44 704 Int_t signalPerBkgrd = GetNSignalPerBkgrd(nEvents);
04bae0a0 705 const char* fileName = ((TObjString*)
706 (fBkgrdFileNames->At(0)))->GetName();
ac1671c0 707 AliInfo(Form("The vertex will be taken from the background "
708 "file %s with nSignalPerBackground = %d",
709 fileName, signalPerBkgrd));
04bae0a0 710 AliVertexGenFile* vtxGen = new AliVertexGenFile(fileName, signalPerBkgrd);
711 gAlice->Generator()->SetVertexGenerator(vtxGen);
712 }
713
85a5290f 714 if (!fRunSimulation) {
85a5290f 715 gAlice->Generator()->SetTrackingFlag(0);
716 }
717
18b43626 718 // set the number of events per file for given detectors and data types
719 for (Int_t i = 0; i < fEventsPerFile.GetEntriesFast(); i++) {
720 if (!fEventsPerFile[i]) continue;
721 const char* detName = fEventsPerFile[i]->GetName();
722 const char* typeName = fEventsPerFile[i]->GetTitle();
723 TString loaderName(detName);
724 loaderName += "Loader";
725 AliLoader* loader = runLoader->GetLoader(loaderName);
726 if (!loader) {
ac1671c0 727 AliError(Form("RunSimulation", "no loader for %s found\n"
728 "Number of events per file not set for %s %s",
729 detName, typeName, detName));
18b43626 730 continue;
731 }
732 AliDataLoader* dataLoader =
733 loader->GetDataLoader(typeName);
734 if (!dataLoader) {
ac1671c0 735 AliError(Form("no data loader for %s found\n"
736 "Number of events per file not set for %s %s",
737 typeName, detName, typeName));
18b43626 738 continue;
739 }
740 dataLoader->SetNumberOfEventsPerFile(fEventsPerFile[i]->GetUniqueID());
ac1671c0 741 AliDebug(1, Form("number of events per file set to %d for %s %s",
742 fEventsPerFile[i]->GetUniqueID(), detName, typeName));
18b43626 743 }
744
ac1671c0 745 AliInfo("running gAlice");
815c2b38 746 StdoutToAliInfo(StderrToAliError(
747 gAlice->Run(nEvents);
748 ););
95601147 749
750 delete runLoader;
85a5290f 751
4df28b43 752
85a5290f 753 return kTRUE;
754}
755
756//_____________________________________________________________________________
95601147 757Bool_t AliSimulation::RunSDigitization(const char* detectors)
85a5290f 758{
759// run the digitization and produce summable digits
760
87932dab 761 AliCodeTimerAuto("")
4df28b43 762
95601147 763 AliRunLoader* runLoader = LoadRun();
764 if (!runLoader) return kFALSE;
765
85a5290f 766 TString detStr = detectors;
95601147 767 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 768 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
769 AliModule* det = (AliModule*) detArray->At(iDet);
770 if (!det || !det->IsActive()) continue;
771 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 772 AliInfo(Form("creating summable digits for %s", det->GetName()));
87932dab 773 AliCodeTimerAuto(Form("creating summable digits for %s", det->GetName()));
5b188f2f 774
85a5290f 775 det->Hits2SDigits();
776 }
777 }
778
779 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 780 AliError(Form("the following detectors were not found: %s",
781 detStr.Data()));
85a5290f 782 if (fStopOnError) return kFALSE;
783 }
784
95601147 785 delete runLoader;
786
85a5290f 787 return kTRUE;
788}
789
790
791//_____________________________________________________________________________
95601147 792Bool_t AliSimulation::RunDigitization(const char* detectors,
793 const char* excludeDetectors)
85a5290f 794{
795// run the digitization and produce digits from sdigits
796
87932dab 797 AliCodeTimerAuto("")
4df28b43 798
95601147 799 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
800 if (gAlice) delete gAlice;
801 gAlice = NULL;
802
803 Int_t nStreams = 1;
05526d44 804 if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
805 Int_t signalPerBkgrd = GetNSignalPerBkgrd();
85a5290f 806 AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
ca30848f 807 // manager->SetEmbeddingFlag(fEmbeddingFlag);
85a5290f 808 manager->SetInputStream(0, fGAliceFileName.Data());
809 for (Int_t iStream = 1; iStream < nStreams; iStream++) {
810 const char* fileName = ((TObjString*)
811 (fBkgrdFileNames->At(iStream-1)))->GetName();
812 manager->SetInputStream(iStream, fileName);
813 }
814
815 TString detStr = detectors;
816 TString detExcl = excludeDetectors;
95601147 817 manager->GetInputStream(0)->ImportgAlice();
818 AliRunLoader* runLoader =
819 AliRunLoader::GetRunLoader(manager->GetInputStream(0)->GetFolderName());
820 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 821 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
822 AliModule* det = (AliModule*) detArray->At(iDet);
823 if (!det || !det->IsActive()) continue;
824 if (IsSelected(det->GetName(), detStr) &&
825 !IsSelected(det->GetName(), detExcl)) {
4df28b43 826 AliDigitizer* digitizer = det->CreateDigitizer(manager);
ca30848f 827
4df28b43 828 if (!digitizer) {
ac1671c0 829 AliError(Form("no digitizer for %s", det->GetName()));
85a5290f 830 if (fStopOnError) return kFALSE;
4df28b43 831 } else {
832 digitizer->SetRegionOfInterest(fRegionOfInterest);
85a5290f 833 }
834 }
835 }
836
837 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 838 AliError(Form("the following detectors were not found: %s",
839 detStr.Data()));
85a5290f 840 if (fStopOnError) return kFALSE;
841 }
842
843 if (!manager->GetListOfTasks()->IsEmpty()) {
ac1671c0 844 AliInfo("executing digitization");
85a5290f 845 manager->Exec("");
846 }
95601147 847
85a5290f 848 delete manager;
849
850 return kTRUE;
851}
852
853//_____________________________________________________________________________
95601147 854Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
85a5290f 855{
856// run the digitization and produce digits from hits
857
87932dab 858 AliCodeTimerAuto("")
4df28b43 859
591f51bb 860 AliRunLoader* runLoader = LoadRun("READ");
95601147 861 if (!runLoader) return kFALSE;
862
85a5290f 863 TString detStr = detectors;
95601147 864 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 865 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
866 AliModule* det = (AliModule*) detArray->At(iDet);
867 if (!det || !det->IsActive()) continue;
868 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 869 AliInfo(Form("creating digits from hits for %s", det->GetName()));
85a5290f 870 det->Hits2Digits();
871 }
872 }
873
874 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 875 AliError(Form("the following detectors were not found: %s",
876 detStr.Data()));
85a5290f 877 if (fStopOnError) return kFALSE;
878 }
879
b17cac0f 880 delete runLoader;
591f51bb 881 //PH Temporary fix to avoid interference with the PHOS loder/getter
882 //PH The problem has to be solved in more general way 09/06/05
95601147 883
85a5290f 884 return kTRUE;
885}
886
0421c3d1 887//_____________________________________________________________________________
06c7e392 888Bool_t AliSimulation::WriteRawData(const char* detectors,
889 const char* fileName,
890 Bool_t deleteIntermediateFiles)
0421c3d1 891{
892// convert the digits to raw data
06c7e392 893// First DDL raw data files for the given detectors are created.
894// If a file name is given, the DDL files are then converted to a DATE file.
895// If deleteIntermediateFiles is true, the DDL raw files are deleted
896// afterwards.
897// If the file name has the extension ".root", the DATE file is converted
898// to a root file.
899// If deleteIntermediateFiles is true, the DATE file is deleted afterwards.
0421c3d1 900
87932dab 901 AliCodeTimerAuto("")
0421c3d1 902
06c7e392 903 if (!WriteRawFiles(detectors)) {
904 if (fStopOnError) return kFALSE;
905 }
906
907 TString dateFileName(fileName);
908 if (!dateFileName.IsNull()) {
909 Bool_t rootOutput = dateFileName.EndsWith(".root");
910 if (rootOutput) dateFileName += ".date";
911 if (!ConvertRawFilesToDate(dateFileName)) {
912 if (fStopOnError) return kFALSE;
913 }
914 if (deleteIntermediateFiles) {
915 AliRunLoader* runLoader = LoadRun("READ");
916 if (runLoader) for (Int_t iEvent = 0;
917 iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
918 char command[256];
919 sprintf(command, "rm -r raw%d", iEvent);
920 gSystem->Exec(command);
921 }
922 }
923
924 if (rootOutput) {
925 if (!ConvertDateToRoot(dateFileName, fileName)) {
926 if (fStopOnError) return kFALSE;
927 }
928 if (deleteIntermediateFiles) {
929 gSystem->Unlink(dateFileName);
930 }
931 }
932 }
933
06c7e392 934 return kTRUE;
935}
936
937//_____________________________________________________________________________
938Bool_t AliSimulation::WriteRawFiles(const char* detectors)
939{
940// convert the digits to raw data DDL files
941
87932dab 942 AliCodeTimerAuto("")
943
06c7e392 944 AliRunLoader* runLoader = LoadRun("READ");
0421c3d1 945 if (!runLoader) return kFALSE;
946
06c7e392 947 // write raw data to DDL files
0421c3d1 948 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
ac1671c0 949 AliInfo(Form("processing event %d", iEvent));
0421c3d1 950 runLoader->GetEvent(iEvent);
951 TString baseDir = gSystem->WorkingDirectory();
952 char dirName[256];
953 sprintf(dirName, "raw%d", iEvent);
954 gSystem->MakeDirectory(dirName);
955 if (!gSystem->ChangeDirectory(dirName)) {
ac1671c0 956 AliError(Form("couldn't change to directory %s", dirName));
0421c3d1 957 if (fStopOnError) return kFALSE; else continue;
958 }
959
960 TString detStr = detectors;
961 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
962 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
963 AliModule* det = (AliModule*) detArray->At(iDet);
964 if (!det || !det->IsActive()) continue;
965 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 966 AliInfo(Form("creating raw data from digits for %s", det->GetName()));
0421c3d1 967 det->Digits2Raw();
968 }
969 }
970
007155cc 971 if (!WriteTriggerRawData())
972 if (fStopOnError) return kFALSE;
973
0421c3d1 974 gSystem->ChangeDirectory(baseDir);
975 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 976 AliError(Form("the following detectors were not found: %s",
977 detStr.Data()));
0421c3d1 978 if (fStopOnError) return kFALSE;
979 }
980 }
981
982 delete runLoader;
87932dab 983
06c7e392 984 return kTRUE;
985}
0421c3d1 986
06c7e392 987//_____________________________________________________________________________
988Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName)
989{
990// convert raw data DDL files to a DATE file with the program "dateStream"
991
87932dab 992 AliCodeTimerAuto("")
993
06c7e392 994 char* path = gSystem->Which(gSystem->Getenv("PATH"), "dateStream");
995 if (!path) {
ac1671c0 996 AliError("the program dateStream was not found");
06c7e392 997 if (fStopOnError) return kFALSE;
998 } else {
999 delete[] path;
1000 }
0421c3d1 1001
06c7e392 1002 AliRunLoader* runLoader = LoadRun("READ");
1003 if (!runLoader) return kFALSE;
1004
ac1671c0 1005 AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
06c7e392 1006 char command[256];
f28a8d2d 1007 // Note the option -s. It is used in order to avoid
1008 // the generation of SOR/EOR events.
1009 sprintf(command, "dateStream -s -D -o %s -# %d -C",
06c7e392 1010 dateFileName, runLoader->GetNumberOfEvents());
1011 FILE* pipe = gSystem->OpenPipe(command, "w");
1012
1013 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1014 fprintf(pipe, "GDC\n");
1015 Float_t ldc = 0;
1016 Int_t prevLDC = -1;
1017
1018 // loop over detectors and DDLs
362c9d61 1019 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1020 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
06c7e392 1021
362c9d61 1022 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
06c7e392 1023 Int_t ldcID = Int_t(ldc + 0.0001);
362c9d61 1024 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
06c7e392 1025
1026 char rawFileName[256];
362c9d61 1027 sprintf(rawFileName, "raw%d/%s",
1028 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
06c7e392 1029
1030 // check existence and size of raw data file
1031 FILE* file = fopen(rawFileName, "rb");
1032 if (!file) continue;
1033 fseek(file, 0, SEEK_END);
1034 unsigned long size = ftell(file);
aa04f81b 1035 fclose(file);
1036 if (!size) continue;
06c7e392 1037
1038 if (ldcID != prevLDC) {
1039 fprintf(pipe, " LDC Id %d\n", ldcID);
1040 prevLDC = ldcID;
1041 }
1042 fprintf(pipe, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1043 }
1044 }
1045 }
1046
1047 Int_t result = gSystem->ClosePipe(pipe);
1048
1049 delete runLoader;
1050 return (result == 0);
1051}
1052
1053//_____________________________________________________________________________
1054Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
1055 const char* rootFileName)
1056{
1057// convert a DATE file to a root file with the program "alimdc"
1058
1059 // ALIMDC setup
f28a8d2d 1060 const Int_t kDBSize = 2000000000;
c83eb8f4 1061 const Int_t kTagDBSize = 1000000000;
06c7e392 1062 const Bool_t kFilter = kFALSE;
8ec1d12b 1063 const Int_t kCompression = 1;
06c7e392 1064
1065 char* path = gSystem->Which(gSystem->Getenv("PATH"), "alimdc");
1066 if (!path) {
ac1671c0 1067 AliError("the program alimdc was not found");
06c7e392 1068 if (fStopOnError) return kFALSE;
1069 } else {
1070 delete[] path;
1071 }
1072
ac1671c0 1073 AliInfo(Form("converting DATE file %s to root file %s",
1074 dateFileName, rootFileName));
06c7e392 1075
f732bd7d 1076 const char* rawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
1077 const char* tagDBFS = "/tmp/mdc1/tags";
f732bd7d 1078
1079 // User defined file system locations
1080 if (gSystem->Getenv("ALIMDC_RAWDB1"))
1081 rawDBFS[0] = gSystem->Getenv("ALIMDC_RAWDB1");
1082 if (gSystem->Getenv("ALIMDC_RAWDB2"))
1083 rawDBFS[1] = gSystem->Getenv("ALIMDC_RAWDB2");
1084 if (gSystem->Getenv("ALIMDC_TAGDB"))
1085 tagDBFS = gSystem->Getenv("ALIMDC_TAGDB");
f732bd7d 1086
1087 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1088 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1089 gSystem->Exec(Form("rm -rf %s",tagDBFS));
f732bd7d 1090
1091 gSystem->Exec(Form("mkdir %s",rawDBFS[0]));
1092 gSystem->Exec(Form("mkdir %s",rawDBFS[1]));
1093 gSystem->Exec(Form("mkdir %s",tagDBFS));
f732bd7d 1094
1095 Int_t result = gSystem->Exec(Form("alimdc %d %d %d %d %s",
1096 kDBSize, kTagDBSize, kFilter, kCompression, dateFileName));
1097 gSystem->Exec(Form("mv %s/*.root %s", rawDBFS[0], rootFileName));
1098
1099 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1100 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1101 gSystem->Exec(Form("rm -rf %s",tagDBFS));
06c7e392 1102
1103 return (result == 0);
0421c3d1 1104}
1105
85a5290f 1106
1107//_____________________________________________________________________________
06c7e392 1108AliRunLoader* AliSimulation::LoadRun(const char* mode) const
95601147 1109{
1110// delete existing run loaders, open a new one and load gAlice
1111
1112 while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
1113 AliRunLoader* runLoader =
1114 AliRunLoader::Open(fGAliceFileName.Data(),
06c7e392 1115 AliConfig::GetDefaultEventFolderName(), mode);
95601147 1116 if (!runLoader) {
ac1671c0 1117 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
95601147 1118 return NULL;
1119 }
1120 runLoader->LoadgAlice();
1121 gAlice = runLoader->GetAliRun();
1122 if (!gAlice) {
ac1671c0 1123 AliError(Form("no gAlice object found in file %s",
1124 fGAliceFileName.Data()));
95601147 1125 return NULL;
1126 }
1127 return runLoader;
1128}
1129
1130//_____________________________________________________________________________
05526d44 1131Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
1132{
1133// get or calculate the number of signal events per background event
1134
1135 if (!fBkgrdFileNames) return 1;
1136 Int_t nBkgrdFiles = fBkgrdFileNames->GetEntriesFast();
1137 if (nBkgrdFiles == 0) return 1;
1138
1139 // get the number of signal events
1140 if (nEvents <= 0) {
1141 AliRunLoader* runLoader =
ca30848f 1142 AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
05526d44 1143 if (!runLoader) return 1;
ca30848f 1144
05526d44 1145 nEvents = runLoader->GetNumberOfEvents();
1146 delete runLoader;
1147 }
1148
1149 Int_t result = 0;
1150 for (Int_t iBkgrdFile = 0; iBkgrdFile < nBkgrdFiles; iBkgrdFile++) {
1151 // get the number of background events
1152 const char* fileName = ((TObjString*)
1153 (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
ca30848f 1154 AliRunLoader* runLoader =
05526d44 1155 AliRunLoader::Open(fileName, "BKGRD");
1156 if (!runLoader) continue;
1157 Int_t nBkgrdEvents = runLoader->GetNumberOfEvents();
1158 delete runLoader;
ca30848f 1159
05526d44 1160 // get or calculate the number of signal per background events
1161 Int_t nSignalPerBkgrd = fBkgrdFileNames->At(iBkgrdFile)->GetUniqueID();
1162 if (nSignalPerBkgrd <= 0) {
1163 nSignalPerBkgrd = (nEvents-1) / nBkgrdEvents + 1;
1164 } else if (result && (result != nSignalPerBkgrd)) {
ac1671c0 1165 AliInfo(Form("the number of signal events per background event "
1166 "will be changed from %d to %d for stream %d",
1167 nSignalPerBkgrd, result, iBkgrdFile+1));
05526d44 1168 nSignalPerBkgrd = result;
1169 }
1170
1171 if (!result) result = nSignalPerBkgrd;
1172 if (nSignalPerBkgrd * nBkgrdEvents < nEvents) {
ac1671c0 1173 AliWarning(Form("not enough background events (%d) for %d signal events "
1174 "using %d signal per background events for stream %d",
1175 nBkgrdEvents, nEvents, nSignalPerBkgrd, iBkgrdFile+1));
05526d44 1176 }
1177 }
1178
1179 return result;
1180}
1181
1182//_____________________________________________________________________________
85a5290f 1183Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
1184{
1185// check whether detName is contained in detectors
1186// if yes, it is removed from detectors
1187
1188 // check if all detectors are selected
1189 if ((detectors.CompareTo("ALL") == 0) ||
1190 detectors.BeginsWith("ALL ") ||
1191 detectors.EndsWith(" ALL") ||
1192 detectors.Contains(" ALL ")) {
1193 detectors = "ALL";
1194 return kTRUE;
1195 }
1196
1197 // search for the given detector
1198 Bool_t result = kFALSE;
1199 if ((detectors.CompareTo(detName) == 0) ||
1200 detectors.BeginsWith(detName+" ") ||
1201 detectors.EndsWith(" "+detName) ||
1202 detectors.Contains(" "+detName+" ")) {
1203 detectors.ReplaceAll(detName, "");
1204 result = kTRUE;
1205 }
1206
1207 // clean up the detectors string
1208 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
1209 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
1210 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
1211
1212 return result;
1213}
ca30848f 1214
1215Bool_t AliSimulation::ConvertRaw2SDigits(const char* rawDirectory, const char* esdFileName)
1216{
1217//
1218// Steering routine to convert raw data in directory rawDirectory/ to fake SDigits.
1219// These can be used for embedding of MC tracks into RAW data using the standard
1220// merging procedure.
1221//
1222// If an ESD file is given the reconstructed vertex is taken from it and stored in the event header.
1223//
1224 if (!gAlice) {
1225 AliError("no gAlice object. Restart aliroot and try again.");
1226 return kFALSE;
1227 }
1228 if (gAlice->Modules()->GetEntries() > 0) {
1229 AliError("gAlice was already run. Restart aliroot and try again.");
1230 return kFALSE;
1231 }
1232
1233 AliInfo(Form("initializing gAlice with config file %s",fConfigFileName.Data()));
1234 StdoutToAliInfo(StderrToAliError(gAlice->Init(fConfigFileName.Data());););
1235//
1236// Initialize CDB
1237 InitCDBStorage();
1238 AliCDBManager* man = AliCDBManager::Instance();
1239 man->SetRun(0); // Should this come from rawdata header ?
1240
1241 Int_t iDet;
1242 //
1243 // Get the runloader
1244 AliRunLoader* runLoader = gAlice->GetRunLoader();
1245 //
1246 // Open esd file if available
1247 TFile* esdFile = TFile::Open(esdFileName);
1248 Bool_t esdOK = (esdFile != 0);
1249 AliESD* esd = new AliESD;
1250 TTree* treeESD = 0;
1251 if (esdOK) {
1252 treeESD = (TTree*) esdFile->Get("esdTree");
1253 if (!treeESD) {
1254 AliWarning("No ESD tree found");
1255 esdOK = kFALSE;
1256 } else {
1257 treeESD->SetBranchAddress("ESD", &esd);
1258 }
1259 }
1260 //
1261 // Create the RawReader
8b31e840 1262 TString fileName(rawDirectory);
1263 AliRawReader* rawReader = 0x0;
1264 if (fileName.EndsWith("/")) {
1265 rawReader = new AliRawReaderFile(fileName);
1266 } else if (fileName.EndsWith(".root")) {
1267 rawReader = new AliRawReaderRoot(fileName);
1268 } else if (!fileName.IsNull()) {
1269 rawReader = new AliRawReaderDate(fileName);
1270 rawReader->SelectEvents(7);
1271 }
1272// if (!fEquipIdMap.IsNull() && fRawReader)
1273// fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
ca30848f 1274 //
1275 // Get list of detectors
1276 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1277 //
1278 // Get Header
1279 AliHeader* header = runLoader->GetHeader();
1280 //
8b31e840 1281 TString detStr = fMakeSDigits;
ca30848f 1282 // Event loop
1283 Int_t nev = 0;
1284 while(kTRUE) {
1285 if (!(rawReader->NextEvent())) break;
1286 //
1287 // Detector loop
1288 for (iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1289 AliModule* det = (AliModule*) detArray->At(iDet);
8b31e840 1290 if (!det || !det->IsActive()) continue;
1291 if (IsSelected(det->GetName(), detStr)) {
1292 AliInfo(Form("Calling Raw2SDigits for %s\n", det->GetName()));
1293 det->Raw2SDigits(rawReader);
1294 rawReader->Reset();
1295 }
ca30848f 1296 } // detectors
1297
5b188f2f 1298
ca30848f 1299 //
1300 // If ESD information available obtain reconstructed vertex and store in header.
1301 if (esdOK) {
1302 treeESD->GetEvent(nev);
1303 const AliESDVertex* esdVertex = esd->GetPrimaryVertex();
1304 Double_t position[3];
1305 esdVertex->GetXYZ(position);
1306 AliGenEventHeader* mcHeader = new AliGenEventHeader("ESD");
1307 TArrayF mcV;
1308 mcV.Set(3);
1309 for (Int_t i = 0; i < 3; i++) mcV[i] = position[i];
1310 mcHeader->SetPrimaryVertex(mcV);
1311 header->Reset(0,nev);
1312 header->SetGenEventHeader(mcHeader);
1313 printf("***** Saved vertex %f %f %f \n", position[0], position[1], position[2]);
1314 }
1315 nev++;
1316//
1317// Finish the event
1318 runLoader->TreeE()->Fill();
1319 runLoader->SetNextEvent();
1320 } // events
1321
1322 delete rawReader;
1323//
1324// Finish the run
1325 runLoader->CdGAFile();
1326 runLoader->WriteHeader("OVERWRITE");
1327 runLoader->WriteRunLoader();
1328
1329 return kTRUE;
1330}
5b188f2f 1331
1332//_____________________________________________________________________________
1333Int_t AliSimulation::GetDetIndex(const char* detector)
1334{
1335 // return the detector index corresponding to detector
1336 Int_t index = -1 ;
1337 for (index = 0; index < fgkNDetectors ; index++) {
1338 if ( strcmp(detector, fgkDetectorName[index]) == 0 )
1339 break ;
1340 }
1341 return index ;
1342}