]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONefficiency.C
macor -shell script for recursive merging
[u/mrichter/AliRoot.git] / MUON / MUONefficiency.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 MUONefficiency.C
20 /// \brief add brief description
21 ///
22 /// Macro (upgraded version of MUONmassPlot_ESD.C, better handling of Jpsi) to make : 
23 /// - Ntuple (Ktuple) containing Upsilon kinematics variables (from kinematics.root files) 
24 /// - Ntuple (ESDtuple) containing Upsilon kinematics variables from reconstruction and 
25 ///   combinations of 2 muons with opposite charges (ESDtupleBck will be used later)
26 /// - Some QA histograms
27 /// Ntuple are stored in the file MUONefficiency.root and  ESD tree and QA histograms in AliESDs.root
28 ///
29 /// \author Christophe Suire, IPN Orsay
30
31
32
33 #if !defined(__CINT__) || defined(__MAKECINT__)
34
35 // MUON includes
36 #include "AliMUONCDB.h"
37 #include "AliMUONTrackParam.h"
38 #include "AliMUONTrackExtrap.h"
39 #include "AliMUONESDInterface.h"
40
41 // STEER includes
42 #include "AliRun.h"
43 #include "AliRunLoader.h"
44 #include "AliHeader.h"
45 #include "AliLoader.h"
46 #include "AliStack.h"
47 #include "AliESDEvent.h"
48 #include "AliESDVertex.h"
49 #include "AliCDBManager.h"
50 #include "AliESDMuonTrack.h"
51
52 // ROOT includes
53 #include "TTree.h"
54 #include "TNtuple.h"
55 #include "TLorentzVector.h"
56 #include "TFile.h"
57 #include "TH1.h"
58 #include "TH2.h"
59 #include "TParticle.h"
60 #include "TString.h"
61 #include <Riostream.h>
62 #include <TGeoManager.h>
63 #include <TROOT.h>
64 #include <TF1.h>
65 #include <TMath.h>
66
67 #endif
68
69 Bool_t MUONefficiency(char* filename = "generated/galice.root", char* esdFileName = "AliESDs.root",
70                       char* geoFilename = "geometry.root", char* ocdbPath = "local://$ALICE_ROOT/OCDB",
71                       Int_t ExtrapToVertex = -1, Int_t ResType = 553, Int_t FirstEvent = 0, Int_t LastEvent = 1000000 )
72
73 /// \param ExtrapToVertex (default -1) 
74 ///  -  <0: no extrapolation;
75 ///  -  =0: extrapolation to (0,0,0);
76 ///  -  >0: extrapolation to ESDVertex if available, else to (0,0,0)
77 /// \param ResType    553 for Upsilon, 443 for J/Psi (default 553)  
78 /// \param FirstEvent (default 0)
79 /// \param LastEvent  (default 1.e6)
80 /// \param Chi2Cut    to keep only tracks with chi2 per d.o.f. < Chi2Cut (default 100)  
81
82
83   // MUONefficiency starts
84
85   Double_t MUON_MASS = 0.105658369;
86   Double_t UPSILON_MASS = 9.4603 ;
87   Double_t JPSI_MASS = 3.097;
88
89   // Upper and lower bound for counting entries in the mass peak
90   // +/- 300 MeV/c^2 in this case.
91   Float_t countingRange = 0.300 ;  
92   
93   Float_t massResonance = 5.;
94   Float_t invMassMinInPeak = 0. ; 
95   Float_t invMassMaxInPeak = 0. ; 
96   
97   Float_t nBinsPerGev = 40 ; 
98   Float_t invMassMin = 0;   Float_t invMassMax = 20; 
99   Float_t ptMinResonance = 0 ; Float_t ptMaxResonance = 20 ; Int_t ptBinsResonance = 100; 
100
101   if (ResType==443) {
102     massResonance = JPSI_MASS ;
103     invMassMinInPeak =  JPSI_MASS - countingRange  ; invMassMaxInPeak = JPSI_MASS + countingRange ; 
104     //limits for histograms
105     invMassMin = 0 ; invMassMax = 6.;
106     ptMinResonance = 0 ; ptMaxResonance = 20 ; ptBinsResonance = 100; 
107   }
108   if (ResType==553) {
109     massResonance = UPSILON_MASS;
110     invMassMinInPeak = UPSILON_MASS - countingRange ; invMassMaxInPeak = UPSILON_MASS + countingRange; 
111     //limits for histograms 
112     invMassMin = 0 ; invMassMax = 12.;
113     ptMinResonance = 0 ; ptMaxResonance = 20 ; ptBinsResonance = 100; 
114   }
115   
116   // Single Tracks muon cuts
117   Float_t Chi2Cut = 100.;
118   Float_t PtCutMin = 0. ;
119   Float_t PtCutMax = 10000. ; 
120
121
122   // Limits for histograms 
123   Float_t ptMinMuon = 0. ; Float_t ptMaxMuon = 20.; Int_t ptBinsMuon = 100 ;
124   Float_t pMinMuon = 0.  ; Float_t pMaxMuon = 200.; Int_t pBinsMuon = 100 ;
125  
126
127   //Reset ROOT and connect tree file
128   gROOT->Reset();
129   
130   // Printing Level 
131   Int_t PRINTLEVEL = 0 ;
132   
133   //for kinematic, i.e. reference tracks
134   TNtuple *Ktuple = new TNtuple("Ktuple","Kinematics NTuple","ev:npart:id:idmo:idgdmo:p:pt:y:theta:pseudorap:vx:vy:vz");
135
136   //for reconstruction  
137   TH1F *hPtMuon = new TH1F("hPtMuon", "Muon Pt (GeV/c)", ptBinsMuon, ptMinMuon, ptMaxMuon);
138   TH1F *hPtMuonPlus = new TH1F("hPtMuonPlus", "Muon+ Pt (GeV/c)", ptBinsMuon, ptMinMuon, ptMaxMuon);
139   TH1F *hPtMuonMinus = new TH1F("hPtMuonMinus", "Muon- Pt (GeV/c)", ptBinsMuon, ptMinMuon, ptMaxMuon);
140   TH1F *hPMuon = new TH1F("hPMuon", "Muon P (GeV/c)", pBinsMuon, pMinMuon, pMaxMuon);
141   
142   TH1F *hInvMassAll;
143   TH1F *hInvMassBg;
144   TH2F *hInvMassAll_vs_Pt;
145   TH2F *hInvMassBgk_vs_Pt;
146   TH1F *hInvMassRes;
147
148
149   hInvMassAll = new TH1F("hInvMassAll", "Mu+Mu- invariant mass (GeV/c2)", (Int_t) (nBinsPerGev*(invMassMax - invMassMin)), invMassMin, invMassMax);
150   hInvMassBg = new TH1F("hInvMassBg", "Mu+Mu- invariant mass BG(GeV/c2)", (Int_t) (nBinsPerGev*(invMassMax- invMassMin)), invMassMin, invMassMax);
151   hInvMassAll_vs_Pt = new TH2F("hInvMassAll_vs_Pt","hInvMassAll_vs_Pt",(Int_t) (nBinsPerGev*(invMassMax- invMassMin)), invMassMin, invMassMax,ptBinsResonance,ptMinResonance,ptMaxResonance);
152   hInvMassBgk_vs_Pt = new TH2F("hInvMassBgk_vs_Pt","hInvMassBgk_vs_Pt",(Int_t) (nBinsPerGev*(invMassMax- invMassMin)), invMassMin, invMassMax,ptBinsResonance,ptMinResonance,ptMaxResonance);
153   
154   hInvMassRes = new TH1F("hInvMassRes", "Mu+Mu- invariant mass (GeV/c2) around Resonance",(Int_t) (nBinsPerGev*3*countingRange*2),massResonance-3*countingRange,massResonance+3*countingRange);
155
156   TH1F *hPrimaryVertex = new TH1F("hPrimaryVertex","SPD reconstructed Z vertex",150,-15,15);
157   TH1F *hChi2PerDof = new TH1F("hChi2PerDof", "Muon track chi2/d.o.f.", 100, 0., 20.);
158   TH1F *hNumberOfTrack = new TH1F("hNumberOfTrack","nb of track /evt ",20,-0.5,19.5);
159   TH1F *hRapMuon = new TH1F("hRapMuon"," Muon Rapidity",50,-4.5,-2);
160   TH1F *hRapResonance = new TH1F("hRapResonance"," Resonance Rapidity",50,-4.5,-2);
161   TH1F *hPtResonance = new TH1F("hPtResonance", "Resonance Pt (GeV/c)", 100, 0., 20.);
162   TH2F *hThetaPhiPlus = new TH2F("hThetaPhiPlus", "Theta vs Phi +", 760, -190., 190., 400, 160., 180.);
163   TH2F *hThetaPhiMinus = new TH2F("hThetaPhiMinus", "Theta vs Phi -", 760, -190., 190., 400, 160., 180.);
164   
165   TNtuple *ESDtuple = new TNtuple("ESDtuple","Reconstructed Mu+Mu- pairs and Upsilon","ev:tw:pt:y:theta:minv:pt1:y1:theta1:q1:trig1:pt2:y2:theta2:q2:trig2");
166   TNtuple *ESDtupleBck = new TNtuple("ESDtupleBck","Reconstructed Mu+Mu- pairs for Background","ev:pt:y:theta:minv:pt1:y1:theta1:pt2:y2:theta2");
167
168
169   // Variables
170   Int_t EventInMass = 0;
171   Int_t EventInMassMatch = 0;
172   Int_t NbTrigger = 0;
173   Int_t ptTrig = 0;
174
175   Double_t fXVertex=0;
176   Double_t fYVertex=0;
177   Double_t fZVertex=0;
178   Double_t errXVtx=0;
179   Double_t errYVtx=0;
180
181   Double_t fPxRec1, fPyRec1, fPzRec1, fE1;
182   Double_t fPxRec2, fPyRec2, fPzRec2, fE2;
183   Int_t fCharge1, fCharge2;
184
185   Int_t ntrackhits, nevents; 
186   Int_t nprocessedevents = 0 ;
187   Double_t fitfmin;
188
189   TLorentzVector fV1, fV2, fVtot;
190
191   // Import TGeo geometry (needed by AliMUONTrackExtrap::ExtrapToVertex)
192   if (!gGeoManager) {
193     TGeoManager::Import(geoFilename);
194     if (!gGeoManager) {
195       Error("MUONefficiency", "getting geometry from file %s failed", geoFilename);
196       return kFALSE;
197     }
198   }
199   
200   // open the ESD file
201   TFile* esdFile = TFile::Open(esdFileName);
202   if (!esdFile || !esdFile->IsOpen()) {
203     Error("MUONefficiency", "opening ESD file %s failed", esdFileName);
204     return kFALSE;
205   }
206
207   AliESDEvent* esd = new AliESDEvent();
208   TTree* tree = (TTree*) esdFile->Get("esdTree");
209   if (!tree) {
210     Error("MUONefficiency", "no ESD tree found");
211     return kFALSE;
212   } 
213   esd->ReadFromTree(tree);
214
215   // get run number
216   if (tree->GetEvent(0) <= 0) {
217     Error("MUONefficiency", "no ESD object found for event 0");
218     return kFALSE;
219   }
220   Int_t runNumber = esd->GetRunNumber();
221
222   // load necessary data from OCDB
223   AliCDBManager::Instance()->SetDefaultStorage(ocdbPath);
224   AliCDBManager::Instance()->SetSpecificStorage("GRP/GRP/Data",
225                           Form("local://%s",gSystem->pwd()));
226   AliCDBManager::Instance()->SetRun(runNumber);
227   if (!AliMUONCDB::LoadField()) return kFALSE;
228
229   // set the magnetic field for track extrapolations
230   AliMUONTrackExtrap::SetField();
231
232   // open run loader and load gAlice, kinematics and header
233   AliRunLoader* runLoader = AliRunLoader::Open(filename);
234   if (!runLoader) {
235     Error("MUONefficiency", "getting run loader from file %s failed", filename);
236     return kFALSE;
237   }
238
239   runLoader->LoadgAlice();
240   gAlice = runLoader->GetAliRun();
241   if (!gAlice) {
242     Error("MUONefficiency", "no galice object found");
243     return kFALSE;
244   }
245
246   runLoader->LoadHeader();
247   if (runNumber != runLoader->GetHeader()->GetRun()) {
248     Error("MUONefficiency", "mismatch between run number from ESD and from runLoader");
249     return kFALSE;
250   }
251
252   nevents = runLoader->GetNumberOfEvents();
253   AliMUONTrackParam trackParam;
254
255   // to access the particle  Stack
256   runLoader->LoadKinematics("READ");
257
258   Int_t numberOfGeneratedResonances = 0 ;
259
260   TParticle *particle; 
261   
262   Int_t track1Trigger = 0 ;
263   Float_t track1TriggerChi2 = 0 ;
264   Int_t track2Trigger = 0 ;
265   Float_t track2TriggerChi2 = 0 ;
266
267
268   // Loop over events
269   for (Int_t iEvent = FirstEvent; iEvent <= TMath::Min(LastEvent, nevents - 1); iEvent++) { // Start event loop
270
271     if (iEvent%1000 == 0 )
272       printf("\n Nb of events analysed: %d \n",iEvent);
273
274     // get current event
275     runLoader->GetEvent(iEvent);
276     nprocessedevents++;
277
278     // get the stack and fill the kine tree
279     AliStack *theStack = runLoader->Stack();
280     if (PRINTLEVEL > 0) theStack->DumpPStack ();    
281     
282     Int_t nparticles = (Int_t)runLoader->TreeK()->GetEntries();
283     Int_t nprimarypart = theStack->GetNprimary();
284     Int_t ntracks = theStack->GetNtrack();
285   
286     if (PRINTLEVEL || (iEvent%100==0)) printf("\n  >>> Event %d \n",iEvent);
287     if (PRINTLEVEL) cout << nprimarypart << " Particles generated (total is " << ntracks << ")"<< endl ;    
288     
289     for(Int_t iparticle=0; iparticle<nparticles; iparticle++) { // Start loop over particles
290       particle = theStack->Particle(iparticle);
291       
292       Int_t   muId = particle->GetPdgCode(); 
293       Int_t   muM  = particle->GetFirstMother();
294       Int_t   muGM = 0;
295       Float_t muP  = particle->P();
296       Float_t muPt = TMath::Sqrt(particle->Px()*particle->Px()+particle->Py()*particle->Py());
297       Float_t muY  = 0.5*TMath::Log((particle->Energy()+particle->Pz()+1.e-13)/(particle->Energy()-particle->Pz()+1.e-13));
298       if (muM >= 0) {
299         TParticle *theMum = theStack->Particle(muM);
300         muM  =  theMum->GetPdgCode();
301         muGM  = theMum->GetFirstMother() ;
302         if (muGM >= 0){
303           TParticle *grandMa = theStack->Particle(muGM);
304           muGM = grandMa->GetPdgCode();
305         }
306         else muGM=0;
307       }
308       else muM=0;
309     
310       if (muId==ResType) numberOfGeneratedResonances++;
311
312   
313       Float_t muT  = particle->Theta()*180/TMath::Pi();
314       Float_t muE  = particle->Eta();
315       
316       Float_t muVx = particle->Vx();
317       Float_t muVy = particle->Vy();
318       Float_t muVz = particle->Vz();
319       
320       // If a write error occurs, the number of bytes returned is -1.
321       // If no data are written, because e.g. the branch is disabled,
322       // the number of bytes returned is 0.
323       Int_t errCode = Ktuple->Fill(iEvent,nparticles,muId,muM,muGM,muP,muPt,muY,muT,muE,muVx,muVy,muVz);
324       if (PRINTLEVEL || errCode < 1) printf("iEvent %d,nparticles %d,muId %d,muM %d,muGM %d,muP %.2f,muPt  %.2f,muY  %.2f,muT  %.2f,muE  %.2f,muVx  %.2f,muVy  %.2f,muVz  %.2f \n", iEvent,nparticles,muId,muM,muGM,muP,muPt,muY,muT,muE,muVx,muVy,muVz);
325
326     } // End loop over particles
327     
328
329     
330     // get the event summary data
331     if (tree->GetEvent(iEvent) <= 0) {
332       Error("CheckESD", "no ESD object found for event %d", iEvent);
333       return kFALSE;
334     }
335     
336     // get the SPD reconstructed vertex (vertexer) and fill the histogram
337     AliESDVertex* Vertex = (AliESDVertex*) esd->GetVertex();
338     if (Vertex->GetNContributors()) {
339       fZVertex = Vertex->GetZv();
340       fYVertex = Vertex->GetYv();
341       fXVertex = Vertex->GetXv();      
342       errXVtx = Vertex->GetXRes();
343       errYVtx = Vertex->GetYRes();
344     }
345     hPrimaryVertex->Fill(fZVertex);
346     
347     Int_t triggerWord = esd->GetTriggerMask();
348     Int_t nTracks = (Int_t)esd->GetNumberOfMuonTracks() ; 
349
350     if (PRINTLEVEL > 0){
351       printf("\n Nb of events analysed: %d \n",iEvent);
352       cout << " number of tracks: " << nTracks  <<endl;
353     }
354
355     // loop over all reconstructed tracks (also first track of combination)
356     for (Int_t iTrack = 0; iTrack <  nTracks;  iTrack++) {
357
358       // skip ghosts
359       if (!esd->GetMuonTrack(iTrack)->ContainTrackerData()) continue;
360
361       AliESDMuonTrack* muonTrack = new AliESDMuonTrack(*(esd->GetMuonTrack(iTrack)));
362
363       // extrapolate to vertex if required and available
364       if (ExtrapToVertex > 0 && Vertex->GetNContributors()) {
365         AliMUONESDInterface::GetParamAtFirstCluster(*muonTrack, trackParam);
366         AliMUONTrackExtrap::ExtrapToVertex(&trackParam, fXVertex, fYVertex, fZVertex, errXVtx, errYVtx);
367         AliMUONESDInterface::SetParamAtVertex(trackParam, *muonTrack); // put the new parameters in this copy of AliESDMuonTrack
368       } else if ((ExtrapToVertex > 0 && !Vertex->GetNContributors()) || ExtrapToVertex == 0){
369         AliMUONESDInterface::GetParamAtFirstCluster(*muonTrack, trackParam);
370         AliMUONTrackExtrap::ExtrapToVertex(&trackParam, 0., 0., 0., 0., 0.);
371         AliMUONESDInterface::SetParamAtVertex(trackParam, *muonTrack); // put the new parameters in this copy of AliESDMuonTrack
372       }
373
374       // Trigger
375       if (PRINTLEVEL > 5) cout << "MatchTrigger " << muonTrack->GetMatchTrigger() << " and Chi2 of matching tracks " << track1TriggerChi2 <<   endl ;
376       track1Trigger = muonTrack->GetMatchTrigger();
377       if (track1Trigger)
378         track1TriggerChi2 = muonTrack->GetChi2MatchTrigger();
379       else 
380         track1TriggerChi2 = 0. ;
381
382       fCharge1 = Int_t(TMath::Sign(1.,muonTrack->GetInverseBendingMomentum()));
383       
384       muonTrack->LorentzP(fV1);
385
386       ntrackhits = muonTrack->GetNHit();
387       fitfmin    = muonTrack->GetChi2();
388
389       // transverse momentum
390       Float_t pt1 = fV1.Pt();
391       
392       // total momentum
393       Float_t p1 = fV1.P();
394       
395       // Rapidity
396       Float_t rapMuon1 = fV1.Rapidity();
397       
398       // chi2 per d.o.f.
399       
400       Float_t ch1 =  fitfmin / (2.0 * ntrackhits - 5);
401       if (PRINTLEVEL > 5 ) printf(" px %f py %f pz %f pt %f NHits %d  Norm.chi2 %f charge %d\n",fV1.Px(), fV1.Py(), fV1.Pz(), pt1, ntrackhits, ch1, fCharge1);
402       
403       
404       if ((ch1 < Chi2Cut) && (pt1 > PtCutMin) && (pt1 < PtCutMax)) { // condition for good track (Chi2Cut and PtCut)
405         if (PRINTLEVEL > 8) cout << "inside pt and chi2 cuts " << endl ; 
406         
407         // fill histos hPtMuon and hChi2PerDof
408         hPtMuon->Fill(pt1);
409         hPMuon->Fill(p1);
410         hChi2PerDof->Fill(ch1);
411         hRapMuon->Fill(rapMuon1);
412
413         if (fCharge1 > 0) {
414           hPtMuonPlus->Fill(pt1);
415           hThetaPhiPlus->Fill(fV1.Phi()*180./TMath::Pi(),fV1.Theta()*180./TMath::Pi());
416         } else {
417           hPtMuonMinus->Fill(pt1);
418           hThetaPhiMinus->Fill(fV1.Phi()*180./TMath::Pi(),fV1.Theta()*180./TMath::Pi());
419         }
420
421         // loop over second track of combination
422         for (Int_t iTrack2 = iTrack + 1; iTrack2 < nTracks; iTrack2++) {
423           
424           // skip ghosts
425           if (!esd->GetMuonTrack(iTrack2)->ContainTrackerData()) continue;
426           
427           AliESDMuonTrack* muonTrack2 = new AliESDMuonTrack(*(esd->GetMuonTrack(iTrack2)));
428           
429           // extrapolate to vertex if required and available
430           if (ExtrapToVertex > 0 && Vertex->GetNContributors()) {
431             AliMUONESDInterface::GetParamAtFirstCluster(*muonTrack2, trackParam);
432             AliMUONTrackExtrap::ExtrapToVertex(&trackParam, fXVertex, fYVertex, fZVertex, errXVtx, errYVtx);
433             AliMUONESDInterface::SetParamAtVertex(trackParam, *muonTrack2); // put the new parameters in this copy of AliESDMuonTrack
434           } else if ((ExtrapToVertex > 0 && !Vertex->GetNContributors()) || ExtrapToVertex == 0){
435             AliMUONESDInterface::GetParamAtFirstCluster(*muonTrack2, trackParam);
436             AliMUONTrackExtrap::ExtrapToVertex(&trackParam, 0., 0., 0., 0., 0.);
437             AliMUONESDInterface::SetParamAtVertex(trackParam, *muonTrack2); // put the new parameters in this copy of AliESDMuonTrack
438           }
439
440           track2Trigger = muonTrack2->GetMatchTrigger();
441           if (track2Trigger) 
442             track2TriggerChi2 = muonTrack2->GetChi2MatchTrigger();
443           else 
444             track2TriggerChi2 = 0. ;
445
446           fCharge2 = Int_t(TMath::Sign(1.,muonTrack2->GetInverseBendingMomentum()));
447
448           muonTrack2->LorentzP(fV2);
449
450           ntrackhits = muonTrack2->GetNHit();
451           fitfmin    = muonTrack2->GetChi2();
452
453           // transverse momentum
454           Float_t pt2 = fV2.Pt();
455
456           // chi2 per d.o.f.
457           Float_t ch2 = fitfmin  / (2.0 * ntrackhits - 5);
458
459
460           // condition for good track (Chi2Cut and PtCut)
461           if ((ch2 < Chi2Cut) && (pt2 > PtCutMin)  && (pt2 < PtCutMax)) {
462             
463             // condition for opposite charges
464             if ((fCharge1 * fCharge2) == -1) {
465               
466               if (PRINTLEVEL > 8) cout << "---------> Now filling the Ntuple "  <<  endl ;
467               
468               // invariant mass
469               fVtot = fV1 + fV2;
470               Float_t invMass = fVtot.M();
471               
472               if (fCharge1 < 0){ //mu_minus is index 1 in the ntuple
473                 Float_t ESDFill[16] = {iEvent,triggerWord,fVtot.Pt(),fVtot.Rapidity(),fVtot.Theta()/TMath::Pi()*180,invMass,fV1.Pt(),fV1.Rapidity(),fV1.Theta()/TMath::Pi()*180,fCharge1,track1TriggerChi2,fV2.Pt(),fV2.Rapidity(),fV2.Theta()/TMath::Pi()*180,fCharge2,track2TriggerChi2};
474                 ESDtuple->Fill(ESDFill);
475               }
476               else{
477                 Float_t ESDFill[16] = {iEvent,triggerWord,fVtot.Pt(),fVtot.Rapidity(),fVtot.Theta()/TMath::Pi()*180,invMass,fV2.Pt(),fV2.Rapidity(),fV2.Theta()/TMath::Pi()*180,fCharge2,track2TriggerChi2,fV1.Pt(),fV1.Rapidity(),fV1.Theta()/TMath::Pi()*180,fCharge1,track1TriggerChi2};
478                 ESDtuple->Fill(ESDFill);
479               }
480               
481               // fill histos hInvMassAll and hInvMassRes
482               hInvMassAll->Fill(invMass);
483               hInvMassRes->Fill(invMass);
484               hInvMassAll_vs_Pt->Fill(invMass,fVtot.Pt());
485               
486               //trigger info 
487               if (ResType == 553)
488                 ptTrig = 0x08;// mask for Hpt unlike sign pair
489               else if (ResType == 443)
490                 ptTrig = 0x04;// mask for Lpt unlike sign pair
491               
492               
493               if (esd->GetTriggerMask() &  ptTrig) NbTrigger++;
494               
495               if (invMass > invMassMinInPeak && invMass < invMassMaxInPeak) {
496                 EventInMass++;
497                 hRapResonance->Fill(fVtot.Rapidity());
498                 hPtResonance->Fill(fVtot.Pt());
499                 
500                 // match with trigger
501                 if (muonTrack2->GetMatchTrigger()>=0 && (esd->GetTriggerMask() & ptTrig))  EventInMassMatch++;
502                 
503               }
504               
505             } // if (fCharge1 * fCharge2) == -1)
506           } // if ((ch2 < Chi2Cut) && (pt2 > PtCutMin) && (pt2 < PtCutMax))
507           delete muonTrack2;
508         } //  for (Int_t iTrack2 = iTrack + 1; iTrack2 < iTrack; iTrack2++)
509       } // if (ch1 < Chi2Cut) && (pt1 > PtCutMin)&& (pt1 < PtCutMax) )
510       delete muonTrack;
511     } // for (Int_t iTrack = 0; iTrack < nrectracks; iTrack++)
512     
513     hNumberOfTrack->Fill(nTracks);
514     //    esdFile->Delete();
515   
516   } // End of event loop
517
518
519   // Loop over events for bg event
520
521   Double_t thetaPlus,  phiPlus;
522   Double_t thetaMinus, phiMinus;
523   Float_t PtMinus, PtPlus;
524   
525   for (Int_t iEvent = 0; iEvent < hInvMassAll->Integral(); iEvent++) {  // Loop over events for bg event
526     // according to Christian a 3d phi-theta-pt random pick  would take better care 
527     // of all correlations 
528
529     hThetaPhiPlus->GetRandom2(phiPlus, thetaPlus);
530     hThetaPhiMinus->GetRandom2(phiMinus,thetaMinus);
531     PtPlus = hPtMuonPlus->GetRandom();
532     PtMinus = hPtMuonMinus->GetRandom();
533
534     fPxRec1  = PtPlus * TMath::Cos(TMath::Pi()/180.*phiPlus);
535     fPyRec1  = PtPlus * TMath::Sin(TMath::Pi()/180.*phiPlus);
536     fPzRec1  = PtPlus / TMath::Tan(TMath::Pi()/180.*thetaPlus);
537
538     fE1 = TMath::Sqrt(MUON_MASS * MUON_MASS + fPxRec1 * fPxRec1 + fPyRec1 * fPyRec1 + fPzRec1 * fPzRec1);
539     fV1.SetPxPyPzE(fPxRec1, fPyRec1, fPzRec1, fE1);
540
541     fPxRec2  = PtMinus * TMath::Cos(TMath::Pi()/180.*phiMinus);
542     fPyRec2  = PtMinus * TMath::Sin(TMath::Pi()/180.*phiMinus);
543     fPzRec2  = PtMinus / TMath::Tan(TMath::Pi()/180.*thetaMinus);
544
545     fE2 = TMath::Sqrt(MUON_MASS * MUON_MASS + fPxRec2 * fPxRec2 + fPyRec2 * fPyRec2 + fPzRec2 * fPzRec2);
546     fV2.SetPxPyPzE(fPxRec2, fPyRec2, fPzRec2, fE2);
547
548     // invariant mass
549     fVtot = fV1 + fV2;
550       
551     // fill histos hInvMassAll and hInvMassRes
552     hInvMassBg->Fill(fVtot.M());
553     hInvMassBgk_vs_Pt->Fill( fVtot.M(), fVtot.Pt() );
554
555     // Ntuple for background... more convenient
556     ESDtupleBck->Fill(iEvent,fVtot.Pt(),fVtot.Rapidity(),fVtot.Theta()/TMath::Pi()*180,fVtot.M(),fV2.Pt(),fV2.Rapidity(),fV2.Theta()/TMath::Pi()*180,fV1.Pt(),fV1.Rapidity(),fV1.Theta()/TMath::Pi()*180);
557
558   } // End loop over events for background
559
560
561   // File for histograms and histogram booking
562   TString outfilename = "MUONefficiency.root";
563   TFile *ntupleFile = new TFile(outfilename.Data(), "RECREATE");  
564   
565   Ktuple->Write();
566   ESDtuple->Write();
567   ESDtupleBck->Write();
568
569   ntupleFile->Close();
570   
571   TFile *histoFile = new TFile("MUONhistos.root", "RECREATE");  
572   hPrimaryVertex->Write();
573   hPtMuon->Write();
574   hPtMuonPlus->Write();
575   hPtMuonMinus->Write();
576   hPMuon->Write();
577   hChi2PerDof->Write();
578   hInvMassAll->Write();
579   hInvMassBg->Write();
580   hInvMassAll_vs_Pt ->Write();
581   hInvMassBgk_vs_Pt->Write();
582   hInvMassRes->Write();
583   hNumberOfTrack->Write();
584   hRapMuon ->Write();
585   hRapResonance ->Write();
586   hPtResonance ->Write();
587   hThetaPhiPlus ->Write();
588   hThetaPhiMinus ->Write();
589   histoFile->Close();
590
591   cout << "" << endl ;
592   cout << "*************************************************" << endl;
593  
594   cout << "MUONefficiency : " << nprocessedevents  << " events processed" << endl;
595   if (ResType==443)
596     cout << "Number of generated J/Psi (443)  : " <<  numberOfGeneratedResonances  << endl ;
597   if (ResType==553)
598     cout << "Number of generated Upsilon (553)  :" <<  numberOfGeneratedResonances  << endl ;
599   cout << "Chi2Cut for muon tracks = " << Chi2Cut << endl;
600   cout << "PtCutMin for muon tracks = " << PtCutMin << endl;
601   cout << "PtCutMax for muon tracks = " << PtCutMax << endl;
602   
603   cout << "Entries (unlike sign dimuons) : " << hInvMassAll->GetEntries();
604   
605   if (hInvMassAll->GetEntries() > 0) {
606     hInvMassAll->Fit("gaus","q0");
607     TF1* f1 = hInvMassAll->GetFunction("gaus");
608     cout << Form(". Rough sigma = %7.2f MeV/c2",f1->GetParameter(2)*1000.0);
609   }
610   
611   cout << endl << "Entries (unlike sign dimuons) in the mass range  ["<<invMassMinInPeak<<";"<<invMassMaxInPeak<<"] : " << EventInMass <<endl;
612   
613   if (ptTrig==0x800) cout << "Unlike Pair - All Pt" ;   
614   if (ptTrig==0x400) cout << "Unlike Pair - High Pt" ;   
615   if (ptTrig==0x200) cout << "Unlike Pair - Low Pt" ; 
616   cout << " triggers : " << NbTrigger << endl;
617   
618   cout << "Entries in the mass range with matching between reconstructed tracks and trigger tracks " << EventInMassMatch << endl;
619
620
621   return kTRUE;
622 }