+
+//_____________________________________________________________________________
+Bool_t AliSimulation::ConvertRaw2SDigits(const char* rawDirectory, const char* esdFileName)
+{
+//
+// Steering routine to convert raw data in directory rawDirectory/ to fake SDigits.
+// These can be used for embedding of MC tracks into RAW data using the standard
+// merging procedure.
+//
+// If an ESD file is given the reconstructed vertex is taken from it and stored in the event header.
+//
+ if (!gAlice) {
+ AliError("no gAlice object. Restart aliroot and try again.");
+ return kFALSE;
+ }
+ if (gAlice->Modules()->GetEntries() > 0) {
+ AliError("gAlice was already run. Restart aliroot and try again.");
+ return kFALSE;
+ }
+
+ AliInfo(Form("initializing gAlice with config file %s",fConfigFileName.Data()));
+ StdoutToAliInfo(StderrToAliError(gAlice->Init(fConfigFileName.Data());););
+//
+// Initialize CDB
+ InitCDB();
+ //AliCDBManager* man = AliCDBManager::Instance();
+ //man->SetRun(0); // Should this come from rawdata header ?
+
+ Int_t iDet;
+ //
+ // Get the runloader
+ AliRunLoader* runLoader = gAlice->GetRunLoader();
+ //
+ // Open esd file if available
+ TFile* esdFile = TFile::Open(esdFileName);
+ Bool_t esdOK = (esdFile != 0);
+ AliESD* esd = new AliESD;
+ TTree* treeESD = 0;
+ if (esdOK) {
+ treeESD = (TTree*) esdFile->Get("esdTree");
+ if (!treeESD) {
+ AliWarning("No ESD tree found");
+ esdOK = kFALSE;
+ } else {
+ treeESD->SetBranchAddress("ESD", &esd);
+ }
+ }
+ //
+ // Create the RawReader
+ TString fileName(rawDirectory);
+ AliRawReader* rawReader = 0x0;
+ if (fileName.EndsWith("/")) {
+ rawReader = new AliRawReaderFile(fileName);
+ } else if (fileName.EndsWith(".root")) {
+ rawReader = new AliRawReaderRoot(fileName);
+ } else if (!fileName.IsNull()) {
+ rawReader = new AliRawReaderDate(fileName);
+ }
+// if (!fEquipIdMap.IsNull() && fRawReader)
+// fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
+ //
+ // Get list of detectors
+ TObjArray* detArray = runLoader->GetAliRun()->Detectors();
+ //
+ // Get Header
+ AliHeader* header = runLoader->GetHeader();
+ //
+ TString detStr = fMakeSDigits;
+ // Event loop
+ Int_t nev = 0;
+ while(kTRUE) {
+ if (!(rawReader->NextEvent())) break;
+ //
+ // Detector loop
+ for (iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
+ AliModule* det = (AliModule*) detArray->At(iDet);
+ if (!det || !det->IsActive()) continue;
+ if (IsSelected(det->GetName(), detStr)) {
+ AliInfo(Form("Calling Raw2SDigits for %s\n", det->GetName()));
+ det->Raw2SDigits(rawReader);
+ rawReader->Reset();
+ }
+ } // detectors
+
+
+ //
+ // If ESD information available obtain reconstructed vertex and store in header.
+ if (esdOK) {
+ treeESD->GetEvent(nev);
+ const AliESDVertex* esdVertex = esd->GetPrimaryVertex();
+ Double_t position[3];
+ esdVertex->GetXYZ(position);
+ AliGenEventHeader* mcHeader = new AliGenEventHeader("ESD");
+ TArrayF mcV;
+ mcV.Set(3);
+ for (Int_t i = 0; i < 3; i++) mcV[i] = position[i];
+ mcHeader->SetPrimaryVertex(mcV);
+ header->Reset(0,nev);
+ header->SetGenEventHeader(mcHeader);
+ printf("***** Saved vertex %f %f %f \n", position[0], position[1], position[2]);
+ }
+ nev++;
+//
+// Finish the event
+ runLoader->TreeE()->Fill();
+ runLoader->SetNextEvent();
+ } // events
+
+ delete rawReader;
+//
+// Finish the run
+ runLoader->CdGAFile();
+ runLoader->WriteHeader("OVERWRITE");
+ runLoader->WriteRunLoader();
+
+ return kTRUE;
+}
+
+//_____________________________________________________________________________
+Int_t AliSimulation::GetDetIndex(const char* detector)
+{
+ // return the detector index corresponding to detector
+ Int_t index = -1 ;
+ for (index = 0; index < fgkNDetectors ; index++) {
+ if ( strcmp(detector, fgkDetectorName[index]) == 0 )
+ break ;
+ }
+ return index ;
+}
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::RunHLT()
+{
+ // Run the HLT simulation
+ // HLT simulation is implemented in HLT/sim/AliHLTSimulation
+ // Disabled if fRunHLT is empty, default vaule is "default".
+ // AliSimulation::SetRunHLT can be used to set the options for HLT simulation
+ // The default simulation depends on the HLT component libraries and their
+ // corresponding agents which define components and chains to run. See
+ // http://web.ift.uib.no/~kjeks/doc/alice-hlt/
+ // http://web.ift.uib.no/~kjeks/doc/alice-hlt/classAliHLTModuleAgent.html
+ //
+ // The libraries to be loaded can be specified as an option.
+ // <pre>
+ // AliSimulation sim;
+ // sim.SetRunHLT("libAliHLTSample.so");
+ // </pre>
+ // will only load <tt>libAliHLTSample.so</tt>
+
+ // Other available options:
+ // \li loglevel=<i>level</i> <br>
+ // logging level for this processing
+ // \li alilog=off
+ // disable redirection of log messages to AliLog class
+ // \li config=<i>macro</i>
+ // configuration macro
+ // \li localrec=<i>configuration</i>
+ // comma separated list of configurations to be run during simulation
+
+ int iResult=0;
+ AliRunLoader* pRunLoader = LoadRun("READ");
+ if (!pRunLoader) return kFALSE;
+
+ // initialize CDB storage, run number, set CDB lock
+ InitCDB();
+ if (!SetRunNumberFromData()) if (fStopOnError) return kFALSE;
+ SetCDBLock();
+
+ // load the library dynamically
+ gSystem->Load(ALIHLTSIMULATION_LIBRARY);
+
+ // check for the library version
+ AliHLTSimulationGetLibraryVersion_t fctVersion=(AliHLTSimulationGetLibraryVersion_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_GET_LIBRARY_VERSION));
+ if (!fctVersion) {
+ AliError(Form("can not load library %s", ALIHLTSIMULATION_LIBRARY));
+ return kFALSE;
+ }
+ if (fctVersion()!= ALIHLTSIMULATION_LIBRARY_VERSION) {
+ AliError(Form("%s version does not match: compiled for version %d, loaded %d", ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_LIBRARY_VERSION, fctVersion()));
+ return kFALSE;
+ }
+
+ // print compile info
+ typedef void (*CompileInfo)( char*& date, char*& time);
+ CompileInfo fctInfo=(CompileInfo)gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, "CompileInfo");
+ if (fctInfo) {
+ char* date="";
+ char* time="";
+ (*fctInfo)(date, time);
+ if (!date) date="unknown";
+ if (!time) time="unknown";
+ AliInfo(Form("%s build on %s (%s)", ALIHLTSIMULATION_LIBRARY, date, time));
+ } else {
+ AliInfo(Form("no build info available for %s", ALIHLTSIMULATION_LIBRARY));
+ }
+
+ // create instance of the HLT simulation
+ AliHLTSimulationCreateInstance_t fctCreate=(AliHLTSimulationCreateInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_CREATE_INSTANCE));
+ AliHLTSimulation* pHLT=NULL;
+ if (fctCreate==NULL || (pHLT=(fctCreate()))==NULL) {
+ AliError(Form("can not create instance of HLT simulation (creator %p)", fctCreate));
+ return kFALSE;
+ }
+
+ // init the HLT simulation
+ TString options;
+ if (fRunHLT.CompareTo("default")!=0) options=fRunHLT;
+ if (!IsSelected("HLT", fWriteRawData)) {
+ options+=" writerawfiles=";
+ } else {
+ options+=" writerawfiles=HLT";
+ }
+ AliHLTSimulationInit_t fctInit=(AliHLTSimulationInit_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_INIT));
+ if (fctInit==NULL || (iResult=(fctInit(pHLT, pRunLoader, options.Data())))<0) {
+ AliError(Form("can not init HLT simulation: error %d (init %p)", iResult, fctInit));
+ } else {
+ // run the HLT simulation
+ AliHLTSimulationRun_t fctRun=(AliHLTSimulationRun_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_RUN));
+ if (fctRun==NULL || (iResult=(fctRun(pHLT, pRunLoader)))<0) {
+ AliError(Form("can not run HLT simulation: error %d (run %p)", iResult, fctRun));
+ }
+ }
+
+ // delete the instance
+ AliHLTSimulationDeleteInstance_t fctDelete=(AliHLTSimulationDeleteInstance_t)(gSystem->DynFindSymbol(ALIHLTSIMULATION_LIBRARY, ALIHLTSIMULATION_DELETE_INSTANCE));
+ if (fctDelete==NULL || fctDelete(pHLT)<0) {
+ AliError(Form("can not delete instance of HLT simulation (creator %p)", fctDelete));
+ }
+ pHLT=NULL;
+
+ return iResult>=0?kTRUE:kFALSE;
+}
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::RunQA()
+{
+ // run the QA on summable hits, digits or digits
+
+ fQASteer->SetRunLoader(gAlice->GetRunLoader()) ;
+
+ TString detectorsw("") ;
+ Bool_t rv = kTRUE ;
+ detectorsw = fQASteer->Run(fQADetectors.Data()) ;
+ if ( detectorsw.IsNull() )
+ rv = kFALSE ;
+ return rv ;
+}
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::SetRunQA(TString detAndAction)
+{
+ // Allows to run QA for a selected set of detectors
+ // and a selected set of tasks among HITS, SDIGITS and DIGITS
+ // all selected detectors run the same selected tasks
+
+ if (!detAndAction.Contains(":")) {
+ AliError( Form("%s is a wrong syntax, use \"DetectorList:ActionList\" \n", detAndAction.Data()) ) ;
+ fRunQA = kFALSE ;
+ return kFALSE ;
+ }
+ Int_t colon = detAndAction.Index(":") ;
+ fQADetectors = detAndAction(0, colon) ;
+ if (fQADetectors.Contains("ALL") )
+ fQADetectors = Form("%s %s", fMakeDigits.Data(), fMakeDigitsFromHits.Data()) ;
+ fQATasks = detAndAction(colon+1, detAndAction.Sizeof() ) ;
+ if (fQATasks.Contains("ALL") ) {
+ fQATasks = Form("%d %d %d", AliQA::kHITS, AliQA::kSDIGITS, AliQA::kDIGITS) ;
+ } else {
+ fQATasks.ToUpper() ;
+ TString tempo("") ;
+ if ( fQATasks.Contains("HIT") )
+ tempo = Form("%d ", AliQA::kHITS) ;
+ if ( fQATasks.Contains("SDIGIT") )
+ tempo += Form("%d ", AliQA::kSDIGITS) ;
+ if ( fQATasks.Contains("DIGIT") )
+ tempo += Form("%d ", AliQA::kDIGITS) ;
+ fQATasks = tempo ;
+ if (fQATasks.IsNull()) {
+ AliInfo("No QA requested\n") ;
+ fRunQA = kFALSE ;
+ return kTRUE ;
+ }
+ }
+ TString tempo(fQATasks) ;
+ tempo.ReplaceAll(Form("%d", AliQA::kHITS), AliQA::GetTaskName(AliQA::kHITS)) ;
+ tempo.ReplaceAll(Form("%d", AliQA::kSDIGITS), AliQA::GetTaskName(AliQA::kSDIGITS)) ;
+ tempo.ReplaceAll(Form("%d", AliQA::kDIGITS), AliQA::GetTaskName(AliQA::kDIGITS)) ;
+ AliInfo( Form("QA will be done on \"%s\" for \"%s\"\n", fQADetectors.Data(), tempo.Data()) ) ;
+ fRunQA = kTRUE ;
+ fQASteer->SetActiveDetectors(fQADetectors) ;
+ fQASteer->SetTasks(fQATasks) ;
+ return kTRUE;
+}
+
+//_____________________________________________________________________________
+void AliSimulation::ProcessEnvironmentVars()
+{
+// Extract run number and random generator seed from env variables
+
+ AliInfo("Processing environment variables");
+
+ // Random Number seed
+
+ // first check that seed is not already set
+ if (fSeed == 0) {
+ if (gSystem->Getenv("CONFIG_SEED")) {
+ fSeed = atoi(gSystem->Getenv("CONFIG_SEED"));
+ }
+ } else {
+ if (gSystem->Getenv("CONFIG_SEED")) {
+ AliInfo(Form("Seed for random number generation already set (%d)"
+ ": CONFIG_SEED variable ignored!", fSeed));
+ }
+ }
+
+ AliInfo(Form("Seed for random number generation = %d ", fSeed));
+
+ // Run Number
+
+ // first check that run number is not already set
+ if(fRun < 0) {
+ if (gSystem->Getenv("DC_RUN")) {
+ fRun = atoi(gSystem->Getenv("DC_RUN"));
+ }
+ } else {
+ if (gSystem->Getenv("DC_RUN")) {
+ AliInfo(Form("Run number already set (%d): DC_RUN variable ignored!", fRun));
+ }
+ }
+
+ AliInfo(Form("Run number = %d", fRun));
+}
+
+//_____________________________________________________________________________
+void AliSimulation::WriteGRPEntry()
+{
+ // Get the necessary information from galice (generator, trigger etc) and
+ // write a GRP entry corresponding to the settings in the Config.C used
+ AliInfo("Writing global run parameters entry into the OCDB");
+
+ TMap *grpMap = new TMap();
+ grpMap->SetName("MONTECARLO");
+
+ grpMap->Add(new TObjString("fRunType"),new TObjString("PHYSICS"));
+ grpMap->Add(new TObjString("fAliceStartTime"),new TObjString("0"));
+ grpMap->Add(new TObjString("fAliceStopTime"),new TObjString("9999"));
+
+ const AliGenerator *gen = gAlice->Generator();
+ if (gen) {
+ grpMap->Add(new TObjString("fAliceBeamEnergy"),new TObjString(Form("%f",gen->GetEnergyCMS())));
+ TString projectile;
+ Int_t a,z;
+ gen->GetProjectile(projectile,a,z);
+ TString target;
+ gen->GetTarget(target,a,z);
+ TString beamType = projectile + "-" + target;
+ if (!beamType.CompareTo("-")) {
+ grpMap->Add(new TObjString("fAliceBeamType"),new TObjString("UNKNOWN"));
+ }
+ else {
+ grpMap->Add(new TObjString("fAliceBeamType"),new TObjString(beamType.Data()));
+ }
+ }
+ else {
+ AliWarning("Unknown beam type and energy!");
+ grpMap->Add(new TObjString("fAliceBeamEnergy"),new TObjString("UNKNOWN"));
+ grpMap->Add(new TObjString("fAliceBeamType"),new TObjString("0"));
+ }
+
+ UInt_t detectorPattern = 0;
+ Int_t nDets = 0;
+ TObjArray *detArray = gAlice->Detectors();
+ for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors-1; iDet++) {
+ if (detArray->FindObject(AliDAQ::OfflineModuleName(iDet))) {
+ detectorPattern |= (1 << iDet);
+ nDets++;
+ }
+ }
+ // HLT
+ if (!fRunHLT.IsNull())
+ detectorPattern |= (1 << AliDAQ::kHLTId);
+
+ grpMap->Add(new TObjString("fNumberOfDetectors"),new TObjString(Form("%d",nDets)));
+ grpMap->Add(new TObjString("fDetectorMask"),new TObjString(Form("%u",detectorPattern)));
+
+ grpMap->Add(new TObjString("fLHCPeriod"),new TObjString("LHC08c"));
+
+ grpMap->Add(new TObjString("fLHCState"),new TObjString("STABLE BEAMS"));
+ grpMap->Add(new TObjString("fLHCCondition"),new TObjString("0"));
+ grpMap->Add(new TObjString("fLHCLuminosity"),new TObjString("0"));
+ grpMap->Add(new TObjString("fBeamIntensity"),new TObjString("0"));
+
+ AliMagF *field = gAlice->Field();
+ Float_t solenoidField = TMath::Abs(field->SolenoidField());
+ Float_t factor = field->Factor();
+ Float_t l3current = TMath::Abs(factor)*solenoidField*30000./5.;
+ grpMap->Add(new TObjString("fL3Current"),new TObjString(Form("%f",l3current)));
+
+ if (factor > 0) {
+ grpMap->Add(new TObjString("fL3Polarity"),new TObjString("0"));
+ grpMap->Add(new TObjString("fDipolePolarity"),new TObjString("0"));
+ }
+ else {
+ grpMap->Add(new TObjString("fL3Polarity"),new TObjString("1"));
+ grpMap->Add(new TObjString("fDipolePolarity"),new TObjString("1"));
+ }
+
+ if (TMath::Abs(factor) != 0)
+ grpMap->Add(new TObjString("fDipoleCurrent"),new TObjString("6000"));
+ else
+ grpMap->Add(new TObjString("fDipoleCurrent"),new TObjString("0"));
+
+ grpMap->Add(new TObjString("fCavernTemperature"),new TObjString("0"));
+ grpMap->Add(new TObjString("fCavernPressure"),new TObjString("0"));
+
+ // Now store the entry in OCDB
+ AliCDBManager* man = AliCDBManager::Instance();
+
+ AliCDBId id("GRP/GRP/Data", man->GetRun(), man->GetRun());
+ AliCDBMetaData *metadata= new AliCDBMetaData();
+
+ metadata->SetResponsible("alice-off@cern.ch");
+ metadata->SetComment("Automatically produced GRP entry for Monte Carlo");
+
+ man->Put(grpMap,id,metadata);
+}