]> git.uio.no Git - u/mrichter/AliRoot.git/commitdiff
Moving the misalignment to AliSimulation and AliReconstruction (Cvetan)
authorhristov <hristov@f7af4fe6-9843-0410-8265-dc069ae4e863>
Mon, 10 Apr 2006 15:39:25 +0000 (15:39 +0000)
committerhristov <hristov@f7af4fe6-9843-0410-8265-dc069ae4e863>
Mon, 10 Apr 2006 15:39:25 +0000 (15:39 +0000)
STEER/AliReconstruction.cxx
STEER/AliReconstruction.h
STEER/AliRun.cxx
STEER/AliRun.h
STEER/AliSimulation.cxx
STEER/AliSimulation.h

index 254063ce30f07006f04b105148963bebfa6fbbdf..6b8f5b779173cf6a4322957b26b18eec4cc0c5a1 100644 (file)
 
 #include "AliTrackPointArray.h"
 #include "AliCDBManager.h"
 
 #include "AliTrackPointArray.h"
 #include "AliCDBManager.h"
+#include "AliCDBEntry.h"
+#include "AliAlignObj.h"
 
 ClassImp(AliReconstruction)
 
 
 ClassImp(AliReconstruction)
 
@@ -164,12 +166,15 @@ AliReconstruction::AliReconstruction(const char* gAliceFilename, const char* cdb
   fStopOnError(kFALSE),
   fCheckPointLevel(0),
   fOptions(),
   fStopOnError(kFALSE),
   fCheckPointLevel(0),
   fOptions(),
+  fLoadAlignFromCDB(kTRUE),
+  fLoadAlignData("ALL"),
 
   fRunLoader(NULL),
   fRawReader(NULL),
 
   fVertexer(NULL),
 
 
   fRunLoader(NULL),
   fRawReader(NULL),
 
   fVertexer(NULL),
 
+  fAlignObjArray(NULL),
   fWriteAlignmentData(kFALSE),
   fCDBUri(cdbUri)
 {
   fWriteAlignmentData(kFALSE),
   fCDBUri(cdbUri)
 {
@@ -181,10 +186,6 @@ AliReconstruction::AliReconstruction(const char* gAliceFilename, const char* cdb
     fTracker[iDet] = NULL;
   }
   AliPID pid;
     fTracker[iDet] = NULL;
   }
   AliPID pid;
-  // Import TGeo geometry
-  TString geom(gSystem->DirName(gAliceFilename));
-  geom += "/geometry.root";
-  TGeoManager::Import(geom.Data());
 }
 
 //_____________________________________________________________________________
 }
 
 //_____________________________________________________________________________
@@ -204,12 +205,15 @@ AliReconstruction::AliReconstruction(const AliReconstruction& rec) :
   fStopOnError(rec.fStopOnError),
   fCheckPointLevel(0),
   fOptions(),
   fStopOnError(rec.fStopOnError),
   fCheckPointLevel(0),
   fOptions(),
+  fLoadAlignFromCDB(rec.fLoadAlignFromCDB),
+  fLoadAlignData(rec.fLoadAlignData),
 
   fRunLoader(NULL),
   fRawReader(NULL),
 
   fVertexer(NULL),
 
 
   fRunLoader(NULL),
   fRawReader(NULL),
 
   fVertexer(NULL),
 
+  fAlignObjArray(rec.fAlignObjArray),
   fWriteAlignmentData(rec.fWriteAlignmentData),
   fCDBUri(rec.fCDBUri)
 {
   fWriteAlignmentData(rec.fWriteAlignmentData),
   fCDBUri(rec.fCDBUri)
 {
@@ -279,6 +283,151 @@ void AliReconstruction::SetSpecificStorage(const char* detName, const char* uri)
 
 }
 
 
 }
 
