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