]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliSimulation.cxx
Major commit related to steering of the reco parameters: AliDAQ and trigger classes...
[u/mrichter/AliRoot.git] / STEER / AliSimulation.cxx
index 0720bd8b6dfeb01a172a87cc5fe5191074a132b8..0887a7bb3639c8a37c5dd9f2874878460fca4678 100644 (file)
 #include "AliMC.h"
 #include "AliHLTSimulation.h"
 #include "AliQADataMakerSteer.h"
+#include "AliSysInfo.h"
 
 ClassImp(AliSimulation)
 
@@ -161,6 +162,7 @@ AliSimulation::AliSimulation(const char* configFileName,
   fWriteRawData(""),
   fRawDataFileName(""),
   fDeleteIntermediateFiles(kFALSE),
+  fWriteSelRawData(kFALSE),
   fStopOnError(kFALSE),
 
   fNEvents(1),
@@ -179,6 +181,8 @@ AliSimulation::AliSimulation(const char* configFileName,
   fInitRunNumberCalled(kFALSE),
   fSetRunNumberFromDataCalled(kFALSE),
   fEmbeddingFlag(kFALSE),
+  fQADetectors("ALL"),                  
+  fQATasks("ALL"),     
   fRunQA(kTRUE), 
   fRunHLT("default")
 {
@@ -206,6 +210,7 @@ AliSimulation::AliSimulation(const AliSimulation& sim) :
   fWriteRawData(sim.fWriteRawData),
   fRawDataFileName(""),
   fDeleteIntermediateFiles(kFALSE),
+  fWriteSelRawData(kFALSE),
   fStopOnError(sim.fStopOnError),
 
   fNEvents(sim.fNEvents),
@@ -224,7 +229,9 @@ AliSimulation::AliSimulation(const AliSimulation& sim) :
   fInitRunNumberCalled(sim.fInitRunNumberCalled),
   fSetRunNumberFromDataCalled(sim.fSetRunNumberFromDataCalled),
   fEmbeddingFlag(sim.fEmbeddingFlag),
-  fRunQA(kTRUE), 
+  fQADetectors(sim.fQADetectors),                  
+  fQATasks(sim.fQATasks),      
+  fRunQA(sim.fRunQA), 
   fRunHLT(sim.fRunHLT)
 {
 // copy constructor
@@ -637,25 +644,46 @@ Bool_t AliSimulation::Run(Int_t nEvents)
   if (!AliGeomManager::GetGeometry()) {
     // Initialize the geometry manager
     AliGeomManager::LoadGeometry("geometry.root");
+    
+//    // Check that the consistency of symbolic names for the activated subdetectors
+//    // in the geometry loaded by AliGeomManager
+//    AliRunLoader* runLoader = LoadRun("READ");
+//    if (!runLoader) return kFALSE;
+//
+//    TString detsToBeChecked = "";
+//    TObjArray* detArray = runLoader->GetAliRun()->Detectors();
+//    for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
+//      AliModule* det = (AliModule*) detArray->At(iDet);
+//      if (!det || !det->IsActive()) continue;
+//      detsToBeChecked += det->GetName();
+//      detsToBeChecked += " ";
+//    } // end loop over detectors
+//    if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
+    if(!AliGeomManager::CheckSymNamesLUT("ALL"))
+       AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
+       
     if (!AliGeomManager::GetGeometry()) if (fStopOnError) return kFALSE;
     // Misalign geometry
     if(!MisalignGeometry()) if (fStopOnError) return kFALSE;
   }
 
+
   // hits -> summable digits
+  AliSysInfo::AddStamp("Start_sdigitization");
   if (!fMakeSDigits.IsNull()) {
     if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
  
   }
+  AliSysInfo::AddStamp("Stop_sdigitization");
   
-
-  
+  AliSysInfo::AddStamp("Start_digitization");  
   // summable digits -> digits  
   if (!fMakeDigits.IsNull()) {
     if (!RunDigitization(fMakeDigits, fMakeDigitsFromHits)) {
       if (fStopOnError) return kFALSE;
     }
    }
+  AliSysInfo::AddStamp("Stop_digitization");
 
   
   
@@ -684,7 +712,7 @@ Bool_t AliSimulation::Run(Int_t nEvents)
   // digits -> raw data
   if (!fWriteRawData.IsNull()) {
     if (!WriteRawData(fWriteRawData, fRawDataFileName, 
-                     fDeleteIntermediateFiles)) {
+                     fDeleteIntermediateFiles,fWriteSelRawData)) {
       if (fStopOnError) return kFALSE;
     }
   }
@@ -827,6 +855,19 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
   // Misalign geometry
 #if ROOT_VERSION_CODE < 331527
   AliGeomManager::SetGeometry(gGeoManager);
+  
+  // Check that the consistency of symbolic names for the activated subdetectors
+  // in the geometry loaded by AliGeomManager
+  TString detsToBeChecked = "";
+  TObjArray* detArray = runLoader->GetAliRun()->Detectors();
+  for (Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++) {
+    AliModule* det = (AliModule*) detArray->At(iDet);
+    if (!det || !det->IsActive()) continue;
+    detsToBeChecked += det->GetName();
+    detsToBeChecked += " ";
+  } // end loop over detectors
+  if(!AliGeomManager::CheckSymNamesLUT(detsToBeChecked.Data()))
+    AliFatalClass("Current loaded geometry differs in the definition of symbolic names!");
   MisalignGeometry(runLoader);
 #endif
 
@@ -890,10 +931,11 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
   }
 
   AliInfo("running gAlice");
+  AliSysInfo::AddStamp("Start_simulation");
   StdoutToAliInfo(StderrToAliError(
     gAlice->Run(nEvents);
   ););
-
+  AliSysInfo::AddStamp("Stop_simulation");
   delete runLoader;
 
   return kTRUE;
@@ -903,7 +945,7 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
 Bool_t AliSimulation::RunSDigitization(const char* detectors)
 {
 // run the digitization and produce summable digits
-
+  static Int_t eventNr=0;
   AliCodeTimerAuto("")
 
   // initialize CDB storage, run number, set CDB lock
@@ -922,8 +964,8 @@ Bool_t AliSimulation::RunSDigitization(const char* detectors)
     if (IsSelected(det->GetName(), detStr)) {
       AliInfo(Form("creating summable digits for %s", det->GetName()));
       AliCodeTimerAuto(Form("creating summable digits for %s", det->GetName()));
-         
       det->Hits2SDigits();
+      AliSysInfo::AddStamp(Form("Digit_%s_%d",det->GetName(),eventNr), 0,1, eventNr);
     }
   }
 
@@ -932,7 +974,7 @@ Bool_t AliSimulation::RunSDigitization(const char* detectors)
                   detStr.Data()));
     if (fStopOnError) return kFALSE;
   }
