]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/STEER/AliSimulation.cxx
Interface for setting up custom monitoring lib policy (Requested by Jens).
[u/mrichter/AliRoot.git] / STEER / 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
3e2e3ece 108#include <TCint.h>
109#include <TFile.h>
f7a1cc68 110#include <TGeoGlobalMagField.h>
e8d02863 111#include <TGeoManager.h>
85a5290f 112#include <TObjString.h>
fc7e1b1c 113#include <TROOT.h>
3e2e3ece 114#include <TSystem.h>
115#include <TVirtualMC.h>
116#include <TVirtualMCApplication.h>
5cf76849 117#include <TDatime.h>
85a5290f 118
3e2e3ece 119#include "AliAlignObj.h"
e8d02863 120#include "AliCDBEntry.h"
121#include "AliCDBManager.h"
732ee20f 122#include "AliGRPManager.h"
3e2e3ece 123#include "AliCDBStorage.h"
124#include "AliCTPRawData.h"
125#include "AliCentralTrigger.h"
e8d02863 126#include "AliCentralTrigger.h"
3e2e3ece 127#include "AliCodeTimer.h"
362c9d61 128#include "AliDAQ.h"
af7ba10c 129#include "AliDigitizer.h"
6d985680 130#include "AliESDEvent.h"
3e2e3ece 131#include "AliGRPObject.h"
132#include "AliGenEventHeader.h"
af7ba10c 133#include "AliGenerator.h"
3e2e3ece 134#include "AliGeomManager.h"
135#include "AliHLTSimulation.h"
136#include "AliHeader.h"
f7a1cc68 137#include "AliLego.h"
138#include "AliLegoGenerator.h"
e8d02863 139#include "AliLog.h"
3e2e3ece 140#include "AliMC.h"
141#include "AliMagF.h"
af7ba10c 142#include "AliModule.h"
3e2e3ece 143#include "AliPDG.h"
144#include "AliRawReaderDate.h"
145#include "AliRawReaderFile.h"
146#include "AliRawReaderRoot.h"
af7ba10c 147#include "AliRun.h"
148#include "AliRunDigitizer.h"
149#include "AliRunLoader.h"
150#include "AliSimulation.h"
27fc67fa 151#include "AliSysInfo.h"
3e2e3ece 152#include "AliVertexGenFile.h"
85a5290f 153
154ClassImp(AliSimulation)
155
b856a511 156AliSimulation *AliSimulation::fgInstance = 0;
820b4d9e 157const char* AliSimulation::fgkDetectorName[AliSimulation::fgkNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE"
158// #ifdef MFT_UPGRADE
159// ,"MFT"
160// #endif
161 ,"MFT" // AU
162 ,"HLT"
163};
85a5290f 164
165//_____________________________________________________________________________
795e4a22 166AliSimulation::AliSimulation(const char* configFileName,
95601147 167 const char* name, const char* title) :
168 TNamed(name, title),
169
170 fRunGeneration(kTRUE),
171 fRunSimulation(kTRUE),
024cf675 172 fLoadAlignFromCDB(kTRUE),
25be1e5c 173 fLoadAlObjsListOfDets("ALL"),
95601147 174 fMakeSDigits("ALL"),
175 fMakeDigits("ALL"),
28da60d3 176 fTriggerConfig(""),
95601147 177 fMakeDigitsFromHits(""),
0421c3d1 178 fWriteRawData(""),
06c7e392 179 fRawDataFileName(""),
180 fDeleteIntermediateFiles(kFALSE),
67327b72 181 fWriteSelRawData(kFALSE),
95601147 182 fStopOnError(kFALSE),
95601147 183 fNEvents(1),
184 fConfigFileName(configFileName),
185 fGAliceFileName("galice.root"),
18b43626 186 fEventsPerFile(),
95601147 187 fBkgrdFileNames(NULL),
995ad051 188 fAlignObjArray(NULL),
04bae0a0 189 fUseBkgrdVertex(kTRUE),
024cf675 190 fRegionOfInterest(kFALSE),
795e4a22 191 fCDBUri(""),
f1c1204d 192 fQARefUri(""),
ca30848f 193 fSpecCDBUri(),
795e4a22 194 fRun(-1),
195 fSeed(0),
196 fInitCDBCalled(kFALSE),
197 fInitRunNumberCalled(kFALSE),
198 fSetRunNumberFromDataCalled(kFALSE),
cae21299 199 fEmbeddingFlag(kFALSE),
3e2e3ece 200 fLego(NULL),
732ee20f 201 fKey(0),
202 fUseVertexFromCDB(0),
203 fUseMagFieldFromGRP(0),
204 fGRPWriteLocation(Form("local://%s", gSystem->pwd())),
475baf89 205 fUseTimeStampFromCDB(0),
206 fTimeStart(0),
207 fTimeEnd(0),
e3b20d9a 208 fQADetectors("ALL"),
209 fQATasks("ALL"),
759c1df1 210 fRunQA(kTRUE),
57acd2d2 211 fEventSpecie(AliRecoParam::kDefault),
75373542 212 fWriteQAExpertData(kTRUE),
1b1f5ae6 213 fGeometryFile(),
fc7e1b1c 214 fRunHLT("default"),
c4affb19 215 fpHLT(NULL),
fc7e1b1c 216 fWriteGRPEntry(kTRUE)
85a5290f 217{
218// create simulation object with default parameters
b856a511 219 fgInstance = this;
0421c3d1 220 SetGAliceFile("galice.root");
5b188f2f 221
222// for QA
634696f5 223 AliQAManager * qam = AliQAManager::QAManager(AliQAv1::kSIMMODE) ;
eca4fa66 224 qam->SetActiveDetectors(fQADetectors) ;
4e25ac79 225 fQATasks = Form("%d %d %d", AliQAv1::kHITS, AliQAv1::kSDIGITS, AliQAv1::kDIGITS) ;
eca4fa66 226 qam->SetTasks(fQATasks) ;
85a5290f 227}
228
85a5290f 229//_____________________________________________________________________________
230AliSimulation::~AliSimulation()
231{
232// clean up
233
18b43626 234 fEventsPerFile.Delete();
024cf675 235// if(fAlignObjArray) fAlignObjArray->Delete(); // fAlignObjArray->RemoveAll() ???
236// delete fAlignObjArray; fAlignObjArray=0;
18b43626 237
95601147 238 if (fBkgrdFileNames) {
239 fBkgrdFileNames->Delete();
240 delete fBkgrdFileNames;
241 }
ec92bee0 242
243 fSpecCDBUri.Delete();
b856a511 244 if (fgInstance==this) fgInstance = 0;
87932dab 245
bf76b847 246 AliQAManager::QAManager()->ShowQA() ;
247 AliQAManager::Destroy() ;
87932dab 248 AliCodeTimer::Instance()->Print();
85a5290f 249}
250
251
252//_____________________________________________________________________________
253void AliSimulation::SetNumberOfEvents(Int_t nEvents)
254{
255// set the number of events for one run
256
257 fNEvents = nEvents;
258}
259
f1c1204d 260//_____________________________________________________________________________
261void AliSimulation::InitQA()
262{
263 // activate a default CDB storage
264 // First check if we have any CDB storage set, because it is used
265 // to retrieve the calibration and alignment constants
266
267 if (fInitCDBCalled) return;
268 fInitCDBCalled = kTRUE;
269
634696f5 270 AliQAManager * qam = AliQAManager::QAManager(AliQAv1::kSIMMODE) ;
eca4fa66 271 qam->SetActiveDetectors(fQADetectors) ;
4e25ac79 272 fQATasks = Form("%d %d %d", AliQAv1::kHITS, AliQAv1::kSDIGITS, AliQAv1::kDIGITS) ;
eca4fa66 273 qam->SetTasks(fQATasks) ;
75373542 274 if (fWriteQAExpertData)
eca4fa66 275 qam->SetWriteExpert() ;
f1c1204d 276
eca4fa66 277 if (qam->IsDefaultStorageSet()) {
f1c1204d 278 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
279 AliWarning("Default QA reference storage has been already set !");
280 AliWarning(Form("Ignoring the default storage declared in AliSimulation: %s",fQARefUri.Data()));
281 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
eca4fa66 282 fQARefUri = qam->GetDefaultStorage()->GetURI();
f1c1204d 283 } else {
284 if (fQARefUri.Length() > 0) {
285 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
286 AliDebug(2, Form("Default QA reference storage is set to: %s", fQARefUri.Data()));
287 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
288 } else {
289 fQARefUri="local://$ALICE_ROOT/QARef";
290 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
291 AliWarning("Default QA reference storage not yet set !!!!");
292 AliWarning(Form("Setting it now to: %s", fQARefUri.Data()));
293 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
294 }
eca4fa66 295 qam->SetDefaultStorage(fQARefUri);
f1c1204d 296 }
297}
298
024cf675 299//_____________________________________________________________________________
795e4a22 300void AliSimulation::InitCDB()
024cf675 301{
302// activate a default CDB storage
303// First check if we have any CDB storage set, because it is used
304// to retrieve the calibration and alignment constants
305
795e4a22 306 if (fInitCDBCalled) return;
307 fInitCDBCalled = kTRUE;
308
024cf675 309 AliCDBManager* man = AliCDBManager::Instance();
ec92bee0 310 if (man->IsDefaultStorageSet())
024cf675 311 {
ec92bee0 312 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
313 AliWarning("Default CDB storage has been already set !");
ced249e6 314 AliWarning(Form("Ignoring the default storage declared in AliSimulation: %s",fCDBUri.Data()));
ec92bee0 315 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
795e4a22 316 fCDBUri = man->GetDefaultStorage()->GetURI();
ec92bee0 317 }
318 else {
795e4a22 319 if (fCDBUri.Length() > 0)
320 {
321 AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
322 AliDebug(2, Form("Default CDB storage is set to: %s", fCDBUri.Data()));
323 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
324 } else {
162637e4 325 fCDBUri="local://$ALICE_ROOT/OCDB";
795e4a22 326 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
327 AliWarning("Default CDB storage not yet set !!!!");
328 AliWarning(Form("Setting it now to: %s", fCDBUri.Data()));
329 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
330
331 }
ec92bee0 332 man->SetDefaultStorage(fCDBUri);
333 }
334
335 // Now activate the detector specific CDB storage locations
336 for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
337 TObject* obj = fSpecCDBUri[i];
338 if (!obj) continue;
b8ec52f6 339 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
340 AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
341 AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
c3a7b59a 342 man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
ec92bee0 343 }
795e4a22 344
024cf675 345}
346
347//_____________________________________________________________________________
795e4a22 348void AliSimulation::InitRunNumber(){
349// check run number. If not set, set it to 0 !!!!
350
351 if (fInitRunNumberCalled) return;
352 fInitRunNumberCalled = kTRUE;
353
354 AliCDBManager* man = AliCDBManager::Instance();
355 if (man->GetRun() >= 0)
356 {
8c8fbdcd 357 AliFatal(Form("Run number cannot be set in AliCDBManager before start of simulation: "
358 "Use external variable DC_RUN or AliSimulation::SetRun()!"));
795e4a22 359 }
8c8fbdcd 360
361 if(fRun >= 0) {
795e4a22 362 AliDebug(2, Form("Setting CDB run number to: %d",fRun));
8c8fbdcd 363 } else {
795e4a22 364 fRun=0;
76c8e8d6 365 AliWarning(Form("Run number not yet set !!!! Setting it now to: %d",
366 fRun));
795e4a22 367 }
8c8fbdcd 368 man->SetRun(fRun);
024cf675 369
795e4a22 370 man->Print();
024cf675 371
372}
373
00aa02d5 374//_____________________________________________________________________________
795e4a22 375void AliSimulation::SetCDBLock() {
376 // Set CDB lock: from now on it is forbidden to reset the run number
377 // or the default storage or to activate any further storage!
378
732ee20f 379 ULong_t key = AliCDBManager::Instance()->SetLock(1);
380 if (key) fKey = key;
795e4a22 381}
382
383//_____________________________________________________________________________
384void AliSimulation::SetDefaultStorage(const char* uri) {
f1c1204d 385 // Store the desired default CDB storage location
386 // Activate it later within the Run() method
387
795e4a22 388 fCDBUri = uri;
f1c1204d 389
390}
00aa02d5 391
f1c1204d 392//_____________________________________________________________________________
393void AliSimulation::SetQARefDefaultStorage(const char* uri) {
394 // Store the desired default CDB storage location
395 // Activate it later within the Run() method
396
397 fQARefUri = uri;
4e25ac79 398 AliQAv1::SetQARefStorage(fQARefUri.Data()) ;
00aa02d5 399}
400
024cf675 401//_____________________________________________________________________________
c3a7b59a 402void AliSimulation::SetSpecificStorage(const char* calibType, const char* uri) {
ec92bee0 403// Store a detector-specific CDB storage location
404// Activate it later within the Run() method
024cf675 405
c3a7b59a 406 AliCDBPath aPath(calibType);
407 if(!aPath.IsValid()){
408 AliError(Form("Not a valid path: %s", calibType));
409 return;
410 }
411
412 TObject* obj = fSpecCDBUri.FindObject(calibType);
ec92bee0 413 if (obj) fSpecCDBUri.Remove(obj);
c3a7b59a 414 fSpecCDBUri.Add(new TNamed(calibType, uri));
024cf675 415
416}
417
795e4a22 418//_____________________________________________________________________________
419void AliSimulation::SetRunNumber(Int_t run)
420{
421// sets run number
422// Activate it later within the Run() method
423
424 fRun = run;
425}
426
427//_____________________________________________________________________________
428void AliSimulation::SetSeed(Int_t seed)
429{
430// sets seed number
431// Activate it later within the Run() method
432
433 fSeed = seed;
434}
435
436//_____________________________________________________________________________
437Bool_t AliSimulation::SetRunNumberFromData()
438{
439 // Set the CDB manager run number
440 // The run number is retrieved from gAlice
441
442 if (fSetRunNumberFromDataCalled) return kTRUE;
443 fSetRunNumberFromDataCalled = kTRUE;
444
445 AliCDBManager* man = AliCDBManager::Instance();
446 Int_t runData = -1, runCDB = -1;
447
448 AliRunLoader* runLoader = LoadRun("READ");
449 if (!runLoader) return kFALSE;
450 else {
d3b3a3b2 451 runData = runLoader->GetHeader()->GetRun();
795e4a22 452 delete runLoader;
453 }
454
455 runCDB = man->GetRun();
456 if(runCDB >= 0) {
457 if (runCDB != runData) {
458 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
459 AliWarning(Form("A run number was previously set in AliCDBManager: %d !", runCDB));
460 AliWarning(Form("It will be replaced with the run number got from run header: %d !", runData));
461 AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
462 }
463
464 }
465
466 man->SetRun(runData);
467 fRun = runData;
468
469 if(man->GetRun() < 0) {
470 AliError("Run number not properly initalized!");
471 return kFALSE;
472 }
473
474 man->Print();
475
476 return kTRUE;
477}
478
85a5290f 479//_____________________________________________________________________________
480void AliSimulation::SetConfigFile(const char* fileName)
481{
482// set the name of the config file
483
484 fConfigFileName = fileName;
485}
486
0421c3d1 487//_____________________________________________________________________________
488void AliSimulation::SetGAliceFile(const char* fileName)
489{
490// set the name of the galice file
491// the path is converted to an absolute one if it is relative
492
493 fGAliceFileName = fileName;
494 if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
495 char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
496 fGAliceFileName);
497 fGAliceFileName = absFileName;
498 delete[] absFileName;
499 }
ac1671c0 500
501 AliDebug(2, Form("galice file name set to %s", fileName));
0421c3d1 502}
503
18b43626 504//_____________________________________________________________________________
505void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
506 Int_t nEvents)
507{
508// set the number of events per file for the given detector and data type
509// ("Hits", "Summable Digits", "Digits", "Reconstructed Points" or "Tracks")
510
511 TNamed* obj = new TNamed(detector, type);
512 obj->SetUniqueID(nEvents);
513 fEventsPerFile.Add(obj);
514}
515
6bae477a 516//_____________________________________________________________________________
517Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
518{
519 // Read the alignment objects from CDB.
520 // Each detector is supposed to have the
521 // alignment objects in DET/Align/Data CDB path.
522 // All the detector objects are then collected,
523 // sorted by geometry level (starting from ALIC) and
524 // then applied to the TGeo geometry.
525 // Finally an overlaps check is performed.
526
74ae4297 527 if (!AliGeomManager::GetGeometry() || !AliGeomManager::GetGeometry()->IsClosed()) {
528 AliError("Can't apply the misalignment! Geometry is not loaded or it is still opened!");
0717eed2 529 return kFALSE;
530 }
795e4a22 531
532 // initialize CDB storage, run number, set CDB lock
533 InitCDB();
534// if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
535 SetCDBLock();
536
6bae477a 537 Bool_t delRunLoader = kFALSE;
538 if (!runLoader) {
539 runLoader = LoadRun("READ");
540 if (!runLoader) return kFALSE;
541 delRunLoader = kTRUE;
542 }
795e4a22 543
b856a511 544 // Export ideal geometry
d97f1dbe 545 if(!IsGeometryFromFile()) AliGeomManager::GetGeometry()->Export("geometry.root");
b856a511 546
25be1e5c 547 // Load alignment data from CDB and apply to geometry through AliGeomManager
6bae477a 548 if(fLoadAlignFromCDB){
25be1e5c 549
550 TString detStr = fLoadAlObjsListOfDets;
551 TString loadAlObjsListOfDets = "";
552
553 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
554 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
555 AliModule* det = (AliModule*) detArray->At(iDet);
556 if (!det || !det->IsActive()) continue;
557 if (IsSelected(det->GetName(), detStr)) {
0717eed2 558 //add det to list of dets to be aligned from CDB
559 loadAlObjsListOfDets += det->GetName();
560 loadAlObjsListOfDets += " ";
25be1e5c 561 }
562 } // end loop over detectors
53dd3c3d 563 loadAlObjsListOfDets.Prepend("GRP "); //add alignment objects for non-sensitive modules
45daac75 564 AliGeomManager::ApplyAlignObjsFromCDB(loadAlObjsListOfDets.Data());
25be1e5c 565 }else{
566 // Check if the array with alignment objects was
567 // provided by the user. If yes, apply the objects
568 // to the present TGeo geometry
569 if (fAlignObjArray) {
45daac75 570 if (AliGeomManager::ApplyAlignObjsToGeom(*fAlignObjArray) == kFALSE) {
0717eed2 571 AliError("The misalignment of one or more volumes failed!"
572 "Compare the list of simulated detectors and the list of detector alignment data!");
573 if (delRunLoader) delete runLoader;
574 return kFALSE;
6bae477a 575 }
576 }
6bae477a 577 }
578
ef7ff1f9 579 // Update the internal geometry of modules (ITS needs it)
25be1e5c 580 TString detStr = fLoadAlObjsListOfDets;
ef7ff1f9 581 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
582 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
583
584 AliModule* det = (AliModule*) detArray->At(iDet);
585 if (!det || !det->IsActive()) continue;
586 if (IsSelected(det->GetName(), detStr)) {
587 det->UpdateInternalGeometry();
588 }
589 } // end loop over detectors
590
591
6bae477a 592 if (delRunLoader) delete runLoader;
593
594 return kTRUE;
595}
596
85a5290f 597//_____________________________________________________________________________
598void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
599{
600// add a file with background events for merging
601
602 TObjString* fileNameStr = new TObjString(fileName);
603 fileNameStr->SetUniqueID(nSignalPerBkgrd);
95601147 604 if (!fBkgrdFileNames) fBkgrdFileNames = new TObjArray;
85a5290f 605 fBkgrdFileNames->Add(fileNameStr);
606}
607
ca30848f 608void AliSimulation::EmbedInto(const char* fileName, Int_t nSignalPerBkgrd)
609{
610// add a file with background events for embeddin
611 MergeWith(fileName, nSignalPerBkgrd);
612 fEmbeddingFlag = kTRUE;
613}
85a5290f 614
615//_____________________________________________________________________________
616Bool_t AliSimulation::Run(Int_t nEvents)
617{
618// run the generation, simulation and digitization
619
c65c502a 620
99c136e1 621 AliCodeTimerAuto("",0)
6fcf567c 622 AliSysInfo::AddStamp("Start_Run");
87932dab 623
795e4a22 624 // Load run number and seed from environmental vars
625 ProcessEnvironmentVars();
6fcf567c 626 AliSysInfo::AddStamp("ProcessEnvironmentVars");
b0314964 627
795e4a22 628 gRandom->SetSeed(fSeed);
629
85a5290f 630 if (nEvents > 0) fNEvents = nEvents;
631
c4affb19 632 // create and setup the HLT instance
633 if (!fRunHLT.IsNull() && !CreateHLT()) {
634 if (fStopOnError) return kFALSE;
635 // disable HLT
636 fRunHLT="";
637 }
c516e34c 638
85a5290f 639 // generation and simulation -> hits
640 if (fRunGeneration) {
85a5290f 641 if (!RunSimulation()) if (fStopOnError) return kFALSE;
642 }
6fcf567c 643 AliSysInfo::AddStamp("RunSimulation");
795e4a22 644
645 // initialize CDB storage from external environment
646 // (either CDB manager or AliSimulation setters),
647 // if not already done in RunSimulation()
648 InitCDB();
6fcf567c 649 AliSysInfo::AddStamp("InitCDB");
795e4a22 650
651 // Set run number in CDBManager from data
652 // From this point on the run number must be always loaded from data!
653 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
654
655 // Set CDB lock: from now on it is forbidden to reset the run number
656 // or the default storage or to activate any further storage!
657 SetCDBLock();
6bae477a 658
25be1e5c 659 // If RunSimulation was not called, load the geometry and misalign it
74ae4297 660 if (!AliGeomManager::GetGeometry()) {
661 // Initialize the geometry manager
1b1f5ae6 662 AliGeomManager::LoadGeometry("geometry.root");
2eab8cf7 663 AliSysInfo::AddStamp("GetGeometry");
ff5970a3 664// // Check that the consistency of symbolic names for the activated subdetectors
665// // in the geometry loaded by AliGeomManager
666// AliRunLoader* runLoader = LoadRun("READ");
667// if (!runLoader) return kFALSE;
668//
669// TString detsToBeChecked = "";
670// TObjArray* detArray = runLoader->GetAliRun()->Detectors();
671// for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
672// AliModule* det = (AliModule*) detArray->At(iDet);
673// if (!det || !det->IsActive()) continue;
674// detsToBeChecked += det->GetName();
675// detsToBeChecked += " ";
676// } // end loop over detectors
677// if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
678 if(!AliGeomManager::CheckSymNamesLUT("ALL"))
0bf7aade 679 AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
ff5970a3 680
74ae4297 681 if (!AliGeomManager::GetGeometry()) if (fStopOnError) return kFALSE;
682 // Misalign geometry
25be1e5c 683 if(!MisalignGeometry()) if (fStopOnError) return kFALSE;
6bae477a 684 }
6fcf567c 685 AliSysInfo::AddStamp("MissalignGeometry");
25be1e5c 686
27fc67fa 687
85a5290f 688 // hits -> summable digits
27fc67fa 689 AliSysInfo::AddStamp("Start_sdigitization");
85a5290f 690 if (!fMakeSDigits.IsNull()) {
691 if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
a1b90a73 692
85a5290f 693 }
27fc67fa 694 AliSysInfo::AddStamp("Stop_sdigitization");
c65c502a 695
27fc67fa 696 AliSysInfo::AddStamp("Start_digitization");
795e4a22 697 // summable digits -> digits
85a5290f 698 if (!fMakeDigits.IsNull()) {
699 if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
700 if (fStopOnError) return kFALSE;
701 }
a1b90a73 702 }
27fc67fa 703 AliSysInfo::AddStamp("Stop_digitization");
85a5290f 704
795e4a22 705
706
85a5290f 707 // hits -> digits
708 if (!fMakeDigitsFromHits.IsNull()) {
95601147 709 if (fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
ac1671c0 710 AliWarning(Form("Merging and direct creation of digits from hits "
711 "was selected for some detectors. "
712 "No merging will be done for the following detectors: %s",
713 fMakeDigitsFromHits.Data()));
85a5290f 714 }
715 if (!RunHitsDigitization(fMakeDigitsFromHits)) {
716 if (fStopOnError) return kFALSE;
717 }
718 }
719
6fcf567c 720 AliSysInfo::AddStamp("Hits2Digits");
795e4a22 721
722
a5a091ce 723 // digits -> trigger
4471cf15 724 if (!fTriggerConfig.IsNull() && !RunTrigger(fTriggerConfig,fMakeDigits)) {
087426de 725 if (fStopOnError) return kFALSE;
a5a091ce 726 }
727
6fcf567c 728 AliSysInfo::AddStamp("RunTrigger");
795e4a22 729
730
0421c3d1 731 // digits -> raw data
732 if (!fWriteRawData.IsNull()) {
06c7e392 733 if (!WriteRawData(fWriteRawData, fRawDataFileName,
67327b72 734 fDeleteIntermediateFiles,fWriteSelRawData)) {
0421c3d1 735 if (fStopOnError) return kFALSE;
736 }
737 }
738
6fcf567c 739 AliSysInfo::AddStamp("WriteRaw");
795e4a22 740
5f6d0c0d 741 // run HLT simulation on simulated digit data if raw data is not
742 // simulated, otherwise its called as part of WriteRawData
743 if (!fRunHLT.IsNull() && fWriteRawData.IsNull()) {
cae21299 744 if (!RunHLT()) {
745 if (fStopOnError) return kFALSE;
746 }
747 }
6fcf567c 748
749 AliSysInfo::AddStamp("RunHLT");
795e4a22 750
04236e67 751 //QA
752 if (fRunQA) {
753 Bool_t rv = RunQA() ;
754 if (!rv)
755 if (fStopOnError)
756 return kFALSE ;
757 }
795e4a22 758
6fcf567c 759 AliSysInfo::AddStamp("RunQA");
760
795e4a22 761 // Cleanup of CDB manager: cache and active storages!
762 AliCDBManager::Instance()->ClearCache();
763
85a5290f 764 return kTRUE;
765}
766
3e2e3ece 767//_______________________________________________________________________
768Bool_t AliSimulation::RunLego(const char *setup, Int_t nc1, Float_t c1min,
769 Float_t c1max,Int_t nc2,Float_t c2min,Float_t c2max,
770 Float_t rmin,Float_t rmax,Float_t zmax, AliLegoGenerator* gener, Int_t nev)
771{
772 //
773 // Generates lego plots of:
774 // - radiation length map phi vs theta
775 // - radiation length map phi vs eta
776 // - interaction length map
777 // - g/cm2 length map
778 //
779 // ntheta bins in theta, eta
780 // themin minimum angle in theta (degrees)
781 // themax maximum angle in theta (degrees)
782 // nphi bins in phi
783 // phimin minimum angle in phi (degrees)
784 // phimax maximum angle in phi (degrees)
785 // rmin minimum radius
786 // rmax maximum radius
787 //
788 //
789 // The number of events generated = ntheta*nphi
790 // run input parameters in macro setup (default="Config.C")
791 //
792 // Use macro "lego.C" to visualize the 3 lego plots in spherical coordinates
793 //Begin_Html
794 /*
795 <img src="picts/AliRunLego1.gif">
796 */
797 //End_Html
798 //Begin_Html
799 /*
800 <img src="picts/AliRunLego2.gif">
801 */
802 //End_Html
803 //Begin_Html
804 /*
805 <img src="picts/AliRunLego3.gif">
806 */
807 //End_Html
808 //
809
810// run the generation and simulation
811
99c136e1 812 AliCodeTimerAuto("",0)
3e2e3ece 813
814 // initialize CDB storage and run number from external environment
815 // (either CDB manager or AliSimulation setters)
816 InitCDB();
817 InitRunNumber();
818 SetCDBLock();
819
820 if (!gAlice) {
821 AliError("no gAlice object. Restart aliroot and try again.");
822 return kFALSE;
823 }
824 if (gAlice->Modules()->GetEntries() > 0) {
825 AliError("gAlice was already run. Restart aliroot and try again.");
826 return kFALSE;
827 }
828
829 AliInfo(Form("initializing gAlice with config file %s",
830 fConfigFileName.Data()));
831
832 // Number of events
833 if (nev == -1) nev = nc1 * nc2;
834
835 // check if initialisation has been done
836 // If runloader has been initialized, set the number of events per file to nc1 * nc2
837
838 // Set new generator
839 if (!gener) gener = new AliLegoGenerator();
840 //
841 // Configure Generator
842
843 gener->SetRadiusRange(rmin, rmax);
844 gener->SetZMax(zmax);
845 gener->SetCoor1Range(nc1, c1min, c1max);
846 gener->SetCoor2Range(nc2, c2min, c2max);
847
848
849 //Create Lego object
850 fLego = new AliLego("lego",gener);
851
852 //__________________________________________________________________________
853
854 gAlice->Announce();
855
856 gROOT->LoadMacro(setup);
857 gInterpreter->ProcessLine(gAlice->GetConfigFunction());
858
859 if(AliCDBManager::Instance()->GetRun() >= 0) {
860 SetRunNumber(AliCDBManager::Instance()->GetRun());
861 } else {
862 AliWarning("Run number not initialized!!");
863 }
f7a1cc68 864
33c3c91a 865 AliRunLoader::Instance()->CdGAFile();
3e2e3ece 866
867 AliPDG::AddParticlesToPdgDataBase();
868
f7a1cc68 869 gMC->SetMagField(TGeoGlobalMagField::Instance()->GetField());
870
55338a9a 871 gAlice->GetMCApp()->Init();
872
f7a1cc68 873
3e2e3ece 874 //Must be here because some MCs (G4) adds detectors here and not in Config.C
875 gAlice->InitLoaders();
33c3c91a 876 AliRunLoader::Instance()->MakeTree("E");
3e2e3ece 877
878 //
879 // Save stuff at the beginning of the file to avoid file corruption
33c3c91a 880 AliRunLoader::Instance()->CdGAFile();
3e2e3ece 881 gAlice->Write();
882
883 //Save current generator
884 AliGenerator *gen=gAlice->GetMCApp()->Generator();
885 gAlice->GetMCApp()->ResetGenerator(gener);
886 //Prepare MC for Lego Run
887 gMC->InitLego();
888
889 //Run Lego Object
890
891
33c3c91a 892 AliRunLoader::Instance()->SetNumberOfEventsPerFile(nev);
3e2e3ece 893 gMC->ProcessRun(nev);
894
895 // End of this run, close files
896 FinishRun();
897 // Restore current generator
898 gAlice->GetMCApp()->ResetGenerator(gen);
899 // Delete Lego Object
900 delete fLego;
901
902 return kTRUE;
903}
904
a5a091ce 905//_____________________________________________________________________________
8480396b 906Bool_t AliSimulation::RunTrigger(const char* config, const char* detectors)
a5a091ce 907{
908 // run the trigger
909
99c136e1 910 AliCodeTimerAuto("",0)
a5a091ce 911
795e4a22 912 // initialize CDB storage from external environment
913 // (either CDB manager or AliSimulation setters),
914 // if not already done in RunSimulation()
915 InitCDB();
916
917 // Set run number in CDBManager from data
918 // From this point on the run number must be always loaded from data!
919 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
920
921 // Set CDB lock: from now on it is forbidden to reset the run number
922 // or the default storage or to activate any further storage!
923 SetCDBLock();
924
bacbe0fd 925 AliRunLoader* runLoader = LoadRun("READ");
926 if (!runLoader) return kFALSE;
51f6d619 927 TString trconfiguration = config;
bacbe0fd 928
51f6d619 929 if (trconfiguration.IsNull()) {
28da60d3 930 if(!fTriggerConfig.IsNull()) {
931 trconfiguration = fTriggerConfig;
087426de 932 }
51f6d619 933 else
8480396b 934 AliWarning("No trigger descriptor is specified. Loading the one that is in the CDB.");
087426de 935 }
936
0f46f5fa 937 runLoader->MakeTree( "GG" );
bacbe0fd 938 AliCentralTrigger* aCTP = runLoader->GetTrigger();
51f6d619 939 // Load Configuration
940 if (!aCTP->LoadConfiguration( trconfiguration ))
941 return kFALSE;
a5a091ce 942
51f6d619 943 // digits -> trigger
8480396b 944 if( !aCTP->RunTrigger( runLoader , detectors ) ) {
a5a091ce 945 if (fStopOnError) {
51f6d619 946 // delete aCTP;
947 return kFALSE;
a5a091ce 948 }
bacbe0fd 949 }
a5a091ce 950
bacbe0fd 951 delete runLoader;
a5a091ce 952
bacbe0fd 953 return kTRUE;
a5a091ce 954}
955
007155cc 956//_____________________________________________________________________________
957Bool_t AliSimulation::WriteTriggerRawData()
958{
959 // Writes the CTP (trigger) DDL raw data
960 // Details of the format are given in the
961 // trigger TDR - pages 134 and 135.
962 AliCTPRawData writer;
963 writer.RawData();
a5a091ce 964
007155cc 965 return kTRUE;
966}
a5a091ce 967
85a5290f 968//_____________________________________________________________________________
95601147 969Bool_t AliSimulation::RunSimulation(Int_t nEvents)
85a5290f 970{
971// run the generation and simulation
972
99c136e1 973 AliCodeTimerAuto("",0)
4df28b43 974
795e4a22 975 // initialize CDB storage and run number from external environment
976 // (either CDB manager or AliSimulation setters)
6fcf567c 977 AliSysInfo::AddStamp("RunSimulation_Begin");
795e4a22 978 InitCDB();
6fcf567c 979 AliSysInfo::AddStamp("RunSimulation_InitCDB");
795e4a22 980 InitRunNumber();
981 SetCDBLock();
6fcf567c 982 AliSysInfo::AddStamp("RunSimulation_SetCDBLock");
795e4a22 983
95601147 984 if (!gAlice) {
ac1671c0 985 AliError("no gAlice object. Restart aliroot and try again.");
95601147 986 return kFALSE;
987 }
988 if (gAlice->Modules()->GetEntries() > 0) {
ac1671c0 989 AliError("gAlice was already run. Restart aliroot and try again.");
95601147 990 return kFALSE;
991 }
992
ac1671c0 993 AliInfo(Form("initializing gAlice with config file %s",
994 fConfigFileName.Data()));
3e2e3ece 995
996 //
997 // Initialize ALICE Simulation run
998 //
3e2e3ece 999 gAlice->Announce();
1000
732ee20f 1001 //
1002 // If requested set the mag. field from the GRP entry.
1003 // After this the field is loccked and cannot be changed by Config.C
1004 if (fUseMagFieldFromGRP) {
1005 AliGRPManager grpM;
1006 grpM.ReadGRPEntry();
1007 grpM.SetMagField();
1008 AliInfo("Field is locked now. It cannot be changed in Config.C");
1009 }
1010//
1011// Execute Config.C
3e2e3ece 1012 gROOT->LoadMacro(fConfigFileName.Data());
1013 gInterpreter->ProcessLine(gAlice->GetConfigFunction());
6fcf567c 1014 AliSysInfo::AddStamp("RunSimulation_Config");
3e2e3ece 1015
732ee20f 1016//
1017// If requested obtain the vertex position and vertex sigma_z from the CDB
1018// This overwrites the settings from the Config.C
1019 if (fUseVertexFromCDB) {
1020 Double_t vtxPos[3] = {0., 0., 0.};
1021 Double_t vtxSig[3] = {0., 0., 0.};
b71fc657 1022 AliCDBEntry* entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertex");
58c338d2 1023 if (entry) {
1024 AliESDVertex* vertex = dynamic_cast<AliESDVertex*> (entry->GetObject());
1025 if (vertex) {
1026 if(vertex->GetXRes()>2.8) { // > pipe radius --> it's a dummy object, don't use it
1027 entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertexSPD");
c134270c 1028 if (entry) vertex = dynamic_cast<AliESDVertex*> (entry->GetObject());
58c338d2 1029 }
1030 }
1031 if (vertex) {
1032 vertex->GetXYZ(vtxPos);
1033 vertex->GetSigmaXYZ(vtxSig);
1034 AliInfo("Overwriting Config.C vertex settings !");
1035 AliInfo(Form("Vertex position from OCDB entry: x = %13.3f, y = %13.3f, z = %13.3f (sigma = %13.3f)\n",
1036 vtxPos[0], vtxPos[1], vtxPos[2], vtxSig[2]));
1037
1038 AliGenerator *gen = gAlice->GetMCApp()->Generator();
1039 gen->SetOrigin(vtxPos[0], vtxPos[1], vtxPos[2]); // vertex position
1040 gen->SetSigmaZ(vtxSig[2]);
766f0285 1041 }
732ee20f 1042 }
1043 }
475baf89 1044
1045 // If requested we take the SOR and EOR time-stamps from the GRP and use them
1046 // in order to generate the event time-stamps
1047 if (fUseTimeStampFromCDB) {
1048 AliGRPManager grpM;
1049 grpM.ReadGRPEntry();
1050 const AliGRPObject *grpObj = grpM.GetGRPData();
1051 if (!grpObj || (grpObj->GetTimeEnd() <= grpObj->GetTimeStart())) {
1052 AliError("Missing GRP or bad SOR/EOR time-stamps! Switching off the time-stamp generation from GRP!");
1053 fTimeStart = fTimeEnd = 0;
1054 fUseTimeStampFromCDB = kFALSE;
1055 }
1056 else {
1057 fTimeStart = grpObj->GetTimeStart();
1058 fTimeEnd = grpObj->GetTimeEnd();
1059 }
1060 }
732ee20f 1061
3e2e3ece 1062 if(AliCDBManager::Instance()->GetRun() >= 0) {
33c3c91a 1063 AliRunLoader::Instance()->SetRunNumber(AliCDBManager::Instance()->GetRun());
c516e34c 1064 AliRunLoader::Instance()->SetNumberOfEventsPerRun(fNEvents);
3e2e3ece 1065 } else {
1066 AliWarning("Run number not initialized!!");
1067 }
1068
33c3c91a 1069 AliRunLoader::Instance()->CdGAFile();
732ee20f 1070
1071
3e2e3ece 1072 AliPDG::AddParticlesToPdgDataBase();
1073
f7a1cc68 1074 gMC->SetMagField(TGeoGlobalMagField::Instance()->GetField());
6fcf567c 1075 AliSysInfo::AddStamp("RunSimulation_GetField");
3e2e3ece 1076
55338a9a 1077 gAlice->GetMCApp()->Init();
1078 AliSysInfo::AddStamp("RunSimulation_InitMCApp");
1079
3e2e3ece 1080 //Must be here because some MCs (G4) adds detectors here and not in Config.C
1081 gAlice->InitLoaders();
33c3c91a 1082 AliRunLoader::Instance()->MakeTree("E");
1083 AliRunLoader::Instance()->LoadKinematics("RECREATE");
1084 AliRunLoader::Instance()->LoadTrackRefs("RECREATE");
1085 AliRunLoader::Instance()->LoadHits("all","RECREATE");
3e2e3ece 1086 //
1087 // Save stuff at the beginning of the file to avoid file corruption
33c3c91a 1088 AliRunLoader::Instance()->CdGAFile();
3e2e3ece 1089 gAlice->Write();
1090 gAlice->SetEventNrInRun(-1); //important - we start Begin event from increasing current number in run
6fcf567c 1091 AliSysInfo::AddStamp("RunSimulation_InitLoaders");
3e2e3ece 1092 //___________________________________________________________________________________________
795e4a22 1093
6fcf567c 1094 AliSysInfo::AddStamp("RunSimulation_TriggerDescriptor");
087426de 1095
6bae477a 1096 // Set run number in CDBManager
024cf675 1097 AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
1098
33c3c91a 1099 AliRunLoader* runLoader = AliRunLoader::Instance();
024cf675 1100 if (!runLoader) {
1101 AliError(Form("gAlice has no run loader object. "
1102 "Check your config file: %s", fConfigFileName.Data()));
1103 return kFALSE;
1104 }
1105 SetGAliceFile(runLoader->GetFileName());
795e4a22 1106
6bae477a 1107 // Misalign geometry
caf9c464 1108#if ROOT_VERSION_CODE < 331527
74ae4297 1109 AliGeomManager::SetGeometry(gGeoManager);
ff5970a3 1110
1111 // Check that the consistency of symbolic names for the activated subdetectors
1112 // in the geometry loaded by AliGeomManager
1113 TString detsToBeChecked = "";
1114 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1115 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1116 AliModule* det = (AliModule*) detArray->At(iDet);
1117 if (!det || !det->IsActive()) continue;
1118 detsToBeChecked += det->GetName();
1119 detsToBeChecked += " ";
1120 } // end loop over detectors
1121 if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
0bf7aade 1122 AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
caf9c464 1123 MisalignGeometry(runLoader);
6fcf567c 1124 AliSysInfo::AddStamp("RunSimulation_MisalignGeometry");
caf9c464 1125#endif
995ad051 1126
33c3c91a 1127// AliRunLoader* runLoader = AliRunLoader::Instance();
024cf675 1128// if (!runLoader) {
1129// AliError(Form("gAlice has no run loader object. "
1130// "Check your config file: %s", fConfigFileName.Data()));
1131// return kFALSE;
1132// }
1133// SetGAliceFile(runLoader->GetFileName());
85a5290f 1134
3e2e3ece 1135 if (!gAlice->GetMCApp()->Generator()) {
ac1671c0 1136 AliError(Form("gAlice has no generator object. "
1137 "Check your config file: %s", fConfigFileName.Data()));
04bae0a0 1138 return kFALSE;
1139 }
fc7e1b1c 1140
1141 // Write GRP entry corresponding to the setting found in Cofig.C
1142 if (fWriteGRPEntry)
1143 WriteGRPEntry();
6fcf567c 1144 AliSysInfo::AddStamp("RunSimulation_WriteGRP");
fc7e1b1c 1145
05526d44 1146 if (nEvents <= 0) nEvents = fNEvents;
04bae0a0 1147
1148 // get vertex from background file in case of merging
1149 if (fUseBkgrdVertex &&
1150 fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
05526d44 1151 Int_t signalPerBkgrd = GetNSignalPerBkgrd(nEvents);
04bae0a0 1152 const char* fileName = ((TObjString*)
1153 (fBkgrdFileNames->At(0)))->GetName();
ac1671c0 1154 AliInfo(Form("The vertex will be taken from the background "
1155 "file %s with nSignalPerBackground = %d",
1156 fileName, signalPerBkgrd));
04bae0a0 1157 AliVertexGenFile* vtxGen = new AliVertexGenFile(fileName, signalPerBkgrd);
3e2e3ece 1158 gAlice->GetMCApp()->Generator()->SetVertexGenerator(vtxGen);
04bae0a0 1159 }
1160
85a5290f 1161 if (!fRunSimulation) {
3e2e3ece 1162 gAlice->GetMCApp()->Generator()->SetTrackingFlag(0);
85a5290f 1163 }
1164
18b43626 1165 // set the number of events per file for given detectors and data types
1166 for (Int_t i = 0; i < fEventsPerFile.GetEntriesFast(); i++) {
1167 if (!fEventsPerFile[i]) continue;
1168 const char* detName = fEventsPerFile[i]->GetName();
1169 const char* typeName = fEventsPerFile[i]->GetTitle();
1170 TString loaderName(detName);
1171 loaderName += "Loader";
1172 AliLoader* loader = runLoader->GetLoader(loaderName);
1173 if (!loader) {
65b25288 1174 AliError(Form("RunSimulation no loader for %s found\n Number of events per file not set for %s %s",
ac1671c0 1175 detName, typeName, detName));
18b43626 1176 continue;
1177 }
1178 AliDataLoader* dataLoader =
1179 loader->GetDataLoader(typeName);
1180 if (!dataLoader) {
ac1671c0 1181 AliError(Form("no data loader for %s found\n"
1182 "Number of events per file not set for %s %s",
1183 typeName, detName, typeName));
18b43626 1184 continue;
1185 }
1186 dataLoader->SetNumberOfEventsPerFile(fEventsPerFile[i]->GetUniqueID());
ac1671c0 1187 AliDebug(1, Form("number of events per file set to %d for %s %s",
1188 fEventsPerFile[i]->GetUniqueID(), detName, typeName));
18b43626 1189 }
1190
ac1671c0 1191 AliInfo("running gAlice");
6fcf567c 1192 AliSysInfo::AddStamp("Start_ProcessRun");
124253ee 1193
124253ee 1194 // Create the Root Tree with one branch per detector
1195 //Hits moved to begin event -> now we are crating separate tree for each event
1196
1197 gMC->ProcessRun(nEvents);
1198
1199 // End of this run, close files
3e2e3ece 1200 if(nEvents>0) FinishRun();
124253ee 1201
6fcf567c 1202 AliSysInfo::AddStamp("Stop_ProcessRun");
95601147 1203 delete runLoader;
85a5290f 1204
1205 return kTRUE;
1206}
1207
1208//_____________________________________________________________________________
95601147 1209Bool_t AliSimulation::RunSDigitization(const char* detectors)
85a5290f 1210{
1211// run the digitization and produce summable digits
27fc67fa 1212 static Int_t eventNr=0;
99c136e1 1213 AliCodeTimerAuto("",0) ;
4df28b43 1214
795e4a22 1215 // initialize CDB storage, run number, set CDB lock
1216 InitCDB();
1217 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1218 SetCDBLock();
1219
95601147 1220 AliRunLoader* runLoader = LoadRun();
1221 if (!runLoader) return kFALSE;
1222
85a5290f 1223 TString detStr = detectors;
95601147 1224 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 1225 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1226 AliModule* det = (AliModule*) detArray->At(iDet);
1227 if (!det || !det->IsActive()) continue;
1228 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 1229 AliInfo(Form("creating summable digits for %s", det->GetName()));
a899e40b 1230 AliCodeTimerStart(Form("creating summable digits for %s", det->GetName()));
85a5290f 1231 det->Hits2SDigits();
a899e40b 1232 AliCodeTimerStop(Form("creating summable digits for %s", det->GetName()));
27fc67fa 1233 AliSysInfo::AddStamp(Form("Digit_%s_%d",det->GetName(),eventNr), 0,1, eventNr);
85a5290f 1234 }
1235 }
1236
1237 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 1238 AliError(Form("the following detectors were not found: %s",
1239 detStr.Data()));
85a5290f 1240 if (fStopOnError) return kFALSE;
1241 }
27fc67fa 1242 eventNr++;
95601147 1243 delete runLoader;
1244
85a5290f 1245 return kTRUE;
1246}
1247
1248
1249//_____________________________________________________________________________
95601147 1250Bool_t AliSimulation::RunDigitization(const char* detectors,
1251 const char* excludeDetectors)
85a5290f 1252{
1253// run the digitization and produce digits from sdigits
1254
99c136e1 1255 AliCodeTimerAuto("",0)
4df28b43 1256
795e4a22 1257 // initialize CDB storage, run number, set CDB lock
1258 InitCDB();
1259 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1260 SetCDBLock();
1261
33c3c91a 1262 delete AliRunLoader::Instance();
124253ee 1263 delete gAlice;
95601147 1264 gAlice = NULL;
1265
1266 Int_t nStreams = 1;
05526d44 1267 if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
1268 Int_t signalPerBkgrd = GetNSignalPerBkgrd();
85a5290f 1269 AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
ca30848f 1270 // manager->SetEmbeddingFlag(fEmbeddingFlag);
85a5290f 1271 manager->SetInputStream(0, fGAliceFileName.Data());
1272 for (Int_t iStream = 1; iStream < nStreams; iStream++) {
1273 const char* fileName = ((TObjString*)
1274 (fBkgrdFileNames->At(iStream-1)))->GetName();
1275 manager->SetInputStream(iStream, fileName);
1276 }
1277
1278 TString detStr = detectors;
1279 TString detExcl = excludeDetectors;
95601147 1280 manager->GetInputStream(0)->ImportgAlice();
1281 AliRunLoader* runLoader =
1282 AliRunLoader::GetRunLoader(manager->GetInputStream(0)->GetFolderName());
1283 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 1284 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1285 AliModule* det = (AliModule*) detArray->At(iDet);
1286 if (!det || !det->IsActive()) continue;
1287 if (IsSelected(det->GetName(), detStr) &&
1288 !IsSelected(det->GetName(), detExcl)) {
4df28b43 1289 AliDigitizer* digitizer = det->CreateDigitizer(manager);
ca30848f 1290
4df28b43 1291 if (!digitizer) {
ac1671c0 1292 AliError(Form("no digitizer for %s", det->GetName()));
85a5290f 1293 if (fStopOnError) return kFALSE;
4df28b43 1294 } else {
1295 digitizer->SetRegionOfInterest(fRegionOfInterest);
85a5290f 1296 }
1297 }
1298 }
1299
1300 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 1301 AliError(Form("the following detectors were not found: %s",
1302 detStr.Data()));
85a5290f 1303 if (fStopOnError) return kFALSE;
1304 }
1305
1306 if (!manager->GetListOfTasks()->IsEmpty()) {
ac1671c0 1307 AliInfo("executing digitization");
85a5290f 1308 manager->Exec("");
1309 }
95601147 1310
85a5290f 1311 delete manager;
1312
1313 return kTRUE;
1314}
1315
1316//_____________________________________________________________________________
95601147 1317Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
85a5290f 1318{
1319// run the digitization and produce digits from hits
1320
99c136e1 1321 AliCodeTimerAuto("",0)
4df28b43 1322
795e4a22 1323 // initialize CDB storage, run number, set CDB lock
1324 InitCDB();
1325 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
1326 SetCDBLock();
1327
591f51bb 1328 AliRunLoader* runLoader = LoadRun("READ");
95601147 1329 if (!runLoader) return kFALSE;
1330
85a5290f 1331 TString detStr = detectors;
95601147 1332 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
85a5290f 1333 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1334 AliModule* det = (AliModule*) detArray->At(iDet);
1335 if (!det || !det->IsActive()) continue;
1336 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 1337 AliInfo(Form("creating digits from hits for %s", det->GetName()));
85a5290f 1338 det->Hits2Digits();
1339 }
1340 }
1341
1342 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 1343 AliError(Form("the following detectors were not found: %s",
1344 detStr.Data()));
85a5290f 1345 if (fStopOnError) return kFALSE;
1346 }
1347
1348 return kTRUE;
1349}
1350
0421c3d1 1351//_____________________________________________________________________________
06c7e392 1352Bool_t AliSimulation::WriteRawData(const char* detectors,
1353 const char* fileName,
67327b72 1354 Bool_t deleteIntermediateFiles,
1355 Bool_t selrawdata)
0421c3d1 1356{
1357// convert the digits to raw data
06c7e392 1358// First DDL raw data files for the given detectors are created.
1359// If a file name is given, the DDL files are then converted to a DATE file.
1360// If deleteIntermediateFiles is true, the DDL raw files are deleted
1361// afterwards.
1362// If the file name has the extension ".root", the DATE file is converted
1363// to a root file.
1364// If deleteIntermediateFiles is true, the DATE file is deleted afterwards.
67327b72 1365// 'selrawdata' flag can be used to enable writing of detectors raw data
1366// accoring to the trigger cluster.
0421c3d1 1367
99c136e1 1368 AliCodeTimerAuto("",0)
6fcf567c 1369 AliSysInfo::AddStamp("WriteRawData_Start");
4cce3e4e 1370
1371 TString detStr = detectors;
4cce3e4e 1372 if (!WriteRawFiles(detStr.Data())) {
06c7e392 1373 if (fStopOnError) return kFALSE;
1374 }
6fcf567c 1375 AliSysInfo::AddStamp("WriteRawFiles");
06c7e392 1376
5f6d0c0d 1377 // run HLT simulation on simulated DDL raw files
1378 // and produce HLT ddl raw files to be included in date/root file
c4affb19 1379 // bugfix 2009-06-26: the decision whether to write HLT raw data
1380 // is taken in RunHLT. Here HLT always needs to be run in order to
1381 // create HLT digits, unless its switched off. This is due to the
1382 // special placement of the HLT between the generation of DDL files
1383 // and conversion to DATE/Root file.
1384 detStr.ReplaceAll("HLT", "");
1385 if (!fRunHLT.IsNull()) {
5f6d0c0d 1386 if (!RunHLT()) {
1387 if (fStopOnError) return kFALSE;
1388 }
1389 }
6fcf567c 1390 AliSysInfo::AddStamp("WriteRawData_RunHLT");
5f6d0c0d 1391
06c7e392 1392 TString dateFileName(fileName);
1393 if (!dateFileName.IsNull()) {
1394 Bool_t rootOutput = dateFileName.EndsWith(".root");
1395 if (rootOutput) dateFileName += ".date";
67327b72 1396 TString selDateFileName;
1397 if (selrawdata) {
1398 selDateFileName = "selected.";
1399 selDateFileName+= dateFileName;
1400 }
1401 if (!ConvertRawFilesToDate(dateFileName,selDateFileName)) {
06c7e392 1402 if (fStopOnError) return kFALSE;
1403 }
6fcf567c 1404 AliSysInfo::AddStamp("ConvertRawFilesToDate");
06c7e392 1405 if (deleteIntermediateFiles) {
1406 AliRunLoader* runLoader = LoadRun("READ");
1407 if (runLoader) for (Int_t iEvent = 0;
1408 iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1409 char command[256];
76880c9a 1410 snprintf(command, 256, "rm -r raw%d", iEvent);
06c7e392 1411 gSystem->Exec(command);
1412 }
a978141c 1413 delete runLoader;
06c7e392 1414 }
1415
1416 if (rootOutput) {
1417 if (!ConvertDateToRoot(dateFileName, fileName)) {
1418 if (fStopOnError) return kFALSE;
1419 }
6fcf567c 1420 AliSysInfo::AddStamp("ConvertDateToRoot");
06c7e392 1421 if (deleteIntermediateFiles) {
1422 gSystem->Unlink(dateFileName);
1423 }
67327b72 1424 if (selrawdata) {
1425 TString selFileName = "selected.";
1426 selFileName += fileName;
1427 if (!ConvertDateToRoot(selDateFileName, selFileName)) {
1428 if (fStopOnError) return kFALSE;
1429 }
1430 if (deleteIntermediateFiles) {
1431 gSystem->Unlink(selDateFileName);
1432 }
1433 }
06c7e392 1434 }
1435 }
1436
06c7e392 1437 return kTRUE;
1438}
1439
1440//_____________________________________________________________________________
1441Bool_t AliSimulation::WriteRawFiles(const char* detectors)
1442{
1443// convert the digits to raw data DDL files
1444
99c136e1 1445 AliCodeTimerAuto("",0)
87932dab 1446
06c7e392 1447 AliRunLoader* runLoader = LoadRun("READ");
0421c3d1 1448 if (!runLoader) return kFALSE;
1449
06c7e392 1450 // write raw data to DDL files
0421c3d1 1451 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
ac1671c0 1452 AliInfo(Form("processing event %d", iEvent));
0421c3d1 1453 runLoader->GetEvent(iEvent);
1454 TString baseDir = gSystem->WorkingDirectory();
1455 char dirName[256];
76880c9a 1456 snprintf(dirName, 256, "raw%d", iEvent);
0421c3d1 1457 gSystem->MakeDirectory(dirName);
1458 if (!gSystem->ChangeDirectory(dirName)) {
ac1671c0 1459 AliError(Form("couldn't change to directory %s", dirName));
0421c3d1 1460 if (fStopOnError) return kFALSE; else continue;
1461 }
1462
3fbd60fe 1463 ofstream runNbFile(Form("run%u",runLoader->GetHeader()->GetRun()));
1464 runNbFile.close();
1465
0421c3d1 1466 TString detStr = detectors;
5f6d0c0d 1467 if (IsSelected("HLT", detStr)) {
1468 // Do nothing. "HLT" will be removed from detStr and HLT raw
1469 // data files are generated in RunHLT.
1470 }
1471
0421c3d1 1472 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1473 for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1474 AliModule* det = (AliModule*) detArray->At(iDet);
1475 if (!det || !det->IsActive()) continue;
1476 if (IsSelected(det->GetName(), detStr)) {
ac1671c0 1477 AliInfo(Form("creating raw data from digits for %s", det->GetName()));
0421c3d1 1478 det->Digits2Raw();
1479 }
1480 }
1481
007155cc 1482 if (!WriteTriggerRawData())
1483 if (fStopOnError) return kFALSE;
1484
0421c3d1 1485 gSystem->ChangeDirectory(baseDir);
1486 if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
ac1671c0 1487 AliError(Form("the following detectors were not found: %s",
1488 detStr.Data()));
0421c3d1 1489 if (fStopOnError) return kFALSE;
1490 }
1491 }
1492
1493 delete runLoader;
87932dab 1494
06c7e392 1495 return kTRUE;
1496}
0421c3d1 1497
06c7e392 1498//_____________________________________________________________________________
67327b72 1499Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName,
1500 const char* selDateFileName)
06c7e392 1501{
1502// convert raw data DDL files to a DATE file with the program "dateStream"
67327b72 1503// The second argument is not empty when the user decides to write
1504// the detectors raw data according to the trigger cluster.
06c7e392 1505
99c136e1 1506 AliCodeTimerAuto("",0)
87932dab 1507
06c7e392 1508 char* path = gSystem->Which(gSystem->Getenv("PATH"), "dateStream");
1509 if (!path) {
ac1671c0 1510 AliError("the program dateStream was not found");
06c7e392 1511 if (fStopOnError) return kFALSE;
1512 } else {
1513 delete[] path;
1514 }
0421c3d1 1515
06c7e392 1516 AliRunLoader* runLoader = LoadRun("READ");
1517 if (!runLoader) return kFALSE;
1518
ac1671c0 1519 AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
67327b72 1520 Bool_t selrawdata = kFALSE;
1521 if (strcmp(selDateFileName,"") != 0) selrawdata = kTRUE;
1522
06c7e392 1523 char command[256];
f28a8d2d 1524 // Note the option -s. It is used in order to avoid
1525 // the generation of SOR/EOR events.
76880c9a 1526 snprintf(command, 256, "dateStream -c -s -D -o %s -# %d -C -run %d",
5c0fd670 1527 dateFileName, runLoader->GetNumberOfEvents(),runLoader->GetHeader()->GetRun());
06c7e392 1528 FILE* pipe = gSystem->OpenPipe(command, "w");
1529
f5e7d9d8 1530 if (!pipe) {
1531 AliError(Form("Cannot execute command: %s",command));
1532 return kFALSE;
1533 }
1534
67327b72 1535 Int_t selEvents = 0;
06c7e392 1536 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
06c7e392 1537
f59c50c6 1538 UInt_t detectorPattern = 0;
1539 runLoader->GetEvent(iEvent);
1540 if (!runLoader->LoadTrigger()) {
1541 AliCentralTrigger *aCTP = runLoader->GetTrigger();
1542 detectorPattern = aCTP->GetClusterMask();
67327b72 1543 // Check if the event was triggered by CTP
f59c50c6 1544 if (selrawdata) {
67327b72 1545 if (aCTP->GetClassMask()) selEvents++;
1546 }
f59c50c6 1547 }
1548 else {
1549 AliWarning("No trigger can be loaded! Some fields in the event header will be empty !");
1550 if (selrawdata) {
67327b72 1551 AliWarning("No trigger can be loaded! Writing of selected raw data is abandoned !");
1552 selrawdata = kFALSE;
1553 }
1554 }
1555
dcf4b43a 1556 fprintf(pipe, "GDC DetectorPattern %u Timestamp %ld\n", detectorPattern, runLoader->GetHeader()->GetTimeStamp());
f59c50c6 1557 Float_t ldc = 0;
1558 Int_t prevLDC = -1;
1559
06c7e392 1560 // loop over detectors and DDLs
362c9d61 1561 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1562 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
06c7e392 1563
362c9d61 1564 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
06c7e392 1565 Int_t ldcID = Int_t(ldc + 0.0001);
362c9d61 1566 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
06c7e392 1567
1568 char rawFileName[256];
76880c9a 1569 snprintf(rawFileName, 256, "raw%d/%s",
362c9d61 1570 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
06c7e392 1571
1572 // check existence and size of raw data file
1573 FILE* file = fopen(rawFileName, "rb");
1574 if (!file) continue;
1575 fseek(file, 0, SEEK_END);
1576 unsigned long size = ftell(file);
aa04f81b 1577 fclose(file);
1578 if (!size) continue;
06c7e392 1579
1580 if (ldcID != prevLDC) {
1581 fprintf(pipe, " LDC Id %d\n", ldcID);
1582 prevLDC = ldcID;
1583 }
1584 fprintf(pipe, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1585 }
1586 }
1587 }
1588
1589 Int_t result = gSystem->ClosePipe(pipe);
1590
cf30933c 1591 if (!(selrawdata && selEvents > 0)) {
67327b72 1592 delete runLoader;
1593 return (result == 0);
1594 }
1595
1596 AliInfo(Form("converting selected by trigger cluster raw data DDL files to DATE file %s", selDateFileName));
1597
76880c9a 1598 snprintf(command, 256, "dateStream -c -s -D -o %s -# %d -C -run %d",
67327b72 1599 selDateFileName,selEvents,runLoader->GetHeader()->GetRun());
1600 FILE* pipe2 = gSystem->OpenPipe(command, "w");
1601
1602 for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
1603
1604 // Get the trigger decision and cluster
f59c50c6 1605 UInt_t detectorPattern = 0;
67327b72 1606 TString detClust;
1607 runLoader->GetEvent(iEvent);
1608 if (!runLoader->LoadTrigger()) {
1609 AliCentralTrigger *aCTP = runLoader->GetTrigger();
1610 if (aCTP->GetClassMask() == 0) continue;
f59c50c6 1611 detectorPattern = aCTP->GetClusterMask();
1612 detClust = AliDAQ::ListOfTriggeredDetectors(detectorPattern);
67327b72 1613 AliInfo(Form("List of detectors to be read out: %s",detClust.Data()));
1614 }
1615
dcf4b43a 1616 fprintf(pipe2, "GDC DetectorPattern %u Timestamp %ld\n", detectorPattern, runLoader->GetHeader()->GetTimeStamp());
67327b72 1617 Float_t ldc = 0;
1618 Int_t prevLDC = -1;
1619
1620 // loop over detectors and DDLs
1621 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
1622 // Write only raw data from detectors that
1623 // are contained in the trigger cluster(s)
1624 if (!IsSelected(AliDAQ::DetectorName(iDet),detClust)) continue;
1625
1626 for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
1627
1628 Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
1629 Int_t ldcID = Int_t(ldc + 0.0001);
1630 ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
1631
1632 char rawFileName[256];
76880c9a 1633 snprintf(rawFileName, 256, "raw%d/%s",
67327b72 1634 iEvent, AliDAQ::DdlFileName(iDet,iDDL));
1635
1636 // check existence and size of raw data file
1637 FILE* file = fopen(rawFileName, "rb");
1638 if (!file) continue;
1639 fseek(file, 0, SEEK_END);
1640 unsigned long size = ftell(file);
1641 fclose(file);
1642 if (!size) continue;
1643
1644 if (ldcID != prevLDC) {
1645 fprintf(pipe2, " LDC Id %d\n", ldcID);
1646 prevLDC = ldcID;
1647 }
1648 fprintf(pipe2, " Equipment Id %d Payload %s\n", ddlID, rawFileName);
1649 }
1650 }
1651 }
1652
1653 Int_t result2 = gSystem->ClosePipe(pipe2);
1654
06c7e392 1655 delete runLoader;
67327b72 1656 return ((result == 0) && (result2 == 0));
06c7e392 1657}
1658
1659//_____________________________________________________________________________
1660Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
1661 const char* rootFileName)
1662{
1663// convert a DATE file to a root file with the program "alimdc"
1664
1665 // ALIMDC setup
f28a8d2d 1666 const Int_t kDBSize = 2000000000;
c83eb8f4 1667 const Int_t kTagDBSize = 1000000000;
06c7e392 1668 const Bool_t kFilter = kFALSE;
8ec1d12b 1669 const Int_t kCompression = 1;
06c7e392 1670
1671 char* path = gSystem->Which(gSystem->Getenv("PATH"), "alimdc");
1672 if (!path) {
ac1671c0 1673 AliError("the program alimdc was not found");
06c7e392 1674 if (fStopOnError) return kFALSE;
1675 } else {
1676 delete[] path;
1677 }
1678
ac1671c0 1679 AliInfo(Form("converting DATE file %s to root file %s",
1680 dateFileName, rootFileName));
06c7e392 1681
f732bd7d 1682 const char* rawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
1683 const char* tagDBFS = "/tmp/mdc1/tags";
f732bd7d 1684
1685 // User defined file system locations
1686 if (gSystem->Getenv("ALIMDC_RAWDB1"))
1687 rawDBFS[0] = gSystem->Getenv("ALIMDC_RAWDB1");
1688 if (gSystem->Getenv("ALIMDC_RAWDB2"))
1689 rawDBFS[1] = gSystem->Getenv("ALIMDC_RAWDB2");
1690 if (gSystem->Getenv("ALIMDC_TAGDB"))
1691 tagDBFS = gSystem->Getenv("ALIMDC_TAGDB");
f732bd7d 1692
1693 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1694 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1695 gSystem->Exec(Form("rm -rf %s",tagDBFS));
f732bd7d 1696
1697 gSystem->Exec(Form("mkdir %s",rawDBFS[0]));
1698 gSystem->Exec(Form("mkdir %s",rawDBFS[1]));
1699 gSystem->Exec(Form("mkdir %s",tagDBFS));
f732bd7d 1700
1701 Int_t result = gSystem->Exec(Form("alimdc %d %d %d %d %s",
1702 kDBSize, kTagDBSize, kFilter, kCompression, dateFileName));
1703 gSystem->Exec(Form("mv %s/*.root %s", rawDBFS[0], rootFileName));
1704
1705 gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
1706 gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
1707 gSystem->Exec(Form("rm -rf %s",tagDBFS));
06c7e392 1708
1709 return (result == 0);
0421c3d1 1710}
1711
85a5290f 1712
95601147 1713//_____________________________________________________________________________
06c7e392 1714AliRunLoader* AliSimulation::LoadRun(const char* mode) const
95601147 1715{
1716// delete existing run loaders, open a new one and load gAlice
1717
33c3c91a 1718 delete AliRunLoader::Instance();
95601147 1719 AliRunLoader* runLoader =
1720 AliRunLoader::Open(fGAliceFileName.Data(),
06c7e392 1721 AliConfig::GetDefaultEventFolderName(), mode);
95601147 1722 if (!runLoader) {
ac1671c0 1723 AliError(Form("no run loader found in file %s", fGAliceFileName.Data()));
95601147 1724 return NULL;
1725 }
1726 runLoader->LoadgAlice();
795e4a22 1727 runLoader->LoadHeader();
95601147 1728 gAlice = runLoader->GetAliRun();
1729 if (!gAlice) {
ac1671c0 1730 AliError(Form("no gAlice object found in file %s",
1731 fGAliceFileName.Data()));
95601147 1732 return NULL;
1733 }
1734 return runLoader;
1735}
1736
85a5290f 1737//_____________________________________________________________________________
05526d44 1738Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
1739{
1740// get or calculate the number of signal events per background event
1741
1742 if (!fBkgrdFileNames) return 1;
1743 Int_t nBkgrdFiles = fBkgrdFileNames->GetEntriesFast();
1744 if (nBkgrdFiles == 0) return 1;
1745
1746 // get the number of signal events
1747 if (nEvents <= 0) {
1748 AliRunLoader* runLoader =
ca30848f 1749 AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
05526d44 1750 if (!runLoader) return 1;
ca30848f 1751
05526d44 1752 nEvents = runLoader->GetNumberOfEvents();
1753 delete runLoader;
1754 }
1755
1756 Int_t result = 0;
1757 for (Int_t iBkgrdFile = 0; iBkgrdFile < nBkgrdFiles; iBkgrdFile++) {
1758 // get the number of background events
1759 const char* fileName = ((TObjString*)
1760 (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
ca30848f 1761 AliRunLoader* runLoader =
05526d44 1762 AliRunLoader::Open(fileName, "BKGRD");
1763 if (!runLoader) continue;
1764 Int_t nBkgrdEvents = runLoader->GetNumberOfEvents();
1765 delete runLoader;
ca30848f 1766
05526d44 1767 // get or calculate the number of signal per background events
1768 Int_t nSignalPerBkgrd = fBkgrdFileNames->At(iBkgrdFile)->GetUniqueID();
1769 if (nSignalPerBkgrd <= 0) {
1770 nSignalPerBkgrd = (nEvents-1) / nBkgrdEvents + 1;
1771 } else if (result && (result != nSignalPerBkgrd)) {
ac1671c0 1772 AliInfo(Form("the number of signal events per background event "
1773 "will be changed from %d to %d for stream %d",
1774 nSignalPerBkgrd, result, iBkgrdFile+1));
05526d44 1775 nSignalPerBkgrd = result;
1776 }
1777
1778 if (!result) result = nSignalPerBkgrd;
1779 if (nSignalPerBkgrd * nBkgrdEvents < nEvents) {
ac1671c0 1780 AliWarning(Form("not enough background events (%d) for %d signal events "
1781 "using %d signal per background events for stream %d",
1782 nBkgrdEvents, nEvents, nSignalPerBkgrd, iBkgrdFile+1));
05526d44 1783 }
1784 }
1785
1786 return result;
1787}
1788
1789//_____________________________________________________________________________
85a5290f 1790Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
1791{
1792// check whether detName is contained in detectors
1793// if yes, it is removed from detectors
1794
1795 // check if all detectors are selected
1796 if ((detectors.CompareTo("ALL") == 0) ||
1797 detectors.BeginsWith("ALL ") ||
1798 detectors.EndsWith(" ALL") ||
1799 detectors.Contains(" ALL ")) {
1800 detectors = "ALL";
1801 return kTRUE;
1802 }
1803
1804 // search for the given detector
1805 Bool_t result = kFALSE;
1806 if ((detectors.CompareTo(detName) == 0) ||
1807 detectors.BeginsWith(detName+" ") ||
1808 detectors.EndsWith(" "+detName) ||
1809 detectors.Contains(" "+detName+" ")) {
1810 detectors.ReplaceAll(detName, "");
1811 result = kTRUE;
1812 }
1813
1814 // clean up the detectors string
1815 while (detectors.Contains(" ")) detectors.ReplaceAll(" ", " ");
1816 while (detectors.BeginsWith(" ")) detectors.Remove(0, 1);
1817 while (detectors.EndsWith(" ")) detectors.Remove(detectors.Length()-1, 1);
1818
1819 return result;
1820}
ca30848f 1821
795e4a22 1822//_____________________________________________________________________________
2eab8cf7 1823Bool_t AliSimulation::ConvertRaw2SDigits(const char* rawDirectory, const char* esdFileName, Int_t N)
ca30848f 1824{
1825//
1826// Steering routine to convert raw data in directory rawDirectory/ to fake SDigits.
1827// These can be used for embedding of MC tracks into RAW data using the standard
1828// merging procedure.
1829//
1830// If an ESD file is given the reconstructed vertex is taken from it and stored in the event header.
1831//
3e2e3ece 1832 if (!gAlice) {
1833 AliError("no gAlice object. Restart aliroot and try again.");
1834 return kFALSE;
1835 }
1836 if (gAlice->Modules()->GetEntries() > 0) {
1837 AliError("gAlice was already run. Restart aliroot and try again.");
1838 return kFALSE;
1839 }
1840
1841 AliInfo(Form("initializing gAlice with config file %s",fConfigFileName.Data()));
1842
1843 gAlice->Announce();
1844
1845 gROOT->LoadMacro(fConfigFileName.Data());
1846 gInterpreter->ProcessLine(gAlice->GetConfigFunction());
1847
1848 if(AliCDBManager::Instance()->GetRun() >= 0) {
1849 SetRunNumber(AliCDBManager::Instance()->GetRun());
1850 } else {
1851 AliWarning("Run number not initialized!!");
1852 }
1853
33c3c91a 1854 AliRunLoader::Instance()->CdGAFile();
ca30848f 1855
3e2e3ece 1856 AliPDG::AddParticlesToPdgDataBase();
1857
f7a1cc68 1858 gMC->SetMagField(TGeoGlobalMagField::Instance()->GetField());
3e2e3ece 1859
55338a9a 1860 gAlice->GetMCApp()->Init();
1861
3e2e3ece 1862 //Must be here because some MCs (G4) adds detectors here and not in Config.C
1863 gAlice->InitLoaders();
33c3c91a 1864 AliRunLoader::Instance()->MakeTree("E");
1865 AliRunLoader::Instance()->LoadKinematics("RECREATE");
1866 AliRunLoader::Instance()->LoadTrackRefs("RECREATE");
1867 AliRunLoader::Instance()->LoadHits("all","RECREATE");
2eab8cf7 1868
3e2e3ece 1869 //
1870 // Save stuff at the beginning of the file to avoid file corruption
33c3c91a 1871 AliRunLoader::Instance()->CdGAFile();
3e2e3ece 1872 gAlice->Write();
ca30848f 1873//
1874// Initialize CDB
795e4a22 1875 InitCDB();
1876 //AliCDBManager* man = AliCDBManager::Instance();
1877 //man->SetRun(0); // Should this come from rawdata header ?
ca30848f 1878
1879 Int_t iDet;
1880 //
1881 // Get the runloader
33c3c91a 1882 AliRunLoader* runLoader = AliRunLoader::Instance();
ca30848f 1883 //
1884 // Open esd file if available
a77627a4 1885 TFile* esdFile = 0;
ca30848f 1886 TTree* treeESD = 0;
a77627a4 1887 AliESDEvent* esd = 0;
2eab8cf7 1888 if (esdFileName && (strlen(esdFileName)>0)) {
a77627a4 1889 esdFile = TFile::Open(esdFileName);
1890 if (esdFile) {
1891 esd = new AliESDEvent();
1892 esdFile->GetObject("esdTree", treeESD);
1893 if (treeESD) esd->ReadFromTree(treeESD);
1894 }
1895 }
6d985680 1896
ca30848f 1897 //
1898 // Create the RawReader
8b31e840 1899 TString fileName(rawDirectory);
1900 AliRawReader* rawReader = 0x0;
1901 if (fileName.EndsWith("/")) {
1902 rawReader = new AliRawReaderFile(fileName);
1903 } else if (fileName.EndsWith(".root")) {
1904 rawReader = new AliRawReaderRoot(fileName);
1905 } else if (!fileName.IsNull()) {
1906 rawReader = new AliRawReaderDate(fileName);
8b31e840 1907 }
58c338d2 1908 if (!rawReader) return (kFALSE);
1909
8b31e840 1910// if (!fEquipIdMap.IsNull() && fRawReader)
1911// fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
ca30848f 1912 //
1913 // Get list of detectors
1914 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
1915 //
1916 // Get Header
1917 AliHeader* header = runLoader->GetHeader();
ca30848f 1918 // Event loop
1919 Int_t nev = 0;
1920 while(kTRUE) {
1921 if (!(rawReader->NextEvent())) break;
b2f03502 1922 runLoader->SetEventNumber(nev);
1923 runLoader->GetHeader()->Reset(rawReader->GetRunNumber(),
1924 nev, nev);
b2f03502 1925 runLoader->GetEvent(nev);
2eab8cf7 1926 AliInfo(Form("We are at event %d",nev));
ca30848f 1927 //
1928 // Detector loop
b2f03502 1929 TString detStr = fMakeSDigits;
ca30848f 1930 for (iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
1931 AliModule* det = (AliModule*) detArray->At(iDet);
8b31e840 1932 if (!det || !det->IsActive()) continue;
1933 if (IsSelected(det->GetName(), detStr)) {
2eab8cf7 1934 AliInfo(Form("Calling Raw2SDigits for %s", det->GetName()));
8b31e840 1935 det->Raw2SDigits(rawReader);
1936 rawReader->Reset();
1937 }
ca30848f 1938 } // detectors
b2f03502 1939
5b188f2f 1940
ca30848f 1941 //
1942 // If ESD information available obtain reconstructed vertex and store in header.
6d985680 1943 if (treeESD) {
ca30848f 1944 treeESD->GetEvent(nev);
1945 const AliESDVertex* esdVertex = esd->GetPrimaryVertex();
1946 Double_t position[3];
1947 esdVertex->GetXYZ(position);
1948 AliGenEventHeader* mcHeader = new AliGenEventHeader("ESD");
1949 TArrayF mcV;
1950 mcV.Set(3);
1951 for (Int_t i = 0; i < 3; i++) mcV[i] = position[i];
1952 mcHeader->SetPrimaryVertex(mcV);
1953 header->Reset(0,nev);
1954 header->SetGenEventHeader(mcHeader);
2eab8cf7 1955 AliInfo(Form("***** Saved vertex %f %f %f \n", position[0], position[1], position[2]));
ca30848f 1956 }
ca30848f 1957//
1958// Finish the event
2d9bb909 1959 runLoader->TreeE()->Fill();
2eab8cf7 1960 AliInfo(Form("Finished event %d",nev));
b2f03502 1961 nev++;
2eab8cf7 1962 if (N>0&&nev>=N)
1963 break;
ca30848f 1964 } // events
1965
1966 delete rawReader;
1967//
1968// Finish the run
1969 runLoader->CdGAFile();
1970 runLoader->WriteHeader("OVERWRITE");
1971 runLoader->WriteRunLoader();
1972
1973 return kTRUE;
1974}
5b188f2f 1975
3e2e3ece 1976//_____________________________________________________________________________
1977void AliSimulation::FinishRun()
1978{
1979 //
1980 // Called at the end of the run.
1981 //
1982
1983 if(IsLegoRun())
1984 {
1985 AliDebug(1, "Finish Lego");
33c3c91a 1986 AliRunLoader::Instance()->CdGAFile();
3e2e3ece 1987 fLego->FinishRun();
1988 }
1989
1990 // Clean detector information
1991 TIter next(gAlice->Modules());
1992 AliModule *detector;
1993 while((detector = dynamic_cast<AliModule*>(next()))) {
1994 AliDebug(2, Form("%s->FinishRun()", detector->GetName()));
1995 detector->FinishRun();
1996 }
1997
33c3c91a 1998 AliDebug(1, "AliRunLoader::Instance()->WriteHeader(OVERWRITE)");
1999 AliRunLoader::Instance()->WriteHeader("OVERWRITE");
3e2e3ece 2000
2001 // Write AliRun info and all detectors parameters
33c3c91a 2002 AliRunLoader::Instance()->CdGAFile();
3e2e3ece 2003 gAlice->Write(0,TObject::kOverwrite);//write AliRun
33c3c91a 2004 AliRunLoader::Instance()->Write(0,TObject::kOverwrite);//write RunLoader itself
3e2e3ece 2005
2006 if(gAlice->GetMCApp()) gAlice->GetMCApp()->FinishRun();
33c3c91a 2007 AliRunLoader::Instance()->Synchronize();
3e2e3ece 2008}
2009
5b188f2f 2010//_____________________________________________________________________________
2011Int_t AliSimulation::GetDetIndex(const char* detector)
2012{
2013 // return the detector index corresponding to detector
2014 Int_t index = -1 ;
2015 for (index = 0; index < fgkNDetectors ; index++) {
2016 if ( strcmp(detector, fgkDetectorName[index]) == 0 )
2017 break ;
2018 }
2019 return index ;
2020}
cae21299 2021
c4affb19 2022//_____________________________________________________________________________
2023Bool_t AliSimulation::CreateHLT()
2024{
2025 // Init the HLT simulation.
2026 // The function loads the library and creates the instance of AliHLTSimulation.
2027 // the main reason for the decoupled creation is to set the transient OCDB
2028 // objects before the OCDB is locked
2029
2030 // load the library dynamically
2031 gSystem->Load(ALIHLTSIMULATION_LIBRARY);
2032
2033 // check for the library version
2034 AliHLTSimulationGetLibraryVersion_t fctVersion=(AliHLTSimulationGetLibraryVersion_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_GET_LIBRARY_VERSION));
2035 if (!fctVersion) {
2036 AliError(Form("can not load library %s", ALIHLTSIMULATION_LIBRARY));
2037 return kFALSE;
2038 }
2039 if (fctVersion()!= ALIHLTSIMULATION_LIBRARY_VERSION) {
b682a4ec 2040 AliWarning(Form("%s version does not match: compiled for version %d, loaded %d", ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_LIBRARY_VERSION, fctVersion()));
c4affb19 2041 }
2042
2043 // print compile info
2044 typedef void (*CompileInfo)( const char*& date, const char*& time);
2045 CompileInfo fctInfo=(CompileInfo)gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, "CompileInfo");
2046 if (fctInfo) {
2047 const char* date="";
2048 const char* time="";
2049 (*fctInfo)(date, time);
2050 if (!date) date="unknown";
2051 if (!time) time="unknown";
2052 AliInfo(Form("%s build on %s (%s)", ALIHLTSIMULATION_LIBRARY, date, time));
2053 } else {
2054 AliInfo(Form("no build info available for %s", ALIHLTSIMULATION_LIBRARY));
2055 }
2056
2057 // create instance of the HLT simulation
2058 AliHLTSimulationCreateInstance_t fctCreate=(AliHLTSimulationCreateInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_CREATE_INSTANCE));
2059 if (fctCreate==NULL || (fpHLT=(fctCreate()))==NULL) {
2060 AliError(Form("can not create instance of HLT simulation (creator %p)", fctCreate));
2061 return kFALSE;
2062 }
2063
2064 TString specObjects;
2065 for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
2066 if (specObjects.Length()>0) specObjects+=" ";
2067 specObjects+=fSpecCDBUri[i]->GetName();
2068 }
2069
2070 AliHLTSimulationSetup_t fctSetup=(AliHLTSimulationSetup_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_SETUP));
2071 if (fctSetup==NULL || fctSetup(fpHLT, this, specObjects.Data())<0) {
2072 AliWarning(Form("failed to setup HLT simulation (function %p)", fctSetup));
2073 }
2074
2075 return kTRUE;
2076}
2077
cae21299 2078//_____________________________________________________________________________
2079Bool_t AliSimulation::RunHLT()
2080{
2081 // Run the HLT simulation
2082 // HLT simulation is implemented in HLT/sim/AliHLTSimulation
2083 // Disabled if fRunHLT is empty, default vaule is "default".
2084 // AliSimulation::SetRunHLT can be used to set the options for HLT simulation
2085 // The default simulation depends on the HLT component libraries and their
2086 // corresponding agents which define components and chains to run. See
5f6d0c0d 2087 // http://web.ift.uib.no/~kjeks/doc/alice-hlt-current/
2088 // http://web.ift.uib.no/~kjeks/doc/alice-hlt-current/classAliHLTModuleAgent.html
cae21299 2089 //
2090 // The libraries to be loaded can be specified as an option.
2091 // <pre>
2092 // AliSimulation sim;
2093 // sim.SetRunHLT("libAliHLTSample.so");
2094 // </pre>
2095 // will only load <tt>libAliHLTSample.so</tt>
2096
2097 // Other available options:
2098 // \li loglevel=<i>level</i> <br>
2099 // logging level for this processing
2100 // \li alilog=off
2101 // disable redirection of log messages to AliLog class
2102 // \li config=<i>macro</i>
2103 // configuration macro
5f6d0c0d 2104 // \li chains=<i>configuration</i>
cae21299 2105 // comma separated list of configurations to be run during simulation
5f6d0c0d 2106 // \li rawfile=<i>file</i>
2107 // source for the RawReader to be created, the default is <i>./</i> if
2108 // raw data is simulated
cae21299 2109
2110 int iResult=0;
c4affb19 2111
2112 if (!fpHLT && !CreateHLT()) {
2113 return kFALSE;
2114 }
2115 AliHLTSimulation* pHLT=fpHLT;
2116
cae21299 2117 AliRunLoader* pRunLoader = LoadRun("READ");
2118 if (!pRunLoader) return kFALSE;
2119
795e4a22 2120 // initialize CDB storage, run number, set CDB lock
c4affb19 2121 // thats for the case of running HLT simulation without all the other steps
2122 // multiple calls are handled by the function, so we can just call
795e4a22 2123 InitCDB();
2124 if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
2125 SetCDBLock();
2126
cae21299 2127 // init the HLT simulation
a441bf51 2128 TString options;
2129 if (fRunHLT.CompareTo("default")!=0) options=fRunHLT;
5f6d0c0d 2130 TString detStr = fWriteRawData;
2131 if (!IsSelected("HLT", detStr)) {
a441bf51 2132 options+=" writerawfiles=";
2133 } else {
2134 options+=" writerawfiles=HLT";
2135 }
5f6d0c0d 2136
2137 if (!detStr.IsNull() && !options.Contains("rawfile=")) {
2138 // as a matter of fact, HLT will run reconstruction and needs the RawReader
2139 // in order to get detector data. By default, RawReaderFile is used to read
2140 // the already simulated ddl files. Date and Root files from the raw data
2141 // are generated after the HLT simulation.
2142 options+=" rawfile=./";
2143 }
2144
cae21299 2145 AliHLTSimulationInit_t fctInit=(AliHLTSimulationInit_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_INIT));
a441bf51 2146 if (fctInit==NULL || (iResult=(fctInit(pHLT, pRunLoader, options.Data())))<0) {
cae21299 2147 AliError(Form("can not init HLT simulation: error %d (init %p)", iResult, fctInit));
2148 } else {
2149 // run the HLT simulation
2150 AliHLTSimulationRun_t fctRun=(AliHLTSimulationRun_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_RUN));
2151 if (fctRun==NULL || (iResult=(fctRun(pHLT, pRunLoader)))<0) {
2152 AliError(Form("can not run HLT simulation: error %d (run %p)", iResult, fctRun));
2153 }
2154 }
2155
2156 // delete the instance
2157 AliHLTSimulationDeleteInstance_t fctDelete=(AliHLTSimulationDeleteInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_DELETE_INSTANCE));
2158 if (fctDelete==NULL || fctDelete(pHLT)<0) {
2159 AliError(Form("can not delete instance of HLT simulation (creator %p)", fctDelete));
2160 }
2161 pHLT=NULL;
2162
2163 return iResult>=0?kTRUE:kFALSE;
2164}
cae17938 2165
2166//_____________________________________________________________________________
a1b90a73 2167Bool_t AliSimulation::RunQA()
cae17938 2168{
a1b90a73 2169 // run the QA on summable hits, digits or digits
940d8e5f 2170
3ce48293 2171 //if(!gAlice) return kFALSE;
eca4fa66 2172 AliQAManager::QAManager()->SetRunLoader(AliRunLoader::Instance()) ;
a1b90a73 2173
e5dedfe2 2174 TString detectorsw("") ;
2175 Bool_t rv = kTRUE ;
eca4fa66 2176 AliQAManager::QAManager()->SetEventSpecie(fEventSpecie) ;
2177 detectorsw = AliQAManager::QAManager()->Run(fQADetectors.Data()) ;
e5dedfe2 2178 if ( detectorsw.IsNull() )
2179 rv = kFALSE ;
a1b90a73 2180 return rv ;
cae17938 2181}
2182
e3b20d9a 2183//_____________________________________________________________________________
2184Bool_t AliSimulation::SetRunQA(TString detAndAction)
2185{
2186 // Allows to run QA for a selected set of detectors
2187 // and a selected set of tasks among HITS, SDIGITS and DIGITS
2188 // all selected detectors run the same selected tasks
2189
2190 if (!detAndAction.Contains(":")) {
2191 AliError( Form("%s is a wrong syntax, use \"DetectorList:ActionList\" \n", detAndAction.Data()) ) ;
2192 fRunQA = kFALSE ;
2193 return kFALSE ;
2194 }
2195 Int_t colon = detAndAction.Index(":") ;
2196 fQADetectors = detAndAction(0, colon) ;
c54a46e3 2197 if (fQADetectors.Contains("ALL") ){
2198 TString tmp = Form("%s %s", fMakeDigits.Data(), fMakeDigitsFromHits.Data()) ;
2199 Int_t minus = fQADetectors.Last('-') ;
2200 TString toKeep = Form("%s %s", fMakeDigits.Data(), fMakeDigitsFromHits.Data()) ;
2201 TString toRemove("") ;
2202 while (minus >= 0) {
2203 toRemove = fQADetectors(minus+1, fQADetectors.Length()) ;
2204 toRemove = toRemove.Strip() ;
2205 toKeep.ReplaceAll(toRemove, "") ;
2206 fQADetectors.ReplaceAll(Form("-%s", toRemove.Data()), "") ;
2207 minus = fQADetectors.Last('-') ;
2208 }
2209 fQADetectors = toKeep ;
2210 }
2211 fQATasks = detAndAction(colon+1, detAndAction.Sizeof() ) ;
e3b20d9a 2212 if (fQATasks.Contains("ALL") ) {
4e25ac79 2213 fQATasks = Form("%d %d %d", AliQAv1::kHITS, AliQAv1::kSDIGITS, AliQAv1::kDIGITS) ;
e3b20d9a 2214 } else {
2215 fQATasks.ToUpper() ;
2216 TString tempo("") ;
2217 if ( fQATasks.Contains("HIT") )
4e25ac79 2218 tempo = Form("%d ", AliQAv1::kHITS) ;
e3b20d9a 2219 if ( fQATasks.Contains("SDIGIT") )
4e25ac79 2220 tempo += Form("%d ", AliQAv1::kSDIGITS) ;
e3b20d9a 2221 if ( fQATasks.Contains("DIGIT") )
4e25ac79 2222 tempo += Form("%d ", AliQAv1::kDIGITS) ;
e3b20d9a 2223 fQATasks = tempo ;
2224 if (fQATasks.IsNull()) {
2225 AliInfo("No QA requested\n") ;
2226 fRunQA = kFALSE ;
2227 return kTRUE ;
2228 }
2229 }
2230 TString tempo(fQATasks) ;
4e25ac79 2231 tempo.ReplaceAll(Form("%d", AliQAv1::kHITS), AliQAv1::GetTaskName(AliQAv1::kHITS)) ;
2232 tempo.ReplaceAll(Form("%d", AliQAv1::kSDIGITS), AliQAv1::GetTaskName(AliQAv1::kSDIGITS)) ;
2233 tempo.ReplaceAll(Form("%d", AliQAv1::kDIGITS), AliQAv1::GetTaskName(AliQAv1::kDIGITS)) ;
e3b20d9a 2234 AliInfo( Form("QA will be done on \"%s\" for \"%s\"\n", fQADetectors.Data(), tempo.Data()) ) ;
2235 fRunQA = kTRUE ;
eca4fa66 2236 AliQAManager::QAManager()->SetActiveDetectors(fQADetectors) ;
2237 AliQAManager::QAManager()->SetTasks(fQATasks) ;
4e25ac79 2238 for (Int_t det = 0 ; det < AliQAv1::kNDET ; det++)
eca4fa66 2239 AliQAManager::QAManager()->SetWriteExpert(AliQAv1::DETECTORINDEX_t(det)) ;
57acd2d2 2240
e3b20d9a 2241 return kTRUE;
2242}
2243
795e4a22 2244//_____________________________________________________________________________
2245void AliSimulation::ProcessEnvironmentVars()
2246{
2247// Extract run number and random generator seed from env variables
2248
2249 AliInfo("Processing environment variables");
2250
2251 // Random Number seed
2252
2253 // first check that seed is not already set
2254 if (fSeed == 0) {
2255 if (gSystem->Getenv("CONFIG_SEED")) {
2256 fSeed = atoi(gSystem->Getenv("CONFIG_SEED"));
2257 }
2258 } else {
2259 if (gSystem->Getenv("CONFIG_SEED")) {
2260 AliInfo(Form("Seed for random number generation already set (%d)"
2261 ": CONFIG_SEED variable ignored!", fSeed));
2262 }
2263 }
2264
2265 AliInfo(Form("Seed for random number generation = %d ", fSeed));
2266
2267 // Run Number
2268
2269 // first check that run number is not already set
2270 if(fRun < 0) {
2271 if (gSystem->Getenv("DC_RUN")) {
2272 fRun = atoi(gSystem->Getenv("DC_RUN"));
2273 }
2274 } else {
2275 if (gSystem->Getenv("DC_RUN")) {
2276 AliInfo(Form("Run number already set (%d): DC_RUN variable ignored!", fRun));
2277 }
2278 }
2279
2280 AliInfo(Form("Run number = %d", fRun));
2281}
ff5970a3 2282
44e45fac 2283//---------------------------------------------------------------------
fc7e1b1c 2284void AliSimulation::WriteGRPEntry()
2285{
2286 // Get the necessary information from galice (generator, trigger etc) and
2287 // write a GRP entry corresponding to the settings in the Config.C used
44e45fac 2288 // note that Hall probes and Cavern and Surface Atmos pressures are not simulated.
2289
2290
fc7e1b1c 2291 AliInfo("Writing global run parameters entry into the OCDB");
2292
44e45fac 2293 AliGRPObject* grpObj = new AliGRPObject();
fc7e1b1c 2294
44e45fac 2295 grpObj->SetRunType("PHYSICS");
475baf89 2296 grpObj->SetTimeStart(fTimeStart);
2297 grpObj->SetTimeEnd(fTimeEnd);
5cf76849 2298 grpObj->SetBeamEnergyIsSqrtSHalfGeV(); // new format of GRP: store sqrt(s)/2 in GeV
fc7e1b1c 2299
3e2e3ece 2300 const AliGenerator *gen = gAlice->GetMCApp()->Generator();
4dcef377 2301 Int_t a = 0;
2302 Int_t z = 0;
2303
fc7e1b1c 2304 if (gen) {
fc7e1b1c 2305 TString projectile;
fc7e1b1c 2306 gen->GetProjectile(projectile,a,z);
2307 TString target;
2308 gen->GetTarget(target,a,z);
2309 TString beamType = projectile + "-" + target;
214a2a31 2310 beamType.ReplaceAll(" ","");
e7c989e4 2311 if (!beamType.CompareTo("-")) {
57acd2d2 2312 grpObj->SetBeamType("UNKNOWN");
4dcef377 2313 grpObj->SetBeamEnergy(gen->GetEnergyCMS()/2);
e7c989e4 2314 }
2315 else {
57acd2d2 2316 grpObj->SetBeamType(beamType);
4dcef377 2317 if (z != 0) {
2318 grpObj->SetBeamEnergy(gen->GetEnergyCMS()/2 * a / z);
2319 } else {
2320 grpObj->SetBeamEnergy(gen->GetEnergyCMS()/2 );
2321 }
57acd2d2 2322 // Heavy ion run, the event specie is set to kHighMult
2323 fEventSpecie = AliRecoParam::kHighMult;
2324 if ((strcmp(beamType,"p-p") == 0) ||
2325 (strcmp(beamType,"p-") == 0) ||
2326 (strcmp(beamType,"-p") == 0) ||
2327 (strcmp(beamType,"P-P") == 0) ||
2328 (strcmp(beamType,"P-") == 0) ||
2329 (strcmp(beamType,"-P") == 0)) {
2330 // Proton run, the event specie is set to kLowMult
2331 fEventSpecie = AliRecoParam::kLowMult;
2332 }
e7c989e4 2333 }
57acd2d2 2334 } else {
44e45fac 2335 AliWarning("Unknown beam type and energy! Setting energy to 0");
2336 grpObj->SetBeamEnergy(0);
2337 grpObj->SetBeamType("UNKNOWN");
fc7e1b1c 2338 }
2339
2340 UInt_t detectorPattern = 0;
2341 Int_t nDets = 0;
2342 TObjArray *detArray = gAlice->Detectors();
2343 for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors-1; iDet++) {
2344 if (detArray->FindObject(AliDAQ::OfflineModuleName(iDet))) {
820b4d9e 2345 AliDebug(1, Form("Detector #%d found: %s", iDet, AliDAQ::OfflineModuleName(iDet)));
fc7e1b1c 2346 detectorPattern |= (1 << iDet);
2347 nDets++;
2348 }
2349 }
1be29b9e 2350 // CTP
28da60d3 2351 if (!fTriggerConfig.IsNull())
1be29b9e 2352 detectorPattern |= (1 << AliDAQ::DetectorID("TRG"));
2353
fc7e1b1c 2354 // HLT
2355 if (!fRunHLT.IsNull())
2356 detectorPattern |= (1 << AliDAQ::kHLTId);
ff5970a3 2357
44e45fac 2358 grpObj->SetNumberOfDetectors((Char_t)nDets);
2359 grpObj->SetDetectorMask((Int_t)detectorPattern);
2360 grpObj->SetLHCPeriod("LHC08c");
2361 grpObj->SetLHCState("STABLE_BEAMS");
99c7d495 2362 //
f7a1cc68 2363 AliMagF *field = (AliMagF*)TGeoGlobalMagField::Instance()->GetField();
2364 Float_t solenoidField = field ? TMath::Abs(field->SolenoidField()) : 0;
fc7e1b1c 2365
99c7d495 2366 Float_t factorSol = field ? field->GetFactorSol() : 0;
2367 Float_t currentSol = TMath::Abs(factorSol)>1E-6 ?
2368 TMath::Nint(TMath::Abs(solenoidField/factorSol))/5.*30000.*TMath::Abs(factorSol) : 0;
2369 //
2370 Float_t factorDip = field ? field->GetFactorDip() : 0;
2371 Float_t currentDip = 6000.*TMath::Abs(factorDip);
2372 //
2373 grpObj->SetL3Current(currentSol,(AliGRPObject::Stats)0);
2374 grpObj->SetDipoleCurrent(currentDip,(AliGRPObject::Stats)0);
2375 grpObj->SetL3Polarity(factorSol>0 ? 0:1);
2376 grpObj->SetDipolePolarity(factorDip>0 ? 0:1);
58c338d2 2377 if (field) grpObj->SetUniformBMap(field->IsUniform()); // for special MC with k5kGUniform map
99c7d495 2378 grpObj->SetPolarityConventionLHC(); // LHC convention +/+ current -> -/- field main components
2379 //
44e45fac 2380 grpObj->SetCavernTemperature(0,(AliGRPObject::Stats)0);
2381
2382 //grpMap->Add(new TObjString("fCavernPressure"),new TObjString("0")); ---> not inserted in simulation with the new object, since it is now an AliDCSSensor
fc7e1b1c 2383
2384 // Now store the entry in OCDB
2385 AliCDBManager* man = AliCDBManager::Instance();
732ee20f 2386
2387 man->SetLock(0, fKey);
2388
2389 AliCDBStorage* sto = man->GetStorage(fGRPWriteLocation.Data());
2390
fc7e1b1c 2391
2b541cc7 2392 AliCDBId id("GRP/GRP/Data", man->GetRun(), man->GetRun(), 1, 1);
fc7e1b1c 2393 AliCDBMetaData *metadata= new AliCDBMetaData();
2394
fc7e1b1c 2395 metadata->SetResponsible("alice-off@cern.ch");
2396 metadata->SetComment("Automatically produced GRP entry for Monte Carlo");
2397
732ee20f 2398 sto->Put(grpObj,id,metadata);
2399 man->SetLock(1, fKey);
fc7e1b1c 2400}
44e45fac 2401
475baf89 2402//_____________________________________________________________________________
2403time_t AliSimulation::GenerateTimeStamp() const
2404{
2405 // Generate event time-stamp according to
2406 // SOR/EOR time from GRP
2407 if (fUseTimeStampFromCDB)
2408 return fTimeStart + gRandom->Integer(fTimeEnd-fTimeStart);
2409 else
2410 return 0;
2411}