]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/MUONRefit.C
Remove the dollar-id-dollar in the responsible name which interfere with CDB_MD metad...
[u/mrichter/AliRoot.git] / MUON / MUONRefit.C
index 1bddca84a2c4347da1e4a206dcd781a1b179a274..7b94be63193683a5a212e96fd75d24f992ffe722 100644 (file)
 #include <TROOT.h>
 
 // STEER includes
-#include "AliMagF.h"
-#include "AliTracker.h"
 #include "AliESDEvent.h"
 #include "AliESDMuonTrack.h"
-#include "AliRecoParam.h"
 #include "AliCDBManager.h"
-#include "AliCDBEntry.h"
-#include "AliCDBPath.h"
 #include "AliGeomManager.h"
 
 // MUON includes
-#include "AliMpCDB.h"
+#include "AliMUONCDB.h"
 #include "AliMUONRecoParam.h"
 #include "AliMUONESDInterface.h"
 #include "AliMUONRefitter.h"
 #include "AliMUONTrack.h"
 #include "AliMUONVTrackStore.h"
 #include "AliMUONTrackParam.h"
-#include "AliMUONTrackExtrap.h"
 #endif
 
 const Int_t printLevel = 1;
 
-void Prepare();
 TTree* GetESDTree(TFile *esdFile);
 
 //-----------------------------------------------------------------------
