]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliSimulation.cxx
Support for automatic pedestal estimation per pad (Marian and Matevz).
[u/mrichter/AliRoot.git] / STEER / AliSimulation.cxx
index 82c32174a8e0d3047293a803010a6ad677ae2909..3f1427952538ba8928466588d07a52e81fb65b9f 100644 (file)
 #include "AliSimulation.h"
 #include "AliVertexGenFile.h"
 #include "AliCentralTrigger.h"
+#include "AliCTPRawData.h"
 
 ClassImp(AliSimulation)
 
@@ -311,6 +312,36 @@ void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
   fEventsPerFile.Add(obj);
 }
 
+//_____________________________________________________________________________
+Bool_t AliSimulation::ApplyAlignObjsToGeom(TObjArray* alObjArray)
+{
+  // Read collection of alignment objects (AliAlignObj derived) saved
+  // in the TClonesArray ClArrayName and apply them to the geometry
+  // manager singleton.
+  //
+  alObjArray->Sort();
+  Int_t nvols = alObjArray->GetEntriesFast();
+
+  Bool_t flag = kTRUE;
+
+  for(Int_t j=0; j<nvols; j++)
+    {
+      AliAlignObj* alobj = (AliAlignObj*) alObjArray->UncheckedAt(j);
+      if (alobj->ApplyToGeometry() == kFALSE) flag = kFALSE;
+    }
+
+  if (AliDebugLevelClass() >= 1) {
+    gGeoManager->CheckOverlaps(20);
+    TObjArray* ovexlist = gGeoManager->GetListOfOverlaps();
+    if(ovexlist->GetEntriesFast()){  
+      AliError("The application of alignment objects to the geometry caused huge overlaps/extrusions!");
+   }
+  }
+
+  return flag;
+
+}
+
 //_____________________________________________________________________________
 Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* fileName, const char* clArrayName)
 {
@@ -332,7 +363,7 @@ Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* fileName, const char* clA
     return kFALSE;
   }
 
-  return gAlice->ApplyAlignObjsToGeom(alObjArray);
+  return ApplyAlignObjsToGeom(alObjArray);
 
 }
 
@@ -349,7 +380,7 @@ Bool_t AliSimulation::ApplyAlignObjsToGeom(AliCDBParam* param, AliCDBId& Id)
   AliCDBEntry* entry = storage->Get(Id);
   TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
 
-  return gAlice->ApplyAlignObjsToGeom(AlObjArray);
+  return ApplyAlignObjsToGeom(AlObjArray);
 
 }
 
@@ -385,55 +416,7 @@ Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* detName, Int_t runnum, In
   if(!entry) return kFALSE;
   TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
 
-  return gAlice->ApplyAlignObjsToGeom(AlObjArray);
-}
-
-
-//_____________________________________________________________________________
-void AliSimulation::SetAlignObjArray(const char* detectors)
-{
-  // Fills array of detectors' alignable objects from CDB
-  // detectors can be "ALL" or "ITS TPC ..."
-  
-  AliRunLoader* runLoader = LoadRun();
-  if (!runLoader) return;
-
-  InitCDBStorage(fCDBUri);
-
-  if(!fAlignObjArray) fAlignObjArray = new TObjArray();
-  fAlignObjArray->SetOwner(0); // AliCDBEntry is owner of the align objects!
-  fAlignObjArray->Clear(); 
-
-  TString detStr = detectors;
-  TObjArray* detArray = runLoader->GetAliRun()->Detectors();
-  TString dataNotLoaded="";
-  TString dataLoaded="";
-
-  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)) {
-    
-       if(!SetAlignObjArraySingleDet(det->GetName())){
-               dataNotLoaded += det->GetName();
-               dataNotLoaded += " ";
-                       } else {
-                               dataLoaded += det->GetName();
-                               dataLoaded += " ";
-                       }
-       }
-  } // end loop over all detectors
-
-  if ((detStr.CompareTo("ALL") == 0)) detStr = "";
-  dataNotLoaded += detStr;
-  AliInfo(Form("Alignment data loaded for: %s",
-               dataLoaded.Data()));
-  AliInfo(Form("Didn't/couldn't load alignment data for: %s",
-               dataNotLoaded.Data()));
-
-  AliDebug(2, Form("fAlignObjArray entries: %d",fAlignObjArray->GetEntries() ));
-  delete detArray;
-
+  return ApplyAlignObjsToGeom(AlObjArray);
 }
 
 //_____________________________________________________________________________
