Adding more bins in QA (Alis)
[u/mrichter/AliRoot.git] / MUON / MUONRefit.C
CommitLineData
7deb8eb0 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
7deb8eb0 35#include "AliESDEvent.h"
36#include "AliESDMuonTrack.h"
7deb8eb0 37#include "AliCDBManager.h"
38#include "AliGeomManager.h"
39
40// MUON includes
a99c3449 41#include "AliMUONCDB.h"
7deb8eb0 42#include "AliMUONRecoParam.h"
43#include "AliMUONESDInterface.h"
44#include "AliMUONRefitter.h"
45#include "AliMUONVDigit.h"
7deb8eb0 46#include "AliMUONTrack.h"
47#include "AliMUONVTrackStore.h"
48#include "AliMUONTrackParam.h"
7deb8eb0 49#endif
50
51const Int_t printLevel = 1;
52
7deb8eb0 53TTree* GetESDTree(TFile *esdFile);
54
55//-----------------------------------------------------------------------
a99c3449 56void 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")
7deb8eb0 58{
a99c3449 59 /// Example of muon refitting:
7deb8eb0 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
7deb8eb0 65 // open the ESD file and tree
66 TFile* esdFile = TFile::Open(esdFileNameIn);
67 TTree* esdTree = GetESDTree(esdFile);
68
fe0324de 69 // connect ESD event to the ESD tree
70 AliESDEvent* esd = new AliESDEvent();
71 esd->ReadFromTree(esdTree);
72
ad3c6eda 73 // create the ESD output file and tree
74 TFile* newESDFile = TFile::Open(esdFileNameOut, "RECREATE");
75 newESDFile->SetCompressionLevel(2);
7deb8eb0 76
fe0324de 77 // connect ESD event to the ESD tree (recreate track branch for backward compatibility)
78 esdTree->SetBranchStatus("*MuonTracks*",0);
79 TTree* newESDTree = esdTree->CloneTree(0);
80 esdTree->SetBranchStatus("*MuonTracks*",1);
81 esd->WriteToTree(newESDTree);
a99c3449 82
83 // get run number
84 if (esdTree->GetEvent(0) <= 0) {
85 Error("MUONRefit", "no ESD object found for event 0");
86 return;
87 }
88 Int_t runNumber = esd->GetRunNumber();
89
90 // Import TGeo geometry
91 if (!gGeoManager) {
92 AliGeomManager::LoadGeometry(geoFilename);
93 if (!gGeoManager) {
94 Error("MUONRefit", "getting geometry from file %s failed", "generated/galice.root");
95 exit(-1);
96 }
97 }
98
99 // load necessary data from OCDB
100 AliCDBManager::Instance()->SetDefaultStorage(ocdbPath);
101 AliCDBManager::Instance()->SetRun(runNumber);
102 if (!AliMUONCDB::LoadField()) return;
103 if (!AliMUONCDB::LoadMapping(kTRUE)) return;
104
105 // reconstruction parameters for the refitting
106 AliMUONRecoParam* recoParam = AliMUONRecoParam::GetLowFluxParam();
107 Info("MUONRefit", "\n Reconstruction parameters for refitting:");
108 recoParam->Print("FULL");
109
110 AliMUONESDInterface esdInterface;
111 AliMUONRefitter refitter(recoParam);
112 refitter.Connect(&esdInterface);
113 gRandom->SetSeed(1);
114
7deb8eb0 115 // timer start...
116 TStopwatch timer;
117
118 // Loop over ESD events
119 if (nevents > 0) nevents = TMath::Min(nevents,(Int_t)esdTree->GetEntries());
120 else nevents = (Int_t)esdTree->GetEntries();
121 for (Int_t iEvent = 0; iEvent < nevents; iEvent++) {
122 if (printLevel>0) cout<<endl<<" ****************event #"<<iEvent+1<<"****************"<<endl;
123
124 //----------------------------------------------//
125 // -------------- process event --------------- //
126 //----------------------------------------------//
127 // get the ESD of current event
128 esdTree->GetEvent(iEvent);
129 if (!esd) {
17e52d9f 130 Error("MUONRefit", "no ESD object found for event %d", iEvent);
7deb8eb0 131 return;
132 }
133 Int_t nTracks = (Int_t)esd->GetNumberOfMuonTracks();
134 if (nTracks < 1) continue;
135
136 // load the current event
a99c3449 137 esdInterface.LoadEvent(*esd, kFALSE);
7deb8eb0 138
fe0324de 139 // remove digits and clusters from ESD
140 esd->FindListObject("MuonClusters")->Clear("C");
141 esd->FindListObject("MuonPads")->Clear("C");
142
143 // loop over digits to modify their charge
7deb8eb0 144 AliMUONVDigit *digit;
145 TIter next(esdInterface.CreateDigitIterator());
b1fea02e 146 while ((digit = static_cast<AliMUONVDigit*>(next()))) {
147 digit->SetCharge(digit->ADC());
148 digit->Calibrated(kFALSE);
149 }
7deb8eb0 150
151 // refit the tracks from digits
fe0324de 152 refitter.SetFirstClusterIndex(0);
7deb8eb0 153 AliMUONVTrackStore* newTrackStore = refitter.ReconstructFromDigits();
154
155 //----------------------------------------------//
156 // ------ fill new ESD and print results ------ //
157 //----------------------------------------------//
158 // loop over the list of ESD tracks
159 TClonesArray *esdTracks = (TClonesArray*) esd->FindListObject("MuonTracks");
160 for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
161
162 // get the ESD track
163 AliESDMuonTrack* esdTrack = (AliESDMuonTrack*) esdTracks->UncheckedAt(iTrack);
b1fea02e 164
165 // skip ghost tracks (leave them unchanged in the new ESD file)
166 if (!esdTrack->ContainTrackerData()) continue;
167
7deb8eb0 168 // get the corresponding MUON track
630711ed 169 AliMUONTrack* track = esdInterface.FindTrack(esdTrack->GetUniqueID());
b1fea02e 170
7deb8eb0 171 // Find the corresponding re-fitted MUON track
172 AliMUONTrack* newTrack = (AliMUONTrack*) newTrackStore->FindObject(esdTrack->GetUniqueID());
173
174 // replace the content of the current ESD track or remove it if the refitting has failed
fe0324de 175 if (newTrack && (!recoParam->ImproveTracks() || newTrack->IsImproved())) {
176
177 // fill the track info
7deb8eb0 178 Double_t vertex[3] = {esdTrack->GetNonBendingCoor(), esdTrack->GetBendingCoor(), esdTrack->GetZ()};
fe0324de 179 AliMUONESDInterface::MUONToESD(*newTrack, *esdTrack, vertex);
180
181 // add the clusters (and the digits if previously there)
182 for (Int_t i = 0; i < newTrack->GetNClusters(); i++) {
183 AliMUONTrackParam *param = static_cast<AliMUONTrackParam*>(newTrack->GetTrackParamAtCluster()->UncheckedAt(i));
184 AliMUONESDInterface::MUONToESD(*(param->GetClusterPtr()), *esd, esdInterface.GetDigits());
185 }
186
187 } else esdTracks->Remove(esdTrack);
7deb8eb0 188
189 // print initial and re-fitted track parameters at first cluster if any
190 if (printLevel>0) {
191 cout<<" ----------------track #"<<iTrack+1<<"----------------"<<endl;
192 cout<<"before refit:"<<endl;
193 AliMUONTrackParam *param = (AliMUONTrackParam*) track->GetTrackParamAtCluster()->First();
194 param->Print("FULL");
195 if (printLevel>1) param->GetCovariances().Print();
196 if (!newTrack) continue;
197 cout<<"after refit:"<<endl;
198 param = (AliMUONTrackParam*) newTrack->GetTrackParamAtCluster()->First();
199 param->Print("FULL");
200 if (printLevel>1) param->GetCovariances().Print();
201 cout<<" ----------------------------------------"<<endl;
202 }
203
204 }
205
206 // free memory
207 delete newTrackStore;
208
209 // fill new ESD tree with new tracks
210 esdTracks->Compress();
211 newESDTree->Fill();
212
213 if (printLevel>0) cout<<" ****************************************"<<endl;
214 }
215
216 // ...timer stop
217 timer.Stop();
218
219 // write output ESD tree
ad3c6eda 220 newESDFile->cd();
7deb8eb0 221 newESDTree->Write();
ad3c6eda 222 delete newESDTree;
7deb8eb0 223 newESDFile->Close();
224
225 // free memory
226 esdFile->Close();
7deb8eb0 227 delete esd;
a99c3449 228 delete recoParam;
7deb8eb0 229
230 cout<<endl<<"time to refit: R:"<<timer.RealTime()<<" C:"<<timer.CpuTime()<<endl<<endl;
231}
232
233//-----------------------------------------------------------------------
7deb8eb0 234TTree* GetESDTree(TFile *esdFile)
235{
236 /// Check that the file is properly open
237 /// Return pointer to the ESD Tree
238
239 if (!esdFile || !esdFile->IsOpen()) {
240 Error("GetESDTree", "opening ESD file failed");
241 exit(-1);
242 }
243
244 TTree* tree = (TTree*) esdFile->Get("esdTree");
245 if (!tree) {
246 Error("GetESDTree", "no ESD tree found");
247 exit(-1);
248 }
249
250 return tree;
251
252}
253