-void MUONRefit(Int_t nevents = -1, const char* esdFileNameIn = "AliESDs.root", const char* esdFileNameOut = "AliESDs_New.root")
+void MUONRefit(Int_t nevents = -1, const char* esdFileNameIn = "AliESDs.root", const char* esdFileNameOut = "AliESDs_New.root",
+              const char* geoFilename = "geometry.root", const char* ocdbPath = "local://$ALICE_ROOT/OCDB")
 {
+  /// Example of muon refitting:
   /// refit ESD tracks from ESD pads (i.e. re-clusterized the attached ESD clusters);
   /// reset the charge of the digit using their raw charge before refitting;
   /// compare results with original ESD tracks; 
   /// write results in a new ESD file
   
-  // prepare the refitting
-  gRandom->SetSeed(1);
-  Prepare();
+  // open the ESD file and tree
+  TFile* esdFile = TFile::Open(esdFileNameIn);
+  TTree* esdTree = GetESDTree(esdFile);
+  
+  // connect ESD event to the ESD tree
+  AliESDEvent* esd = new AliESDEvent();
+  esd->ReadFromTree(esdTree);
+  
+  // create the ESD output file and tree
+  TFile* newESDFile = TFile::Open(esdFileNameOut, "RECREATE");
+  newESDFile->SetCompressionLevel(2);
+  
+  // connect ESD event to the ESD tree (recreate track branch for backward compatibility)
+  esdTree->SetBranchStatus("*MuonTracks*",0);
+  TTree* newESDTree = esdTree->CloneTree(0);
+  esdTree->SetBranchStatus("*MuonTracks*",1);
+  esd->WriteToTree(newESDTree);
+  
+  // get run number
+  if (esdTree->GetEvent(0) <= 0) {
+    Error("MUONRefit", "no ESD object found for event 0");
+    return;
+  }
+  Int_t runNumber = esd->GetRunNumber();
+  
+  // Import TGeo geometry
+  if (!gGeoManager) {
+    AliGeomManager::LoadGeometry(geoFilename);
+    if (!gGeoManager) {
+      Error("MUONRefit", "getting geometry from file %s failed", "generated/galice.root");
+      exit(-1);
+    }
+  }
+  
+  // load necessary data from OCDB
+  AliCDBManager::Instance()->SetDefaultStorage(ocdbPath);
+  AliCDBManager::Instance()->SetRun(runNumber);
+  if (!AliMUONCDB::LoadField()) return;
+  if (!AliMUONCDB::LoadMapping(kTRUE)) return;
   
   // reconstruction parameters for the refitting
   AliMUONRecoParam* recoParam = AliMUONRecoParam::GetLowFluxParam();
@@ -79,20 +110,8 @@ void MUONRefit(Int_t nevents = -1, const char* esdFileNameIn = "AliESDs.root", c
   AliMUONESDInterface esdInterface;
   AliMUONRefitter refitter(recoParam);
   refitter.Connect(&esdInterface);
+  gRandom->SetSeed(1);
   
-  // open the ESD file and tree
-  TFile* esdFile = TFile::Open(esdFileNameIn);
-  TTree* esdTree = GetESDTree(esdFile);
-  
-  // create the ESD output file and tree
-  TFile* newESDFile = TFile::Open(esdFileNameOut, "RECREATE");
-  newESDFile->SetCompressionLevel(2);
-  TTree* newESDTree = esdTree->CloneTree(0);
-  
-  // connect ESD event to the ESD tree
-  AliESDEvent* esd = new AliESDEvent();
-  esd->ReadFromTree(esdTree);
-
   // timer start...
   TStopwatch timer;
   
@@ -115,9 +134,13 @@ void MUONRefit(Int_t nevents = -1, const char* esdFileNameIn = "AliESDs.root", c
     if (nTracks < 1) continue;
     
     // load the current event
-    esdInterface.LoadEvent(*esd);
+    esdInterface.LoadEvent(*esd, kFALSE);
+    
+    // remove digits and clusters from ESD
+    esd->FindListObject("MuonClusters")->Clear("C");
+    esd->FindListObject("MuonPads")->Clear("C");
     
-    // loop over digit to modify their charge
+    // loop over digits to modify their charge
     AliMUONVDigit *digit;
     TIter next(esdInterface.CreateDigitIterator());
     while ((digit = static_cast<AliMUONVDigit*>(next()))) {
@@ -126,6 +149,7 @@ void MUONRefit(Int_t nevents = -1, const char* esdFileNameIn = "AliESDs.root", c
     }
     
     // refit the tracks from digits
+    refitter.SetFirstClusterIndex(0);
     AliMUONVTrackStore* newTrackStore = refitter.ReconstructFromDigits();
     
     //----------------------------------------------//
@@ -148,12 +172,19 @@ void MUONRefit(Int_t nevents = -1, const char* esdFileNameIn = "AliESDs.root", c
       AliMUONTrack* newTrack = (AliMUONTrack*) newTrackStore->FindObject(esdTrack->GetUniqueID());
       
       // replace the content of the current ESD track or remove it if the refitting has failed
-      if (newTrack) {
+      if (newTrack && (!recoParam->ImproveTracks() || newTrack->IsImproved())) {
+       
+       // fill the track info
        Double_t vertex[3] = {esdTrack->GetNonBendingCoor(), esdTrack->GetBendingCoor(), esdTrack->GetZ()};
-       AliMUONESDInterface::MUONToESD(*newTrack, *esdTrack, vertex, esdInterface.GetDigits());
-      } else {
-       esdTracks->Remove(esdTrack);
-      }
+       AliMUONESDInterface::MUONToESD(*newTrack, *esdTrack, vertex);
+       
+       // add the clusters (and the digits if previously there)
+       for (Int_t i = 0; i < newTrack->GetNClusters(); i++) {
+         AliMUONTrackParam *param = static_cast<AliMUONTrackParam*>(newTrack->GetTrackParamAtCluster()->UncheckedAt(i));
+         AliMUONESDInterface::MUONToESD(*(param->GetClusterPtr()), *esd, esdInterface.GetDigits());
+       }
+       
+      } else esdTracks->Remove(esdTrack);
       
       // print initial and re-fitted track parameters at first cluster if any
       if (printLevel>0) {
@@ -194,62 +225,11 @@ void MUONRefit(Int_t nevents = -1, const char* esdFileNameIn = "AliESDs.root", c
   // free memory
   esdFile->Close();
   delete esd;
+  delete recoParam;
   
   cout<<endl<<"time to refit: R:"<<timer.RealTime()<<" C:"<<timer.CpuTime()<<endl<<endl;
 }
 
-//-----------------------------------------------------------------------
-void Prepare()
-{
-  /// Set the geometry, the magnetic field, the mapping and the reconstruction parameters
-  
-  // Import TGeo geometry (needed by AliMUONTrackExtrap::ExtrapToVertex)
-  if (!gGeoManager) {
-    AliGeomManager::LoadGeometry("geometry.root");
-    if (!gGeoManager) {
-      Error("MUONRefit", "getting geometry from file %s failed", "generated/galice.root");
-      return;
-    }
-  }
-  
-  // set  mag field 
-  // waiting for mag field in CDB 
-  if (!TGeoGlobalMagField::Instance()->GetField()) {
-    printf("Loading field map...\n");
-    AliMagF* field = new AliMagF("Maps","Maps",1.,1.,AliMagF::k5kG);
-    TGeoGlobalMagField::Instance()->SetField(field);
-  }
-  // set the magnetic field for track extrapolations
-  AliMUONTrackExtrap::SetField();
-  
-  // Load mapping
-  AliCDBManager* man = AliCDBManager::Instance();
-  man->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
-  man->SetRun(0);
-  if ( ! AliMpCDB::LoadDDLStore() ) {
-    Error("MUONRefit","Could not access mapping from OCDB !");
-    exit(-1);
-  }
-  
-  // Load initial reconstruction parameters from OCDB
-  AliMUONRecoParam* recoParam = 0x0;
-  AliCDBPath path("MUON","Calib","RecoParam");
-  AliCDBEntry *entry=man->Get(path.GetPath());
-  if(entry) {
-    recoParam = dynamic_cast<AliMUONRecoParam*>(entry->GetObject());
-    entry->SetOwner(0);
-    AliCDBManager::Instance()->UnloadFromCache(path.GetPath());
-  }
-  if (!recoParam) {
-    printf("Couldn't find RecoParam object in OCDB: create default one");
-    recoParam = AliMUONRecoParam::GetLowFluxParam();
-  }
-  Info("MUONRefit", "\n initial recontruction parameters:");
-  recoParam->Print("FULL");
-  AliMUONESDInterface::ResetTracker(recoParam);
-  
-}
-
 //-----------------------------------------------------------------------
 TTree* GetESDTree(TFile *esdFile)
 {