-
+  eventNr++;
   delete runLoader;
 
   return kTRUE;
@@ -1048,7 +1090,8 @@ Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
 //_____________________________________________________________________________
 Bool_t AliSimulation::WriteRawData(const char* detectors, 
                                   const char* fileName,
-                                  Bool_t deleteIntermediateFiles)
+                                  Bool_t deleteIntermediateFiles,
+                                  Bool_t selrawdata)
 {
 // convert the digits to raw data
 // First DDL raw data files for the given detectors are created.
@@ -1058,10 +1101,19 @@ Bool_t AliSimulation::WriteRawData(const char* detectors,
 // If the file name has the extension ".root", the DATE file is converted
 // to a root file.
 // If deleteIntermediateFiles is true, the DATE file is deleted afterwards.
+// 'selrawdata' flag can be used to enable writing of detectors raw data
+// accoring to the trigger cluster.
 
   AliCodeTimerAuto("")
+  
+  TString detStr = detectors;
+  if (IsSelected("HLT", detStr))
+  {
+       // Do nothing. "HLT" will be removed from detStr because the HLT raw
+       // data files are generated in RunHLT.
+  }
 
-  if (!WriteRawFiles(detectors)) {
+  if (!WriteRawFiles(detStr.Data())) {
     if (fStopOnError) return kFALSE;
   }
 
@@ -1069,7 +1121,12 @@ Bool_t AliSimulation::WriteRawData(const char* detectors,
   if (!dateFileName.IsNull()) {
     Bool_t rootOutput = dateFileName.EndsWith(".root");
     if (rootOutput) dateFileName += ".date";
-    if (!ConvertRawFilesToDate(dateFileName)) {
+    TString selDateFileName;
+    if (selrawdata) {
+      selDateFileName = "selected.";
+      selDateFileName+= dateFileName;
+    }
+    if (!ConvertRawFilesToDate(dateFileName,selDateFileName)) {
       if (fStopOnError) return kFALSE;
     }
     if (deleteIntermediateFiles) {
@@ -1089,6 +1146,16 @@ Bool_t AliSimulation::WriteRawData(const char* detectors,
       if (deleteIntermediateFiles) {
        gSystem->Unlink(dateFileName);
       }
+      if (selrawdata) {
+       TString selFileName = "selected.";
+       selFileName        += fileName;
+       if (!ConvertDateToRoot(selDateFileName, selFileName)) {
+         if (fStopOnError) return kFALSE;
+       }
+       if (deleteIntermediateFiles) {
+         gSystem->Unlink(selDateFileName);
+       }
+      }
     }
   }
 
@@ -1146,9 +1213,12 @@ Bool_t AliSimulation::WriteRawFiles(const char* detectors)
 }
 
 //_____________________________________________________________________________
-Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName)
+Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName,
+                                           const char* selDateFileName)
 {
 // convert raw data DDL files to a DATE file with the program "dateStream"
+// The second argument is not empty when the user decides to write
+// the detectors raw data according to the trigger cluster.
 
   AliCodeTimerAuto("")
   
@@ -1164,6 +1234,9 @@ Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName)
   if (!runLoader) return kFALSE;
 
   AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
+  Bool_t selrawdata = kFALSE;
+  if (strcmp(selDateFileName,"") != 0) selrawdata = kTRUE;
+
   char command[256];
   // Note the option -s. It is used in order to avoid
   // the generation of SOR/EOR events.
@@ -1171,11 +1244,25 @@ Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName)
          dateFileName, runLoader->GetNumberOfEvents(),runLoader->GetHeader()->GetRun());
   FILE* pipe = gSystem->OpenPipe(command, "w");
 
+  Int_t selEvents = 0;
   for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
     fprintf(pipe, "GDC\n");
     Float_t ldc = 0;
     Int_t prevLDC = -1;
 
+    if (selrawdata) {
+      // Check if the event was triggered by CTP
+      runLoader->GetEvent(iEvent);
+      if (!runLoader->LoadTrigger()) {
+       AliCentralTrigger *aCTP = runLoader->GetTrigger();
+       if (aCTP->GetClassMask()) selEvents++;
+      }
+      else {
+       AliWarning("No trigger can be loaded! Writing of selected raw data is abandoned !");
+       selrawdata = kFALSE;
+      }
+    }
+
     // loop over detectors and DDLs
     for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
       for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
@@ -1207,8 +1294,70 @@ Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName)
 
   Int_t result = gSystem->ClosePipe(pipe);
 
