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