]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONmassPlot_ESD.C
Mods for MacOSX
[u/mrichter/AliRoot.git] / MUON / MUONmassPlot_ESD.C
1 #if !defined(__CINT__) || defined(__MAKECINT__)
2 // ROOT includes
3 #include "TTree.h"
4 #include "TBranch.h"
5 #include "TClonesArray.h"
6 #include "TLorentzVector.h"
7 #include "TFile.h"
8 #include "TH1.h"
9 #include "TH2.h"
10 #include "TParticle.h"
11 #include "TTree.h"
12 #include <Riostream.h>
13
14 // STEER includes
15 #include "AliRun.h"
16 #include "AliRunLoader.h"
17 #include "AliHeader.h"
18 #include "AliLoader.h"
19 #include "AliStack.h"
20 #include "AliMagF.h"
21 #include "AliESD.h"
22
23 // MUON includes
24 #include "AliESDMuonTrack.h"
25 #endif
26 //
27 // Macro MUONmassPlot.C for ESD
28 // Ch. Finck, Subatech, April. 2004
29 //
30
31 // macro to make invariant mass plots
32 // for combinations of 2 muons with opposite charges,
33 // from root file "MUON.tracks.root" containing the result of track reconstruction.
34 // Histograms are stored on the "MUONmassPlot.root" file.
35 // introducing TLorentzVector for parameter calculations (Pt, P,rap,etc...)
36 // using Invariant Mass for rapidity.
37
38 // Arguments:
39 //   FirstEvent (default 0)
40 //   LastEvent (default 0)
41 //   ResType (default 553)
42 //      553 for Upsilon, anything else for J/Psi
43 //   Chi2Cut (default 100)
44 //      to keep only tracks with chi2 per d.o.f. < Chi2Cut
45 //   PtCutMin (default 1)
46 //      to keep only tracks with transverse momentum > PtCutMin
47 //   PtCutMax (default 10000)
48 //      to keep only tracks with transverse momentum < PtCutMax
49 //   massMin (default 9.17 for Upsilon) 
50 //      &  massMax (default 9.77 for Upsilon) 
51 //         to calculate the reconstruction efficiency for resonances with invariant mass
52 //         massMin < mass < massMax.
53
54 // Add parameters and histograms for analysis 
55
56 Bool_t MUONmassPlot(char* filename = "galice.root", Int_t FirstEvent = 0, Int_t LastEvent = 10000,
57                   char* esdFileName = "AliESDs.root", Int_t ResType = 553, 
58                   Float_t Chi2Cut = 100., Float_t PtCutMin = 1., Float_t PtCutMax = 10000.,
59                   Float_t massMin = 9.17,Float_t massMax = 9.77)
60 {
61   cout << "MUONmassPlot " << endl;
62   cout << "FirstEvent " << FirstEvent << endl;
63   cout << "LastEvent " << LastEvent << endl;
64   cout << "ResType " << ResType << endl;
65   cout << "Chi2Cut " << Chi2Cut << endl;
66   cout << "PtCutMin " << PtCutMin << endl;
67   cout << "PtCutMax " << PtCutMax << endl;
68   cout << "massMin " << massMin << endl;
69   cout << "massMax " << massMax << endl;
70
71  
72   //Reset ROOT and connect tree file
73   gROOT->Reset();
74
75
76   // File for histograms and histogram booking
77   TFile *histoFile = new TFile("MUONmassPlot.root", "RECREATE");
78   TH1F *hPtMuon = new TH1F("hPtMuon", "Muon Pt (GeV/c)", 100, 0., 20.);
79   TH1F *hPtMuonPlus = new TH1F("hPtMuonPlus", "Muon+ Pt (GeV/c)", 100, 0., 20.);
80   TH1F *hPtMuonMinus = new TH1F("hPtMuonMinus", "Muon- Pt (GeV/c)", 100, 0., 20.);
81   TH1F *hPMuon = new TH1F("hPMuon", "Muon P (GeV/c)", 100, 0., 200.);
82   TH1F *hChi2PerDof = new TH1F("hChi2PerDof", "Muon track chi2/d.o.f.", 100, 0., 20.);
83   TH1F *hInvMassAll = new TH1F("hInvMassAll", "Mu+Mu- invariant mass (GeV/c2)", 480, 0., 12.);
84   TH1F *hInvMassBg = new TH1F("hInvMassBg", "Mu+Mu- invariant mass BG(GeV/c2)", 480, 0., 12.);
85 TH2F *hInvMassAll_vs_Pt = new TH2F("hInvMassAll_vs_Pt","hInvMassAll_vs_Pt",480,0.,12.,80,0.,20.);
86 TH2F *hInvMassBgk_vs_Pt = new TH2F("hInvMassBgk_vs_Pt","hInvMassBgk_vs_Pt",480,0.,12.,80,0.,20.);
87 TH1F *hInvMassRes;
88
89   if (ResType == 553) {
90     hInvMassRes = new TH1F("hInvMassRes", "Mu+Mu- invariant mass (GeV/c2) around Upsilon", 60, 8., 11.);
91   } else {
92     hInvMassRes = new TH1F("hInvMassRes", "Mu+Mu- invariant mass (GeV/c2) around J/Psi", 80, 0., 5.);
93   }
94
95   TH1F *hNumberOfTrack = new TH1F("hNumberOfTrack","nb of track /evt ",20,-0.5,19.5);
96   TH1F *hRapMuon = new TH1F("hRapMuon"," Muon Rapidity",50,-4.5,-2);
97   TH1F *hRapResonance = new TH1F("hRapResonance"," Resonance Rapidity",50,-4.5,-2);
98   TH1F *hPtResonance = new TH1F("hPtResonance", "Resonance Pt (GeV/c)", 100, 0., 20.);
99   TH2F *hThetaPhiPlus = new TH2F("hThetaPhiPlus", "Theta vs Phi +", 760, -190., 190., 400, 160., 180.);
100   TH2F *hThetaPhiMinus = new TH2F("hThetaPhiMinus", "Theta vs Phi -", 760, -190., 190., 400, 160., 180.);
101
102
103   // settings
104   Int_t EventInMass = 0;
105   Float_t muonMass = 0.105658389;
106 //   Float_t UpsilonMass = 9.46037;
107 //   Float_t JPsiMass = 3.097;
108
109   Double_t thetaX, thetaY, pYZ;
110   Double_t fPxRec1, fPyRec1, fPzRec1, fE1;
111   Double_t fPxRec2, fPyRec2, fPzRec2, fE2;
112   Int_t fCharge, fCharge2;
113
114   Int_t ntrackhits, nevents;
115   Double_t fitfmin;
116
117  
118   TLorentzVector fV1, fV2, fVtot;
119
120   // set off mag field 
121   AliMagF::SetReadField(kFALSE);
122
123   // open run loader and load gAlice, kinematics and header
124   AliRunLoader* runLoader = AliRunLoader::Open(filename);
125   if (!runLoader) {
126     Error("MUONmass_ESD", "getting run loader from file %s failed", 
127             filename);
128     return kFALSE;
129   }
130
131   runLoader->LoadgAlice();
132   gAlice = runLoader->GetAliRun();
133   if (!gAlice) {
134     Error("MUONmass_ESD", "no galice object found");
135     return kFALSE;
136   }
137   
138
139   // open the ESD file
140   TFile* esdFile = TFile::Open(esdFileName);
141   if (!esdFile || !esdFile->IsOpen()) {
142     Error("MUONmass_ESD", "opening ESD file %s failed", esdFileName);
143     return kFALSE;
144   }
145   
146   AliESD* esd = new AliESD();
147   TTree* tree = (TTree*) esdFile->Get("esdTree");
148   if (!tree) {
149     Error("CheckESD", "no ESD tree found");
150     return kFALSE;
151   }
152   tree->SetBranchAddress("ESD", &esd);
153
154   runLoader->LoadHeader();
155   nevents = runLoader->GetNumberOfEvents();
156         
157   // Loop over events
158   for (Int_t iEvent = FirstEvent; iEvent <= TMath::Min(LastEvent, nevents - 1); iEvent++) {
159
160     // get current event
161     runLoader->GetEvent(iEvent);
162    
163     // get the event summary data
164     tree->GetEvent(iEvent);
165     if (!esd) {
166       Error("CheckESD", "no ESD object found for event %d", iEvent);
167       return kFALSE;
168     }
169
170     Int_t nTracks = (Int_t)esd->GetNumberOfMuonTracks() ; 
171
172     //    printf("\n Nb of events analysed: %d\r",iEvent);
173     //      cout << " number of tracks: " << nTracks  <<endl;
174   
175     // loop over all reconstructed tracks (also first track of combination)
176     for (Int_t iTrack = 0; iTrack <  nTracks;  iTrack++) {
177
178       AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack);
179
180       thetaX = muonTrack->GetThetaX();
181       thetaY = muonTrack->GetThetaY();
182
183       pYZ     =  1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
184       fPzRec1  = - pYZ / TMath::Sqrt(1.0 + TMath::Tan(thetaY)*TMath::Tan(thetaY));
185       fPxRec1  = fPzRec1 * TMath::Tan(thetaX);
186       fPyRec1  = fPzRec1 * TMath::Tan(thetaY);
187       fCharge = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
188
189       fE1 = TMath::Sqrt(muonMass * muonMass + fPxRec1 * fPxRec1 + fPyRec1 * fPyRec1 + fPzRec1 * fPzRec1);
190       fV1.SetPxPyPzE(fPxRec1, fPyRec1, fPzRec1, fE1);
191
192       ntrackhits = muonTrack->GetNHit();
193       fitfmin    = muonTrack->GetChi2();
194
195       // transverse momentum
196       Float_t pt1 = fV1.Pt();
197
198       // total momentum
199       Float_t p1 = fV1.P();
200
201       // Rapidity
202       Float_t rapMuon1 = fV1.Rapidity();
203
204       // chi2 per d.o.f.
205       Float_t ch1 =  fitfmin / (2.0 * ntrackhits - 5);
206 //      printf(" px %f py %f pz %f NHits %d  Norm.chi2 %f charge %d\n", 
207 //           fPxRec1, fPyRec1, fPzRec1, ntrackhits, ch1, fCharge);
208
209       // condition for good track (Chi2Cut and PtCut)
210
211       if ((ch1 < Chi2Cut) && (pt1 > PtCutMin) && (pt1 < PtCutMax)) {
212
213         // fill histos hPtMuon and hChi2PerDof
214         hPtMuon->Fill(pt1);
215         hPMuon->Fill(p1);
216         hChi2PerDof->Fill(ch1);
217         hRapMuon->Fill(rapMuon1);
218         if (fCharge > 0) {
219           hPtMuonPlus->Fill(pt1);
220           hThetaPhiPlus->Fill(TMath::ATan2(fPyRec1,fPxRec1)*180./TMath::Pi(),TMath::ATan2(pt1,fPzRec1)*180./3.1415);
221         } else {
222           hPtMuonMinus->Fill(pt1);
223           hThetaPhiMinus->Fill(TMath::ATan2(fPyRec1,fPxRec1)*180./TMath::Pi(),TMath::ATan2(pt1,fPzRec1)*180./3.1415);
224         }
225         // loop over second track of combination
226         for (Int_t iTrack2 = iTrack + 1; iTrack2 < nTracks; iTrack2++) {
227           
228           AliESDMuonTrack* muonTrack = esd->GetMuonTrack(iTrack2);
229
230           thetaX = muonTrack->GetThetaX();
231           thetaY = muonTrack->GetThetaY();
232
233           pYZ     =  1./TMath::Abs(muonTrack->GetInverseBendingMomentum());
234           fPzRec2  = - pYZ / TMath::Sqrt(1.0 + TMath::Tan(thetaY)*TMath::Tan(thetaY));
235           fPxRec2  = fPzRec2 * TMath::Tan(thetaX);
236           fPyRec2  = fPzRec2 * TMath::Tan(thetaY);
237           fCharge2 = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
238
239           fE2 = TMath::Sqrt(muonMass * muonMass + fPxRec2 * fPxRec2 + fPyRec2 * fPyRec2 + fPzRec2 * fPzRec2);
240           fV2.SetPxPyPzE(fPxRec2, fPyRec2, fPzRec2, fE2);
241
242           ntrackhits = muonTrack->GetNHit();
243           fitfmin    = muonTrack->GetChi2();
244
245           // transverse momentum
246           Float_t pt2 = fV2.Pt();
247
248           // chi2 per d.o.f.
249           Float_t ch2 = fitfmin  / (2.0 * ntrackhits - 5);
250
251           // condition for good track (Chi2Cut and PtCut)
252           if ((ch2 < Chi2Cut) && (pt2 > PtCutMin)  && (pt2 < PtCutMax)) {
253
254             // condition for opposite charges
255             if ((fCharge * fCharge2) == -1) {
256
257               // invariant mass
258               fVtot = fV1 + fV2;
259               Float_t invMass = fVtot.M();
260                     
261               // fill histos hInvMassAll and hInvMassRes
262               hInvMassAll->Fill(invMass);
263               hInvMassRes->Fill(invMass);
264               hInvMassAll_vs_Pt->Fill(invMass,fVtot.Pt());
265               if (invMass > massMin && invMass < massMax) {
266                 EventInMass++;
267                 hRapResonance->Fill(fVtot.Rapidity());
268                 hPtResonance->Fill(fVtot.Pt());
269               }
270
271             } // if (fCharge * fCharge2) == -1)
272           } // if ((ch2 < Chi2Cut) && (pt2 > PtCutMin) && (pt2 < PtCutMax))
273         } //  for (Int_t iTrack2 = iTrack + 1; iTrack2 < iTrack; iTrack2++)
274       } // if (ch1 < Chi2Cut) && (pt1 > PtCutMin)&& (pt1 < PtCutMax) )
275     } // for (Int_t iTrack = 0; iTrack < nrectracks; iTrack++)
276
277     hNumberOfTrack->Fill(nTracks);
278     //    esdFile->Delete();
279   } // for (Int_t iEvent = FirstEvent;
280
281 // Loop over events for bg event
282
283   Double_t thetaPlus,  phiPlus;
284   Double_t thetaMinus, phiMinus;
285   Float_t PtMinus, PtPlus;
286   
287   for (Int_t iEvent = 0; iEvent < hInvMassAll->Integral(); iEvent++) {
288
289     hThetaPhiPlus->GetRandom2(phiPlus, thetaPlus);
290     hThetaPhiMinus->GetRandom2(phiMinus,thetaMinus);
291     PtPlus = hPtMuonPlus->GetRandom();
292     PtMinus = hPtMuonMinus->GetRandom();
293
294     fPxRec1  = PtPlus * TMath::Cos(TMath::Pi()/180.*phiPlus);
295     fPyRec1  = PtPlus * TMath::Sin(TMath::Pi()/180.*phiPlus);
296     fPzRec1  = PtPlus / TMath::Tan(TMath::Pi()/180.*thetaPlus);
297
298     fE1 = TMath::Sqrt(muonMass * muonMass + fPxRec1 * fPxRec1 + fPyRec1 * fPyRec1 + fPzRec1 * fPzRec1);
299     fV1.SetPxPyPzE(fPxRec1, fPyRec1, fPzRec1, fE1);
300
301     fPxRec2  = PtMinus * TMath::Cos(TMath::Pi()/180.*phiMinus);
302     fPyRec2  = PtMinus * TMath::Sin(TMath::Pi()/180.*phiMinus);
303     fPzRec2  = PtMinus / TMath::Tan(TMath::Pi()/180.*thetaMinus);
304
305     fE2 = TMath::Sqrt(muonMass * muonMass + fPxRec2 * fPxRec2 + fPyRec2 * fPyRec2 + fPzRec2 * fPzRec2);
306     fV2.SetPxPyPzE(fPxRec2, fPyRec2, fPzRec2, fE2);
307
308     // invariant mass
309     fVtot = fV1 + fV2;
310       
311     // fill histos hInvMassAll and hInvMassRes
312     hInvMassBg->Fill(fVtot.M());
313     hInvMassBgk_vs_Pt->Fill( fVtot.M(), fVtot.Pt() );
314   }
315
316   histoFile->Write();
317   histoFile->Close();
318
319   cout << "MUONmassPlot " << endl;
320   cout << "FirstEvent " << FirstEvent << endl;
321   cout << "LastEvent " << LastEvent << endl;
322   cout << "ResType " << ResType << endl;
323   cout << "Chi2Cut " << Chi2Cut << endl;
324   cout << "PtCutMin " << PtCutMin << endl;
325   cout << "PtCutMax " << PtCutMax << endl;
326   cout << "massMin " << massMin << endl;
327   cout << "massMax " << massMax << endl;
328   cout << "EventInMass " << EventInMass << endl;
329
330   return kTRUE;
331 }
332