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