Fix Coverity 24835
[u/mrichter/AliRoot.git] / PWG / DevNanoAOD / AliAnalysisTaskNanoAODFilter.cxx
CommitLineData
778636d4 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// Generic task to produce nanoAOD
19// Author: Michele Floris, michele.floris@cern.ch
20
21#include "AliAnalysisTaskNanoAODFilter.h"
22
23#include "Riostream.h"
24#include "TChain.h"
25#include "TTree.h"
26#include "TH1F.h"
27#include "TH2F.h"
28#include "TCanvas.h"
29#include "TList.h"
30
31#include "AliAnalysisTaskSE.h"
32#include "AliAnalysisManager.h"
33#include "AliStack.h"
34#include "AliESDtrackCuts.h"
35#include "AliESDEvent.h"
36#include "AliESDInputHandler.h"
37#include "AliAODEvent.h"
38#include "AliAODTrack.h"
39#include "AliMCEvent.h"
40#include "AliESDtrack.h"
41#include "AliAODHandler.h"
42#include "AliNanoAODReplicator.h"
43#include "AliNanoAODTrackMapping.h"
44
5e2539d9 45using std::cout;
46using std::endl;
47
778636d4 48ClassImp(AliAnalysisTaskNanoAODFilter)
49
50
51//________________________________________________________________________
52AliAnalysisTaskNanoAODFilter::AliAnalysisTaskNanoAODFilter() // All data members should be initialised here
53:AliAnalysisTaskSE(),
54 fTrkrep(0),
55 fVarList(""),
56 fVarListHead(""),
57 fEvtCuts(0),
58 fTrkCuts(0),
59 fSetter(0),
60 fSaveCutsFlag(0)
61{
62 // Dummy constructor ALWAYS needed for I/O.
63}
64
65//________________________________________________________________________
66AliAnalysisTaskNanoAODFilter::AliAnalysisTaskNanoAODFilter(const char *name, Bool_t saveCutsFlag) // All data members should be initialised here
67 :AliAnalysisTaskSE(name),
68 fTrkrep(0),
69 fVarList(""),
70 fVarListHead(""),
71 fEvtCuts(0),
72 fTrkCuts(0),
73 fSetter(0),
74 fSaveCutsFlag(saveCutsFlag)
75
76{
77 // Constructor
78 if(fSaveCutsFlag) {
79 DefineOutput(1, AliAnalysisCuts::Class());
80 DefineOutput(2, AliAnalysisCuts::Class());
81 }
82
83
84}
85
86//________________________________________________________________________
87AliAnalysisTaskNanoAODFilter::~AliAnalysisTaskNanoAODFilter()
88{
89 // Destructor. Clean-up the output list, but not the histograms that are put inside
90 // (the list is owner and will clean-up these histograms). Protect in PROOF case.
91}
92
93//________________________________________________________________________
94void AliAnalysisTaskNanoAODFilter::UserCreateOutputObjects()
95{
96 // Create histograms
97 // Called once (on the worker node)
98
99 if(fSaveCutsFlag) {
100 PostData(1, fEvtCuts);
101 PostData(2, fTrkCuts);
102 }
103}
104
105void AliAnalysisTaskNanoAODFilter::AddFilteredAOD(const char* aodfilename, const char* title)
106{
107 // The replicator is added to the extension
108
109 AliAODHandler *aodH = (AliAODHandler*)((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
110 if (!aodH) AliFatal("No AOD handler");
111 AliAODExtension* ext = aodH->AddFilteredAOD(aodfilename,title);
112
113 if (!ext){
114 AliFatal("Cannot get extension");
115 }
116
117
118 AliNanoAODReplicator * rep = new AliNanoAODReplicator("NanoAODReplicator",
119 "remove non interesting tracks, "
120 "writes special tracks array tracks",
121 fVarList,
122 fTrkCuts,
123 fMCMode);
124
125
126 cout<<"rep: "<<rep<<endl;
127 rep->SetCustomSetter(fSetter);
128 std::cout << "SETTER: " << fSetter << " " << rep->GetCustomSetter() << std::endl;
129
130 ext->DropUnspecifiedBranches(); // all branches not part of a FilterBranch call (below) will be dropped
131
132 ext->FilterBranch("tracks",rep);
133 ext->FilterBranch("vertices",rep);
134 ext->FilterBranch("header",rep);
135
136 if ( fMCMode > 0 )
137 {
138 // MC branches will be copied (if present), as they are, but only
139 // for events with at least one muon.
140 // For events w/o muon, mcparticles array will be empty and mcheader will be dummy
141 // (e.g. strlen(GetGeneratorName())==0)
142
143 ext->FilterBranch("mcparticles",rep);
144 ext->FilterBranch("mcHeader",rep);
145 }
146}
147
148void AliAnalysisTaskNanoAODFilter::Init()
149{
150
151 // Initialization
152 AddFilteredAOD("AliAOD.NanoAOD.root", "NanoAODTracksEvents");
153
154}
155
156
157
158//________________________________________________________________________
159void AliAnalysisTaskNanoAODFilter::UserExec(Option_t *)
160{
161 // Main loop
162 Long64_t ientry = Entry();
163 if(fDebug)printf("Nano AOD Filter: Analysing event # %5d\n", (Int_t) ientry);
164
165 AliAODEvent *lAODevent=(AliAODEvent*)InputEvent();
166
167
168 AliAODHandler* handler = dynamic_cast<AliAODHandler*>(AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler());
169
170 if(fEvtCuts && !fEvtCuts->IsSelected(lAODevent)) return;// FIXME: should event cuts be called here or in the branch replicator? Do we get duplicated events if we skip here (arrays not reset in the branch replicator?)
171
172 if ( handler ){
173 AliAODExtension *extNanoAOD = handler->GetFilteredAOD("AliAOD.NanoAOD.root");
174 if ( extNanoAOD ) {
175 extNanoAOD->SetEvent(lAODevent);
176 extNanoAOD->SelectEvent();
177 extNanoAOD->FinishEvent();
178 }
179 }
180
181
182 if(fSaveCutsFlag) {
183 PostData(1, fEvtCuts);
184 PostData(2, fTrkCuts);
185 }
186}
187
188
189//________________________________________________________________________
190void AliAnalysisTaskNanoAODFilter::Terminate(Option_t *)
191{
192 // print some debug info
193
194}
195
196void AliAnalysisTaskNanoAODFilter::FinishTaskOutput() {
197
198 // We save here the user info //
199
200 AliAODHandler* handler = dynamic_cast<AliAODHandler*>(AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler());
201 AliAODExtension *extNanoAOD = handler->GetFilteredAOD("AliAOD.NanoAOD.root");
202 extNanoAOD->GetTree()->GetUserInfo()->Add(AliNanoAODTrackMapping::GetInstance());
203
204}