Generation of muon filtered AOD files from standard AOD files (Roberta)
[u/mrichter/AliRoot.git] / PWG3 / muon / AliAnalysisTaskMuonAODCreation.cxx
1 //#ifndef ALIANALYSISTASKMUONAODCREATION_CXX
2 //#define ALIANALYSISTASKMUONAODCREATION_CXX
3
4 #include <TChain.h>
5 #include <TTree.h>
6 #include <TList.h>
7 #include <TCanvas.h>
8 #include <TClonesArray.h>
9 #include <TH1.h>
10
11 #include "AliAnalysisTaskMuonAODCreation.h"
12 #include "AliAnalysisTaskSE.h"
13 #include "AliAnalysisManager.h"
14 #include "AliAnalysisDataSlot.h"
15 #include "AliESDEvent.h"
16 #include "AliESD.h"
17 #include "AliAODEvent.h"
18 #include "AliAODHeader.h"
19 #include "AliVEvent.h"
20 #include "AliAODHandler.h"
21 #include "AliAODInputHandler.h"
22 #include "AliMCEventHandler.h"
23 #include "AliInputEventHandler.h"
24 #include "AliMCEvent.h"
25 #include "AliStack.h"
26 #include "AliLog.h"
27
28 ClassImp(AliAnalysisTaskMuonAODCreation)
29
30 //__________________________________________________________________________
31 AliAnalysisTaskMuonAODCreation::AliAnalysisTaskMuonAODCreation() :
32   fOutput(0x0),
33   fTree(0x0),
34   fOutputAOD(0x0)
35 {
36 }
37 //___________________________________________________________________________
38 AliAnalysisTaskMuonAODCreation::AliAnalysisTaskMuonAODCreation(const Char_t* name) :
39   AliAnalysisTaskSE(name),
40   fOutput(0x0),
41   fTree(0x0),
42   fOutputAOD(0x0)
43 {
44   // Constructor. Initialization of Inputs and Outputs
45   //
46     
47   DefineOutput(1,TList::Class());
48
49 }
50
51 //___________________________________________________________________________
52 AliAnalysisTaskMuonAODCreation& AliAnalysisTaskMuonAODCreation::operator=(const AliAnalysisTaskMuonAODCreation& c) 
53 {
54   //
55   // Assignment operator
56   //
57   if (this!=&c) {
58     AliAnalysisTaskSE::operator=(c) ;
59   }
60   return *this;
61 }
62
63 //___________________________________________________________________________
64 AliAnalysisTaskMuonAODCreation::AliAnalysisTaskMuonAODCreation(const AliAnalysisTaskMuonAODCreation& c) :
65   AliAnalysisTaskSE(c),
66   fOutput(c.fOutput),
67   fTree(c.fTree),
68   fOutputAOD(c.fOutputAOD)
69  {
70   //
71   // Copy Constructor                                                                           
72   //
73 }
74
75 //___________________________________________________________________________
76 AliAnalysisTaskMuonAODCreation::~AliAnalysisTaskMuonAODCreation() {
77   //
78   //destructor
79   //
80   Info("~AliAnalysisTaskMuonAODCreation","Calling Destructor");
81 }
82
83 //___________________________________________________________________________
84 void AliAnalysisTaskMuonAODCreation::UserCreateOutputObjects(){
85         
86   AliAODHandler* handler = (AliAODHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
87  
88   fOutputAOD   = handler->GetAOD();
89   fTree = handler->GetTree();
90
91   fOutput = new TList();
92   fOutput->SetOwner(); 
93  
94   TH1D *pt_alltracks    = new TH1D("pt_alltracks","pt_alltracks",10,0,20);      
95   TH1D *pt_muontracks    = new TH1D("pt_muontracks","pt_muontracks",10,0,20);   
96         
97   fOutput->Add(pt_alltracks);   
98   fOutput->Add(pt_muontracks);  
99   fOutput->ls(); 
100
101
102
103
104 //_________________________________________________
105 void AliAnalysisTaskMuonAODCreation::UserExec(Option_t *)
106 {
107   
108   AliAODEvent *aod = dynamic_cast<AliAODEvent*> (InputEvent());
109   
110   Int_t nMuons=0;
111  
112   for (Int_t j = 0; j<aod->GetNumberOfTracks(); j++) { 
113     AliAODTrack *track = aod->GetTrack(j);
114     ((TH1D*)(fOutput->FindObject("pt_alltracks")))->Fill(track->Pt()); 
115     if(track->IsMuonTrack()) {
116       nMuons++;
117       ((TH1D*)(fOutput->FindObject("pt_muontracks")))->Fill(track->Pt()); 
118     }  
119   }
120          
121   AliAODHandler* outputHandler = (AliAODHandler*) 
122       ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());         
123    
124   outputHandler->SetFillAOD(kFALSE);
125    
126   if(nMuons>0) { 
127     outputHandler->SetFillAOD(kTRUE);
128    
129     PostData(0,fTree);
130     PostData(1,fOutput);
131   } else {
132     return;
133   }
134 }
135
136
137 //________________________________________________________________________
138 void AliAnalysisTaskMuonAODCreation::Terminate(Option_t *) 
139 {
140   TCanvas *c = new TCanvas("c","plots",20,20,600,600);
141   c->Divide(2,2);
142   
143   TH1D *h_pt_all = dynamic_cast<TH1D*> (fOutput->FindObject("pt_alltracks"));  
144   TH1D *h_pt_muons = dynamic_cast<TH1D*> (fOutput->FindObject("pt_muontracks"));  
145   c->cd(1);
146   h_pt_all->Draw();
147   c->cd(2);
148   h_pt_muons->Draw();
149   c->cd(3);
150   h_pt_all->Draw();
151   h_pt_muons->Draw("same");
152 }
153