@@ -472,6 +455,102 @@ Bool_t AliSimulation::SetAlignObjArraySingleDet(const char* detName)
 
 }
 
+//_____________________________________________________________________________
+Bool_t AliSimulation::MisalignGeometry(AliRunLoader *runLoader)
+{
+  // Read the alignment objects from CDB.
+  // Each detector is supposed to have the
+  // alignment objects in DET/Align/Data CDB path.
+  // All the detector objects are then collected,
+  // sorted by geometry level (starting from ALIC) and
+  // then applied to the TGeo geometry.
+  // Finally an overlaps check is performed.
+
+  Bool_t delRunLoader = kFALSE;
+  if (!runLoader) {
+    runLoader = LoadRun("READ");
+    if (!runLoader) return kFALSE;
+    delRunLoader = kTRUE;
+  }
+
+  // Load alignment data from CDB and fill fAlignObjArray 
+  if(fLoadAlignFromCDB){
+       if(!fAlignObjArray) fAlignObjArray = new TObjArray();
+       
+       //fAlignObjArray->RemoveAll(); 
+       fAlignObjArray->Clear();        
+       fAlignObjArray->SetOwner(0);
+       TString detStr = fLoadAlignData;
+       TString dataNotLoaded="";
+       TString dataLoaded="";
+  
+       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)) {
+                       if(!SetAlignObjArraySingleDet(det->GetName())){
+                               dataNotLoaded += det->GetName();
+                               dataNotLoaded += " ";
+                       } else {
+                               dataLoaded += det->GetName();
+                               dataLoaded += " ";
+                       }
+               }
+       } // end loop over detectors
+  
+       if ((detStr.CompareTo("ALL") == 0)) detStr = "";
+       dataNotLoaded += detStr;
+       AliInfo(Form("Alignment data loaded for: %s",
+                         dataLoaded.Data()));
+       AliInfo(Form("Didn't/couldn't load alignment data for: %s",
+                         dataNotLoaded.Data()));
+  } // fLoadAlignFromCDB flag
+  // Check if the array with alignment objects was
+  // provided by the user. If yes, apply the objects
+  // to the present TGeo geometry
+  if (fAlignObjArray) {
+    if (gGeoManager && gGeoManager->IsClosed()) {
+      if (ApplyAlignObjsToGeom(fAlignObjArray) == kFALSE) {
+       AliError("The misalignment of one or more volumes failed!"
+                "Compare the list of simulated detectors and the list of detector alignment data!");
+       if (delRunLoader) delete runLoader;
+       return kFALSE;
+      }
+    }
+    else {
+      AliError("Can't apply the misalignment! gGeoManager doesn't exist or it is still opened!");
+      if (delRunLoader) delete runLoader;
+      return kFALSE;
+    }
+  }
+
+  if (delRunLoader) delete runLoader;
+
+  return kTRUE;
+}
+
+
+//_____________________________________________________________________________
+Bool_t AliSimulation::SetRunNumber()
+{
+  // Set the CDB manager run number
+  // The run number is retrieved from gAlice
+
+  if(AliCDBManager::Instance()->GetRun() < 0) {
+    AliRunLoader* runLoader = LoadRun("READ");
+    if (!runLoader) return kFALSE;
+    else {
+      AliCDBManager::Instance()->SetRun(runLoader->GetAliRun()->GetRunNumber());
+      AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
+      delete runLoader;
+    }
+  }
+  return kTRUE;
+}
+
 //_____________________________________________________________________________
 void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
 {
@@ -498,6 +577,16 @@ Bool_t AliSimulation::Run(Int_t nEvents)
     if (!RunSimulation()) if (fStopOnError) return kFALSE;
   }
 
+  // Set run number in CDBManager (if it is not already set in RunSimulation)
+  if (!SetRunNumber()) if (fStopOnError) return kFALSE;
+
+  // Load and misalign the geometry
+  if (!gGeoManager) {
+    TGeoManager::Import("geometry.root");
+    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;
@@ -524,10 +613,8 @@ Bool_t AliSimulation::Run(Int_t nEvents)
   }
 
   // digits -> trigger
