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