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