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