+  if (!(selrawdata && selEvents > 0)) {
+    delete runLoader;
+    return (result == 0);
+  }
+
+  AliInfo(Form("converting selected by trigger cluster raw data DDL files to DATE file %s", selDateFileName));
+  
+  sprintf(command, "dateStream -c -s -D -o %s -# %d -C -run %d", 
+         selDateFileName,selEvents,runLoader->GetHeader()->GetRun());
+  FILE* pipe2 = gSystem->OpenPipe(command, "w");
+
+  for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
+
+    // Get the trigger decision and cluster
+    TString detClust;
+    runLoader->GetEvent(iEvent);
+    if (!runLoader->LoadTrigger()) {
+      AliCentralTrigger *aCTP = runLoader->GetTrigger();
+      if (aCTP->GetClassMask() == 0) continue;
+      detClust = AliDAQ::ListOfTriggeredDetectors(aCTP->GetClusterMask());
+      AliInfo(Form("List of detectors to be read out: %s",detClust.Data()));
+    }
+
+    fprintf(pipe2, "GDC\n");
+    Float_t ldc = 0;
+    Int_t prevLDC = -1;
+
+    // loop over detectors and DDLs
+    for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
+      // Write only raw data from detectors that
+      // are contained in the trigger cluster(s)
+      if (!IsSelected(AliDAQ::DetectorName(iDet),detClust)) continue;
+
+      for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
+
+        Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
+        Int_t ldcID = Int_t(ldc + 0.0001);
+        ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
+
+        char rawFileName[256];
+        sprintf(rawFileName, "raw%d/%s", 
+                iEvent, AliDAQ::DdlFileName(iDet,iDDL));
+
+       // check existence and size of raw data file
+        FILE* file = fopen(rawFileName, "rb");
+        if (!file) continue;
+        fseek(file, 0, SEEK_END);
+        unsigned long size = ftell(file);
+       fclose(file);
+        if (!size) continue;
+
+        if (ldcID != prevLDC) {
+          fprintf(pipe2, " LDC Id %d\n", ldcID);
+          prevLDC = ldcID;
+        }
+        fprintf(pipe2, "  Equipment Id %d Payload %s\n", ddlID, rawFileName);
+      }
+    }
+  }
+
+  Int_t result2 = gSystem->ClosePipe(pipe2);
+
   delete runLoader;
