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