+//_____________________________________________________________________________
+Bool_t AliReconstruction::SetRunNumber()
+{
+  // The method is called in Run() in order
+  // to set a correct run number.
+  // In case of raw data reconstruction the
+  // run number is taken from the raw data header
+
+  if(AliCDBManager::Instance()->GetRun() < 0) {
+    if (!fRunLoader) {
+      AliError("No run loader is found !"); 
+      return kFALSE;
+    }
+    // read run number from gAlice
+    AliCDBManager::Instance()->SetRun(fRunLoader->GetAliRun()->GetRunNumber());
+    AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
+  }
+  return kTRUE;
+}
+
+//_____________________________________________________________________________
+Bool_t AliReconstruction::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();
+
+  for(Int_t j=0; j<nvols; j++)
+    {
+      AliAlignObj* alobj = (AliAlignObj*) alObjArray->UncheckedAt(j);
+      if (alobj->ApplyToGeometry() == kFALSE)
+       return 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 kTRUE;
+
+}
+
+//_____________________________________________________________________________
+Bool_t AliReconstruction::SetAlignObjArraySingleDet(const char* detName)
+{
+  // Fills array of single detector's alignable objects from CDB
+  
+  AliDebug(2, Form("Loading alignment data for detector: %s",detName));
+  
+  AliCDBEntry *entry;
+       
+  AliCDBPath path(detName,"Align","Data");
+       
+  entry=AliCDBManager::Instance()->Get(path.GetPath());
+  if(!entry){ 
+       AliDebug(2,Form("Couldn't load alignment data for detector %s",detName));
+       return kFALSE;
+  }
+  entry->SetOwner(1);
+  TClonesArray *alignArray = (TClonesArray*) entry->GetObject();       
+  alignArray->SetOwner(0);
+  AliDebug(2,Form("Found %d alignment objects for %s",
+                       alignArray->GetEntries(),detName));
+
+  AliAlignObj *alignObj=0;
+  TIter iter(alignArray);
+       
+  // loop over align objects in detector
+  while( ( alignObj=(AliAlignObj *) iter.Next() ) ){
+       fAlignObjArray->Add(alignObj);
+  }
+  // delete entry --- Don't delete, it is cached!
+       
+  AliDebug(2, Form("fAlignObjArray entries: %d",fAlignObjArray->GetEntries() ));
+  return kTRUE;
+
+}
+
+//_____________________________________________________________________________
+Bool_t AliReconstruction::MisalignGeometry(const TString& detectors)
+{
+  // 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.
+
+  // Load alignment data from CDB and fill fAlignObjArray 
+  if(fLoadAlignFromCDB){
+       if(!fAlignObjArray) fAlignObjArray = new TObjArray();
+       
+       //fAlignObjArray->RemoveAll(); 
+       fAlignObjArray->Clear();        
+       fAlignObjArray->SetOwner(0);
+       TString detStr = detectors;
+       TString dataNotLoaded="";
+       TString dataLoaded="";
+  
+       for (Int_t iDet = 0; iDet < fgkNDetectors; iDet++) {
+         if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
+         if(!SetAlignObjArraySingleDet(fgkDetectorName[iDet])){
+           dataNotLoaded += fgkDetectorName[iDet];
+           dataNotLoaded += " ";
+         } else {
+           dataLoaded += fgkDetectorName[iDet];
+           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 application of misalignment failed! Restart aliroot and try again. ");
+       return kFALSE;
+      }
+    }
+    else {
+      AliError("Can't apply the misalignment! gGeoManager doesn't exist or it is still opened!");
+      return kFALSE;
+    }
+  }
+
+  return kTRUE;
+}
 
 //_____________________________________________________________________________
 void AliReconstruction::SetGAliceFile(const char* fileName)
 
 //_____________________________________________________________________________
 void AliReconstruction::SetGAliceFile(const char* fileName)
