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