-  return (result == 0);
+  return ((result == 0) && (result2 == 0));
 }
 
 //_____________________________________________________________________________
@@ -1430,7 +1579,6 @@ Bool_t AliSimulation::ConvertRaw2SDigits(const char* rawDirectory, const char* e
       rawReader = new AliRawReaderRoot(fileName);
     } else if (!fileName.IsNull()) {
       rawReader = new AliRawReaderDate(fileName);
-      rawReader->SelectEvents(7);
     }
 //     if (!fEquipIdMap.IsNull() && fRawReader)
 //       fRawReader->LoadEquipmentIdsMap(fEquipIdMap);
@@ -1579,9 +1727,15 @@ Bool_t AliSimulation::RunHLT()
   }
 
   // init the HLT simulation
-  if (fRunHLT.CompareTo("default")==0) fRunHLT="";
+  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, fRunHLT.Data())))<0) {
+  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
@@ -1606,20 +1760,69 @@ Bool_t AliSimulation::RunQA()
 {
        // run the QA on summable hits, digits or digits
 
-       AliQADataMakerSteer qas ; 
+       AliQADataMakerSteer qas("sim") ; 
     qas.SetRunLoader(gAlice->GetRunLoader()) ;
 
-       Bool_t rv =  qas.Run("ALL", AliQA::kHITS) ; 
-//     qas.Reset() ; 
-       rv *= qas.Run(fMakeSDigits.Data(), AliQA::kSDIGITS) ;   
+       TString detectorsw("") ;  
+       Bool_t rv = kTRUE ; 
+       if (fQATasks.Contains(Form("%d", AliQA::kHITS))) 
+               detectorsw =  qas.Run(fQADetectors.Data(), AliQA::kHITS) ; 
 //     qas.Reset() ; 
-       rv *= qas.Run(fMakeDigits.Data(), AliQA::kDIGITS) ;     
+       if (fQATasks.Contains(Form("%d", AliQA::kSDIGITS))) 
+               detectorsw += qas.Run(fQADetectors.Data(), AliQA::kSDIGITS) ;   
 //     qas.Reset() ; 
-       rv *= qas.Run(fMakeDigitsFromHits.Data(), AliQA::kDIGITS) ; 
+       if (fQATasks.Contains(Form("%d", AliQA::kDIGITS))) 
+               detectorsw += qas.Run(fQADetectors.Data(), AliQA::kDIGITS) ;    
 
+       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 ;
+       return kTRUE; 
+} 
+
 //_____________________________________________________________________________
 void AliSimulation::ProcessEnvironmentVars()
 {
@@ -1658,3 +1861,5 @@ void AliSimulation::ProcessEnvironmentVars()
     
     AliInfo(Form("Run number = %d", fRun)); 
 }
+
+