]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliSimulation.cxx
Removing some junk printout
[u/mrichter/AliRoot.git] / STEER / AliSimulation.cxx
index 49fbe1b92a96e4fb94cd738f254a77a9c09848eb..82c32174a8e0d3047293a803010a6ad677ae2909 100644 (file)
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
+#include <TGeoManager.h>
 #include <TObjString.h>
 #include <TStopwatch.h>
 #include <TSystem.h>
+#include <TFile.h>
 
-#include "AliLog.h"
+#include "AliCDBStorage.h"
+#include "AliCDBEntry.h"
+#include "AliCDBManager.h"
+#include "AliAlignObj.h"
+#include "AliCentralTrigger.h"
+#include "AliDAQConfig.h"
 #include "AliDigitizer.h"
 #include "AliGenerator.h"
+#include "AliLog.h"
 #include "AliModule.h"
 #include "AliRun.h"
 #include "AliRunDigitizer.h"
 #include "AliVertexGenFile.h"
 #include "AliCentralTrigger.h"
 
-#include "AliDAQConfig.h"
-#include "AliAlignObj.h"
-#include "AliAlignObjAngles.h"
-#include "AliAlignObjMatrix.h"
-
 ClassImp(AliSimulation)
 
 
 //_____________________________________________________________________________
-AliSimulation::AliSimulation(const char* configFileName,
+AliSimulation::AliSimulation(const char* configFileName, const char* cdbUri,
                             const char* name, const char* title) :
   TNamed(name, title),
 
   fRunGeneration(kTRUE),
   fRunSimulation(kTRUE),
+  fLoadAlignFromCDB(kTRUE),
+  fLoadAlignData("ALL"),
   fMakeSDigits("ALL"),
   fMakeDigits("ALL"),
   fMakeTrigger(""),
@@ -149,8 +154,10 @@ AliSimulation::AliSimulation(const char* configFileName,
   fGAliceFileName("galice.root"),
   fEventsPerFile(),
   fBkgrdFileNames(NULL),
+  fAlignObjArray(NULL),
   fUseBkgrdVertex(kTRUE),
-  fRegionOfInterest(kFALSE)
+  fRegionOfInterest(kFALSE),
+  fCDBUri(cdbUri)
 {
 // create simulation object with default parameters
 
@@ -163,6 +170,8 @@ AliSimulation::AliSimulation(const AliSimulation& sim) :
 
   fRunGeneration(sim.fRunGeneration),
   fRunSimulation(sim.fRunSimulation),
+  fLoadAlignFromCDB(sim.fLoadAlignFromCDB),
+  fLoadAlignData(sim.fLoadAlignData),
   fMakeSDigits(sim.fMakeSDigits),
   fMakeDigits(sim.fMakeDigits),
   fMakeTrigger(sim.fMakeTrigger),
@@ -177,8 +186,10 @@ AliSimulation::AliSimulation(const AliSimulation& sim) :
   fGAliceFileName(sim.fGAliceFileName),
   fEventsPerFile(),
   fBkgrdFileNames(NULL),
+  fAlignObjArray(NULL),
   fUseBkgrdVertex(sim.fUseBkgrdVertex),
-  fRegionOfInterest(sim.fRegionOfInterest)
+  fRegionOfInterest(sim.fRegionOfInterest),
+  fCDBUri(sim.fCDBUri)
 {
 // copy constructor
 
@@ -210,6 +221,8 @@ AliSimulation::~AliSimulation()
 // clean up
 
   fEventsPerFile.Delete();
+//  if(fAlignObjArray) fAlignObjArray->Delete(); // fAlignObjArray->RemoveAll() ???
+//  delete fAlignObjArray; fAlignObjArray=0;
 
   if (fBkgrdFileNames) {
     fBkgrdFileNames->Delete();
@@ -226,6 +239,41 @@ void AliSimulation::SetNumberOfEvents(Int_t nEvents)
   fNEvents = nEvents;
 }
 
+//_____________________________________________________________________________
+void AliSimulation::InitCDBStorage(const char* uri)
+{
+// activate a default CDB storage
+// First check if we have any CDB storage set, because it is used 
+// to retrieve the calibration and alignment constants
+
+  AliCDBManager* man = AliCDBManager::Instance();
+  if (!man->IsDefaultStorageSet())
+  {
+    AliWarningClass("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+    AliWarningClass("Default CDB storage not yet set");
+    AliWarningClass(Form("Using default storage declared in AliSimulation: %s",uri));
+    AliWarningClass("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+    SetDefaultStorage(uri);
+  }  
+  
+}
+
+//_____________________________________________________________________________
+void AliSimulation::SetDefaultStorage(const char* uri) {
+// activate a default CDB storage 
+
+   AliCDBManager::Instance()->SetDefaultStorage(uri);
+
+}
+
+//_____________________________________________________________________________
+void AliSimulation::SetSpecificStorage(const char* detName, const char* uri) {
+// activate a detector-specific CDB storage 
+
+   AliCDBManager::Instance()->SetSpecificStorage(detName, uri);
+
+}
+
 //_____________________________________________________________________________
 void AliSimulation::SetConfigFile(const char* fileName)
 {
@@ -264,7 +312,7 @@ void AliSimulation::SetEventsPerFile(const char* detector, const char* type,
 }
 
 //_____________________________________________________________________________
-Bool_t AliSimulation::ApplyDisplacements(const char* fileName, const char* ClArrayName)
+Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* fileName, const char* clArrayName)
 {
   // read collection of alignment objects (AliAlignObj derived) saved
   // in the TClonesArray ClArrayName in the file fileName and apply
@@ -277,52 +325,36 @@ Bool_t AliSimulation::ApplyDisplacements(const char* fileName, const char* ClArr
     return kFALSE;
   }
 
-  TClonesArray* AlObjArray = ((TClonesArray*) inFile->Get(ClArrayName));
+  TClonesArray* alObjArray = ((TClonesArray*) inFile->Get(clArrayName));
   inFile->Close();
+  if (!alObjArray) {
+    AliErrorClass(Form("Could not get array (%s) from file (%s) !",clArrayName,fileName));
+    return kFALSE;
+  }
 
-  return AliSimulation::ApplyDisplacements(AlObjArray);
+  return gAlice->ApplyAlignObjsToGeom(alObjArray);
 
 }
 
 //_____________________________________________________________________________
-Bool_t AliSimulation::ApplyDisplacements(TClonesArray* AlObjArray)
+Bool_t AliSimulation::ApplyAlignObjsToGeom(AliCDBParam* param, AliCDBId& Id)
 {
-  // Read collection of alignment objects (AliAlignObj derived) saved
-  // in the TClonesArray ClArrayName and apply them to the geometry
-  // manager singleton.
+  // read collection of alignment objects (AliAlignObj derived) saved
+  // in the TClonesArray ClArrayName in the AliCDBEntry identified by
+  // param (to get the AliCDBStorage) and Id; apply the alignment objects
+  // to the TGeo geometry passed as argument
   //
-  Int_t nvols = AlObjArray->GetEntriesFast();
-
-  AliAlignObj::ELayerID layerId; // unique identity for volume in the alobj
-  Int_t modId; // unique identity for volume in the alobj
-  Bool_t ispathvalid; // false if volume path for alobj is not valid for TGeo
-
-  TGeoHMatrix dm;
-
-  for(Int_t j=0; j<nvols; j++)
-    {
-      AliAlignObj* alobj = (AliAlignObj*) AlObjArray->UncheckedAt(j);
-      const char* volpath = alobj->GetVolPath();
-      TGeoPhysicalNode* node = (TGeoPhysicalNode*) gGeoManager->MakePhysicalNode(volpath);
-      alobj->GetMatrix(dm);
-      alobj->GetVolUID(layerId, modId);
-      ispathvalid = gGeoManager->cd(volpath);
-      if(!ispathvalid){
-       AliWarningClass(Form("Volume path %s not valid!",volpath));
-       return kFALSE;
-      }
-      TGeoHMatrix* hm = gGeoManager->GetCurrentMatrix();
-      hm->MultiplyLeft(&dm);
-      AliInfoClass(Form("Aligning volume %s of detector layer %d with local ID %d",volpath,layerId,modId));
-      node->Align(hm);
-    }
 
-  return kTRUE;
+  AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage(param);
+  AliCDBEntry* entry = storage->Get(Id);
+  TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
+
+  return gAlice->ApplyAlignObjsToGeom(AlObjArray);
 
 }
 
 //_____________________________________________________________________________
-Bool_t AliSimulation::ApplyDisplacements(AliCDBParam* param, AliCDBId& Id)
+Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* uri, const char* path, Int_t runnum, Int_t version, Int_t sversion)
 {
   // read collection of alignment objects (AliAlignObj derived) saved
   // in the TClonesArray ClArrayName in the AliCDBEntry identified by
@@ -330,16 +362,15 @@ Bool_t AliSimulation::ApplyDisplacements(AliCDBParam* param, AliCDBId& Id)
   // to the TGeo geometry passed as argument
   //
 
-  AliCDBStorage* storage = AliCDBManager::Instance()->GetStorage(param);
-  AliCDBEntry* entry = storage->Get(Id);
-  TClonesArray* AlObjArray = ((TClonesArray*) entry->GetObject());
+  AliCDBParam* param = AliCDBManager::Instance()->CreateParameter(uri);
+  AliCDBId id(path, runnum, runnum, version, sversion);
 
-  return AliSimulation::ApplyDisplacements(AlObjArray);
+  return ApplyAlignObjsToGeom(param, id);
 
 }
 
 //_____________________________________________________________________________
-Bool_t AliSimulation::ApplyDisplacements(const char* uri, const char* path, Int_t runnum, Int_t version, Int_t sversion)
+Bool_t AliSimulation::ApplyAlignObjsToGeom(const char* detName, Int_t runnum, Int_t version, Int_t sversion)
 {
   // read collection of alignment objects (AliAlignObj derived) saved
   // in the TClonesArray ClArrayName in the AliCDBEntry identified by
@@ -347,10 +378,97 @@ Bool_t AliSimulation::ApplyDisplacements(const char* uri, const char* path, Int_
   // to the TGeo geometry passed as argument
   //
 
-  AliCDBParam* param = AliCDBManager::Instance()->CreateParameter(uri);
-  AliCDBId id(path, runnum, runnum, version, sversion);
+  InitCDBStorage("local://$ALICE_ROOT");
+  AliCDBPath path(detName,"Align","Data");
+  AliCDBEntry* entry = AliCDBManager::Instance()->Get(path.GetPath(),runnum,version,sversion);
+
+  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 ApplyDisplacements(param, id);
+//_____________________________________________________________________________
+Bool_t AliSimulation::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;
 
 }
 
@@ -371,6 +489,8 @@ Bool_t AliSimulation::Run(Int_t nEvents)
 {
 // run the generation, simulation and digitization
 
+  InitCDBStorage(fCDBUri);
+
   if (nEvents > 0) fNEvents = nEvents;
 
   // generation and simulation -> hits
@@ -426,75 +546,32 @@ Bool_t AliSimulation::RunTrigger(const char* descriptors)
 {
   // run the trigger
 
-  TStopwatch stopwatch;
-  stopwatch.Start();
+   TStopwatch stopwatch;
+   stopwatch.Start();
 
-  AliRunLoader* runLoader = LoadRun("READ");
-  if (!runLoader) return kFALSE;
-  TString des = descriptors;
+   AliRunLoader* runLoader = LoadRun("READ");
+   if (!runLoader) return kFALSE;
+   TString des = descriptors;
+
+   runLoader->MakeTree( "CT" );
+   AliCentralTrigger* aCTP = runLoader->GetTrigger();
   // Load Descriptors
-  AliCentralTrigger* aCTP = new AliCentralTrigger( des );
+   aCTP->LoadDescriptor( des );
 
   // digits -> trigger
-  if( !aCTP->RunTrigger( runLoader ) ) {
-    if (fStopOnError) {
-      delete aCTP;
-      return kFALSE;
-    }
-  }
-
-/*
-  // Process each event
-  for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
-    AliInfo(Form("processing event %d", iEvent));
-    runLoader->GetEvent(iEvent);
-
-    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)) {
-        AliInfo(Form("triggering from digits for %s", det->GetName()));
-
-     //   AliLoader* loader = fLoader[iDet];
-     //   loader->LoadDigits("read");
-     //   TTree* digitsTree = loader->TreeD();
-     //   det->Trigger( digitsTree );
-     // or
-        AliTriggerDetector* tdet = det->CreateTriggerDetector();
-        TObjArray* detInp = dtrg->GetTriggerInputs();
-        for( Int_t i=0; i<detInp->GetEntriesFast(); i++ )
-               fInputs.AddLast( detInp->At(i) );
-
-        AliInfo(Form("Execution time for %s: R:%.2fs C:%.2fs",
-                det->GetName(),stopwatchDet.RealTime(),stopwatchDet.CpuTime()));
-      }
-    }
-
-    if ((detStr.CompareTo("ALL") != 0) && !detStr.IsNull()) {
-      AliError(Form("the following detectors were not found: %s",
-                    detStr.Data()));
+   if( !aCTP->RunTrigger( runLoader ) ) {
       if (fStopOnError) {
-         delete centralTP;
+    //  delete aCTP;
          return kFALSE;
       }
-    }
-
-    // Check trigger conditions
-    centralTP->TriggerConditions();
+   }
 
-    // Write trigger ????
-    centralTP->Write();
+   AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
+           stopwatch.RealTime(),stopwatch.CpuTime()));
 
-  } */
+   delete runLoader;
 
-  AliInfo(Form("Execution time: R:%.2fs C:%.2fs",
-               stopwatch.RealTime(),stopwatch.CpuTime()));
-
-  delete aCTP;
-  delete runLoader;
-
-  return kTRUE;
+   return kTRUE;
 }
 
 
@@ -521,13 +598,83 @@ Bool_t AliSimulation::RunSimulation(Int_t nEvents)
   StdoutToAliInfo(StderrToAliError(
     gAlice->Init(fConfigFileName.Data());
   ););
+
+  // Set run number in CDBManager (here????)
+  AliCDBManager::Instance()->SetRun(gAlice->GetRunNumber());
+  AliInfo(Form("Run number: %d",AliCDBManager::Instance()->GetRun()));
+
   AliRunLoader* runLoader = gAlice->GetRunLoader();
   if (!runLoader) {
-    AliError(Form("gAlice has no run loader object. "
-                  "Check your config file: %s", fConfigFileName.Data()));
-    return kFALSE;
+             AliError(Form("gAlice has no run loader object. "
+                            "Check your config file: %s", fConfigFileName.Data()));
+             return kFALSE;
   }
   SetGAliceFile(runLoader->GetFileName());
+  // 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");
+
+//   AliRunLoader* runLoader = gAlice->GetRunLoader();
+//   if (!runLoader) {
+//     AliError(Form("gAlice has no run loader object. "
+//                   "Check your config file: %s", fConfigFileName.Data()));
+//     return kFALSE;
+//   }
+//   SetGAliceFile(runLoader->GetFileName());
 
   if (!gAlice->Generator()) {
     AliError(Form("gAlice has no generator object. "
@@ -919,20 +1066,38 @@ Bool_t AliSimulation::ConvertDateToRoot(const char* dateFileName,
   AliInfo(Form("converting DATE file %s to root file %s", 
                dateFileName, rootFileName));
 
-  gSystem->Exec("rm -rf /tmp/mdc1");
-  gSystem->Exec("rm -rf /tmp/mdc2");
-
-  gSystem->Exec("mkdir /tmp/mdc1");
-  gSystem->Exec("mkdir /tmp/mdc2");
-
-  char command[256];
-  sprintf(command, "alimdc %d %d %d %d %s", 
-         kDBSize, kTagDBSize, kFilter, kCompression, dateFileName);
-  Int_t result = gSystem->Exec(command);
-  sprintf(command, "mv /tmp/mdc1/*.root %s", rootFileName);
-  gSystem->Exec(command);
-  gSystem->Exec("rm -rf /tmp/mdc1");
-  gSystem->Exec("rm -rf /tmp/mdc2");
+  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")) 
+    rawDBFS[0] = gSystem->Getenv("ALIMDC_RAWDB1");
+  if (gSystem->Getenv("ALIMDC_RAWDB2")) 
+    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));
+  gSystem->Exec(Form("mv %s/*.root %s", rawDBFS[0], rootFileName));
+
+  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);
 }