]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliSimulation.cxx
A few improvements, a small bug fix, and removal of the dependency on AliTracker...
[u/mrichter/AliRoot.git] / STEER / AliSimulation.cxx
index bd3638fc448a374b9fb7ea16f9b0c9731d90b39c..2cc0e1f1fd53b655c53023d952a8541f9dd50660 100644 (file)
 #include "AliCDBManager.h"
 #include "AliAlignObj.h"
 #include "AliCentralTrigger.h"
-#include "AliDAQConfig.h"
+#include "AliDAQ.h"
 #include "AliDigitizer.h"
 #include "AliGenerator.h"
 #include "AliLog.h"
 #include "AliVertexGenFile.h"
 #include "AliCentralTrigger.h"
 #include "AliCTPRawData.h"
+#include "AliRawReaderFile.h"
+#include "AliESD.h"
+#include "AliHeader.h"
+#include "AliGenEventHeader.h"
 
 ClassImp(AliSimulation)
 
@@ -159,7 +163,8 @@ AliSimulation::AliSimulation(const char* configFileName, const char* cdbUri,
   fUseBkgrdVertex(kTRUE),
   fRegionOfInterest(kFALSE),
   fCDBUri(cdbUri),
-  fSpecCDBUri()
+  fSpecCDBUri(),
+  fEmbeddingFlag(kFALSE)
 {
 // create simulation object with default parameters
 
@@ -192,7 +197,8 @@ AliSimulation::AliSimulation(const AliSimulation& sim) :
   fUseBkgrdVertex(sim.fUseBkgrdVertex),
   fRegionOfInterest(sim.fRegionOfInterest),
   fCDBUri(sim.fCDBUri),
-  fSpecCDBUri()
+  fSpecCDBUri(),
+  fEmbeddingFlag(sim.fEmbeddingFlag)
 {
 // copy constructor
 
@@ -266,9 +272,9 @@ void AliSimulation::InitCDBStorage()
     fCDBUri = "";
   }
   else {
-    AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
-    AliWarning(Form("Default CDB storage is set to: %s",fCDBUri.Data()));
-    AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+    AliDebug(2,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+    AliDebug(2, Form("Default CDB storage is set to: %s",fCDBUri.Data()));
+    AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
     man->SetDefaultStorage(fCDBUri);
   }
 
@@ -276,12 +282,12 @@ void AliSimulation::InitCDBStorage()
   for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
     TObject* obj = fSpecCDBUri[i];
     if (!obj) continue;
-    AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
-    AliWarning(Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
-    AliWarning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
-    man->SetSpecificStorage(obj->GetName(),obj->GetTitle());
+    AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+    AliDebug(2, Form("Specific CDB storage for %s is set to: %s",obj->GetName(),obj->GetTitle()));
+    AliDebug(2, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+    man->SetSpecificStorage(obj->GetName(), obj->GetTitle());
   }
-  
+  man->Print();
 }
 
 //_____________________________________________________________________________
@@ -294,13 +300,19 @@ void AliSimulation::SetDefaultStorage(const char* uri) {
 }
 
 //_____________________________________________________________________________
-void AliSimulation::SetSpecificStorage(const char* detName, const char* uri) {
+void AliSimulation::SetSpecificStorage(const char* calibType, const char* uri) {
 // Store a detector-specific CDB storage location
 // Activate it later within the Run() method
 
-  TObject* obj = fSpecCDBUri.FindObject(detName);
+  AliCDBPath aPath(calibType);
+  if(!aPath.IsValid()){
+       AliError(Form("Not a valid path: %s", calibType));
+       return;
+  }
+
+  TObject* obj = fSpecCDBUri.FindObject(calibType);
   if (obj) fSpecCDBUri.Remove(obj);
-  fSpecCDBUri.Add(new TNamed(detName, uri));
+  fSpecCDBUri.Add(new TNamed(calibType, uri));
 
 }
 
@@ -360,7 +372,7 @@ Bool_t AliSimulation::ApplyAlignObjsToGeom(TObjArray* alObjArray)
     }
 
   if (AliDebugLevelClass() >= 1) {
-    gGeoManager->GetTopNode()->CheckOverlaps(20);
+    gGeoManager->GetTopNode()->CheckOverlaps(1);
     TObjArray* ovexlist = gGeoManager->GetListOfOverlaps();
     if(ovexlist->GetEntriesFast()){  
       AliError("The application of alignment objects to the geometry caused huge overlaps/extrusions!");
@@ -530,9 +542,9 @@ Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
   
        if ((detStr.CompareTo("ALL") == 0)) detStr = "";
        dataNotLoaded += detStr;
-       AliInfo(Form("Alignment data loaded for: %s",
+       if(!dataLoaded.IsNull()) AliInfo(Form("Alignment data loaded for: %s",
                          dataLoaded.Data()));
-       AliInfo(Form("Didn't/couldn't load alignment data for: %s",
+       if(!dataNotLoaded.IsNull()) AliInfo(Form("Didn't/couldn't load alignment data for: %s",
                          dataNotLoaded.Data()));
   } // fLoadAlignFromCDB flag
  
@@ -555,8 +567,25 @@ Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
     }
   }
 
+
+  // Update the internal geometry of modules (ITS needs it)
+  TString detStr = fLoadAlignData;
+  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)) {
+      det->UpdateInternalGeometry();
+    }
+  } // end loop over detectors
+
+
   if (delRunLoader) delete runLoader;
 
+  // Update the TGeoPhysicalNodes
+  gGeoManager->RefreshPhysicalNodes();
+
   return kTRUE;
 }
 
@@ -590,6 +619,12 @@ void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
   fBkgrdFileNames->Add(fileNameStr);
 }
 
+void AliSimulation::EmbedInto(const char* fileName, Int_t nSignalPerBkgrd)
+{
+// add a file with background events for embeddin
+  MergeWith(fileName, nSignalPerBkgrd);
+  fEmbeddingFlag = kTRUE;
+}
 
 //_____________________________________________________________________________
 Bool_t AliSimulation::Run(Int_t nEvents)
@@ -614,7 +649,7 @@ Bool_t AliSimulation::Run(Int_t nEvents)
     if (!gGeoManager) if (fStopOnError) return kFALSE;
     if (!MisalignGeometry()) if (fStopOnError) return kFALSE;
   }