@@ -322,6 +471,33 @@ Bool_t AliReconstruction::Run(const char* input,
   // get the run loader
   if (!InitRunLoader()) return kFALSE;
 
   // get the run loader
   if (!InitRunLoader()) return kFALSE;
 
+  // Set run number in CDBManager (if it is not already set by the user)
+  if (!SetRunNumber()) if (fStopOnError) return kFALSE;
+
+  // Import ideal TGeo geometry and apply misalignment
+  if (!gGeoManager) {
+    TString geom(gSystem->DirName(fGAliceFileName));
+    geom += "/geometry.root";
+    TGeoManager::Import(geom.Data());
+    if (!gGeoManager) if (fStopOnError) return kFALSE;
+  }
+  if (!MisalignGeometry(fLoadAlignData)) if (fStopOnError) return kFALSE;
+
+  // 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();
+       }
+      }
+    }
+  }
+
   // local reconstruction
   if (!fRunLocalReconstruction.IsNull()) {
     if (!RunLocalReconstruction(fRunLocalReconstruction)) {
   // local reconstruction
   if (!fRunLocalReconstruction.IsNull()) {
     if (!RunLocalReconstruction(fRunLocalReconstruction)) {
index 8f39c79901bd974d16bf7d30d92892ecab609550..28f217c0d46563afcabe92c20b46277535dbf448 100644 (file)
@@ -62,6 +62,9 @@ public:
     SetRunLocalReconstruction(detectors); 
     SetRunTracking(detectors);
     SetFillESD(detectors);};
     SetRunLocalReconstruction(detectors); 
     SetRunTracking(detectors);
     SetFillESD(detectors);};
+  void           SetLoadAlignFromCDB(Bool_t load)  {fLoadAlignFromCDB = load;};
+  void           SetLoadAlignData(const char* detectors) 
+    {fLoadAlignData = detectors;};
 
    void SetUniformFieldTracking(){fUniformField=kTRUE;} 
    void SetNonuniformFieldTracking(){fUniformField=kFALSE;} 
 
    void SetUniformFieldTracking(){fUniformField=kTRUE;} 
    void SetNonuniformFieldTracking(){fUniformField=kFALSE;} 
@@ -76,6 +79,16 @@ public:
   void SetDefaultStorage(const char* uri);
   void SetSpecificStorage(const char* detName, const char* uri);    
 
   void SetDefaultStorage(const char* uri);
   void SetSpecificStorage(const char* detName, const char* uri);    
 
+  Bool_t SetRunNumber();
+
+  Bool_t SetAlignObjArraySingleDet(const char* detName);
+  Bool_t MisalignGeometry(const TString& detectors);
+
+  void           SetAlignObjArray(TObjArray *array)
+                   {fAlignObjArray = array;
+                  fLoadAlignFromCDB = kFALSE;}
+  Bool_t         ApplyAlignObjsToGeom(TObjArray* alObjArray);
+
   virtual Bool_t Run(const char* input, 
                     Int_t firstEvent, Int_t lastEvent = -1);
   Bool_t         Run(const char* input = NULL)
   virtual Bool_t Run(const char* input, 
                     Int_t firstEvent, Int_t lastEvent = -1);
   Bool_t         Run(const char* input = NULL)
@@ -125,6 +138,8 @@ private:
   Bool_t         fStopOnError;        // stop or continue on errors
   Int_t          fCheckPointLevel;    // level of ESD check points
   TObjArray      fOptions;            // options for reconstructor objects
   Bool_t         fStopOnError;        // stop or continue on errors
   Int_t          fCheckPointLevel;    // level of ESD check points
   TObjArray      fOptions;            // options for reconstructor objects
+  Bool_t         fLoadAlignFromCDB;   // Load alignment data from CDB and apply it to geometry or not
+  TString        fLoadAlignData;      // Load alignment data from CDB for these detectors
 
   AliRunLoader*  fRunLoader;          //! current run loader object
   AliRawReader*  fRawReader;          //! current raw data reader
 
   AliRunLoader*  fRunLoader;          //! current run loader object
   AliRawReader*  fRawReader;          //! current raw data reader
@@ -136,6 +151,7 @@ private:
   AliVertexer*   fVertexer;                //! vertexer for ITS
   AliTracker*    fTracker[fgkNDetectors];  //! trackers
 
   AliVertexer*   fVertexer;                //! vertexer for ITS
   AliTracker*    fTracker[fgkNDetectors];  //! trackers
 
+  TObjArray*    fAlignObjArray;      // array with the alignment objects to be applied to the geometry
   Bool_t         fWriteAlignmentData; // write track space-points flag
 
   TString       fCDBUri;             // Uri of the default CDB storage
   Bool_t         fWriteAlignmentData; // write track space-points flag
 
   TString       fCDBUri;             // Uri of the default CDB storage
index 724f977d40e11bc9db95be43cf38b9587b20bed4..1ccb98f433a6ce411c56f6606252df9961af62a9 100644 (file)
@@ -958,33 +958,3 @@ void AliRun::AddModule(AliModule* mod)
   TString str = name; gSystem->ExpandPathName(str);
   return !gSystem->AccessPathName(str.Data(),mode);
 }
   TString str = name; gSystem->ExpandPathName(str);
   return !gSystem->AccessPathName(str.Data(),mode);
 }
