]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliSimulation.cxx
Splitting of TRD library (T.Kuhr)
[u/mrichter/AliRoot.git] / STEER / AliSimulation.cxx
index da034993db79deb185e2212693065a3cad3aa1a0..0aca64ef70308fd8ebdda8afb6a2068a5b9f9299 100644 (file)
 // this feature is not available for all detectors and that merging is not   //
 // possible, when digits are created directly from hits.                     //
 //                                                                           //
-// Backgound events can be merged by calling                                 //
+// Background events can be merged by calling                                //
 //                                                                           //
 //   sim.MergeWith("background/galice.root", 2);                             //
 //                                                                           //
 // The first argument is the file name of the background galice file. The    //
 // second argument is the number of signal events per background event.      //
-// The default value for this is 1. MergeWith can be called several times    //
-// to merge more than two event streams. It is assumed that the sdigits      //
-// were already produced for the background events.                          //
+// By default this number is calculated from the number of available         //
+// background events. MergeWith can be called several times to merge more    //
+// than two event streams. It is assumed that the sdigits were already       //
+// produced for the background events.                                       //
 //                                                                           //
-// The methods RunSimulation, RunSDigitization, RunDigitization and          //
-// RunHitsDigitization can be used to run only parts of the full simulation  //
-// chain.                                                                    //
+// The output of raw data can be switched on by calling                      //
+//                                                                           //
+//   sim.SetWriteRawData("MUON");   // write raw data for MUON               //
+//                                                                           //
+// The methods RunSimulation, RunSDigitization, RunDigitization,             //
+// RunHitsDigitization and WriteRawData can be used to run only parts of     //
+// the full simulation chain.                                                //
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
+#include <TObjString.h>
+#include <TStopwatch.h>
+#include <TSystem.h>
 
-#include "AliSimulation.h"
-#include "AliRunLoader.h"
-#include "AliRun.h"
-#include "AliModule.h"
+#include "AliDigitizer.h"
 #include "AliGenerator.h"
-#include "AliVertexGenFile.h"
+#include "AliModule.h"
+#include "AliRun.h"
 #include "AliRunDigitizer.h"
-#include "AliDigitizer.h"
-#include <TObjString.h>
-
+#include "AliRunLoader.h"
+#include "AliSimulation.h"
+#include "AliVertexGenFile.h"
 
 ClassImp(AliSimulation)
 
@@ -102,6 +108,7 @@ AliSimulation::AliSimulation(const char* configFileName,
   fMakeSDigits("ALL"),
   fMakeDigits("ALL"),
   fMakeDigitsFromHits(""),
+  fWriteRawData(""),
   fStopOnError(kFALSE),
 
   fNEvents(1),
@@ -113,6 +120,7 @@ AliSimulation::AliSimulation(const char* configFileName,
 {
 // create simulation object with default parameters
 
+  SetGAliceFile("galice.root");
 }
 
 //_____________________________________________________________________________
@@ -124,6 +132,7 @@ AliSimulation::AliSimulation(const AliSimulation& sim) :
   fMakeSDigits(sim.fMakeSDigits),
   fMakeDigits(sim.fMakeDigits),
   fMakeDigitsFromHits(sim.fMakeDigitsFromHits),
+  fWriteRawData(sim.fWriteRawData),
   fStopOnError(sim.fStopOnError),
 
   fNEvents(sim.fNEvents),
@@ -180,6 +189,21 @@ void AliSimulation::SetConfigFile(const char* fileName)
   fConfigFileName = fileName;
 }
 
+//_____________________________________________________________________________
+void AliSimulation::SetGAliceFile(const char* fileName)
+{
+// set the name of the galice file
+// the path is converted to an absolute one if it is relative
+
+  fGAliceFileName = fileName;
+  if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) {
+    char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(),
+                                               fGAliceFileName);
+    fGAliceFileName = absFileName;
+    delete[] absFileName;
+  }
+}
+
 //_____________________________________________________________________________
 void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
 {
@@ -229,6 +253,13 @@ Bool_t AliSimulation::Run(Int_t nEvents)
     }
   }
 
+  // digits -> raw data
+  if (!fWriteRawData.IsNull()) {
+    if (!WriteRawData(fWriteRawData)) {
+      if (fStopOnError) return kFALSE;
+    }
+  }
+
   return kTRUE;
 }
 
@@ -259,18 +290,19 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
          "Check your config file: %s", fConfigFileName.Data());
     return kFALSE;
   }
-  fGAliceFileName = runLoader->GetFileName();
+  SetGAliceFile(runLoader->GetFileName());
 
   if (!gAlice->Generator()) {
     Error("RunSimulation", "gAlice has no generator object. "
          "Check your config file: %s", fConfigFileName.Data());
     return kFALSE;
   }
+  if (nEvents <= 0) nEvents = fNEvents;
 
   // get vertex from background file in case of merging
   if (fUseBkgrdVertex &&
       fBkgrdFileNames && (fBkgrdFileNames->GetEntriesFast() > 0)) {
-    Int_t signalPerBkgrd = fBkgrdFileNames->At(0)->GetUniqueID();
+    Int_t signalPerBkgrd = GetNSignalPerBkgrd(nEvents);
     const char* fileName = ((TObjString*)
                            (fBkgrdFileNames->At(0)))->GetName();
     Info("RunSimulation", "The vertex will be taken from the background "
@@ -285,7 +317,6 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
   }
 
   Info("RunSimulation", "running gAlice");
-  if (nEvents <= 0) nEvents = fNEvents;
   gAlice->Run(nEvents);
 
   delete runLoader;
@@ -315,7 +346,11 @@ Bool_t AliSimulation::RunSDigitization(const char* detectors)
     if (IsSelected(det->GetName(), detStr)) {
       Info("RunSDigitization", "creating summable digits for %s", 
           det->GetName());
+      TStopwatch stopwatchDet;
+      stopwatchDet.Start();
       det->Hits2SDigits();
+      Info("RunSDigitization", "execution time for %s:", det->GetName());
+      stopwatchDet.Print();
     }
   }
 