-
+  
   // hits -> summable digits
   if (!fMakeSDigits.IsNull()) {
     if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
@@ -678,7 +713,7 @@ Bool_t AliSimulation::RunTrigger(const char* descriptors)
      }
    }
 
-   runLoader->MakeTree( "CT" );
+   runLoader->MakeTree( "GG" );
    AliCentralTrigger* aCTP = runLoader->GetTrigger();
   // Load Descriptors
    aCTP->LoadDescriptor( des );
@@ -766,21 +801,6 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
 //   }
   MisalignGeometry(runLoader);
 
-  // Temporary fix by A.Gheata
-  // Could be removed with the next Root version (>5.11)
-  if (gGeoManager) {
-    TIter next(gGeoManager->GetListOfVolumes());
-    TGeoVolume *vol;
-    while ((vol = (TGeoVolume *)next())) {
-      if (vol->GetVoxels()) {
-       if (vol->GetVoxels()->NeedRebuild()) {
-         vol->GetVoxels()->Voxelize();
-         vol->FindOverlaps();
-       }
-      }
-    }
-  }
-
   // Export (mis)aligned geometry 
   if (gGeoManager) gGeoManager->Export("misaligned_geometry.root");
 
@@ -914,6 +934,7 @@ Bool_t AliSimulation::RunDigitization(const char* detectors,
   if (fBkgrdFileNames) nStreams = fBkgrdFileNames->GetEntriesFast() + 1;
   Int_t signalPerBkgrd = GetNSignalPerBkgrd();
   AliRunDigitizer* manager = new AliRunDigitizer(nStreams, signalPerBkgrd);
+  // manager->SetEmbeddingFlag(fEmbeddingFlag);
   manager->SetInputStream(0, fGAliceFileName.Data());
   for (Int_t iStream = 1; iStream < nStreams; iStream++) {
     const char* fileName = ((TObjString*)
@@ -933,6 +954,7 @@ Bool_t AliSimulation::RunDigitization(const char* detectors,
     if (IsSelected(det->GetName(), detStr) && 
        !IsSelected(det->GetName(), detExcl)) {
       AliDigitizer* digitizer = det->CreateDigitizer(manager);
+      
       if (!digitizer) {
        AliError(Form("no digitizer for %s", det->GetName()));
        if (fStopOnError) return kFALSE;
@@ -1118,7 +1140,9 @@ Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName)
 
   AliInfo(Form("converting raw data DDL files to DATE file %s", dateFileName));
   char command[256];
-  sprintf(command, "dateStream -D -o %s -# %d -C", 
+  // Note the option -s. It is used in order to avoid
+  // the generation of SOR/EOR events.
+  sprintf(command, "dateStream -s -D -o %s -# %d -C", 
          dateFileName, runLoader->GetNumberOfEvents());
   FILE* pipe = gSystem->OpenPipe(command, "w");
 
@@ -1128,16 +1152,16 @@ Bool_t AliSimulation::ConvertRawFilesToDate(const char* dateFileName)
     Int_t prevLDC = -1;
 
     // loop over detectors and DDLs
-    for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
-      for (Int_t iDDL = 0; iDDL < kDetectorDDLs[iDet]; iDDL++) {
+    for (Int_t iDet = 0; iDet < AliDAQ::kNDetectors; iDet++) {
+      for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(iDet); iDDL++) {
 
-        Int_t ddlID = 0x100*iDet + iDDL;
+        Int_t ddlID = AliDAQ::DdlID(iDet,iDDL);
         Int_t ldcID = Int_t(ldc + 0.0001);
-        ldc += kDetectorLDCs[iDet] / kDetectorDDLs[iDet];
+        ldc += AliDAQ::NumberOfLdcs(iDet) / AliDAQ::NumberOfDdls(iDet);
 
         char rawFileName[256];
-        sprintf(rawFileName, "raw%d/%s_%d.ddl", 
-                iEvent, kDetectors[iDet], ddlID);
+        sprintf(rawFileName, "raw%d/%s", 
+                iEvent, AliDAQ::DdlFileName(iDet,iDDL));
 
        // check existence and size of raw data file
         FILE* file = fopen(rawFileName, "rb");
@@ -1169,10 +1193,10 @@ Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
 // convert a DATE file to a root file with the program "alimdc"
 
   // ALIMDC setup
-  const Int_t kDBSize = 1000000000;
+  const Int_t kDBSize = 2000000000;
   const Int_t kTagDBSize = 1000000000;
   const Bool_t kFilter = kFALSE;
-  const Int_t kCompression = 1;
+  const Int_t kCompression = 0;
 
   char* path = gSystem->Which(gSystem->Getenv("PATH"), "alimdc");
   if (!path) {
@@ -1187,7 +1211,6 @@ Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
 
   const char* rawDBFS[2] = { "/tmp/mdc1", "/tmp/mdc2" };
   const char* tagDBFS    = "/tmp/mdc1/tags";
-  const char* runDBFS    = "/tmp/mdc1/meta";
 
   // User defined file system locations
   if (gSystem->Getenv("ALIMDC_RAWDB1")) 
@@ -1196,18 +1219,14 @@ Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
     rawDBFS[1] = gSystem->Getenv("ALIMDC_RAWDB2");
   if (gSystem->Getenv("ALIMDC_TAGDB")) 
     tagDBFS = gSystem->Getenv("ALIMDC_TAGDB");
-  if (gSystem->Getenv("ALIMDC_RUNDB")) 
-    runDBFS = gSystem->Getenv("ALIMDC_RUNDB");
 
   gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
   gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
   gSystem->Exec(Form("rm -rf %s",tagDBFS));
-  gSystem->Exec(Form("rm -rf %s",runDBFS));
 
   gSystem->Exec(Form("mkdir %s",rawDBFS[0]));
   gSystem->Exec(Form("mkdir %s",rawDBFS[1]));
   gSystem->Exec(Form("mkdir %s",tagDBFS));
-  gSystem->Exec(Form("mkdir %s",runDBFS));
 
   Int_t result = gSystem->Exec(Form("alimdc %d %d %d %d %s", 
                                    kDBSize, kTagDBSize, kFilter, kCompression, dateFileName));
@@ -1216,7 +1235,6 @@ Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
   gSystem->Exec(Form("rm -rf %s",rawDBFS[0]));
   gSystem->Exec(Form("rm -rf %s",rawDBFS[1]));
   gSystem->Exec(Form("rm -rf %s",tagDBFS));
-  gSystem->Exec(Form("rm -rf %s",runDBFS));
 
   return (result == 0);
 }
@@ -1257,8 +1275,9 @@ Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
   // get the number of signal events
   if (nEvents <= 0) {
     AliRunLoader* runLoader = 
-      AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
+       AliRunLoader::Open(fGAliceFileName.Data(), "SIGNAL");
     if (!runLoader) return 1;
+    
     nEvents = runLoader->GetNumberOfEvents();
     delete runLoader;
   }
@@ -1268,12 +1287,12 @@ Int_t AliSimulation::GetNSignalPerBkgrd(Int_t nEvents) const
     // get the number of background events
     const char* fileName = ((TObjString*)
                            (fBkgrdFileNames->At(iBkgrdFile)))->GetName();
-    AliRunLoader* runLoader = 
+    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) {
@@ -1328,3 +1347,104 @@ Bool_t AliSimulation::IsSelected(TString detName, TString& detectors) const
 
   return result;
 }
+
+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     
+    InitCDBStorage();
+    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
+    AliRawReaderFile* rawReader = new AliRawReaderFile(rawDirectory);
+    //
+    // Get list of detectors
+    TObjArray* detArray = runLoader->GetAliRun()->Detectors();
+    //
+    // Get Header
+    AliHeader* header = runLoader->GetHeader();
+    //
+    // 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);
+           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;
+}