-
-//_____________________________________________________________________________
-Bool_t AliRun::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();
-
-  for(Int_t j=0; j<nvols; j++)
-    {
-      AliAlignObj* alobj = (AliAlignObj*) AlObjArray->UncheckedAt(j);
-      if (alobj->ApplyToGeometry() == kFALSE)
-       return kFALSE;
-    }
-
-  if (AliDebugLevelClass() >= 1) {
-    gGeoManager->CheckOverlaps(20);
-    TObjArray* ovexlist = gGeoManager->GetListOfOverlaps();
-    if(ovexlist->GetEntriesFast()){  
-      AliErrorClass("The application of alignment objects to the geometry caused huge overlaps/extrusions!");
-   }
-  }
-
-  return kTRUE;
-
-}
-
index 7989bba075724db8d02263bf12af0920ce0ff131..13de693eae8db8ad7a49b7ae6265ff1baaaa8f00 100644 (file)
@@ -139,10 +139,6 @@ public:
       else ::Warning(method, "method is depricated\nPlease use: %s", replace);
     }
 
       else ::Warning(method, "method is depricated\nPlease use: %s", replace);
     }
 
-  // Method to introduce the detector misliagnment
-  // It is called by AliSimulation
-  static Bool_t  ApplyAlignObjsToGeom(TObjArray* AlObjArray);
-
 protected:
   virtual  void  Tree2Tree(Option_t *option, const char *detector=0);
   Int_t          fRun;               //! Current run number
 protected:
   virtual  void  Tree2Tree(Option_t *option, const char *detector=0);
   Int_t          fRun;               //! Current run number
index c9726ee33535137d59215f79be4111cec589330c..54bcd5d0e7bd9620f5e1ba9951b90c7ad569d414 100644 (file)
@@ -311,6 +311,35 @@ void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
   fEventsPerFile.Add(obj);
 }
 
   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();
+
+  for(Int_t j=0; j<nvols; j++)
+    {
+      AliAlignObj* alobj = (AliAlignObj*) alObjArray->UncheckedAt(j);
+      if (alobj->ApplyToGeometry() == kFALSE)
+       return 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 kTRUE;
+
+}
+
 //_____________________________________________________________________________
 Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* fileName, const char* clArrayName)
 {
 //_____________________________________________________________________________
 Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* fileName, const char* clArrayName)
 {
@@ -332,7 +361,7 @@ Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* fileName, const char* clA
     return kFALSE;
   }
 
     return kFALSE;
   }
 
-  return gAlice->ApplyAlignObjsToGeom(alObjArray);
+  return ApplyAlignObjsToGeom(alObjArray);
 
 }
 
 
 }
 
@@ -349,7 +378,7 @@ Bool_t AliSimulation::ApplyAlignObjsToGeom(AliCDBParam* param, AliCDBId& Id)
   AliCDBEntry* entry = storage->Get(Id);
   TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
 
   AliCDBEntry* entry = storage->Get(Id);
   TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
 
-  return gAlice->ApplyAlignObjsToGeom(AlObjArray);
+  return ApplyAlignObjsToGeom(AlObjArray);
 
 }
 
 
 }
 
@@ -385,55 +414,7 @@ Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* detName, Int_t runnum, In
   if(!entry) return kFALSE;
   TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
 
   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 +453,99 @@ 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 application of misalignment failed! Restart aliroot and try again. ");
+       return kFALSE;
+      }
+    }
+    else {
+      AliError("Can't apply the misalignment! gGeoManager doesn't exist or it is still opened!");
+      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)
 {
 //_____________________________________________________________________________
 void AliSimulation::MergeWith(const char* fileName, Int_t nSignalPerBkgrd)
 {
@@ -498,6 +572,16 @@ Bool_t AliSimulation::Run(Int_t nEvents)
     if (!RunSimulation()) if (fStopOnError) return kFALSE;
   }
 
     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;
   // hits -> summable digits
   if (!fMakeSDigits.IsNull()) {
     if (!RunSDigitization(fMakeSDigits)) if (fStopOnError) return kFALSE;
@@ -599,7 +683,7 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
     gAlice->Init(fConfigFileName.Data());
   ););
 
     gAlice->Init(fConfigFileName.Data());
   ););
 