-  if (!fMakeTrigger.IsNull()) {
-    if (!RunTrigger(fMakeTrigger)) {
-      if (fStopOnError) return kFALSE;
-    }
+  if (!RunTrigger(fMakeTrigger)) {
+    if (fStopOnError) return kFALSE;
   }
 
   // digits -> raw data
@@ -553,6 +640,16 @@ Bool_t AliSimulation::RunTrigger(const char* descriptors)
    if (!runLoader) return kFALSE;
    TString des = descriptors;
 
+   if (des.IsNull()) {
+     if (gAlice->GetTriggerDescriptor() != "") {
+       des = gAlice->GetTriggerDescriptor();
+     }
+     else {
+       AliWarning("No trigger descriptor is specified. Skipping the trigger simulation...");
+       return kTRUE;
+     }
+   }
+
    runLoader->MakeTree( "CT" );
    AliCentralTrigger* aCTP = runLoader->GetTrigger();
   // Load Descriptors
@@ -574,7 +671,17 @@ Bool_t AliSimulation::RunTrigger(const char* descriptors)
    return kTRUE;
 }
 
+//_____________________________________________________________________________
+Bool_t AliSimulation::WriteTriggerRawData()
+{
+  // Writes the CTP (trigger) DDL raw data
+  // Details of the format are given in the
+  // trigger TDR - pages 134 and 135.
+  AliCTPRawData writer;
+  writer.RawData();
 
+  return kTRUE;
+}
 
 //_____________________________________________________________________________
 Bool_t AliSimulation::RunSimulation(Int_t nEvents)
@@ -599,7 +706,17 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
     gAlice->Init(fConfigFileName.Data());
   ););
 
-  // Set run number in CDBManager (here????)
+  // Get the trigger descriptor string
+  // Either from AliSimulation or from
+  // gAlice
+  if (fMakeTrigger.IsNull()) {
+    if (gAlice->GetTriggerDescriptor() != "")
+      fMakeTrigger = gAlice->GetTriggerDescriptor();
+  }
+  else
+    gAlice->SetTriggerDescriptor(fMakeTrigger.Data());
+
+  // Set run number in CDBManager
   AliCDBManager::Instance()->SetRun(gAlice->GetRunNumber());
   AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
 
@@ -614,55 +731,26 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
   // Export ideal geometry 
   if (gGeoManager) gGeoManager->Export("geometry.root");
 
-  // Load alignment data from CDB and fill fAlignObjArray 
-  if(fLoadAlignFromCDB){
-       if(!fAlignObjArray) fAlignObjArray = new TObjArray();
-       
-       //fAlignObjArray->RemoveAll(); 
-       fAlignObjArray->Clear();        
-       fAlignObjArray->SetOwner(0);
-       TString detStr = fLoadAlignData;
-       TString dataNotLoaded="";
-       TString dataLoaded="";
-  
-       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)) {
-                       if(!SetAlignObjArraySingleDet(det->GetName())){
-                               dataNotLoaded += det->GetName();
-                               dataNotLoaded += " ";
-                       } else {
-                               dataLoaded += det->GetName();
-                               dataLoaded += " ";
-                       }
-               }
-       } // end loop over detectors
-  
-       if ((detStr.CompareTo("ALL") == 0)) detStr = "";
-       dataNotLoaded += detStr;
-       AliInfo(Form("Alignment data loaded for: %s",
-                         dataLoaded.Data()));
-       AliInfo(Form("Didn't/couldn't load alignment data for: %s",
-                         dataNotLoaded.Data()));
-  } // fLoadAlignFromCDB flag
-  // Check if the array with alignment objects was
-  // provided by the user. If yes, apply the objects
-  // to the present TGeo geometry
-  if (fAlignObjArray) {
-    if (gGeoManager && gGeoManager->IsClosed()) {
-      if (gAlice->ApplyAlignObjsToGeom(fAlignObjArray) == kFALSE) {
-       AliError("The application of misalignment failed! Restart aliroot and try again. ");
-       return kFALSE;
+  // Misalign geometry
+//   if (!MisalignGeometry(runLoader)) {
+//     delete runLoader;
+//     return kFALSE;
+//   }
+  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();
+       }
       }
     }
-    else {
-      AliError("Can't apply the misalignment! gGeoManager doesn't exist or it is still opened!");
-      return kFALSE;
-    }
   }
 
   // Export (mis)aligned geometry 
@@ -969,6 +1057,9 @@ Bool_t AliSimulation::WriteRawFiles(const char* detectors)
       }
     }
 
+    if (!WriteTriggerRawData())
+      if (fStopOnError) return kFALSE;
+
     gSystem->ChangeDirectory(baseDir);
     if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
       AliError(Form("the following detectors were not found: %s", 
@@ -999,7 +1090,7 @@ 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 -o %s -# %d -C", 
+  sprintf(command, "dateStream -D -o %s -# %d -C", 
          dateFileName, runLoader->GetNumberOfEvents());
   FILE* pipe = gSystem->OpenPipe(command, "w");