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