-  // Set run number in CDBManager (here????)
+  // Set run number in CDBManager
   AliCDBManager::Instance()->SetRun(gAlice->GetRunNumber());
   AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
 
   AliCDBManager::Instance()->SetRun(gAlice->GetRunNumber());
   AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
 
@@ -614,59 +698,8 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
   // Export ideal geometry 
   if (gGeoManager) gGeoManager->Export("geometry.root");
 
   // 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;
-      }
-    }
-    else {
-      AliError("Can't apply the misalignment! gGeoManager doesn't exist or it is still opened!");
-      return kFALSE;
-    }
-  }
-
-  // Export (mis)aligned geometry 
-  if (gGeoManager) gGeoManager->Export("misaligned_geometry.root");
+  // Misalign geometry
+  if (!MisalignGeometry(runLoader)) return kFALSE;
 
   // Temporary fix by A.Gheata
   // Could be removed with the next Root version (>5.11)
 
   // Temporary fix by A.Gheata
   // Could be removed with the next Root version (>5.11)
@@ -683,6 +716,9 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
     }
   }
 
     }
   }
 
+  // Export (mis)aligned geometry 
+  if (gGeoManager) gGeoManager->Export("misaligned_geometry.root");
+
 //   AliRunLoader* runLoader = gAlice->GetRunLoader();
 //   if (!runLoader) {
 //     AliError(Form("gAlice has no run loader object. "
 //   AliRunLoader* runLoader = gAlice->GetRunLoader();
 //   if (!runLoader) {
 //     AliError(Form("gAlice has no run loader object. "
index 7804f4573ae9170580bee2ae4e0b287b5d9f3422..fa271f3d72604c0e68e734ecec4528377a902e27 100644 (file)
@@ -60,21 +60,26 @@ public:
                    {fWriteRawData = detectors; fRawDataFileName = fileName;
                   fDeleteIntermediateFiles = deleteIntermediateFiles;};
 
                    {fWriteRawData = detectors; fRawDataFileName = fileName;
                   fDeleteIntermediateFiles = deleteIntermediateFiles;};
 
-  static Bool_t  ApplyAlignObjsToGeom(const char* fileName,
-                                   const char* clArrayName);
-  static Bool_t  ApplyAlignObjsToGeom(AliCDBParam* param,
-                                   AliCDBId& Id);
-  static Bool_t  ApplyAlignObjsToGeom(const char* uri, const char* path,
-                                   Int_t runnum, Int_t version,
-                                   Int_t sversion);
-  static Bool_t  ApplyAlignObjsToGeom(const char* detName, Int_t runnum, Int_t version,
-                                   Int_t sversion);
+  Bool_t         ApplyAlignObjsToGeom(TObjArray* alObjArray);
+
+  Bool_t         ApplyAlignObjsToGeom(const char* fileName,
+                                     const char* clArrayName);
+  Bool_t         ApplyAlignObjsToGeom(AliCDBParam* param,
+                                     AliCDBId& Id);
+  Bool_t         ApplyAlignObjsToGeom(const char* uri, const char* path,
+                                     Int_t runnum, Int_t version,
+                                     Int_t sversion);
+  Bool_t         ApplyAlignObjsToGeom(const char* detName, Int_t runnum, Int_t version,
+                                     Int_t sversion);
   void           SetAlignObjArray(TObjArray *array)
   void           SetAlignObjArray(TObjArray *array)
-                                   {fAlignObjArray = array;
-                                    fLoadAlignFromCDB = kFALSE;}
+                   {fAlignObjArray = array;
+                  fLoadAlignFromCDB = kFALSE;}
 
 
-  void           SetAlignObjArray(const char* detectors="ALL"); 
   Bool_t         SetAlignObjArraySingleDet(const char* detName);                  
   Bool_t         SetAlignObjArraySingleDet(const char* detName);                  
+
+  Bool_t         MisalignGeometry(AliRunLoader *runLoader = NULL);
+
+  Bool_t         SetRunNumber();
                   
   // CDB storage activation
   static void InitCDBStorage(const char *uri);
                   
   // CDB storage activation
   static void InitCDBStorage(const char *uri);