implemented destructors
[u/mrichter/AliRoot.git] / PWG0 / dNdEta / AlidNdEtaAnalysisESDSelector.cxx
CommitLineData
dc740de4 1/* $Id$ */
2
3#include "AlidNdEtaAnalysisESDSelector.h"
4
5#include <TStyle.h>
6#include <TSystem.h>
7#include <TCanvas.h>
8#include <TVector3.h>
7029240a 9#include <TChain.h>
16e24ca3 10#include <TFile.h>
dc740de4 11
12#include <AliLog.h>
fcf2fb36 13#include <AliESDVertex.h>
14#include <AliESD.h>
dc740de4 15
16#include "esdTrackCuts/AliESDtrackCuts.h"
16e24ca3 17#include "dNdEta/dNdEtaAnalysis.h"
45e97e28 18#include "dNdEta/AlidNdEtaCorrection.h"
19#include "AliPWG0Helper.h"
dc740de4 20
21ClassImp(AlidNdEtaAnalysisESDSelector)
22
23AlidNdEtaAnalysisESDSelector::AlidNdEtaAnalysisESDSelector() :
16e24ca3 24 AliSelector(),
1afae8ff 25 fdNdEtaAnalysisMBVtx(0),
26 fdNdEtaAnalysisMB(0),
27 fdNdEtaAnalysis(0),
45e97e28 28 fEsdTrackCuts(0),
29 fdNdEtaCorrection(0)
dc740de4 30{
31 //
32 // Constructor. Initialization of pointers
33 //
16e24ca3 34
0ab29cfa 35 AliLog::SetClassDebugLevel("AlidNdEtaAnalysisESDSelector", AliLog::kDebug);
dc740de4 36}
37
38AlidNdEtaAnalysisESDSelector::~AlidNdEtaAnalysisESDSelector()
39{
40 //
41 // Destructor
42 //
43
44 // histograms are in the output list and deleted when the output
45 // list is deleted by the TSelector dtor
38233af1 46
47 if (fdNdEtaAnalysis)
48 {
49 delete fdNdEtaAnalysis;
50 fdNdEtaAnalysis = 0;
51 }
52
53 if (fdNdEtaAnalysisMB)
54 {
55 delete fdNdEtaAnalysisMB;
56 fdNdEtaAnalysisMB = 0;
57 }
58
59 if (fdNdEtaAnalysisMBVtx)
60 {
61 delete fdNdEtaAnalysisMBVtx;
62 fdNdEtaAnalysisMBVtx = 0;
63 }
64
65 if (fEsdTrackCuts)
66 {
67 delete fEsdTrackCuts;
68 fEsdTrackCuts = 0;
69 }
70
71 if (fdNdEtaCorrection)
72 {
73 delete fdNdEtaCorrection;
74 fdNdEtaCorrection = 0;
75 }
dc740de4 76}
77
0ab29cfa 78void AlidNdEtaAnalysisESDSelector::Begin(TTree* tree)
dc740de4 79{
0ab29cfa 80 // Begin function
dc740de4 81
0ab29cfa 82 ReadUserObjects(tree);
83}
dc740de4 84
0ab29cfa 85void AlidNdEtaAnalysisESDSelector::ReadUserObjects(TTree* tree)
86{
87 // read the user objects, called from slavebegin and begin
dc740de4 88
16e24ca3 89 if (!fEsdTrackCuts && fInput)
90 fEsdTrackCuts = dynamic_cast<AliESDtrackCuts*> (fInput->FindObject("AliESDtrackCuts"));
91
1afae8ff 92 if (!fEsdTrackCuts && tree)
93 fEsdTrackCuts = dynamic_cast<AliESDtrackCuts*> (tree->GetUserInfo()->FindObject("AliESDtrackCuts"));
94
16e24ca3 95 if (!fEsdTrackCuts)
96 AliDebug(AliLog::kError, "ERROR: Could not read EsdTrackCuts from input list.");
97
1afae8ff 98
99 if (!fdNdEtaCorrection && fInput)
100 fdNdEtaCorrection = dynamic_cast<AlidNdEtaCorrection*> (fInput->FindObject("dndeta_correction"));
101
0ab29cfa 102 if (!fdNdEtaCorrection && tree)
103 fdNdEtaCorrection = dynamic_cast<AlidNdEtaCorrection*> (tree->GetUserInfo()->FindObject("dndeta_correction"));
1afae8ff 104
105 if (!fdNdEtaCorrection)
106 AliDebug(AliLog::kError, "ERROR: Could not read dndeta_correction from input list.");
0ab29cfa 107}
1afae8ff 108
0ab29cfa 109void AlidNdEtaAnalysisESDSelector::SlaveBegin(TTree* tree)
110{
111 // The SlaveBegin() function is called after the Begin() function.
112 // When running with PROOF SlaveBegin() is called on each slave server.
113 // The tree argument is deprecated (on PROOF 0 is passed).
114
115 AliSelector::SlaveBegin(tree);
116
117 ReadUserObjects(tree);
1afae8ff 118
119 fdNdEtaAnalysisMBVtx = new dNdEtaAnalysis("dndeta_mbvtx", "dndeta_mbvtx");
120 fdNdEtaAnalysisMB = new dNdEtaAnalysis("dndeta_mb", "dndeta_mb");
16e24ca3 121 fdNdEtaAnalysis = new dNdEtaAnalysis("dndeta", "dndeta");
122}
123
124void AlidNdEtaAnalysisESDSelector::Init(TTree* tree)
125{
126 // read the user objects
127
128 AliSelector::Init(tree);
dc740de4 129}
130
131Bool_t AlidNdEtaAnalysisESDSelector::Process(Long64_t entry)
132{
133 // The Process() function is called for each entry in the tree (or possibly
134 // keyed object in the case of PROOF) to be processed. The entry argument
135 // specifies which entry in the currently loaded tree is to be processed.
136 // It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
137 // to read either all or the required parts of the data. When processing
138 // keyed objects with PROOF, the object is already loaded and is available
139 // via the fObject pointer.
140 //
141 // This function should contain the "body" of the analysis. It can contain
142 // simple or elaborate selection criteria, run algorithms on the data
143 // of the event and typically fill histograms.
144
145 // WARNING when a selector is used with a TChain, you must use
146 // the pointer to the current TTree to call GetEntry(entry).
147 // The entry is always the local entry number in the current tree.
16e24ca3 148 // Assuming that fTree is the pointer to the TChain being processed,
149 // use fTree->GetTree()->GetEntry(entry).
dc740de4 150
16e24ca3 151 if (AliSelector::Process(entry) == kFALSE)
dc740de4 152 return kFALSE;
153
154 // Check prerequisites
155 if (!fESD)
156 {
157 AliDebug(AliLog::kError, "ESD branch not available");
158 return kFALSE;
159 }
160
161 if (!fEsdTrackCuts)
162 {
163 AliDebug(AliLog::kError, "fESDTrackCuts not available");
164 return kFALSE;
165 }
166
45e97e28 167 if (!fdNdEtaCorrection)
168 {
169 AliDebug(AliLog::kError, "fdNdEtaCorrection not available");
170 return kFALSE;
171 }
dc740de4 172
847489f7 173 if (AliPWG0Helper::IsEventTriggered(fESD) == kFALSE)
174 return kTRUE;
175
45e97e28 176 if (AliPWG0Helper::IsVertexReconstructed(fESD) == kFALSE)
dc740de4 177 return kTRUE;
178
45e97e28 179 // ########################################################
180 // get the EDS vertex
181 const AliESDVertex* vtxESD = fESD->GetVertex();
dc740de4 182 Double_t vtx[3];
183 vtxESD->GetXYZ(vtx);
184
45e97e28 185 // get number of "good" tracks
186 TObjArray* list = fEsdTrackCuts->GetAcceptedTracks(fESD);
187 Int_t nGoodTracks = list->GetEntries();
188
189 Float_t vertexRecoCorr = fdNdEtaCorrection->GetVertexRecoCorrection(vtx[2], nGoodTracks);
847489f7 190 if (vertexRecoCorr <= 0)
191 {
192 AliDebug(AliLog::kError, Form("INFO: Skipping event because vertexRecoCorr is <= 0 (%f)", vertexRecoCorr));
193 delete list;
194 return kTRUE;
195 }
45e97e28 196
1afae8ff 197 Float_t triggerCorr = fdNdEtaCorrection->GetTriggerCorrection(vtx[2], nGoodTracks);
198 if (triggerCorr <= 0)
199 {
200 AliDebug(AliLog::kError, Form("INFO: Skipping event because triggerCorr is <= 0 (%f)", triggerCorr));
201 delete list;
202 return kTRUE;
203 }
204
dc740de4 205 // loop over esd tracks
45e97e28 206 for (Int_t t=0; t<nGoodTracks; t++)
dc740de4 207 {
45e97e28 208 AliESDtrack* esdTrack = dynamic_cast<AliESDtrack*> (list->At(t));
dc740de4 209 if (!esdTrack)
210 {
211 AliDebug(AliLog::kError, Form("ERROR: Could not retrieve track %d.", t));
212 continue;
213 }
214
dc740de4 215 Double_t p[3];
38233af1 216 esdTrack->GetConstrainedPxPyPz(p); // ### TODO should be okay because we have a vertex, however GetInnerPxPyPy / GetOuterPxPyPy also exist
dc740de4 217 TVector3 vector(p);
218
219 Float_t theta = vector.Theta();
220 Float_t eta = -TMath::Log(TMath::Tan(theta/2.));
45e97e28 221 Float_t pt = vector.Pt();
222
45e97e28 223 Float_t track2particleCorr = fdNdEtaCorrection->GetTrack2ParticleCorrection(vtx[2], eta, pt);
224
1afae8ff 225 Float_t weight = track2particleCorr * vertexRecoCorr * triggerCorr;
847489f7 226 if (weight <= 0)
227 {
1afae8ff 228 AliDebug(AliLog::kError, Form("INFO: Skipping track because weight is <= 0 (track %d, weight %f) (vtx %f, eta %f, pt %f)", t, weight, vtx[2], eta, pt));
847489f7 229 continue;
230 }
dc740de4 231
1afae8ff 232 fdNdEtaAnalysisMBVtx->FillTrack(vtx[2], eta, pt, track2particleCorr);
233 fdNdEtaAnalysisMB->FillTrack(vtx[2], eta, pt, track2particleCorr * vertexRecoCorr);
847489f7 234 fdNdEtaAnalysis->FillTrack(vtx[2], eta, pt, weight);
dc740de4 235 } // end of track loop
236
45e97e28 237 delete list;
238 list = 0;
239
dc740de4 240 // for event count per vertex
1afae8ff 241 fdNdEtaAnalysisMBVtx->FillEvent(vtx[2], 1);
242 fdNdEtaAnalysisMB->FillEvent(vtx[2], vertexRecoCorr);
243 fdNdEtaAnalysis->FillEvent(vtx[2], vertexRecoCorr * triggerCorr);
dc740de4 244
245 return kTRUE;
246}
247
16e24ca3 248void AlidNdEtaAnalysisESDSelector::SlaveTerminate()
249{
250 // The SlaveTerminate() function is called after all entries or objects
251 // have been processed. When running with PROOF SlaveTerminate() is called
252 // on each slave server.
253
254 AliSelector::SlaveTerminate();
255
256 // Add the histograms to the output on each slave server
257 if (!fOutput)
258 {
259 AliDebug(AliLog::kError, Form("ERROR: Output list not initialized."));
260 return;
261 }
262
38233af1 263 // Add the objects to the output list and set them to 0, so that the destructor does not delete them.
264
16e24ca3 265 fOutput->Add(fdNdEtaAnalysis);
38233af1 266 fdNdEtaAnalysis = 0;
267
0ab29cfa 268 fOutput->Add(fdNdEtaAnalysisMB);
38233af1 269 fdNdEtaAnalysisMB = 0;
270
0ab29cfa 271 fOutput->Add(fdNdEtaAnalysisMBVtx);
38233af1 272 fdNdEtaAnalysisMBVtx = 0;
16e24ca3 273}
274
275void AlidNdEtaAnalysisESDSelector::Terminate()
dc740de4 276{
16e24ca3 277 // The Terminate() function is the last function to be called during
278 // a query. It always runs on the client, it can be used to present
279 // the results graphically or save the results to file.
280
281 AliSelector::Terminate();
282
283 fdNdEtaAnalysis = dynamic_cast<dNdEtaAnalysis*> (fOutput->FindObject("dndeta"));
0ab29cfa 284 fdNdEtaAnalysisMB = dynamic_cast<dNdEtaAnalysis*> (fOutput->FindObject("dndeta_mb"));
285 fdNdEtaAnalysisMBVtx = dynamic_cast<dNdEtaAnalysis*> (fOutput->FindObject("dndeta_mbvtx"));
16e24ca3 286
0ab29cfa 287 if (!fdNdEtaAnalysis || !fdNdEtaAnalysisMB || !fdNdEtaAnalysisMBVtx)
16e24ca3 288 {
0ab29cfa 289 AliDebug(AliLog::kError, Form("ERROR: Histograms not available %p %p %p", (void*) fdNdEtaAnalysis, (void*) fdNdEtaAnalysisMB, (void*) fdNdEtaAnalysisMBVtx));
16e24ca3 290 return;
291 }
292
0ab29cfa 293 if (fdNdEtaAnalysis)
294 fdNdEtaAnalysis->Finish(fdNdEtaCorrection, 0.3);
295
296 if (fdNdEtaAnalysisMB)
297 fdNdEtaAnalysisMB->Finish(fdNdEtaCorrection, 0.3);
298
299 if (fdNdEtaAnalysisMBVtx)
300 fdNdEtaAnalysisMBVtx->Finish(fdNdEtaCorrection, 0.3);
d09fb536 301
16e24ca3 302 TFile* fout = new TFile("analysis_esd.root","RECREATE");
303
304 if (fdNdEtaAnalysis)
305 fdNdEtaAnalysis->SaveHistograms();
dc740de4 306
1afae8ff 307 if (fdNdEtaAnalysisMB)
308 fdNdEtaAnalysisMB->SaveHistograms();
309
310 if (fdNdEtaAnalysisMBVtx)
311 fdNdEtaAnalysisMBVtx->SaveHistograms();
312
dc740de4 313 if (fEsdTrackCuts)
314 fEsdTrackCuts->SaveHistograms("esd_tracks_cuts");
16e24ca3 315
316 fout->Write();
317 fout->Close();
dc740de4 318}