]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONRefit.C
Fix coverity defects
[u/mrichter/AliRoot.git] / MUON / MUONRefit.C
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 /// \ingroup macros
19 /// \file MUONRefit.C
20 /// \brief Macro for refitting ESD tracks from ESD pads
21 ///
22 /// \author Philippe Pillot, SUBATECH
23
24 #if !defined(__CINT__) || defined(__MAKECINT__)
25 #include <TStopwatch.h>
26 #include <TFile.h>
27 #include <TTree.h>
28 #include <TString.h>
29 #include <Riostream.h>
30 #include <TGeoManager.h>
31 #include <TRandom.h>
32 #include <TROOT.h>
33
34 // STEER includes
35 #include "AliESDEvent.h"
36 #include "AliESDMuonTrack.h"
37 #include "AliCDBManager.h"
38 #include "AliGeomManager.h"
39
40 // MUON includes
41 #include "AliMUONCDB.h"
42 #include "AliMUONRecoParam.h"
43 #include "AliMUONESDInterface.h"
44 #include "AliMUONRefitter.h"
45 #include "AliMUONVDigit.h"
46 #include "AliMUONTrack.h"
47 #include "AliMUONVTrackStore.h"
48 #include "AliMUONTrackParam.h"
49 #endif
50
51 const Int_t printLevel = 1;
52
53 TTree* GetESDTree(TFile *esdFile);
54
55 //-----------------------------------------------------------------------
56 void MUONRefit(Int_t nevents = -1, const char* esdFileNameIn = "AliESDs.root", const char* esdFileNameOut = "AliESDs_New.root",
57                const char* geoFilename = "geometry.root", const char* ocdbPath = "local://$ALICE_ROOT/OCDB")
58 {
59   /// Example of muon refitting:
60   /// refit ESD tracks from ESD pads (i.e. re-clusterized the attached ESD clusters);
61   /// reset the charge of the digit using their raw charge before refitting;
62   /// compare results with original ESD tracks; 
63   /// write results in a new ESD file
64   
65   // open the ESD file and tree
66   TFile* esdFile = TFile::Open(esdFileNameIn);
67   TTree* esdTree = GetESDTree(esdFile);
68   
69   // create the ESD output file and tree
70   TFile* newESDFile = TFile::Open(esdFileNameOut, "RECREATE");
71   newESDFile->SetCompressionLevel(2);
72   TTree* newESDTree = esdTree->CloneTree(0);
73   
74   // connect ESD event to the ESD tree
75   AliESDEvent* esd = new AliESDEvent();
76   esd->ReadFromTree(esdTree);
77   
78   // get run number
79   if (esdTree->GetEvent(0) <= 0) {
80     Error("MUONRefit", "no ESD object found for event 0");
81     return;
82   }
83   Int_t runNumber = esd->GetRunNumber();
84   
85   // Import TGeo geometry
86   if (!gGeoManager) {
87     AliGeomManager::LoadGeometry(geoFilename);
88     if (!gGeoManager) {
89       Error("MUONRefit", "getting geometry from file %s failed", "generated/galice.root");
90       exit(-1);
91     }
92   }
93   
94   // load necessary data from OCDB
95   AliCDBManager::Instance()->SetDefaultStorage(ocdbPath);
96   AliCDBManager::Instance()->SetRun(runNumber);
97   if (!AliMUONCDB::LoadField()) return;
98   if (!AliMUONCDB::LoadMapping(kTRUE)) return;
99   
100   // reconstruction parameters for the refitting
101   AliMUONRecoParam* recoParam = AliMUONRecoParam::GetLowFluxParam();
102   Info("MUONRefit", "\n Reconstruction parameters for refitting:");
103   recoParam->Print("FULL");
104   
105   AliMUONESDInterface esdInterface;
106   AliMUONRefitter refitter(recoParam);
107   refitter.Connect(&esdInterface);
108   gRandom->SetSeed(1);
109   
110   // timer start...
111   TStopwatch timer;
112   
113   // Loop over ESD events
114   if (nevents > 0) nevents = TMath::Min(nevents,(Int_t)esdTree->GetEntries());
115   else nevents = (Int_t)esdTree->GetEntries();
116   for (Int_t iEvent = 0; iEvent < nevents; iEvent++) {
117     if (printLevel>0) cout<<endl<<"            ****************event #"<<iEvent+1<<"****************"<<endl;
118     
119     //----------------------------------------------//
120     // -------------- process event --------------- //
121     //----------------------------------------------//
122     // get the ESD of current event
123     esdTree->GetEvent(iEvent);
124     if (!esd) {
125       Error("MUONRefit", "no ESD object found for event %d", iEvent);
126       return;
127     }
128     Int_t nTracks = (Int_t)esd->GetNumberOfMuonTracks();
129     if (nTracks < 1) continue;
130     
131     // load the current event
132     esdInterface.LoadEvent(*esd, kFALSE);
133     
134     // loop over digit to modify their charge
135     AliMUONVDigit *digit;
136     TIter next(esdInterface.CreateDigitIterator());
137     while ((digit = static_cast<AliMUONVDigit*>(next()))) {
138       digit->SetCharge(digit->ADC());
139       digit->Calibrated(kFALSE);
140     }
141     
142     // refit the tracks from digits
143     AliMUONVTrackStore* newTrackStore = refitter.ReconstructFromDigits();
144     
145     //----------------------------------------------//
146     // ------ fill new ESD and print results ------ //
147     //----------------------------------------------//
148     // loop over the list of ESD tracks
149     TClonesArray *esdTracks = (TClonesArray*) esd->FindListObject("MuonTracks");
150     for (Int_t iTrack = 0; iTrack <  nTracks; iTrack++) {
151       
152       // get the ESD track
153       AliESDMuonTrack* esdTrack = (AliESDMuonTrack*) esdTracks->UncheckedAt(iTrack);
154       
155       // skip ghost tracks (leave them unchanged in the new ESD file)
156       if (!esdTrack->ContainTrackerData()) continue;
157       
158       // get the corresponding MUON track
159       AliMUONTrack* track = esdInterface.FindTrack(esdTrack->GetUniqueID());
160       
161       // Find the corresponding re-fitted MUON track
162       AliMUONTrack* newTrack = (AliMUONTrack*) newTrackStore->FindObject(esdTrack->GetUniqueID());
163       
164       // replace the content of the current ESD track or remove it if the refitting has failed
165       if (newTrack) {
166         Double_t vertex[3] = {esdTrack->GetNonBendingCoor(), esdTrack->GetBendingCoor(), esdTrack->GetZ()};
167         AliMUONESDInterface::MUONToESD(*newTrack, *esdTrack, vertex, esdInterface.GetDigits());
168       } else {
169         esdTracks->Remove(esdTrack);
170       }
171       
172       // print initial and re-fitted track parameters at first cluster if any
173       if (printLevel>0) {
174         cout<<"            ----------------track #"<<iTrack+1<<"----------------"<<endl;
175         cout<<"before refit:"<<endl;
176         AliMUONTrackParam *param = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
177         param->Print("FULL");
178         if (printLevel>1) param->GetCovariances().Print();
179         if (!newTrack) continue;
180         cout<<"after refit:"<<endl;
181         param = (AliMUONTrackParam*) newTrack->GetTrackParamAtCluster()->First();
182         param->Print("FULL");
183         if (printLevel>1) param->GetCovariances().Print();
184         cout<<"            ----------------------------------------"<<endl;
185       }
186       
187     }
188     
189     // free memory
190     delete newTrackStore;
191     
192     // fill new ESD tree with new tracks
193     esdTracks->Compress();
194     newESDTree->Fill();
195     
196     if (printLevel>0) cout<<"            ****************************************"<<endl;
197   }
198   
199   // ...timer stop
200   timer.Stop();
201   
202   // write output ESD tree
203   newESDFile->cd();
204   newESDTree->Write();
205   delete newESDTree;
206   newESDFile->Close();
207   
208   // free memory
209   esdFile->Close();
210   delete esd;
211   delete recoParam;
212   
213   cout<<endl<<"time to refit: R:"<<timer.RealTime()<<" C:"<<timer.CpuTime()<<endl<<endl;
214 }
215
216 //-----------------------------------------------------------------------
217 TTree* GetESDTree(TFile *esdFile)
218 {
219   /// Check that the file is properly open
220   /// Return pointer to the ESD Tree
221   
222   if (!esdFile || !esdFile->IsOpen()) {
223     Error("GetESDTree", "opening ESD file failed");
224     exit(-1);
225   }
226   
227   TTree* tree = (TTree*) esdFile->Get("esdTree");
228   if (!tree) {
229     Error("GetESDTree", "no ESD tree found");
230     exit(-1);
231   }
232   
233   return tree;
234   
235 }
236