]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/STEER/AliReconstruction.cxx
LoadCDB will query detector/Trigger apart from detector/Calib (Cvetan)
[u/mrichter/AliRoot.git] / STEER / STEER / AliReconstruction.cxx
index 4a7cb877a865ac88b00de362dcf56db7c31b8252..b10a1382a154f4729a950b1cac7b2b93065e4958 100644 (file)
 #include <TRandom.h>
 
 #include "AliAlignObj.h"
+#include "AliAnalysisManager.h"
+#include "AliAnalysisDataContainer.h"
 #include "AliCDBEntry.h"
 #include "AliCDBManager.h"
 #include "AliCDBStorage.h"
 #include "AliRawReaderDate.h"
 #include "AliRawReaderFile.h"
 #include "AliRawReaderRoot.h"
+#include "AliRecoInputHandler.h"
 #include "AliReconstruction.h"
 #include "AliReconstructor.h"
 #include "AliRun.h"
 ClassImp(AliReconstruction)
 
 //_____________________________________________________________________________
-const char* AliReconstruction::fgkDetectorName[AliReconstruction::kNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE", "HLT"};
+const char* AliReconstruction::fgkDetectorName[AliReconstruction::kNDetectors] = {"ITS", "TPC", "TRD", "TOF", "PHOS", "HMPID", "EMCAL", "MUON", "FMD", "ZDC", "PMD", "T0", "VZERO", "ACORDE"
+// #ifdef MFT_UPGRADE
+//                                                                                   , "MFT"
+// #endif 
+                                                                                  , "MFT"    // AU
+                                                                                 , "HLT"
+};
 
 //_____________________________________________________________________________
 AliReconstruction::AliReconstruction(const char* gAliceFilename) :
@@ -272,6 +281,8 @@ AliReconstruction::AliReconstruction(const char* gAliceFilename) :
   fQARefUri(),
   fSpecCDBUri(), 
   fInitCDBCalled(kFALSE),
+  fFromCDBSnapshot(kFALSE),
+  fSnapshotFileName(""),
   fSetRunNumberFromDataCalled(kFALSE),
   fQADetectors("ALL"), 
   fQATasks("ALL"), 
@@ -299,7 +310,10 @@ AliReconstruction::AliReconstruction(const char* gAliceFilename) :
   fSspecie(0),
   fNhighPt(0),
   fShighPt(0),
-  fUpgradeModule("") 
+  fUpgradeModule(""),
+  fAnalysisMacro(),
+  fAnalysis(0),
+  fRecoHandler(0) 
 {
 // create reconstruction object with default parameters
   gGeoManager = NULL;
@@ -387,6 +401,8 @@ AliReconstruction::AliReconstruction(const AliReconstruction& rec) :
   fQARefUri(rec.fQARefUri),
   fSpecCDBUri(), 
   fInitCDBCalled(rec.fInitCDBCalled),
+  fFromCDBSnapshot(rec.fFromCDBSnapshot),
+  fSnapshotFileName(rec.fSnapshotFileName),
   fSetRunNumberFromDataCalled(rec.fSetRunNumberFromDataCalled),
   fQADetectors(rec.fQADetectors), 
   fQATasks(rec.fQATasks), 
@@ -414,7 +430,10 @@ AliReconstruction::AliReconstruction(const AliReconstruction& rec) :
   fSspecie(0),
   fNhighPt(0),
   fShighPt(0),
-  fUpgradeModule("")
+  fUpgradeModule(""),
+  fAnalysisMacro(rec.fAnalysisMacro),
+  fAnalysis(0),
+  fRecoHandler(0)
 {
 // copy constructor
 
@@ -550,6 +569,8 @@ AliReconstruction& AliReconstruction::operator = (const AliReconstruction& rec)
   fQARefUri      = rec.fQARefUri;
   fSpecCDBUri.Delete();
   fInitCDBCalled               = rec.fInitCDBCalled;
+  fFromCDBSnapshot             = rec.fFromCDBSnapshot;
+  fSnapshotFileName            = rec.fSnapshotFileName;
   fSetRunNumberFromDataCalled  = rec.fSetRunNumberFromDataCalled;
   fQADetectors                 = rec.fQADetectors;
   fQATasks                     = rec.fQATasks; 
@@ -578,6 +599,9 @@ AliReconstruction& AliReconstruction::operator = (const AliReconstruction& rec)
   fNhighPt = 0;
   fShighPt = 0;
   fUpgradeModule="";
+  fAnalysisMacro = rec.fAnalysisMacro;
+  fAnalysis = 0;
+  fRecoHandler = 0;
 
   return *this;
 }
@@ -1058,6 +1082,7 @@ Bool_t AliReconstruction::InitGRP() {
     AliError("GRP/GRP/Data entry:  missing value for the detector mask ! Using 1074790399");
     activeDetectors = 1074790399;
   }
+  AliDebug(1, Form("activeDetectors = %d", activeDetectors));
 
   fRunInfo = new AliRunInfo(lhcState, beamType, beamEnergy, runType, activeDetectors);
   fRunInfo->Dump();
@@ -1066,7 +1091,9 @@ Bool_t AliReconstruction::InitGRP() {
   // Process the list of active detectors
   if (activeDetectors) {
     UInt_t detMask = activeDetectors;
+    AliDebug(1, Form("Detector List = %s", fRunLocalReconstruction.Data()));
     fRunLocalReconstruction = MatchDetectorList(fRunLocalReconstruction,detMask);
+    AliDebug(1, Form("Detector List = %s", fRunLocalReconstruction.Data()));
     fRunTracking = MatchDetectorList(fRunTracking,detMask);
     fFillESD = MatchDetectorList(fFillESD,detMask);
     fQADetectors = MatchDetectorList(fQADetectors,detMask);
@@ -1212,6 +1239,7 @@ Bool_t AliReconstruction::LoadCDB()
   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
     AliCDBManager::Instance()->GetAll(Form("%s/Calib/*",fgkDetectorName[iDet]));
+    AliCDBManager::Instance()->GetAll(Form("%s/Trigger/*",fgkDetectorName[iDet]));
   }
 
   // Temporary fix - one has to define the correct policy in order
@@ -1484,6 +1512,14 @@ void AliReconstruction::Begin(TTree *)
     AliSysInfo::AddStamp("CheckGeom");
   }
 
+  if(fFromCDBSnapshot){
+      AliDebug(2,"Initializing from a CDB snapshot");
+      if(!AliCDBManager::Instance()->InitFromSnapshot(fSnapshotFileName.Data())){
+         Abort("InitFromSnapshot", TSelector::kAbortProcess);
+         return;
+      }
+  }
+
   if (!MisalignGeometry(fLoadAlignData)) {
     Abort("MisalignGeometry", TSelector::kAbortProcess);
     return;
@@ -1497,11 +1533,13 @@ void AliReconstruction::Begin(TTree *)
   }
   AliSysInfo::AddStamp("InitGRP");
 
-  if (!LoadCDB()) {
-    Abort("LoadCDB", TSelector::kAbortProcess);
-    return;
+  if(!fFromCDBSnapshot){
+      if (!LoadCDB()) {
+         Abort("LoadCDB", TSelector::kAbortProcess);
+         return;
+      }
+      AliSysInfo::AddStamp("LoadCDB");
   }
-  AliSysInfo::AddStamp("LoadCDB");
 
   if (!LoadTriggerScalersCDB()) {
     Abort("LoadTriggerScalersCDB", TSelector::kAbortProcess);
@@ -1538,6 +1576,10 @@ void AliReconstruction::Begin(TTree *)
     AliMagF *magFieldMap = (AliMagF*)TGeoGlobalMagField::Instance()->GetField();
     magFieldMap->SetName("MagneticFieldMap");
     gProof->AddInputData(magFieldMap,kTRUE);
+    if (fAnalysis) {
+      fAnalysis->SetName("Analysis");
+      gProof->AddInputData(fAnalysis,kTRUE);
+    }  
   }
 
 }
@@ -1579,6 +1621,11 @@ void AliReconstruction::SlaveBegin(TTree*)
       TGeoGlobalMagField::Instance()->SetField(newMap);
       TGeoGlobalMagField::Instance()->Lock();
     }
+    if (!fAnalysis) {
+       // Attempt to get the analysis manager from the input list
+       fAnalysis = (AliAnalysisManager*)fInput->FindObject("Analysis");
+       if (fAnalysis) AliInfo("==== Analysis manager retrieved from input list ====");
+    }   
     if (TNamed *outputFileName = (TNamed*)fInput->FindObject("PROOF_OUTPUTFILE"))
       fProofOutputFileName = outputFileName->GetTitle();
     if (TNamed *outputLocation = (TNamed*)fInput->FindObject("PROOF_OUTPUTFILE_LOCATION"))
@@ -1602,7 +1649,22 @@ void AliReconstruction::SlaveBegin(TTree*)
     }
     AliSysInfo::AddStamp("ReadInputInSlaveBegin");
   }
