]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGHF/correlationHF/AliDxHFEParticleSelection.cxx
Fixes for std:: need with the trunk of Root (Jochen, Yves)
[u/mrichter/AliRoot.git] / PWGHF / correlationHF / AliDxHFEParticleSelection.cxx
1 // $Id$
2
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE Project            * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //*                  Sedat Altinpinar <Sedat.Altinpinar@cern.ch>           *
9 //*                  Hege Erdal       <hege.erdal@gmail.com>               *
10 //*                                                                        *
11 //* Permission to use, copy, modify and distribute this software and its   *
12 //* documentation strictly for non-commercial purposes is hereby granted   *
13 //* without fee, provided that the above copyright notice appears in all   *
14 //* copies and that both the copyright notice and this permission notice   *
15 //* appear in the supporting documentation. The authors make no claims     *
16 //* about the suitability of this software for any purpose. It is          *
17 //* provided "as is" without express or implied warranty.                  *
18 //**************************************************************************
19
20 /// @file   AliDxHFEParticleSelection.cxx
21 /// @author Sedat Altinpinar, Hege Erdal, Matthias Richter
22 /// @date   2012-03-19
23 /// @brief  Base class for particle selection
24 ///
25
26 #include "AliDxHFEParticleSelection.h"
27 #include "AliLog.h"
28 #include "AliVEvent.h"
29 #include "AliVParticle.h"
30 #include "TObjArray.h"
31 #include "TList.h"
32 #include "TMath.h"
33 #include "TH1D.h"
34 #include "THnSparse.h"
35 #include "TFile.h"
36 #include <iostream>
37 #include <cerrno>
38 #include <memory>
39
40 using namespace std;
41
42 /// ROOT macro for the implementation of ROOT specific class methods
43 ClassImp(AliDxHFEParticleSelection)
44
45 AliDxHFEParticleSelection::AliDxHFEParticleSelection(const char* name, const char* opt)
46   : TNamed(name?name:"AliDxHFEParticleSelection", name?name:"AliDxHFEParticleSelection")
47   , fOption(opt)
48   , fSelectedTracks(NULL)
49   , fControlObjects(NULL)
50   , fhEventControl(NULL)
51   , fhTrackControl(NULL)
52   , fUseMC(false)
53   , fVerbosity(0)
54 {
55   // constructor
56   // 
57   // 
58   // 
59   // 
60 }
61
62 const char* AliDxHFEParticleSelection::fgkEventControlBinNames[]={
63   "nEventsAll",
64   "nEventsSelected",
65   "nEventsD0"
66 };
67
68 AliDxHFEParticleSelection::~AliDxHFEParticleSelection()
69 {
70   // destructor
71   if (fSelectedTracks) delete fSelectedTracks;
72   fSelectedTracks=NULL;
73   if (fControlObjects) delete fControlObjects;
74   fControlObjects=NULL;
75   fhEventControl=NULL;
76   fhTrackControl=NULL;
77 }
78
79 int AliDxHFEParticleSelection::InitControlObjects()
80 {
81   // init control objects
82   if (fVerbosity>0) {
83     AliInfo("Setting up control objects");
84   }
85
86   /// init the control objects, can be overloaded by childs which should
87   /// call AliDxHFEParticleSelection::InitControlObjects() explicitly
88   std::auto_ptr<TH1D> hEventControl(new TH1D("hEventControl", "hEventControl", 10, 0, 10));
89   std::auto_ptr<TH1D> hTrackControl(new TH1D("hTrackControl", "hTrackControl", 10, 0, 10));
90
91   fhEventControl=hEventControl.release();
92   for (int iLabel=0; iLabel<kNEventPropertyLabels; iLabel++)
93     fhEventControl->GetXaxis()->SetBinLabel(iLabel, fgkEventControlBinNames[iLabel]);
94   AddControlObject(fhEventControl);
95   fhTrackControl=hTrackControl.release();
96   AddControlObject(fhTrackControl);
97
98   return 0;
99 }
100
101 int AliDxHFEParticleSelection::AddControlObject(TObject* pObj)
102 {
103   /// add control object to list, the base class becomes owner of the object
104   if (!pObj) return -EINVAL;
105   if (!fControlObjects) {
106     fControlObjects=new TList;
107     if (!fControlObjects) return -ENOMEM;
108     fControlObjects->SetOwner();
109   }
110   if (fControlObjects->FindObject(pObj->GetName())) {
111     AliError(Form("ignoring duplicate object '%s' of type %s", pObj->GetName(), pObj->ClassName()));
112     return -EEXIST;
113   }
114   if (GetVerbosity()>0) {
115     AliInfo(Form("Adding object '%s' of type %s",pObj->GetName(),pObj->ClassName()));
116   }
117   fControlObjects->Add(pObj);
118   return 0;
119 }
120
121 int AliDxHFEParticleSelection::HistogramEventProperties(int bin)
122 {
123   /// histogram event properties
124   if (!fControlObjects) return 0;
125
126   // TODO: use enums for the bins of the control histogram
127   // for now: 0=all, 1=events with D0s, 2=events with correlated D0s
128   fhEventControl->Fill(bin);
129   return 0;
130 }
131
132 int AliDxHFEParticleSelection::HistogramParticleProperties(AliVParticle* p, int selected)
133 {
134   /// histogram particle properties
135   if (!p) return -EINVAL;
136   if (!fControlObjects) return 0;
137
138   // TODO: use enums for the bins of the control histogram
139   fhTrackControl->Fill(0);
140   if (selected) fhTrackControl->Fill(1);
141   return 0;
142 }
143
144 TObjArray* AliDxHFEParticleSelection::Select(const AliVEvent* pEvent)
145 {
146   /// create selection from 'Tracks' member of the event,
147   /// array contains only pointers but does not own the objects
148   /// object array needs to be deleted by caller
149   if (!pEvent) return NULL;
150   TObjArray* selectedTracks=new TObjArray;
151   if (!selectedTracks) return NULL;
152   int nofTracks=pEvent->GetNumberOfTracks();
153   for (int itrack=0; itrack<nofTracks; itrack++) {
154     AliVParticle* track=pEvent->GetTrack(itrack);
155     int selectionCode=IsSelected(track);
156     HistogramParticleProperties(track, selectionCode);
157     if (selectionCode==0) continue;
158     selectedTracks->Add(track);
159   }
160   return selectedTracks;
161 }
162
163 TObjArray* AliDxHFEParticleSelection::Select(TObjArray* pParticles, const AliVEvent* pEvent)
164 {
165   /// create selection from the array of particles,
166   /// array contains only pointers but does not own the objects
167   /// object array needs to be deleted by caller
168   if (!pParticles) return NULL;
169   TObjArray* selectedTracks=new TObjArray;
170   if (!selectedTracks) return NULL;
171   TIter next(pParticles);
172   TObject* pObj=NULL;
173   while ((pObj=next())) {
174     AliVParticle* track=dynamic_cast<AliVParticle*>(pObj);
175     if (!track) continue;
176     int selectionCode=IsSelected(track, pEvent);
177     HistogramParticleProperties(track, selectionCode);
178     if (selectionCode ==0) continue;
179     selectedTracks->Add(track);
180   }
181   return selectedTracks;
182 }
183
184 int AliDxHFEParticleSelection::CheckAndAdd(AliVParticle* /*p*/)
185 {
186   /// check and add track to internal array
187   /// TODO: check if needed
188   return -ENOSYS;
189 }
190
191 int AliDxHFEParticleSelection::IsSelected(AliVParticle* /*p*/, const AliVEvent* /*e*/)
192 {
193   /// check particle if it passes the selection criteria
194   /// childs can overload, by default all tracks are selected
195   return 1;
196 }
197
198 void AliDxHFEParticleSelection::AliDxHFEParticleSelection::Clear(Option_t * /*option*/)
199 {
200   /// inherited from TObject: cleanup
201 }
202
203 void AliDxHFEParticleSelection::Print(Option_t */*option*/) const
204 {
205   /// inherited from TObject: print info
206   cout << "====================================================================" << endl;
207   TNamed::Print();
208   if (fControlObjects) fControlObjects->Print();
209 }
210  
211 void AliDxHFEParticleSelection::SaveAs(const char* filename, Option_t */*option*/) const
212 {
213   /// inherited from TObject: save selection criteria
214   TString fileoption;
215   // TODO: options recreate
216   fileoption="RECREATE";
217   //else fileoption="UPDATE";
218
219   std::auto_ptr<TFile> output(TFile::Open(filename,fileoption));
220   if (!output.get() || output->IsZombie()) {
221     AliError(Form("can not open file %s from writing", filename));
222     return;
223   }
224   output->cd();
225   if (fControlObjects) fControlObjects->Write();
226   output->Close();
227 }
228
229 void AliDxHFEParticleSelection::Draw(Option_t* /*option*/)
230 {
231   /// inherited from TObject: draw content
232
233   // TODO: implement drawing code
234   // - create canvas objects
235   // - plot internal objects
236   // - optionally save canvases to file
237   //
238   // It might be appropriate to have another Draw function taking a
239   // TList as argument and implementing the actual drawing. If this
240   // function is 'static', it can be used stand-alone also from macros
241 }
242
243 TObject* AliDxHFEParticleSelection::FindObject(const char* name) const
244 {
245   /// inherited from TObject: find object by name
246
247   if (fControlObjects) {
248     return fControlObjects->FindObject(name);
249   }
250   return NULL;
251 }
252
253 TObject* AliDxHFEParticleSelection::FindObject(const TObject* obj) const
254 {
255   /// inherited from TObject: find object by pointer
256   if (fControlObjects) {
257     return fControlObjects->FindObject(obj);
258   }
259   return NULL;
260 }