AliPhysics  master (3d17d9d)
AliAnalysisTaskForStudents.cxx
Go to the documentation of this file.
1 /*************************************************************************
2 * Copyright(c) 1998-2008, 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 /**************************************
17 * template class for student projects *
18 **************************************/
19 
20 #include "Riostream.h"
22 #include "AliLog.h"
23 #include "AliAODEvent.h"
24 #include "AliAODInputHandler.h"
25 #include "TFile.h"
26 
27 using std::cout;
28 using std::endl;
29 
31 
32 //================================================================================================================
33 
34 AliAnalysisTaskForStudents::AliAnalysisTaskForStudents(const char *name, Bool_t useParticleWeights):
35  AliAnalysisTaskSE(name),
36  fHistList(NULL),
37  // Control histograms:
38  fControlHistogramsList(NULL),
39  fPtHist(NULL),
40  fNbinsPt(1000),
41  fMinBinPt(0.),
42  fMaxBinPt(10.),
43  // Final results:
44  fFinalResultsList(NULL)
45  {
46  // Constructor.
47 
48  AliDebug(2,"AliAnalysisTaskForStudents::AliAnalysisTaskForStudents(const char *name, Bool_t useParticleWeights)");
49 
50  // Base list:
51  fHistList = new TList();
52  fHistList->SetName("outputStudentAnalysis");
53  fHistList->SetOwner(kTRUE);
54 
55  // Initialize all arrays:
56  this->InitializeArrays();
57 
58  // Define input and output slots here
59  // Input slot #0 works with an AliFlowEventSimple
60  //DefineInput(0, AliFlowEventSimple::Class());
61  // Input slot #1 is needed for the weights input file:
62  //if(useParticleWeights)
63  //{
64  // DefineInput(1, TList::Class());
65  //}
66  // Output slot #0 is reserved
67  // Output slot #1 writes into a TList container
68 
69  DefineOutput(1, TList::Class());
70 
71  if(useParticleWeights)
72  {
73  // not needed for the time being
74  }
75 
76 } // AliAnalysisTaskForStudents::AliAnalysisTaskForStudents(const char *name, Bool_t useParticleWeights):
77 
78 //================================================================================================================
79 
82  fHistList(NULL),
83  // Control histograms:
84  fControlHistogramsList(NULL),
85  fPtHist(NULL),
86  fNbinsPt(1000),
87  fMinBinPt(0.),
88  fMaxBinPt(10.),
89  // Final results:
90  fFinalResultsList(NULL)
91 {
92  // Dummy constructor.
93 
94  AliDebug(2,"AliAnalysisTaskForStudents::AliAnalysisTaskForStudents()");
95 
96 } // AliAnalysisTaskForStudents::AliAnalysisTaskForStudents():
97 
98 //================================================================================================================
99 
101 {
102  // Destructor.
103 
104  if(fHistList) delete fHistList;
105 
106 } // AliAnalysisTaskForStudents::~AliAnalysisTaskForStudents()
107 
108 //================================================================================================================
109 
111 {
112  // Called at every worker node to initialize.
113 
114  // a) Trick to avoid name clashes, part 1;
115  // b) Book and nest all lists;
116  // c) Book all objects;
117  // *) Trick to avoid name clashes, part 2.
118 
119  // a) Trick to avoid name clashes, part 1:
120  Bool_t oldHistAddStatus = TH1::AddDirectoryStatus();
121  TH1::AddDirectory(kFALSE);
122 
123  // b) Book and nest all lists:
124  this->BookAndNestAllLists();
125 
126  // c) Book all objects:
127  this->BookControlHistograms();
129 
130  // *) Trick to avoid name clashes, part 2:
131  TH1::AddDirectory(oldHistAddStatus);
132 
133  PostData(1,fHistList);
134 
135 } // void AliAnalysisTaskForStudents::UserCreateOutputObjects()
136 
137 //================================================================================================================
138 
140 {
141  // Main loop (called for each event).
142  // a) Get pointer to AOD event:
143  // b) Start analysis over AODs;
144  // c) Reset event-by-event objects;
145  // d) PostData.
146 
147  // a) Get pointer to AOD event:
148  AliAODEvent *aAOD = dynamic_cast<AliAODEvent*>(InputEvent()); // from TaskSE
149  if(!aAOD){return;}
150 
151  // b) Start analysis over AODs:
152  Int_t nTracks = aAOD->GetNumberOfTracks(); // number of all tracks in current event
153  for(Int_t iTrack=0;iTrack<nTracks;iTrack++) // starting a loop over all tracks
154  {
155  AliAODTrack *aTrack = dynamic_cast<AliAODTrack*>(aAOD->GetTrack(iTrack)); // getting a pointer to a track
156  if(!aTrack){continue;} // protection against NULL pointers
157  if(!aTrack->TestFilterBit(128)){continue;} // filter bit 128 denotes TPC-only tracks, use only them for the analysis
158 
159  // example variables for each track: (for more options, please see class STEER/AOD/AliAODTrack.h)
160  Double_t pt = aTrack->Pt(); // Pt
161  //Double_t px = aTrack->Px(); // x-component of momenta
162  //Double_t py = aTrack->Py(); // y-component of momenta
163  //Double_t pz = aTrack->Pz(); // z-component of momenta
164  //Double_t e = aTrack->E(); // energy
165  //Double_t phi = aTrack->Phi(); // azimuthal angle
166  //Double_t eta = aTrack->Eta(); // pseudorapidity
167  //Double_t charge = aTrack->Charge(); // charge
168 
169  // apply some cuts: e.g. take for the analysis only particles in 0.2 < pT < 5.0 GeV
170  if( (0.2 < pt) && (pt < 5.0) ) // example cuts
171  {
172  // fill some control histograms:
173  fPtHist->Fill(pt); // filling pt distribution
174 
175  // do some analysis only with the particles which passed the cuts
176  // ... your analysis code ...
177 
178  } // if( (0.2 < pT) && (pT < 5.0) )
179 
180  } // for(Int_t iTrack=0;iTrack<nTracks;iTrack++) // starting a loop over all tracks
181 
182  // c) Reset event-by-event objects:
183  // ...
184 
185  // d) PostData:
186  PostData(1,fHistList);
187 
188 } // void AliAnalysisTaskForStudents::UserExec(Option_t *)
189 
190 //================================================================================================================
191 
193 {
194  // Accessing the merged output list.
195 
196  fHistList = (TList*)GetOutputData(1);
197  if(!fHistList){exit(1);}
198 
199  // Do some calculation in offline mode here:
200 
201  // ... your code for offline calculations ...
202 
203  // Update the output file with new results:
204  TFile *f = new TFile("AnalysisResults.root","RECREATE");
205  fHistList->Write(fHistList->GetName(),TObject::kSingleKey);
206 
207  delete f;
208 
209 } // end of void AliAnalysisTaskForStudents::Terminate(Option_t *)
210 
211 //================================================================================================================
212 
214 {
215  // Initialize all data members which are arrays in this method.
216 
217 } // void AliAnalysisTaskForStudents::InitializeArrays()
218 
219 //=======================================================================================================================
220 
222 {
223  // Book and nest all lists nested in the base list fHistList.
224 
225  // a) Book and nest lists for control histograms;
226  // b) Book and nest lists for final results.
227 
228  TString sMethodName = "void AliAnalysisTaskForStudents::BookAndNestAllLists()";
229  if(!fHistList){Fatal(sMethodName.Data(),"fHistList is NULL");}
230 
231  // a) Book and nest lists for control histograms:
233  fControlHistogramsList->SetName("ControlHistograms");
234  fControlHistogramsList->SetOwner(kTRUE);
235  fHistList->Add(fControlHistogramsList);
236 
237  // b) Book and nest lists for final results:
238  fFinalResultsList = new TList();
239  fFinalResultsList->SetName("FinalResults");
240  fFinalResultsList->SetOwner(kTRUE);
242 
243 } // void AliAnalysisTaskForStudents::BookAndNestAllLists()
244 
245 //=======================================================================================================================
246 
248 {
249  // Book all control histograms.
250 
251  // a) Book histogram to hold pt spectra;
252  // b) ...
253 
254  // a) Book histogram to hold pt spectra:
255  fPtHist = new TH1F("fPtHist","atrack->Pt()",fNbinsPt,fMinBinPt,fMaxBinPt);
256  fPtHist->SetStats(kFALSE);
257  fPtHist->SetFillColor(kBlue-10);
258  fPtHist->GetXaxis()->SetTitle("p_{t}");
260 
261  // b) ...
262 
263 } // void AliAnalysisTaskForStudents::BookControlHistograms()
264 
265 //=======================================================================================================================
266 
268 {
269  // Book all histograms to hold the final results.
270 
271 } // void AliAnalysisTaskForStudents::BookFinalResultsHistograms()
272 
273 //=======================================================================================================================
274 
double Double_t
Definition: External.C:58
int Int_t
Definition: External.C:63
const char Option_t
Definition: External.C:48
bool Bool_t
Definition: External.C:53