-
+  // Check if analysis was requested in the reconstruction event loop
+  if (!fAnalysis) {
+    // Attempt to connect in-memory singleton
+    fAnalysis = AliAnalysisManager::GetAnalysisManager();
+    if (fAnalysis) AliInfo(Form("==== Analysis manager <%s> found in memory ====", fAnalysis->GetName()));
+    // Check if an analysis macro was specified
+    if (!fAnalysis && !fAnalysisMacro.IsNull()) {
+      // Run specified analysis macro
+      gROOT->ProcessLine(Form(".x %s",fAnalysisMacro.Data()));
+      fAnalysis = AliAnalysisManager::GetAnalysisManager();
+      if (!fAnalysis) AliError(Form("No analysis manager produced by analysis macro %s", fAnalysisMacro.Data()));
+      else AliInfo(Form("==== Analysis manager <%s> produced by analysis macro <%s> ====", 
+                        fAnalysis->GetName(), fAnalysisMacro.Data()));
+    }
+  }
+  
   // get the run loader
   if (!InitRunLoader()) {
     Abort("InitRunLoader", TSelector::kAbortProcess);
@@ -1704,7 +1766,28 @@ void AliReconstruction::SlaveBegin(TTree*)
 
   if (strcmp(gProgName,"alieve") == 0)
     fRunAliEVE = InitAliEVE();
-
+  // If we have an analysis manager, connect the AliRecoInputHandler here  
+  if (fAnalysis) {
+    if (!dynamic_cast<AliRecoInputHandler*>(fAnalysis->GetInputEventHandler())) {
+       AliError("Analysis manager used in reconstruction should use AliRecoInputHandler - \
+                 \n  ->Replacing with AliRecoInputHandler instance.");
+       delete fAnalysis->GetInputEventHandler();
+    }
+    // Set the event and other data pointers
+    fRecoHandler = new AliRecoInputHandler();
+    fRecoHandler->Init(ftree, "LOCAL");
+    fRecoHandler->SetEvent(fesd);
+    fRecoHandler->SetESDfriend(fesdf);
+    fRecoHandler->SetHLTEvent(fhltesd);
+    fRecoHandler->SetHLTTree(fhlttree);
+    fAnalysis->SetInputEventHandler(fRecoHandler);
+    // Enter external loop mode
+    fAnalysis->SetExternalLoop(kTRUE);
+    // Initialize analysis
+    fAnalysis->StartAnalysis("local", (TTree*)0);
+    // Connect ESD tree with the input container
+    fAnalysis->GetCommonInputContainer()->SetData(ftree);
+  }  
   return;
 }
 
