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