@@ -348,11 +383,8 @@ Bool_t AliSimulation::RunDigitization(const char* detectors,
   gAlice = NULL;
 
   Int_t nStreams = 1;
-  Int_t signalPerBkgrd = 1;
-  if (fBkgrdFileNames) {
-    nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
-    if (nStreams > 1) signalPerBkgrd = fBkgrdFileNames->At(0)->GetUniqueID();
-  }
+  if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
+  Int_t signalPerBkgrd = GetNSignalPerBkgrd();
   AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
   manager->SetInputStream(0, fGAliceFileName.Data());
   for (Int_t iStream = 1; iStream < nStreams; iStream++) {
@@ -438,6 +470,57 @@ Bool_t AliSimulation::RunHitsDigitization(const char* detectors)
   return kTRUE;
 }
 
+//_____________________________________________________________________________
+Bool_t AliSimulation::WriteRawData(const char* detectors)
+{
+// convert the digits to raw data
+
+  TStopwatch stopwatch;
+  stopwatch.Start();
+
+  AliRunLoader* runLoader = LoadRun();
+  if (!runLoader) return kFALSE;
+
+  for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
+    Info("WriteRawData", "processing event %d", iEvent);
+    runLoader->GetEvent(iEvent);
+    TString baseDir = gSystem->WorkingDirectory();
+    char dirName[256];
+    sprintf(dirName, "raw%d", iEvent);
+    gSystem->MakeDirectory(dirName);
+    if (!gSystem->ChangeDirectory(dirName)) {
+      Error("WriteRawData", "couldn't change to directory %s", dirName);
+      if (fStopOnError) return kFALSE; else continue;
+    }
+
+    TString detStr = detectors;
+    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;
+      if (IsSelected(det->GetName(), detStr)) {
+       Info("WriteRawData", "creating raw data from digits for %s", 
+            det->GetName());
+       det->Digits2Raw();
+      }
+    }
+
+    gSystem->ChangeDirectory(baseDir);
+    if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
+      Error("WriteRawData", "the following detectors were not found: %s", 
+           detStr.Data());
+      if (fStopOnError) return kFALSE;
+    }
+  }
+
+  delete runLoader;
+
+  Info("WriteRawData", "execution time:");
+  stopwatch.Print();
+
+  return kTRUE;
+}
+
 
 //_____________________________________________________________________________
 AliRunLoader* AliSimulation::LoadRun() const
@@ -447,7 +530,7 @@ AliRunLoader* AliSimulation::LoadRun() const
   while (AliRunLoader::GetRunLoader()) delete AliRunLoader::GetRunLoader();
   AliRunLoader* runLoader = 
     AliRunLoader::Open(fGAliceFileName.Data(), 
-                      AliConfig::fgkDefaultEventFolderName, "UPDATE");
+                      AliConfig::GetDefaultEventFolderName(), "UPDATE");
   if (!runLoader) {
     Error("LoadRun", "no run loader found in file %s", 
          fGAliceFileName.Data());
@@ -463,6 +546,58 @@ AliRunLoader* AliSimulation::LoadRun() const
   return runLoader;
 }
 
+//_____________________________________________________________________________
+Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
+{
+// get or calculate the number of signal events per background event
+
+  if (!fBkgrdFileNames) return 1;
+  Int_t nBkgrdFiles = fBkgrdFileNames->GetEntriesFast();
+  if (nBkgrdFiles == 0) return 1;
+
+  // get the number of signal events
+  if (nEvents <= 0) {
+    AliRunLoader* runLoader = 
+      AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
+    if (!runLoader) return 1;
+    nEvents = runLoader->GetNumberOfEvents();
+    delete runLoader;
+  }
+
+  Int_t result = 0;
+  for (Int_t iBkgrdFile = 0; iBkgrdFile < nBkgrdFiles; iBkgrdFile++) {
+    // get the number of background events
+    const char* fileName = ((TObjString*)
+                           (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
+    AliRunLoader* runLoader = 
+      AliRunLoader::Open(fileName, "BKGRD");
+    if (!runLoader) continue;
+    Int_t nBkgrdEvents = runLoader->GetNumberOfEvents();
+    delete runLoader;
+
+    // get or calculate the number of signal per background events
+    Int_t nSignalPerBkgrd = fBkgrdFileNames->At(iBkgrdFile)->GetUniqueID();
+    if (nSignalPerBkgrd <= 0) {
+      nSignalPerBkgrd = (nEvents-1) / nBkgrdEvents + 1;
+    } else if (result && (result != nSignalPerBkgrd)) {
+      Info("GetNSignalPerBkgrd", "the number of signal events per "
+          "background event will be changed from %d to %d for stream %d", 
+          nSignalPerBkgrd, result, iBkgrdFile+1);
+      nSignalPerBkgrd = result;
+    }
+
+    if (!result) result = nSignalPerBkgrd;
+    if (nSignalPerBkgrd * nBkgrdEvents < nEvents) {
+      Warning("GetNSignalPerBkgrd", "not enough background events (%d) for "
+             "%d signal events using %d signal per background events for "
+             "stream %d", 
+             nBkgrdEvents, nEvents, nSignalPerBkgrd, iBkgrdFile+1);
+    }
+  }
+
+  return result;
+}
+
 //_____________________________________________________________________________
 Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
 {