@@ -1720,7 +1803,7 @@ Bool_t AliReconstruction::Process(Long64_t entry)
   currTree->SetBranchAddress("rawevent",&event);
   currTree->GetEntry(entry);
   fRawReader = new AliRawReaderRoot(event);
-  fStatus = ProcessEvent(fRunLoader->GetNumberOfEvents());  
+  fStatus = ProcessEvent(fRunLoader->GetNumberOfEvents());
   delete fRawReader;
   fRawReader = NULL;
   delete event;
@@ -1953,23 +2036,6 @@ Bool_t AliReconstruction::ProcessEvent(Int_t iEvent)
        if (fStopOnError) {CleanUp(); return kFALSE;}
       }
     }
-
-    // AdC+FN
-    if (fReconstructor[3])
-      GetReconstructor(3)->FillEventTimeWithTOF(fesd,&pid);
-
-    // combined PID
-    pid.MakePID(fesd);
-
-    if (fFillTriggerESD) {
-      if (!FillTriggerESD(fesd)) {
-       if (fStopOnError) {CleanUp(); return kFALSE;}
-      }
-    }
-    // Always fill scalers
-    if (!FillTriggerScalers(fesd)) {
-       if (fStopOnError) {CleanUp(); return kFALSE;}
-    }
     
 
     ffile->cd();
