Removing obsolete option
[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>
124253ee 109#include <TVirtualMC.h>
e8d02863 110#include <TGeoManager.h>
85a5290f 111#include <TObjString.h>
af7ba10c 112#include <TSystem.h>
bfbd5665 113#include <TFile.h>
fc7e1b1c 114#include <TROOT.h>
85a5290f 115
87932dab 116#include "AliCodeTimer.h"
e8d02863 117#include "AliCDBStorage.h"
118#include "AliCDBEntry.h"
119#include "AliCDBManager.h"
25be1e5c 120#include "AliGeomManager.h"
e8d02863 121#include "AliAlignObj.h"
122#include "AliCentralTrigger.h"
362c9d61 123#include "AliDAQ.h"
af7ba10c 124#include "AliDigitizer.h"
125#include "AliGenerator.h"
e8d02863 126#include "AliLog.h"
af7ba10c 127#include "AliModule.h"
128#include "AliRun.h"
129#include "AliRunDigitizer.h"
130#include "AliRunLoader.h"
131#include "AliSimulation.h"
132#include "AliVertexGenFile.h"
a5a091ce 133#include "AliCentralTrigger.h"
007155cc 134#include "AliCTPRawData.h"
ca30848f 135#include "AliRawReaderFile.h"
8b31e840 136#include "AliRawReaderRoot.h"
137#include "AliRawReaderDate.h"
ca30848f 138#include "AliESD.h"
139#include "AliHeader.h"
140#include "AliGenEventHeader.h"
b856a511 141#include "AliMC.h"
cae21299 142#include "AliHLTSimulation.h"
27fc67fa 143#include "AliSysInfo.h"
fc7e1b1c 144#include "AliMagF.h"
44e45fac 145#include "AliGRPObject.h"
85a5290f 146
147ClassImp(AliSimulation)
148
b856a511 149AliSimulation *AliSimulation::fgInstance = 0;
5b188f2f 150const char* AliSimulation::fgkDetectorName[AliSimulation::fgkNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE", "HLT"};
85a5290f 151
152//_____________________________________________________________________________
795e4a22 153AliSimulation::AliSimulation(const char* configFileName,
95601147 154 const char* name, const char* title) :
155 TNamed(name, title),
156
157 fRunGeneration(kTRUE),
158 fRunSimulation(kTRUE),
024cf675 159 fLoadAlignFromCDB(kTRUE),
25be1e5c 160 fLoadAlObjsListOfDets("ALL"),
95601147 161 fMakeSDigits("ALL"),
162 fMakeDigits("ALL"),
a5a091ce 163 fMakeTrigger(""),
95601147 164 fMakeDigitsFromHits(""),
0421c3d1 165 fWriteRawData(""),
06c7e392 166 fRawDataFileName(""),
167 fDeleteIntermediateFiles(kFALSE),
67327b72 168 fWriteSelRawData(kFALSE),
95601147 169 fStopOnError(kFALSE),
95601147 170 fNEvents(1),
171 fConfigFileName(configFileName),
172 fGAliceFileName("galice.root"),
18b43626 173 fEventsPerFile(),
95601147 174 fBkgrdFileNames(NULL),
995ad051 175 fAlignObjArray(NULL),
04bae0a0 176 fUseBkgrdVertex(kTRUE),
024cf675 177 fRegionOfInterest(kFALSE),
795e4a22 178 fCDBUri(""),
ca30848f 179 fSpecCDBUri(),
795e4a22 180 fRun(-1),
181 fSeed(0),
182 fInitCDBCalled(kFALSE),
183 fInitRunNumberCalled(kFALSE),
184 fSetRunNumberFromDataCalled(kFALSE),
cae21299 185 fEmbeddingFlag(kFALSE),
e3b20d9a 186 fQADetectors("ALL"),
187 fQATasks("ALL"),
940d8e5f 188 fQASteer(NULL),
759c1df1 189 fRunQA(kTRUE),
fc7e1b1c 190 fRunHLT("default"),
191 fWriteGRPEntry(kTRUE)
85a5290f 192{
193// create simulation object with default parameters
b856a511 194 fgInstance = this;
0421c3d1 195 SetGAliceFile("galice.root");
5b188f2f 196
197// for QA
940d8e5f 198 fQASteer = new AliQADataMakerSteer("sim") ;
199 fQASteer->SetActiveDetectors(fQADetectors) ;
678caae8 200 fQATasks = Form("%d %d %d", AliQA::kHITS, AliQA::kSDIGITS, AliQA::kDIGITS) ;
940d8e5f 201 fQASteer->SetTasks(fQATasks) ;
85a5290f 202}
203
204//_____________________________________________________________________________
85a5290f 205AliSimulation::~AliSimulation()
206{
207// clean up
208
18b43626 209 fEventsPerFile.Delete();
024cf675 210// if(fAlignObjArray) fAlignObjArray->Delete(); // fAlignObjArray->RemoveAll() ???
211// delete fAlignObjArray; fAlignObjArray=0;
18b43626 212
95601147 213 if (fBkgrdFileNames) {
214 fBkgrdFileNames->Delete();
215 delete fBkgrdFileNames;
216 }
ec92bee0 217
218 fSpecCDBUri.Delete();
b856a511 219 if (fgInstance==this) fgInstance = 0;
87932dab 220
940d8e5f 221 delete fQASteer ;
222
87932dab 223 AliCodeTimer::Instance()->Print();
85a5290f 224}
225
226
227//_____________________________________________________________________________
228void AliSimulation::SetNumberOfEvents(Int_t nEvents)
229{
230// set the number of events for one run
231
232 fNEvents = nEvents;
233}
234
235//_____________________________________________________________________________
795e4a22 236void AliSimulation::InitCDB()
024cf675 237{
238// activate a default CDB storage
239// First check if we have any CDB storage set, because it is used
240// to retrieve the calibration and alignment constants
241
795e4a22 242 if (fInitCDBCalled) return;
243 fInitCDBCalled = kTRUE;
244
024cf675 245 AliCDBManager* man = AliCDBManager::Instance();
ec92bee0 246 if (man->IsDefaultStorageSet())
024cf675 247 {
ec92bee0 248 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
249 AliWarning("Default CDB storage has been already set !");
ced249e6 250 AliWarning(Form("Ignoring the default storage declared in AliSimulation: %s",fCDBUri.Data()));
ec92bee0 251 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
795e4a22 252 fCDBUri = man->GetDefaultStorage()->GetURI();
ec92bee0 253 }
254 else {
795e4a22 255 if (fCDBUri.Length() > 0)
256 {
257 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
258 AliDebug(2, Form("Default CDB storage is set to: %s", fCDBUri.Data()));
259 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
260 } else {
261 fCDBUri="local://$ALICE_ROOT";
262 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
263 AliWarning("Default CDB storage not yet set !!!!");
264 AliWarning(Form("Setting it now to: %s", fCDBUri.Data()));
265 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
266
267 }
ec92bee0 268 man->SetDefaultStorage(fCDBUri);
269 }
270
271 // Now activate the detector specific CDB storage locations
272 for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
273 TObject* obj = fSpecCDBUri[i];
274 if (!obj) continue;
b8ec52f6 275 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
276 AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
277 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
c3a7b59a 278 man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
ec92bee0 279 }
795e4a22 280
024cf675 281}
282
283//_____________________________________________________________________________
795e4a22 284void AliSimulation::InitRunNumber(){
285// check run number. If not set, set it to 0 !!!!
286
287 if (fInitRunNumberCalled) return;
288 fInitRunNumberCalled = kTRUE;
289
290 AliCDBManager* man = AliCDBManager::Instance();
291 if (man->GetRun() >= 0)
292 {
8c8fbdcd 293 AliFatal(Form("Run number cannot be set in AliCDBManager before start of simulation: "
294 "Use external variable DC_RUN or AliSimulation::SetRun()!"));
795e4a22 295 }
8c8fbdcd 296
297 if(fRun >= 0) {
795e4a22 298 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
299 AliDebug(2, Form("Setting CDB run number to: %d",fRun));
300 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
8c8fbdcd 301 } else {
795e4a22 302 fRun=0;
303 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
304 AliWarning("Run number not yet set !!!!");
305 AliWarning(Form("Setting it now to: %d", fRun));
306 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
307
795e4a22 308 }
8c8fbdcd 309 man->SetRun(fRun);
024cf675 310
795e4a22 311 man->Print();
024cf675 312
313}
314
315//_____________________________________________________________________________
795e4a22 316void AliSimulation::SetCDBLock() {
317 // Set CDB lock: from now on it is forbidden to reset the run number
318 // or the default storage or to activate any further storage!
319
320 AliCDBManager::Instance()->SetLock(1);
321}
322
323//_____________________________________________________________________________
324void AliSimulation::SetDefaultStorage(const char* uri) {
325// Store the desired default CDB storage location
00aa02d5 326// Activate it later within the Run() method
00aa02d5 327
795e4a22 328 fCDBUri = uri;
00aa02d5 329
330}
331
332//_____________________________________________________________________________
c3a7b59a 333void AliSimulation::SetSpecificStorage(const char* calibType, const char* uri) {
ec92bee0 334// Store a detector-specific CDB storage location
335// Activate it later within the Run() method
024cf675 336
c3a7b59a 337 AliCDBPath aPath(calibType);
338 if(!aPath.IsValid()){
339 AliError(Form("Not a valid path: %s", calibType));
340 return;
341 }
342
343 TObject* obj = fSpecCDBUri.FindObject(calibType);
ec92bee0 344 if (obj) fSpecCDBUri.Remove(obj);
c3a7b59a 345 fSpecCDBUri.Add(new TNamed(calibType, uri));
024cf675 346
347}
348
349//_____________________________________________________________________________
795e4a22 350void AliSimulation::SetRunNumber(Int_t run)
351{
352// sets run number
353// Activate it later within the Run() method
354
355 fRun = run;
356}
357
358//_____________________________________________________________________________
359void AliSimulation::SetSeed(Int_t seed)
360{
361// sets seed number
362// Activate it later within the Run() method
363
364 fSeed = seed;
365}
366
367//_____________________________________________________________________________
368Bool_t AliSimulation::SetRunNumberFromData()
369{
370 // Set the CDB manager run number
371 // The run number is retrieved from gAlice
372
373 if (fSetRunNumberFromDataCalled) return kTRUE;
374 fSetRunNumberFromDataCalled = kTRUE;
375
376 AliCDBManager* man = AliCDBManager::Instance();
377 Int_t runData = -1, runCDB = -1;
378
379 AliRunLoader* runLoader = LoadRun("READ");
380 if (!runLoader) return kFALSE;
381 else {
d3b3a3b2 382 runData = runLoader->GetHeader()->GetRun();
795e4a22 383 delete runLoader;
384 }
385
386 runCDB = man->GetRun();
387 if(runCDB >= 0) {
388 if (runCDB != runData) {
389 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
390 AliWarning(Form("A run number was previously set in AliCDBManager: %d !", runCDB));
391 AliWarning(Form("It will be replaced with the run number got from run header: %d !", runData));
392 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
393 }
394
395 }
396
397 man->SetRun(runData);
398 fRun = runData;
399
400 if(man->GetRun() < 0) {
401 AliError("Run number not properly initalized!");
402 return kFALSE;
403 }
404
405 man->Print();
406
407 return kTRUE;
408}
409
410//_____________________________________________________________________________
85a5290f 411void AliSimulation::SetConfigFile(const char* fileName)
412{
413// set the name of the config file
414
415 fConfigFileName = fileName;
416}
417
418//_____________________________________________________________________________
0421c3d1 419void AliSimulation::SetGAliceFile(const char* fileName)
420{
421// set the name of the galice file
422// the path is converted to an absolute one if it is relative
423
424 fGAliceFileName = fileName;
425 if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
426 char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
427 fGAliceFileName);
428 fGAliceFileName = absFileName;
429 delete[] absFileName;
430 }
ac1671c0 431
432 AliDebug(2, Form("galice file name set to %s", fileName));
0421c3d1 433}
434
435//_____________________________________________________________________________
18b43626 436void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
437 Int_t nEvents)
438{
439// set the number of events per file for the given detector and data type
440// ("Hits", "Summable Digits", "Digits", "Reconstructed Points" or "Tracks")
441
442 TNamed* obj = new TNamed(detector, type);
443 obj->SetUniqueID(nEvents);
444 fEventsPerFile.Add(obj);
445}
446
447//_____________________________________________________________________________
6bae477a 448Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
449{
450 // Read the alignment objects from CDB.
451 // Each detector is supposed to have the
452 // alignment objects in DET/Align/Data CDB path.
453 // All the detector objects are then collected,
454 // sorted by geometry level (starting from ALIC) and
455 // then applied to the TGeo geometry.
456 // Finally an overlaps check is performed.
457
74ae4297 458 if (!AliGeomManager::GetGeometry() || !AliGeomManager::GetGeometry()->IsClosed()) {
459 AliError("Can't apply the misalignment! Geometry is not loaded or it is still opened!");
0717eed2 460 return kFALSE;
461 }
795e4a22 462
463 // initialize CDB storage, run number, set CDB lock
464 InitCDB();
465// if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
466 SetCDBLock();
467
6bae477a 468 Bool_t delRunLoader = kFALSE;
469 if (!runLoader) {
470 runLoader = LoadRun("READ");
471 if (!runLoader) return kFALSE;
472 delRunLoader = kTRUE;
473 }
795e4a22 474
b856a511 475 // Export ideal geometry
ced249e6 476 if(!gAlice->IsRootGeometry()) AliGeomManager::GetGeometry()->Export("geometry.root");
b856a511 477
25be1e5c 478 // Load alignment data from CDB and apply to geometry through AliGeomManager
6bae477a 479 if(fLoadAlignFromCDB){
25be1e5c 480
481 TString detStr = fLoadAlObjsListOfDets;
482 TString loadAlObjsListOfDets = "";
483
484 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
485 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
486 AliModule* det = (AliModule*) detArray->At(iDet);
487 if (!det || !det->IsActive()) continue;
488 if (IsSelected(det->GetName(), detStr)) {
0717eed2 489 //add det to list of dets to be aligned from CDB
490 loadAlObjsListOfDets += det->GetName();
491 loadAlObjsListOfDets += " ";
25be1e5c 492 }
493 } // end loop over detectors
53dd3c3d 494 loadAlObjsListOfDets.Prepend("GRP "); //add alignment objects for non-sensitive modules
45daac75 495 AliGeomManager::ApplyAlignObjsFromCDB(loadAlObjsListOfDets.Data());
25be1e5c 496 }else{
497 // Check if the array with alignment objects was
498 // provided by the user. If yes, apply the objects
499 // to the present TGeo geometry
500 if (fAlignObjArray) {
45daac75 501 if (AliGeomManager::ApplyAlignObjsToGeom(*fAlignObjArray) == kFALSE) {
0717eed2 502 AliError("The misalignment of one or more volumes failed!"
503 "Compare the list of simulated detectors and the list of detector alignment data!");
504 if (delRunLoader) delete runLoader;
505 return kFALSE;
6bae477a 506 }
507 }
6bae477a 508 }
509
ef7ff1f9 510 // Update the internal geometry of modules (ITS needs it)
25be1e5c 511 TString detStr = fLoadAlObjsListOfDets;
ef7ff1f9 512 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
513 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
514
515 AliModule* det = (AliModule*) detArray->At(iDet);
516 if (!det || !det->IsActive()) continue;
517 if (IsSelected(det->GetName(), detStr)) {
518 det->UpdateInternalGeometry();
519 }
520 } // end loop over detectors
521
522
6bae477a 523 if (delRunLoader) delete runLoader;
524
525 return kTRUE;
526}
527
6bae477a 528//_____________________________________________________________________________
85a5290f 529void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
530{
531// add a file with background events for merging
532
533 TObjString* fileNameStr = new TObjString(fileName);
534 fileNameStr->SetUniqueID(nSignalPerBkgrd);
95601147 535 if (!fBkgrdFileNames) fBkgrdFileNames = new TObjArray;
85a5290f 536 fBkgrdFileNames->Add(fileNameStr);
537}
538
ca30848f 539void AliSimulation::EmbedInto(const char* fileName, Int_t nSignalPerBkgrd)
540{
541// add a file with background events for embeddin
542 MergeWith(fileName, nSignalPerBkgrd);
543 fEmbeddingFlag = kTRUE;
544}
85a5290f 545
546//_____________________________________________________________________________
547Bool_t AliSimulation::Run(Int_t nEvents)
548{
549// run the generation, simulation and digitization
550
c65c502a 551
795e4a22 552 AliCodeTimerAuto("")
87932dab 553
795e4a22 554 // Load run number and seed from environmental vars
555 ProcessEnvironmentVars();
b0314964 556
795e4a22 557 gRandom->SetSeed(fSeed);
558
85a5290f 559 if (nEvents > 0) fNEvents = nEvents;
560
561 // generation and simulation -> hits
562 if (fRunGeneration) {
85a5290f 563 if (!RunSimulation()) if (fStopOnError) return kFALSE;
564 }
795e4a22 565
566 // initialize CDB storage from external environment
567 // (either CDB manager or AliSimulation setters),
568 // if not already done in RunSimulation()
569 InitCDB();
570
571 // Set run number in CDBManager from data
572 // From this point on the run number must be always loaded from data!
573 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
574
575 // Set CDB lock: from now on it is forbidden to reset the run number
576 // or the default storage or to activate any further storage!
577 SetCDBLock();
6bae477a 578
25be1e5c 579 // If RunSimulation was not called, load the geometry and misalign it
74ae4297 580 if (!AliGeomManager::GetGeometry()) {
581 // Initialize the geometry manager
45daac75 582 AliGeomManager::LoadGeometry("geometry.root");
ff5970a3 583
584// // Check that the consistency of symbolic names for the activated subdetectors
585// // in the geometry loaded by AliGeomManager
586// AliRunLoader* runLoader = LoadRun("READ");
587// if (!runLoader) return kFALSE;
588//
589// TString detsToBeChecked = "";
590// TObjArray* detArray = runLoader->GetAliRun()->Detectors();
591// for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
592// AliModule* det = (AliModule*) detArray->At(iDet);
593// if (!det || !det->IsActive()) continue;
594// detsToBeChecked += det->GetName();
595// detsToBeChecked += " ";
596// } // end loop over detectors
597// if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
598 if(!AliGeomManager::CheckSymNamesLUT("ALL"))
0bf7aade 599 AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
ff5970a3 600
74ae4297 601 if (!AliGeomManager::GetGeometry()) if (fStopOnError) return kFALSE;
602 // Misalign geometry
25be1e5c 603 if(!MisalignGeometry()) if (fStopOnError) return kFALSE;
6bae477a 604 }
25be1e5c 605
27fc67fa 606
85a5290f 607 // hits -> summable digits
27fc67fa 608 AliSysInfo::AddStamp("Start_sdigitization");
85a5290f 609 if (!fMakeSDigits.IsNull()) {
610 if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
a1b90a73 611
85a5290f 612 }
27fc67fa 613 AliSysInfo::AddStamp("Stop_sdigitization");
c65c502a 614
27fc67fa 615 AliSysInfo::AddStamp("Start_digitization");
795e4a22 616 // summable digits -> digits
85a5290f 617 if (!fMakeDigits.IsNull()) {
618 if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
619 if (fStopOnError) return kFALSE;
620 }
a1b90a73 621 }
27fc67fa 622 AliSysInfo::AddStamp("Stop_digitization");
85a5290f 623
795e4a22 624
625
85a5290f 626 // hits -> digits
627 if (!fMakeDigitsFromHits.IsNull()) {
95601147 628 if (fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
ac1671c0 629 AliWarning(Form("Merging and direct creation of digits from hits "
630 "was selected for some detectors. "
631 "No merging will be done for the following detectors: %s",
632 fMakeDigitsFromHits.Data()));
85a5290f 633 }
634 if (!RunHitsDigitization(fMakeDigitsFromHits)) {
635 if (fStopOnError) return kFALSE;
636 }
637 }
638
795e4a22 639
640
a5a091ce 641 // digits -> trigger
8480396b 642 if (!RunTrigger(fMakeTrigger,fMakeDigits)) {
087426de 643 if (fStopOnError) return kFALSE;
a5a091ce 644 }
645
795e4a22 646
647
0421c3d1 648 // digits -> raw data
649 if (!fWriteRawData.IsNull()) {
06c7e392 650 if (!WriteRawData(fWriteRawData, fRawDataFileName,
67327b72 651 fDeleteIntermediateFiles,fWriteSelRawData)) {
0421c3d1 652 if (fStopOnError) return kFALSE;
653 }
654 }
655
795e4a22 656
5f6d0c0d 657 // run HLT simulation on simulated digit data if raw data is not
658 // simulated, otherwise its called as part of WriteRawData
659 if (!fRunHLT.IsNull() && fWriteRawData.IsNull()) {
cae21299 660 if (!RunHLT()) {
661 if (fStopOnError) return kFALSE;
662 }
663 }
795e4a22 664
04236e67 665 //QA
666 if (fRunQA) {
667 Bool_t rv = RunQA() ;
668 if (!rv)
669 if (fStopOnError)
670 return kFALSE ;
671 }
795e4a22 672
673 // Cleanup of CDB manager: cache and active storages!
674 AliCDBManager::Instance()->ClearCache();
675
85a5290f 676 return kTRUE;
677}
678
679//_____________________________________________________________________________
8480396b 680Bool_t AliSimulation::RunTrigger(const char* config, const char* detectors)
a5a091ce 681{
682 // run the trigger
683
87932dab 684 AliCodeTimerAuto("")
a5a091ce 685
795e4a22 686 // initialize CDB storage from external environment
687 // (either CDB manager or AliSimulation setters),
688 // if not already done in RunSimulation()
689 InitCDB();
690
691 // Set run number in CDBManager from data
692 // From this point on the run number must be always loaded from data!
693 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
694
695 // Set CDB lock: from now on it is forbidden to reset the run number
696 // or the default storage or to activate any further storage!
697 SetCDBLock();
698
bacbe0fd 699 AliRunLoader* runLoader = LoadRun("READ");
700 if (!runLoader) return kFALSE;
51f6d619 701 TString trconfiguration = config;
bacbe0fd 702
51f6d619 703 if (trconfiguration.IsNull()) {
f12d42ce 704 if (strcmp(gAlice->GetTriggerDescriptor(),"")) {
51f6d619 705 trconfiguration = gAlice->GetTriggerDescriptor();
087426de 706 }
51f6d619 707 else
8480396b 708 AliWarning("No trigger descriptor is specified. Loading the one that is in the CDB.");
087426de 709 }
710
0f46f5fa 711 runLoader->MakeTree( "GG" );
bacbe0fd 712 AliCentralTrigger* aCTP = runLoader->GetTrigger();
51f6d619 713 // Load Configuration
714 if (!aCTP->LoadConfiguration( trconfiguration ))
715 return kFALSE;
a5a091ce 716
51f6d619 717 // digits -> trigger
8480396b 718 if( !aCTP->RunTrigger( runLoader , detectors ) ) {
a5a091ce 719 if (fStopOnError) {
51f6d619 720 // delete aCTP;
721 return kFALSE;
a5a091ce 722 }
bacbe0fd 723 }
a5a091ce 724
bacbe0fd 725 delete runLoader;
a5a091ce 726
bacbe0fd 727 return kTRUE;
a5a091ce 728}
729
007155cc 730//_____________________________________________________________________________
731Bool_t AliSimulation::WriteTriggerRawData()
732{
733 // Writes the CTP (trigger) DDL raw data
734 // Details of the format are given in the
735 // trigger TDR - pages 134 and 135.
736 AliCTPRawData writer;
737 writer.RawData();
a5a091ce 738
007155cc 739 return kTRUE;
740}
a5a091ce 741
742//_____________________________________________________________________________
95601147 743Bool_t AliSimulation::RunSimulation(Int_t nEvents)
85a5290f 744{
745// run the generation and simulation
746
87932dab 747 AliCodeTimerAuto("")
4df28b43 748
795e4a22 749 // initialize CDB storage and run number from external environment
750 // (either CDB manager or AliSimulation setters)
751 InitCDB();
752 InitRunNumber();
753 SetCDBLock();
754
95601147 755 if (!gAlice) {
ac1671c0 756 AliError("no gAlice object. Restart aliroot and try again.");
95601147 757 return kFALSE;
758 }
759 if (gAlice->Modules()->GetEntries() > 0) {
ac1671c0 760 AliError("gAlice was already run. Restart aliroot and try again.");
95601147 761 return kFALSE;
762 }
763
ac1671c0 764 AliInfo(Form("initializing gAlice with config file %s",
765 fConfigFileName.Data()));
815c2b38 766 StdoutToAliInfo(StderrToAliError(
767 gAlice->Init(fConfigFileName.Data());
768 ););
795e4a22 769
087426de 770 // Get the trigger descriptor string
771 // Either from AliSimulation or from
772 // gAlice
773 if (fMakeTrigger.IsNull()) {
f12d42ce 774 if (strcmp(gAlice->GetTriggerDescriptor(),""))
087426de 775 fMakeTrigger = gAlice->GetTriggerDescriptor();
776 }
777 else
778 gAlice->SetTriggerDescriptor(fMakeTrigger.Data());
779
6bae477a 780 // Set run number in CDBManager
024cf675 781 AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
782
d3b3a3b2 783 AliRunLoader* runLoader = AliRunLoader::GetRunLoader();
024cf675 784 if (!runLoader) {
785 AliError(Form("gAlice has no run loader object. "
786 "Check your config file: %s", fConfigFileName.Data()));
787 return kFALSE;
788 }
789 SetGAliceFile(runLoader->GetFileName());
795e4a22 790
6bae477a 791 // Misalign geometry
caf9c464 792#if ROOT_VERSION_CODE < 331527
74ae4297 793 AliGeomManager::SetGeometry(gGeoManager);
ff5970a3 794
795 // Check that the consistency of symbolic names for the activated subdetectors
796 // in the geometry loaded by AliGeomManager
797 TString detsToBeChecked = "";
798 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
799 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
800 AliModule* det = (AliModule*) detArray->At(iDet);
801 if (!det || !det->IsActive()) continue;
802 detsToBeChecked += det->GetName();
803 detsToBeChecked += " ";
804 } // end loop over detectors
805 if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
0bf7aade 806 AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
caf9c464 807 MisalignGeometry(runLoader);
808#endif
995ad051 809
d3b3a3b2 810// AliRunLoader* runLoader = AliRunLoader::GetRunLoader();
024cf675 811// if (!runLoader) {
812// AliError(Form("gAlice has no run loader object. "
813// "Check your config file: %s", fConfigFileName.Data()));
814// return kFALSE;
815// }
816// SetGAliceFile(runLoader->GetFileName());
85a5290f 817
04bae0a0 818 if (!gAlice->Generator()) {
ac1671c0 819 AliError(Form("gAlice has no generator object. "
820 "Check your config file: %s", fConfigFileName.Data()));
04bae0a0 821 return kFALSE;
822 }
fc7e1b1c 823
824 // Write GRP entry corresponding to the setting found in Cofig.C
825 if (fWriteGRPEntry)
826 WriteGRPEntry();
827
05526d44 828 if (nEvents <= 0) nEvents = fNEvents;
04bae0a0 829
830 // get vertex from background file in case of merging
831 if (fUseBkgrdVertex &&
832 fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
05526d44 833 Int_t signalPerBkgrd = GetNSignalPerBkgrd(nEvents);
04bae0a0 834 const char* fileName = ((TObjString*)
835 (fBkgrdFileNames->At(0)))->GetName();
ac1671c0 836 AliInfo(Form("The vertex will be taken from the background "
837 "file %s with nSignalPerBackground = %d",
838 fileName, signalPerBkgrd));
04bae0a0 839 AliVertexGenFile* vtxGen = new AliVertexGenFile(fileName, signalPerBkgrd);
840 gAlice->Generator()->SetVertexGenerator(vtxGen);
841 }
842
85a5290f 843 if (!fRunSimulation) {
85a5290f 844 gAlice->Generator()->SetTrackingFlag(0);
845 }
846
18b43626 847 // set the number of events per file for given detectors and data types
848 for (Int_t i = 0; i < fEventsPerFile.GetEntriesFast(); i++) {
849 if (!fEventsPerFile[i]) continue;
850 const char* detName = fEventsPerFile[i]->GetName();
851 const char* typeName = fEventsPerFile[i]->GetTitle();
852 TString loaderName(detName);
853 loaderName += "Loader";
854 AliLoader* loader = runLoader->GetLoader(loaderName);
855 if (!loader) {
ac1671c0 856 AliError(Form("RunSimulation", "no loader for %s found\n"
857 "Number of events per file not set for %s %s",
858 detName, typeName, detName));
18b43626 859 continue;
860 }
861 AliDataLoader* dataLoader =
862 loader->GetDataLoader(typeName);
863 if (!dataLoader) {
ac1671c0 864 AliError(Form("no data loader for %s found\n"
865 "Number of events per file not set for %s %s",
866 typeName, detName, typeName));
18b43626 867 continue;
868 }
869 dataLoader->SetNumberOfEventsPerFile(fEventsPerFile[i]->GetUniqueID());
ac1671c0 870 AliDebug(1, Form("number of events per file set to %d for %s %s",
871 fEventsPerFile[i]->GetUniqueID(), detName, typeName));
18b43626 872 }
873
ac1671c0 874 AliInfo("running gAlice");
27fc67fa 875 AliSysInfo::AddStamp("Start_simulation");
124253ee 876
877 gAlice->InitMC("Config.C");
878
879 // Create the Root Tree with one branch per detector
880 //Hits moved to begin event -> now we are crating separate tree for each event
881
882 gMC->ProcessRun(nEvents);
883
884 // End of this run, close files
885 if(nEvents>0) gAlice->FinishRun();
886
27fc67fa 887 AliSysInfo::AddStamp("Stop_simulation");
95601147 888 delete runLoader;
85a5290f 889
890 return kTRUE;
891}
892
893//_____________________________________________________________________________
95601147 894Bool_t AliSimulation::RunSDigitization(const char* detectors)
85a5290f 895{
896// run the digitization and produce summable digits
27fc67fa 897 static Int_t eventNr=0;
87932dab 898 AliCodeTimerAuto("")
4df28b43 899
795e4a22 900 // initialize CDB storage, run number, set CDB lock
901 InitCDB();
902 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
903 SetCDBLock();
904
95601147 905 AliRunLoader* runLoader = LoadRun();
906 if (!runLoader) return kFALSE;
907
85a5290f 908 TString detStr = detectors;
95601147 909 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 910 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
911 AliModule* det = (AliModule*) detArray->At(iDet);
912 if (!det || !det->IsActive()) continue;
913 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 914 AliInfo(Form("creating summable digits for %s", det->GetName()));
87932dab 915 AliCodeTimerAuto(Form("creating summable digits for %s", det->GetName()));
85a5290f 916 det->Hits2SDigits();
27fc67fa 917 AliSysInfo::AddStamp(Form("Digit_%s_%d",det->GetName(),eventNr), 0,1, eventNr);
85a5290f 918 }
919 }
920
921 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 922 AliError(Form("the following detectors were not found: %s",
923 detStr.Data()));
85a5290f 924 if (fStopOnError) return kFALSE;
925 }
27fc67fa 926 eventNr++;
95601147 927 delete runLoader;
928
85a5290f 929 return kTRUE;
930}
931
932
933//_____________________________________________________________________________
95601147 934Bool_t AliSimulation::RunDigitization(const char* detectors,
935 const char* excludeDetectors)
85a5290f 936{
937// run the digitization and produce digits from sdigits
938
87932dab 939 AliCodeTimerAuto("")
4df28b43 940
795e4a22 941 // initialize CDB storage, run number, set CDB lock
942 InitCDB();
943 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
944 SetCDBLock();
945
124253ee 946 delete AliRunLoader::GetRunLoader();
947 delete gAlice;
95601147 948 gAlice = NULL;
949
950 Int_t nStreams = 1;
05526d44 951 if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
952 Int_t signalPerBkgrd = GetNSignalPerBkgrd();
85a5290f 953 AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
ca30848f 954 // manager->SetEmbeddingFlag(fEmbeddingFlag);
85a5290f 955 manager->SetInputStream(0, fGAliceFileName.Data());
956 for (Int_t iStream = 1; iStream < nStreams; iStream++) {
957 const char* fileName = ((TObjString*)
958 (fBkgrdFileNames->At(iStream-1)))->GetName();
959 manager->SetInputStream(iStream, fileName);
960 }
961
962 TString detStr = detectors;
963 TString detExcl = excludeDetectors;
95601147 964 manager->GetInputStream(0)->ImportgAlice();
965 AliRunLoader* runLoader =
966 AliRunLoader::GetRunLoader(manager->GetInputStream(0)->GetFolderName());
967 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 968 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
969 AliModule* det = (AliModule*) detArray->At(iDet);
970 if (!det || !det->IsActive()) continue;
971 if (IsSelected(det->GetName(), detStr) &&
972 !IsSelected(det->GetName(), detExcl)) {
4df28b43 973 AliDigitizer* digitizer = det->CreateDigitizer(manager);
ca30848f 974
4df28b43 975 if (!digitizer) {
ac1671c0 976 AliError(Form("no digitizer for %s", det->GetName()));
85a5290f 977 if (fStopOnError) return kFALSE;
4df28b43 978 } else {
979 digitizer->SetRegionOfInterest(fRegionOfInterest);
85a5290f 980 }
981 }
982 }
983
984 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 985 AliError(Form("the following detectors were not found: %s",
986 detStr.Data()));
85a5290f 987 if (fStopOnError) return kFALSE;
988 }
989
990 if (!manager->GetListOfTasks()->IsEmpty()) {
ac1671c0 991 AliInfo("executing digitization");
85a5290f 992 manager->Exec("");
993 }
95601147 994
85a5290f 995 delete manager;
996
997 return kTRUE;
998}
999
1000//_____________________________________________________________________________
95601147 1001Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
85a5290f 1002{
1003// run the digitization and produce digits from hits
1004
87932dab 1005 AliCodeTimerAuto("")
4df28b43 1006
795e4a22 1007 // initialize CDB storage, run number, set CDB lock
1008 InitCDB();
1009 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1010 SetCDBLock();
1011
591f51bb 1012 AliRunLoader* runLoader = LoadRun("READ");
95601147 1013 if (!runLoader) return kFALSE;
1014
85a5290f 1015 TString detStr = detectors;
95601147 1016 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 1017 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1018 AliModule* det = (AliModule*) detArray->At(iDet);
1019 if (!det || !det->IsActive()) continue;
1020 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 1021 AliInfo(Form("creating digits from hits for %s", det->GetName()));
85a5290f 1022 det->Hits2Digits();
1023 }
1024 }
1025
1026 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 1027 AliError(Form("the following detectors were not found: %s",
1028 detStr.Data()));
85a5290f 1029 if (fStopOnError) return kFALSE;
1030 }
1031
1032 return kTRUE;
1033}
1034
0421c3d1 1035//_____________________________________________________________________________
06c7e392 1036Bool_t AliSimulation::WriteRawData(const char* detectors,
1037 const char* fileName,
67327b72 1038 Bool_t deleteIntermediateFiles,
1039 Bool_t selrawdata)
0421c3d1 1040{
1041// convert the digits to raw data
06c7e392 1042// First DDL raw data files for the given detectors are created.
1043// If a file name is given, the DDL files are then converted to a DATE file.
1044// If deleteIntermediateFiles is true, the DDL raw files are deleted
1045// afterwards.
1046// If the file name has the extension ".root", the DATE file is converted
1047// to a root file.
1048// If deleteIntermediateFiles is true, the DATE file is deleted afterwards.
67327b72 1049// 'selrawdata' flag can be used to enable writing of detectors raw data
1050// accoring to the trigger cluster.
0421c3d1 1051
87932dab 1052 AliCodeTimerAuto("")
4cce3e4e 1053
1054 TString detStr = detectors;
4cce3e4e 1055 if (!WriteRawFiles(detStr.Data())) {
06c7e392 1056 if (fStopOnError) return kFALSE;
1057 }
1058
5f6d0c0d 1059 // run HLT simulation on simulated DDL raw files
1060 // and produce HLT ddl raw files to be included in date/root file
1061 if (IsSelected("HLT", detStr) && !fRunHLT.IsNull()) {
1062 if (!RunHLT()) {
1063 if (fStopOnError) return kFALSE;
1064 }
1065 }
1066
06c7e392 1067 TString dateFileName(fileName);
1068 if (!dateFileName.IsNull()) {
1069 Bool_t rootOutput = dateFileName.EndsWith(".root");
1070 if (rootOutput) dateFileName += ".date";
67327b72 1071 TString selDateFileName;
1072 if (selrawdata) {
1073 selDateFileName = "selected.";
1074 selDateFileName+= dateFileName;
1075 }
1076 if (!ConvertRawFilesToDate(dateFileName,selDateFileName)) {
06c7e392 1077 if (fStopOnError) return kFALSE;
1078 }
1079 if (deleteIntermediateFiles) {
1080 AliRunLoader* runLoader = LoadRun("READ");
1081 if (runLoader) for (Int_t iEvent = 0;
1082 iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1083 char command[256];
1084 sprintf(command, "rm -r raw%d", iEvent);
1085 gSystem->Exec(command);
1086 }
1087 }
1088
1089 if (rootOutput) {
1090 if (!ConvertDateToRoot(dateFileName, fileName)) {
1091 if (fStopOnError) return kFALSE;
1092 }
1093 if (deleteIntermediateFiles) {
1094 gSystem->Unlink(dateFileName);
1095 }
67327b72 1096 if (selrawdata) {
1097 TString selFileName = "selected.";
1098 selFileName += fileName;
1099 if (!ConvertDateToRoot(selDateFileName, selFileName)) {
1100 if (fStopOnError) return kFALSE;
1101 }
1102 if (deleteIntermediateFiles) {
1103 gSystem->Unlink(selDateFileName);
1104 }
1105 }
06c7e392 1106 }
1107 }
1108
06c7e392 1109 return kTRUE;
1110}
1111
1112//_____________________________________________________________________________
1113Bool_t AliSimulation::WriteRawFiles(const char* detectors)
1114{
1115// convert the digits to raw data DDL files
1116
87932dab 1117 AliCodeTimerAuto("")
1118
06c7e392 1119 AliRunLoader* runLoader = LoadRun("READ");
0421c3d1 1120 if (!runLoader) return kFALSE;
1121
06c7e392 1122 // write raw data to DDL files
0421c3d1 1123 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
ac1671c0 1124 AliInfo(Form("processing event %d", iEvent));
0421c3d1 1125 runLoader->GetEvent(iEvent);
1126 TString baseDir = gSystem->WorkingDirectory();
1127 char dirName[256];
1128 sprintf(dirName, "raw%d", iEvent);
1129 gSystem->MakeDirectory(dirName);
1130 if (!gSystem->ChangeDirectory(dirName)) {
ac1671c0 1131 AliError(Form("couldn't change to directory %s", dirName));
0421c3d1 1132 if (fStopOnError) return kFALSE; else continue;
1133 }
1134
3fbd60fe 1135 ofstream runNbFile(Form("run%u",runLoader->GetHeader()->GetRun()));
1136 runNbFile.close();
1137
0421c3d1 1138 TString detStr = detectors;
5f6d0c0d 1139 if (IsSelected("HLT", detStr)) {
1140 // Do nothing. "HLT" will be removed from detStr and HLT raw
1141 // data files are generated in RunHLT.
1142 }
1143
0421c3d1 1144 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1145 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1146 AliModule* det = (AliModule*) detArray->At(iDet);
1147 if (!det || !det->IsActive()) continue;
1148 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 1149 AliInfo(Form("creating raw data from digits for %s", det->GetName()));
0421c3d1 1150 det->Digits2Raw();
1151 }
1152 }
1153
007155cc 1154 if (!WriteTriggerRawData())
1155 if (fStopOnError) return kFALSE;
1156
0421c3d1 1157 gSystem->ChangeDirectory(baseDir);
1158 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 1159 AliError(Form("the following detectors were not found: %s",
1160 detStr.Data()));
0421c3d1 1161 if (fStopOnError) return kFALSE;
1162 }
1163 }
1164
1165 delete runLoader;
87932dab 1166
06c7e392 1167 return kTRUE;
1168}
0421c3d1 1169
06c7e392 1170//_____________________________________________________________________________
67327b72 1171Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName,
1172 const char* selDateFileName)
06c7e392 1173{
1174// convert raw data DDL files to a DATE file with the program "dateStream"
67327b72 1175// The second argument is not empty when the user decides to write
1176// the detectors raw data according to the trigger cluster.
06c7e392 1177
87932dab 1178 AliCodeTimerAuto("")
1179
06c7e392 1180 char* path = gSystem->Which(gSystem->Getenv("PATH"), "dateStream");
1181 if (!path) {
ac1671c0 1182 AliError("the program dateStream was not found");
06c7e392 1183 if (fStopOnError) return kFALSE;
1184 } else {
1185 delete[] path;
1186 }
0421c3d1 1187
06c7e392 1188 AliRunLoader* runLoader = LoadRun("READ");
1189 if (!runLoader) return kFALSE;
1190
ac1671c0 1191 AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
67327b72 1192 Bool_t selrawdata = kFALSE;
1193 if (strcmp(selDateFileName,"") != 0) selrawdata = kTRUE;
1194
06c7e392 1195 char command[256];
f28a8d2d 1196 // Note the option -s. It is used in order to avoid
1197 // the generation of SOR/EOR events.
f86775a3 1198 sprintf(command, "dateStream -c -s -D -o %s -# %d -C -run %d",
5c0fd670 1199 dateFileName, runLoader->GetNumberOfEvents(),runLoader->GetHeader()->GetRun());
06c7e392 1200 FILE* pipe = gSystem->OpenPipe(command, "w");
1201
f5e7d9d8 1202 if (!pipe) {
1203 AliError(Form("Cannot execute command: %s",command));
1204 return kFALSE;
1205 }
1206
67327b72 1207 Int_t selEvents = 0;
06c7e392 1208 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
06c7e392 1209
f59c50c6 1210 UInt_t detectorPattern = 0;
1211 runLoader->GetEvent(iEvent);
1212 if (!runLoader->LoadTrigger()) {
1213 AliCentralTrigger *aCTP = runLoader->GetTrigger();
1214 detectorPattern = aCTP->GetClusterMask();
67327b72 1215 // Check if the event was triggered by CTP
f59c50c6 1216 if (selrawdata) {
67327b72 1217 if (aCTP->GetClassMask()) selEvents++;
1218 }
f59c50c6 1219 }
1220 else {
1221 AliWarning("No trigger can be loaded! Some fields in the event header will be empty !");
1222 if (selrawdata) {
67327b72 1223 AliWarning("No trigger can be loaded! Writing of selected raw data is abandoned !");
1224 selrawdata = kFALSE;
1225 }
1226 }
1227
f59c50c6 1228 fprintf(pipe, "GDC DetectorPattern %u\n", detectorPattern);
1229 Float_t ldc = 0;
1230 Int_t prevLDC = -1;
1231
06c7e392 1232 // loop over detectors and DDLs
362c9d61 1233 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1234 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
06c7e392 1235
362c9d61 1236 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
06c7e392 1237 Int_t ldcID = Int_t(ldc + 0.0001);
362c9d61 1238 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
06c7e392 1239
1240 char rawFileName[256];
362c9d61 1241 sprintf(rawFileName, "raw%d/%s",
1242 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
06c7e392 1243
1244 // check existence and size of raw data file
1245 FILE* file = fopen(rawFileName, "rb");
1246 if (!file) continue;
1247 fseek(file, 0, SEEK_END);
1248 unsigned long size = ftell(file);
aa04f81b 1249 fclose(file);
1250 if (!size) continue;
06c7e392 1251
1252 if (ldcID != prevLDC) {
1253 fprintf(pipe, " LDC Id %d\n", ldcID);
1254 prevLDC = ldcID;
1255 }
1256 fprintf(pipe, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1257 }
1258 }
1259 }
1260
1261 Int_t result = gSystem->ClosePipe(pipe);
1262
cf30933c 1263 if (!(selrawdata && selEvents > 0)) {
67327b72 1264 delete runLoader;
1265 return (result == 0);
1266 }
1267
1268 AliInfo(Form("converting selected by trigger cluster raw data DDL files to DATE file %s", selDateFileName));
1269
1270 sprintf(command, "dateStream -c -s -D -o %s -# %d -C -run %d",
1271 selDateFileName,selEvents,runLoader->GetHeader()->GetRun());
1272 FILE* pipe2 = gSystem->OpenPipe(command, "w");
1273
1274 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1275
1276 // Get the trigger decision and cluster
f59c50c6 1277 UInt_t detectorPattern = 0;
67327b72 1278 TString detClust;
1279 runLoader->GetEvent(iEvent);
1280 if (!runLoader->LoadTrigger()) {
1281 AliCentralTrigger *aCTP = runLoader->GetTrigger();
1282 if (aCTP->GetClassMask() == 0) continue;
f59c50c6 1283 detectorPattern = aCTP->GetClusterMask();
1284 detClust = AliDAQ::ListOfTriggeredDetectors(detectorPattern);
67327b72 1285 AliInfo(Form("List of detectors to be read out: %s",detClust.Data()));
1286 }
1287
f59c50c6 1288 fprintf(pipe2, "GDC DetectorPattern %u\n", detectorPattern);
67327b72 1289 Float_t ldc = 0;
1290 Int_t prevLDC = -1;
1291
1292 // loop over detectors and DDLs
1293 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1294 // Write only raw data from detectors that
1295 // are contained in the trigger cluster(s)
1296 if (!IsSelected(AliDAQ::DetectorName(iDet),detClust)) continue;
1297
1298 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
1299
1300 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
1301 Int_t ldcID = Int_t(ldc + 0.0001);
1302 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
1303
1304 char rawFileName[256];
1305 sprintf(rawFileName, "raw%d/%s",
1306 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
1307
1308 // check existence and size of raw data file
1309 FILE* file = fopen(rawFileName, "rb");
1310 if (!file) continue;
1311 fseek(file, 0, SEEK_END);
1312 unsigned long size = ftell(file);
1313 fclose(file);
1314 if (!size) continue;
1315
1316 if (ldcID != prevLDC) {
1317 fprintf(pipe2, " LDC Id %d\n", ldcID);
1318 prevLDC = ldcID;
1319 }
1320 fprintf(pipe2, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1321 }
1322 }
1323 }
1324
1325 Int_t result2 = gSystem->ClosePipe(pipe2);
1326
06c7e392 1327 delete runLoader;
67327b72 1328 return ((result == 0) && (result2 == 0));
06c7e392 1329}
1330
1331//_____________________________________________________________________________
1332Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
1333 const char* rootFileName)
1334{
1335// convert a DATE file to a root file with the program "alimdc"
1336
1337 // ALIMDC setup
f28a8d2d 1338 const Int_t kDBSize = 2000000000;
c83eb8f4 1339 const Int_t kTagDBSize = 1000000000;
06c7e392 1340 const Bool_t kFilter = kFALSE;
8ec1d12b 1341 const Int_t kCompression = 1;
06c7e392 1342
1343 char* path = gSystem->Which(gSystem->Getenv("PATH"), "alimdc");
1344 if (!path) {
ac1671c0 1345 AliError("the program alimdc was not found");
06c7e392 1346 if (fStopOnError) return kFALSE;
1347 } else {
1348 delete[] path;
1349 }
1350
ac1671c0 1351 AliInfo(Form("converting DATE file %s to root file %s",
1352 dateFileName, rootFileName));
06c7e392 1353
f732bd7d 1354 const char* rawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
1355 const char* tagDBFS = "/tmp/mdc1/tags";
f732bd7d 1356
1357 // User defined file system locations
1358 if (gSystem->Getenv("ALIMDC_RAWDB1"))
1359 rawDBFS[0] = gSystem->Getenv("ALIMDC_RAWDB1");
1360 if (gSystem->Getenv("ALIMDC_RAWDB2"))
1361 rawDBFS[1] = gSystem->Getenv("ALIMDC_RAWDB2");
1362 if (gSystem->Getenv("ALIMDC_TAGDB"))
1363 tagDBFS = gSystem->Getenv("ALIMDC_TAGDB");
f732bd7d 1364
1365 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1366 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1367 gSystem->Exec(Form("rm -rf %s",tagDBFS));
f732bd7d 1368
1369 gSystem->Exec(Form("mkdir %s",rawDBFS[0]));
1370 gSystem->Exec(Form("mkdir %s",rawDBFS[1]));
1371 gSystem->Exec(Form("mkdir %s",tagDBFS));
f732bd7d 1372
1373 Int_t result = gSystem->Exec(Form("alimdc %d %d %d %d %s",
1374 kDBSize, kTagDBSize, kFilter, kCompression, dateFileName));
1375 gSystem->Exec(Form("mv %s/*.root %s", rawDBFS[0], rootFileName));
1376
1377 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1378 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1379 gSystem->Exec(Form("rm -rf %s",tagDBFS));
06c7e392 1380
1381 return (result == 0);
0421c3d1 1382}
1383
85a5290f 1384
1385//_____________________________________________________________________________
06c7e392 1386AliRunLoader* AliSimulation::LoadRun(const char* mode) const
95601147 1387{
1388// delete existing run loaders, open a new one and load gAlice
1389
124253ee 1390 delete AliRunLoader::GetRunLoader();
95601147 1391 AliRunLoader* runLoader =
1392 AliRunLoader::Open(fGAliceFileName.Data(),
06c7e392 1393 AliConfig::GetDefaultEventFolderName(), mode);
95601147 1394 if (!runLoader) {
ac1671c0 1395 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
95601147 1396 return NULL;
1397 }
1398 runLoader->LoadgAlice();
795e4a22 1399 runLoader->LoadHeader();
95601147 1400 gAlice = runLoader->GetAliRun();
1401 if (!gAlice) {
ac1671c0 1402 AliError(Form("no gAlice object found in file %s",
1403 fGAliceFileName.Data()));
95601147 1404 return NULL;
1405 }
1406 return runLoader;
1407}
1408
1409//_____________________________________________________________________________
05526d44 1410Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
1411{
1412// get or calculate the number of signal events per background event
1413
1414 if (!fBkgrdFileNames) return 1;
1415 Int_t nBkgrdFiles = fBkgrdFileNames->GetEntriesFast();
1416 if (nBkgrdFiles == 0) return 1;
1417
1418 // get the number of signal events
1419 if (nEvents <= 0) {
1420 AliRunLoader* runLoader =
ca30848f 1421 AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
05526d44 1422 if (!runLoader) return 1;
ca30848f 1423
05526d44 1424 nEvents = runLoader->GetNumberOfEvents();
1425 delete runLoader;
1426 }
1427
1428 Int_t result = 0;
1429 for (Int_t iBkgrdFile = 0; iBkgrdFile < nBkgrdFiles; iBkgrdFile++) {
1430 // get the number of background events
1431 const char* fileName = ((TObjString*)
1432 (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
ca30848f 1433 AliRunLoader* runLoader =
05526d44 1434 AliRunLoader::Open(fileName, "BKGRD");
1435 if (!runLoader) continue;
1436 Int_t nBkgrdEvents = runLoader->GetNumberOfEvents();
1437 delete runLoader;
ca30848f 1438
05526d44 1439 // get or calculate the number of signal per background events
1440 Int_t nSignalPerBkgrd = fBkgrdFileNames->At(iBkgrdFile)->GetUniqueID();
1441 if (nSignalPerBkgrd <= 0) {
1442 nSignalPerBkgrd = (nEvents-1) / nBkgrdEvents + 1;
1443 } else if (result && (result != nSignalPerBkgrd)) {
ac1671c0 1444 AliInfo(Form("the number of signal events per background event "
1445 "will be changed from %d to %d for stream %d",
1446 nSignalPerBkgrd, result, iBkgrdFile+1));
05526d44 1447 nSignalPerBkgrd = result;
1448 }
1449
1450 if (!result) result = nSignalPerBkgrd;
1451 if (nSignalPerBkgrd * nBkgrdEvents < nEvents) {
ac1671c0 1452 AliWarning(Form("not enough background events (%d) for %d signal events "
1453 "using %d signal per background events for stream %d",
1454 nBkgrdEvents, nEvents, nSignalPerBkgrd, iBkgrdFile+1));
05526d44 1455 }
1456 }
1457
1458 return result;
1459}
1460
1461//_____________________________________________________________________________
85a5290f 1462Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
1463{
1464// check whether detName is contained in detectors
1465// if yes, it is removed from detectors
1466
1467 // check if all detectors are selected
1468 if ((detectors.CompareTo("ALL") == 0) ||
1469 detectors.BeginsWith("ALL ") ||
1470 detectors.EndsWith(" ALL") ||
1471 detectors.Contains(" ALL ")) {
1472 detectors = "ALL";
1473 return kTRUE;
1474 }
1475
1476 // search for the given detector
1477 Bool_t result = kFALSE;
1478 if ((detectors.CompareTo(detName) == 0) ||
1479 detectors.BeginsWith(detName+" ") ||
1480 detectors.EndsWith(" "+detName) ||
1481 detectors.Contains(" "+detName+" ")) {
1482 detectors.ReplaceAll(detName, "");
1483 result = kTRUE;
1484 }
1485
1486 // clean up the detectors string
1487 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
1488 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
1489 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
1490
1491 return result;
1492}
ca30848f 1493
795e4a22 1494//_____________________________________________________________________________
ca30848f 1495Bool_t AliSimulation::ConvertRaw2SDigits(const char* rawDirectory, const char* esdFileName)
1496{
1497//
1498// Steering routine to convert raw data in directory rawDirectory/ to fake SDigits.
1499// These can be used for embedding of MC tracks into RAW data using the standard
1500// merging procedure.
1501//
1502// If an ESD file is given the reconstructed vertex is taken from it and stored in the event header.
1503//
1504 if (!gAlice) {
1505 AliError("no gAlice object. Restart aliroot and try again.");
1506 return kFALSE;
1507 }
1508 if (gAlice->Modules()->GetEntries() > 0) {
1509 AliError("gAlice was already run. Restart aliroot and try again.");
1510 return kFALSE;
1511 }
1512
1513 AliInfo(Form("initializing gAlice with config file %s",fConfigFileName.Data()));
1514 StdoutToAliInfo(StderrToAliError(gAlice->Init(fConfigFileName.Data());););
1515//
1516// Initialize CDB
795e4a22 1517 InitCDB();
1518 //AliCDBManager* man = AliCDBManager::Instance();
1519 //man->SetRun(0); // Should this come from rawdata header ?
ca30848f 1520
1521 Int_t iDet;
1522 //
1523 // Get the runloader
d3b3a3b2 1524 AliRunLoader* runLoader = AliRunLoader::GetRunLoader();
ca30848f 1525 //
1526 // Open esd file if available
1527 TFile* esdFile = TFile::Open(esdFileName);
1528 Bool_t esdOK = (esdFile != 0);
1529 AliESD* esd = new AliESD;
1530 TTree* treeESD = 0;
1531 if (esdOK) {
1532 treeESD = (TTree*) esdFile->Get("esdTree");
1533 if (!treeESD) {
1534 AliWarning("No ESD tree found");
1535 esdOK = kFALSE;
1536 } else {
1537 treeESD->SetBranchAddress("ESD", &esd);
1538 }
1539 }
1540 //
1541 // Create the RawReader
8b31e840 1542 TString fileName(rawDirectory);
1543 AliRawReader* rawReader = 0x0;
1544 if (fileName.EndsWith("/")) {
1545 rawReader = new AliRawReaderFile(fileName);
1546 } else if (fileName.EndsWith(".root")) {
1547 rawReader = new AliRawReaderRoot(fileName);
1548 } else if (!fileName.IsNull()) {
1549 rawReader = new AliRawReaderDate(fileName);
8b31e840 1550 }
1551// if (!fEquipIdMap.IsNull() && fRawReader)
1552// fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
ca30848f 1553 //
1554 // Get list of detectors
1555 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1556 //
1557 // Get Header
1558 AliHeader* header = runLoader->GetHeader();
1559 //
8b31e840 1560 TString detStr = fMakeSDigits;
ca30848f 1561 // Event loop
1562 Int_t nev = 0;
1563 while(kTRUE) {
1564 if (!(rawReader->NextEvent())) break;
1565 //
1566 // Detector loop
1567 for (iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1568 AliModule* det = (AliModule*) detArray->At(iDet);
8b31e840 1569 if (!det || !det->IsActive()) continue;
1570 if (IsSelected(det->GetName(), detStr)) {
1571 AliInfo(Form("Calling Raw2SDigits for %s\n", det->GetName()));
1572 det->Raw2SDigits(rawReader);
1573 rawReader->Reset();
1574 }
ca30848f 1575 } // detectors
1576
5b188f2f 1577
ca30848f 1578 //
1579 // If ESD information available obtain reconstructed vertex and store in header.
1580 if (esdOK) {
1581 treeESD->GetEvent(nev);
1582 const AliESDVertex* esdVertex = esd->GetPrimaryVertex();
1583 Double_t position[3];
1584 esdVertex->GetXYZ(position);
1585 AliGenEventHeader* mcHeader = new AliGenEventHeader("ESD");
1586 TArrayF mcV;
1587 mcV.Set(3);
1588 for (Int_t i = 0; i < 3; i++) mcV[i] = position[i];
1589 mcHeader->SetPrimaryVertex(mcV);
1590 header->Reset(0,nev);
1591 header->SetGenEventHeader(mcHeader);
1592 printf("***** Saved vertex %f %f %f \n", position[0], position[1], position[2]);
1593 }
1594 nev++;
1595//
1596// Finish the event
1597 runLoader->TreeE()->Fill();
1598 runLoader->SetNextEvent();
1599 } // events
1600
1601 delete rawReader;
1602//
1603// Finish the run
1604 runLoader->CdGAFile();
1605 runLoader->WriteHeader("OVERWRITE");
1606 runLoader->WriteRunLoader();
1607
1608 return kTRUE;
1609}
5b188f2f 1610
1611//_____________________________________________________________________________
1612Int_t AliSimulation::GetDetIndex(const char* detector)
1613{
1614 // return the detector index corresponding to detector
1615 Int_t index = -1 ;
1616 for (index = 0; index < fgkNDetectors ; index++) {
1617 if ( strcmp(detector, fgkDetectorName[index]) == 0 )
1618 break ;
1619 }
1620 return index ;
1621}
cae21299 1622
1623//_____________________________________________________________________________
1624Bool_t AliSimulation::RunHLT()
1625{
1626 // Run the HLT simulation
1627 // HLT simulation is implemented in HLT/sim/AliHLTSimulation
1628 // Disabled if fRunHLT is empty, default vaule is "default".
1629 // AliSimulation::SetRunHLT can be used to set the options for HLT simulation
1630 // The default simulation depends on the HLT component libraries and their
1631 // corresponding agents which define components and chains to run. See
5f6d0c0d 1632 // http://web.ift.uib.no/~kjeks/doc/alice-hlt-current/
1633 // http://web.ift.uib.no/~kjeks/doc/alice-hlt-current/classAliHLTModuleAgent.html
cae21299 1634 //
1635 // The libraries to be loaded can be specified as an option.
1636 // <pre>
1637 // AliSimulation sim;
1638 // sim.SetRunHLT("libAliHLTSample.so");
1639 // </pre>
1640 // will only load <tt>libAliHLTSample.so</tt>
1641
1642 // Other available options:
1643 // \li loglevel=<i>level</i> <br>
1644 // logging level for this processing
1645 // \li alilog=off
1646 // disable redirection of log messages to AliLog class
1647 // \li config=<i>macro</i>
1648 // configuration macro
5f6d0c0d 1649 // \li chains=<i>configuration</i>
cae21299 1650 // comma separated list of configurations to be run during simulation
5f6d0c0d 1651 // \li rawfile=<i>file</i>
1652 // source for the RawReader to be created, the default is <i>./</i> if
1653 // raw data is simulated
cae21299 1654
1655 int iResult=0;
1656 AliRunLoader* pRunLoader = LoadRun("READ");
1657 if (!pRunLoader) return kFALSE;
1658
795e4a22 1659 // initialize CDB storage, run number, set CDB lock
1660 InitCDB();
1661 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1662 SetCDBLock();
1663
cae21299 1664 // load the library dynamically
1665 gSystem->Load(ALIHLTSIMULATION_LIBRARY);
1666
1667 // check for the library version
1668 AliHLTSimulationGetLibraryVersion_t fctVersion=(AliHLTSimulationGetLibraryVersion_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_GET_LIBRARY_VERSION));
1669 if (!fctVersion) {
1670 AliError(Form("can not load library %s", ALIHLTSIMULATION_LIBRARY));
1671 return kFALSE;
1672 }
1673 if (fctVersion()!= ALIHLTSIMULATION_LIBRARY_VERSION) {
1674 AliError(Form("%s version does not match: compiled for version %d, loaded %d", ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_LIBRARY_VERSION, fctVersion()));
1675 return kFALSE;
1676 }
1677
1678 // print compile info
1679 typedef void (*CompileInfo)( char*& date, char*& time);
1680 CompileInfo fctInfo=(CompileInfo)gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, "CompileInfo");
1681 if (fctInfo) {
a6e0ebfe 1682 Char_t* date=0;
1683 Char_t* time=0;
1684 (*fctInfo)(date,time);
1685 if (!date) {date=new Char_t[8]; strcpy(date,"unknown");}
1686 if (!time) {time=new Char_t[8]; strcpy(time,"unknown");}
cae21299 1687 AliInfo(Form("%s build on %s (%s)", ALIHLTSIMULATION_LIBRARY, date, time));
a6e0ebfe 1688 delete date;
1689 delete time;
cae21299 1690 } else {
1691 AliInfo(Form("no build info available for %s", ALIHLTSIMULATION_LIBRARY));
1692 }
1693
1694 // create instance of the HLT simulation
1695 AliHLTSimulationCreateInstance_t fctCreate=(AliHLTSimulationCreateInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_CREATE_INSTANCE));
1696 AliHLTSimulation* pHLT=NULL;
1697 if (fctCreate==NULL || (pHLT=(fctCreate()))==NULL) {
1698 AliError(Form("can not create instance of HLT simulation (creator %p)", fctCreate));
1699 return kFALSE;
1700 }
1701
1702 // init the HLT simulation
a441bf51 1703 TString options;
1704 if (fRunHLT.CompareTo("default")!=0) options=fRunHLT;
5f6d0c0d 1705 TString detStr = fWriteRawData;
1706 if (!IsSelected("HLT", detStr)) {
a441bf51 1707 options+=" writerawfiles=";
1708 } else {
1709 options+=" writerawfiles=HLT";
1710 }
5f6d0c0d 1711
1712 if (!detStr.IsNull() && !options.Contains("rawfile=")) {
1713 // as a matter of fact, HLT will run reconstruction and needs the RawReader
1714 // in order to get detector data. By default, RawReaderFile is used to read
1715 // the already simulated ddl files. Date and Root files from the raw data
1716 // are generated after the HLT simulation.
1717 options+=" rawfile=./";
1718 }
1719
cae21299 1720 AliHLTSimulationInit_t fctInit=(AliHLTSimulationInit_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_INIT));
a441bf51 1721 if (fctInit==NULL || (iResult=(fctInit(pHLT, pRunLoader, options.Data())))<0) {
cae21299 1722 AliError(Form("can not init HLT simulation: error %d (init %p)", iResult, fctInit));
1723 } else {
1724 // run the HLT simulation
1725 AliHLTSimulationRun_t fctRun=(AliHLTSimulationRun_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_RUN));
1726 if (fctRun==NULL || (iResult=(fctRun(pHLT, pRunLoader)))<0) {
1727 AliError(Form("can not run HLT simulation: error %d (run %p)", iResult, fctRun));
1728 }
1729 }
1730
1731 // delete the instance
1732 AliHLTSimulationDeleteInstance_t fctDelete=(AliHLTSimulationDeleteInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_DELETE_INSTANCE));
1733 if (fctDelete==NULL || fctDelete(pHLT)<0) {
1734 AliError(Form("can not delete instance of HLT simulation (creator %p)", fctDelete));
1735 }
1736 pHLT=NULL;
1737
1738 return iResult>=0?kTRUE:kFALSE;
1739}
cae17938 1740
1741//_____________________________________________________________________________
a1b90a73 1742Bool_t AliSimulation::RunQA()
cae17938 1743{
a1b90a73 1744 // run the QA on summable hits, digits or digits
940d8e5f 1745
f00f2d9c 1746 if(!gAlice) return kFALSE;
d3b3a3b2 1747 fQASteer->SetRunLoader(AliRunLoader::GetRunLoader()) ;
a1b90a73 1748
e5dedfe2 1749 TString detectorsw("") ;
1750 Bool_t rv = kTRUE ;
940d8e5f 1751 detectorsw = fQASteer->Run(fQADetectors.Data()) ;
e5dedfe2 1752 if ( detectorsw.IsNull() )
1753 rv = kFALSE ;
6294b9a7 1754 else
1755 fQASteer->EndOfCycle(detectorsw) ;
a1b90a73 1756 return rv ;
cae17938 1757}
1758
795e4a22 1759//_____________________________________________________________________________
e3b20d9a 1760Bool_t AliSimulation::SetRunQA(TString detAndAction)
1761{
1762 // Allows to run QA for a selected set of detectors
1763 // and a selected set of tasks among HITS, SDIGITS and DIGITS
1764 // all selected detectors run the same selected tasks
1765
1766 if (!detAndAction.Contains(":")) {
1767 AliError( Form("%s is a wrong syntax, use \"DetectorList:ActionList\" \n", detAndAction.Data()) ) ;
1768 fRunQA = kFALSE ;
1769 return kFALSE ;
1770 }
1771 Int_t colon = detAndAction.Index(":") ;
1772 fQADetectors = detAndAction(0, colon) ;
1773 if (fQADetectors.Contains("ALL") )
1774 fQADetectors = Form("%s %s", fMakeDigits.Data(), fMakeDigitsFromHits.Data()) ;
678caae8 1775 fQATasks = detAndAction(colon+1, detAndAction.Sizeof() ) ;
e3b20d9a 1776 if (fQATasks.Contains("ALL") ) {
1777 fQATasks = Form("%d %d %d", AliQA::kHITS, AliQA::kSDIGITS, AliQA::kDIGITS) ;
1778 } else {
1779 fQATasks.ToUpper() ;
1780 TString tempo("") ;
1781 if ( fQATasks.Contains("HIT") )
1782 tempo = Form("%d ", AliQA::kHITS) ;
1783 if ( fQATasks.Contains("SDIGIT") )
1784 tempo += Form("%d ", AliQA::kSDIGITS) ;
1785 if ( fQATasks.Contains("DIGIT") )
1786 tempo += Form("%d ", AliQA::kDIGITS) ;
1787 fQATasks = tempo ;
1788 if (fQATasks.IsNull()) {
1789 AliInfo("No QA requested\n") ;
1790 fRunQA = kFALSE ;
1791 return kTRUE ;
1792 }
1793 }
1794 TString tempo(fQATasks) ;
1795 tempo.ReplaceAll(Form("%d", AliQA::kHITS), AliQA::GetTaskName(AliQA::kHITS)) ;
1796 tempo.ReplaceAll(Form("%d", AliQA::kSDIGITS), AliQA::GetTaskName(AliQA::kSDIGITS)) ;
1797 tempo.ReplaceAll(Form("%d", AliQA::kDIGITS), AliQA::GetTaskName(AliQA::kDIGITS)) ;
1798 AliInfo( Form("QA will be done on \"%s\" for \"%s\"\n", fQADetectors.Data(), tempo.Data()) ) ;
1799 fRunQA = kTRUE ;
940d8e5f 1800 fQASteer->SetActiveDetectors(fQADetectors) ;
1801 fQASteer->SetTasks(fQATasks) ;
e3b20d9a 1802 return kTRUE;
1803}
1804
1805//_____________________________________________________________________________
795e4a22 1806void AliSimulation::ProcessEnvironmentVars()
1807{
1808// Extract run number and random generator seed from env variables
1809
1810 AliInfo("Processing environment variables");
1811
1812 // Random Number seed
1813
1814 // first check that seed is not already set
1815 if (fSeed == 0) {
1816 if (gSystem->Getenv("CONFIG_SEED")) {
1817 fSeed = atoi(gSystem->Getenv("CONFIG_SEED"));
1818 }
1819 } else {
1820 if (gSystem->Getenv("CONFIG_SEED")) {
1821 AliInfo(Form("Seed for random number generation already set (%d)"
1822 ": CONFIG_SEED variable ignored!", fSeed));
1823 }
1824 }
1825
1826 AliInfo(Form("Seed for random number generation = %d ", fSeed));
1827
1828 // Run Number
1829
1830 // first check that run number is not already set
1831 if(fRun < 0) {
1832 if (gSystem->Getenv("DC_RUN")) {
1833 fRun = atoi(gSystem->Getenv("DC_RUN"));
1834 }
1835 } else {
1836 if (gSystem->Getenv("DC_RUN")) {
1837 AliInfo(Form("Run number already set (%d): DC_RUN variable ignored!", fRun));
1838 }
1839 }
1840
1841 AliInfo(Form("Run number = %d", fRun));
1842}
ff5970a3 1843
44e45fac 1844//---------------------------------------------------------------------
1845
fc7e1b1c 1846void AliSimulation::WriteGRPEntry()
1847{
1848 // Get the necessary information from galice (generator, trigger etc) and
1849 // write a GRP entry corresponding to the settings in the Config.C used
44e45fac 1850 // note that Hall probes and Cavern and Surface Atmos pressures are not simulated.
1851
1852
fc7e1b1c 1853 AliInfo("Writing global run parameters entry into the OCDB");
1854
44e45fac 1855 AliGRPObject* grpObj = new AliGRPObject();
fc7e1b1c 1856
44e45fac 1857 grpObj->SetRunType("PHYSICS");
1858 grpObj->SetTimeStart(0);
1859 grpObj->SetTimeEnd(9999);
fc7e1b1c 1860
e7c989e4 1861 const AliGenerator *gen = gAlice->Generator();
fc7e1b1c 1862 if (gen) {
44e45fac 1863 grpObj->SetBeamEnergy(gen->GetEnergyCMS());
fc7e1b1c 1864 TString projectile;
1865 Int_t a,z;
1866 gen->GetProjectile(projectile,a,z);
1867 TString target;
1868 gen->GetTarget(target,a,z);
1869 TString beamType = projectile + "-" + target;
214a2a31 1870 beamType.ReplaceAll(" ","");
e7c989e4 1871 if (!beamType.CompareTo("-")) {
44e45fac 1872
1873 grpObj->SetBeamType("UNKNOWN");
e7c989e4 1874 }
1875 else {
44e45fac 1876 grpObj->SetBeamType(beamType);
e7c989e4 1877 }
fc7e1b1c 1878 }
1879 else {
44e45fac 1880 AliWarning("Unknown beam type and energy! Setting energy to 0");
1881 grpObj->SetBeamEnergy(0);
1882 grpObj->SetBeamType("UNKNOWN");
fc7e1b1c 1883 }
1884
1885 UInt_t detectorPattern = 0;
1886 Int_t nDets = 0;
1887 TObjArray *detArray = gAlice->Detectors();
1888 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors-1; iDet++) {
1889 if (detArray->FindObject(AliDAQ::OfflineModuleName(iDet))) {
1890 detectorPattern |= (1 << iDet);
1891 nDets++;
1892 }
1893 }
1be29b9e 1894 // CTP
1895 if (!fMakeTrigger.IsNull() || strcmp(gAlice->GetTriggerDescriptor(),""))
1896 detectorPattern |= (1 << AliDAQ::DetectorID("TRG"));
1897
fc7e1b1c 1898 // HLT
1899 if (!fRunHLT.IsNull())
1900 detectorPattern |= (1 << AliDAQ::kHLTId);
ff5970a3 1901
44e45fac 1902 grpObj->SetNumberOfDetectors((Char_t)nDets);
1903 grpObj->SetDetectorMask((Int_t)detectorPattern);
1904 grpObj->SetLHCPeriod("LHC08c");
1905 grpObj->SetLHCState("STABLE_BEAMS");
1906 grpObj->SetLHCLuminosity(0,(AliGRPObject::Stats)0);
1907 grpObj->SetBeamIntensity(0,(AliGRPObject::Stats)0);
fc7e1b1c 1908
1909 AliMagF *field = gAlice->Field();
1910 Float_t solenoidField = TMath::Abs(field->SolenoidField());
1911 Float_t factor = field->Factor();
1912 Float_t l3current = TMath::Abs(factor)*solenoidField*30000./5.;
44e45fac 1913 grpObj->SetL3Current(l3current,(AliGRPObject::Stats)0);
fc7e1b1c 1914
1915 if (factor > 0) {
44e45fac 1916 grpObj->SetL3Polarity(0);
1917 grpObj->SetDipolePolarity(0);
fc7e1b1c 1918 }
1919 else {
44e45fac 1920 grpObj->SetL3Polarity(1);
1921 grpObj->SetDipolePolarity(1);
fc7e1b1c 1922 }
1923
1924 if (TMath::Abs(factor) != 0)
44e45fac 1925 grpObj->SetDipoleCurrent(6000,(AliGRPObject::Stats)0);
fc7e1b1c 1926 else
44e45fac 1927 grpObj->SetDipoleCurrent(0,(AliGRPObject::Stats)0);
fc7e1b1c 1928
44e45fac 1929 grpObj->SetCavernTemperature(0,(AliGRPObject::Stats)0);
1930
1931 //grpMap->Add(new TObjString("fCavernPressure"),new TObjString("0")); ---> not inserted in simulation with the new object, since it is now an AliDCSSensor
fc7e1b1c 1932
1933 // Now store the entry in OCDB
1934 AliCDBManager* man = AliCDBManager::Instance();
1935
2b541cc7 1936 AliCDBId id("GRP/GRP/Data", man->GetRun(), man->GetRun(), 1, 1);
fc7e1b1c 1937 AliCDBMetaData *metadata= new AliCDBMetaData();
1938
fc7e1b1c 1939 metadata->SetResponsible("alice-off@cern.ch");
1940 metadata->SetComment("Automatically produced GRP entry for Monte Carlo");
1941
44e45fac 1942 man->Put(grpObj,id,metadata);
fc7e1b1c 1943}
44e45fac 1944
1945