@@ -2034,8 +2100,8 @@ Bool_t AliReconstruction::ProcessEvent(Int_t iEvent)
        if (grpRecoParam) {
         Int_t nCutsVertexer = grpRecoParam->GetVertexerTracksNCuts();
         Double_t *cutsVertexer = new Double_t[nCutsVertexer];
-        grpRecoParam->GetVertexerTracksCutsITS(cutsVertexer);
-        ftVertexer->SetCuts(cutsVertexer);
+        grpRecoParam->GetVertexerTracksCutsITS(cutsVertexer,nCutsVertexer);
+        ftVertexer->SetCuts(cutsVertexer,nCutsVertexer);
         delete [] cutsVertexer; cutsVertexer = NULL; 
         if(grpRecoParam->GetVertexerTracksConstraintITS()) { 
           if(fDiamondProfile && fDiamondProfile->GetXRes()<kRadius){
@@ -2074,8 +2140,8 @@ Bool_t AliReconstruction::ProcessEvent(Int_t iEvent)
        if (grpRecoParam) {
         Int_t nCutsVertexer = grpRecoParam->GetVertexerTracksNCuts();
         Double_t *cutsVertexer = new Double_t[nCutsVertexer];
-        grpRecoParam->GetVertexerTracksCutsTPC(cutsVertexer);
-        ftVertexer->SetCuts(cutsVertexer);
+        grpRecoParam->GetVertexerTracksCutsTPC(cutsVertexer,nCutsVertexer);
+        ftVertexer->SetCuts(cutsVertexer,nCutsVertexer);
         delete [] cutsVertexer; cutsVertexer = NULL; 
         if(fDiamondProfileTPC && grpRecoParam->GetVertexerTracksConstraintTPC()) { 
           if(fDiamondProfileTPC->GetXRes()<kRadius) ftVertexer->SetVtxStart(fDiamondProfileTPC); // apply constraint only if sigmax is smaller than the beam pipe radius 
@@ -2129,6 +2195,23 @@ Bool_t AliReconstruction::ProcessEvent(Int_t iEvent)
        }
     }
 
+    // AdC+FN
+    if (fReconstructor[3])
+      GetReconstructor(3)->FillEventTimeWithTOF(fesd,&pid);
+
+    // combined PID
+    pid.MakePID(fesd);
+
+    if (fFillTriggerESD) {
+      if (!FillTriggerESD(fesd)) {
+       if (fStopOnError) {CleanUp(); return kFALSE;}
+      }
+    }
+    // Always fill scalers
+    if (!FillTriggerScalers(fesd)) {
+       if (fStopOnError) {CleanUp(); return kFALSE;}
+    }
+
     // write ESD
     if (fCleanESD) CleanESD(fesd);
     // 
@@ -2162,7 +2245,15 @@ Bool_t AliReconstruction::ProcessEvent(Int_t iEvent)
   if (pHLTSrc && pHLTTgt) {
     pHLTSrc->Copy(*pHLTTgt);
   }
-
+  //
+  // Perform analysis of this event if requested
+  // RS: Should be done before WriteESDfriend, since the latter may clean the esdfriend
+  if (fAnalysis) {
+    fRecoHandler->BeginEvent(iEvent);
+    fAnalysis->ExecAnalysis();
+    fRecoHandler->FinishEvent();
+  }  
+  //
     if (fWriteESDfriend) 
       fesd->GetESDfriend(fesdf);
 
@@ -2170,7 +2261,6 @@ Bool_t AliReconstruction::ProcessEvent(Int_t iEvent)
     if (fWriteESDfriend) {
       WriteESDfriend();
     }
-
     // Auto-save the ESD tree in case of prompt reco @P2
     if (fRawReader && fRawReader->UseAutoSaveESD()) {
       ftree->AutoSave("SaveSelf");
@@ -2182,7 +2272,7 @@ Bool_t AliReconstruction::ProcessEvent(Int_t iEvent)
 
     // call AliEVE
     if (fRunAliEVE) RunAliEVE();
-
+    //
     fesd->Reset();
     fhltesd->Reset();
     if (fWriteESDfriend) {
@@ -2214,7 +2304,7 @@ Bool_t AliReconstruction::ProcessEvent(Int_t iEvent)
        
   if (fRunQA || fRunGlobalQA) 
     AliQAManager::QAManager()->Increment() ; 
-  
+
     return kTRUE;
 }
 
@@ -2225,6 +2315,12 @@ void AliReconstruction::SlaveTerminate()
   // Called after the exit
   // from the event loop
   AliCodeTimerAuto("",0);
+  // If analysis was done during reconstruction, we need to call SlaveTerminate for it
+  if (fAnalysis) {
+     fAnalysis->PackOutput(fOutput);
+     fAnalysis->SetSkipTerminate(kTRUE);
+     fAnalysis->Terminate();
+  }   
 
   if (fIsNewRunLoader) { // galice.root didn't exist
     fRunLoader->WriteHeader("OVERWRITE");
@@ -2296,8 +2392,7 @@ void AliReconstruction::SlaveTerminate()
   }
 
   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
-    AliReconstructor *reconstructor = GetReconstructor(iDet);
-    if (reconstructor) reconstructor->Terminate();
+    if (fReconstructor[iDet]) fReconstructor[iDet]->Terminate();
   }
   // End of cycle for the in-loop  
 
@@ -2386,15 +2481,22 @@ Bool_t AliReconstruction::RunLocalEventReconstruction(const TString& detectors)
       // https://savannah.cern.ch/bugs/?35473
       AliInfo("running reconstruction for HLT");
       if (fRawReader) {
+       AliInfo("reconstructor->Reconstruct(fRawReader, NULL)");
         reconstructor->Reconstruct(fRawReader, NULL);
-      } else {
+      } 
+      else {
+       AliInfo("reconstructor->Reconstruct(dummy, NULL)");
         TTree* dummy=NULL;
         reconstructor->Reconstruct(dummy, NULL);
       }
     }
   }
+
+  AliInfo(Form("kNDetectors = %d",kNDetectors));
+
   for (Int_t iDet = 0; iDet < kNDetectors; iDet++) {
     if (!IsSelected(fgkDetectorName[iDet], detStr)) continue;
+    AliDebug(1, Form("Detector: %s", fgkDetectorName[iDet]));
     AliReconstructor* reconstructor = GetReconstructor(iDet);
     if (!reconstructor) continue;
     AliLoader* loader = fLoader[iDet];
@@ -2419,20 +2521,28 @@ Bool_t AliReconstruction::RunLocalEventReconstruction(const TString& detectors)
     // local reconstruction
     AliInfo(Form("running reconstruction for %s", fgkDetectorName[iDet]));
     //AliCodeTimerAuto(Form("running reconstruction for %s", fgkDetectorName[iDet]),0);
+    AliDebug(1, "Loading Rec Points");
     loader->LoadRecPoints("update");
+    AliDebug(1, "Cleaning Rec Points");
     loader->CleanRecPoints();
+    AliDebug(1, "Making Rec Points Container");
     loader->MakeRecPointsContainer();
     TTree* clustersTree = loader->TreeR();
     if (fRawReader && !reconstructor->HasDigitConversion()) {
       reconstructor->Reconstruct(fRawReader, clustersTree);
-    } else {
+    } 
+    else {
+      AliDebug(1, "Loading Digits");
       loader->LoadDigits("read");
       TTree* digitsTree = loader->TreeD();
+      AliDebug(1, Form("Digits Tree = %p",digitsTree));
       if (!digitsTree) {
         AliError(Form("Can't get the %s digits tree", fgkDetectorName[iDet]));
         if (fStopOnError) 
           return kFALSE;
-      } else {
+      } 
+      else {
+       AliDebug(1, "Digits -> Clusters");
         reconstructor->Reconstruct(digitsTree, clustersTree);
         if (fRunQA && IsInTasks(AliQAv1::kDIGITSR)) {
           AliQAManager::QAManager()->SetEventSpecie(fRecoParam.GetEventSpecie()) ;
@@ -2675,9 +2785,7 @@ Bool_t AliReconstruction::RunMuonTracking(AliESDEvent*& esd)
     AliError("Missing runLoader!");
     return kFALSE;
   }
-  Int_t iDet = 7; // for MUON
-
-  AliInfo("is running...");
+  Int_t iDet =  GetDetIndex("MUON"); // for MUON
 
   // Get a pointer to the MUON reconstructor
   AliReconstructor *reconstructor = GetReconstructor(iDet);
@@ -2703,8 +2811,6 @@ Bool_t AliReconstruction::RunMuonTracking(AliESDEvent*& esd)
 
   tracker->UnloadClusters();
   
-  delete tracker;
-  
   if ( rv )
   {
     AliError(Form("%s Clusters2Tracks failed", fgkDetectorName[iDet]));
@@ -3361,8 +3467,9 @@ void AliReconstruction::CleanUp()
 
   if (AliQAManager::QAManager())
     AliQAManager::QAManager()->ShowQA() ; 
-  //  AliQAManager::Destroy() ; 
-  
+  //  AliQAManager::Destroy() ;
+  delete fAnalysis; 
+  fAnalysis = NULL;
 }
 
 void AliReconstruction::WriteAlignmentData(AliESDEvent* esd)
@@ -4107,14 +4214,9 @@ void AliReconstruction::WriteESDfriend() {
 
   if (IsCosmicOrCalibSpecie()) { // Selection of calib or cosmic events
     fNspecie++;
-    Double_t curentSpecieFraction = ((Double_t)(fNspecie+1))/((Double_t)(fNall+1)); 
-    // "Bayesian" estimate supposing that without events all the events are of the required type
-    
-    Double_t rnd = gRandom->Rndm()*curentSpecieFraction;
-    if (rnd<fFractionFriends) {
-      isSelected = kTRUE;
-      fSspecie++;
-    }
+
+    isSelected = kTRUE;
+    fSspecie++;
   }
   
   Double_t remainingFraction = fFractionFriends;