3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE Project *
5 //* ALICE Experiment at CERN, All rights reserved. *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 //* Sedat Altinpinar <Sedat.Altinpinar@cern.ch> *
9 //* Hege Erdal <hege.erdal@gmail.com> *
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 //**************************************************************************
20 /// @file AliDxHFEParticleSelection.cxx
21 /// @author Sedat Altinpinar, Hege Erdal, Matthias Richter
23 /// @brief Base class for particle selection
26 #include "AliDxHFEParticleSelection.h"
28 #include "AliVEvent.h"
29 #include "AliVParticle.h"
30 #include "TObjArray.h"
35 #include "THnSparse.h"
36 #include "AliReducedParticle.h"
38 #include "TPRegexp.h" // for TStringToken
45 /// ROOT macro for the implementation of ROOT specific class methods
46 ClassImp(AliDxHFEParticleSelection)
48 AliDxHFEParticleSelection::AliDxHFEParticleSelection(const char* name, const char* opt)
49 : TNamed(name?name:"AliDxHFEParticleSelection", name?name:"AliDxHFEParticleSelection")
51 , fSelectedTracks(NULL)
52 , fControlObjects(NULL)
53 , fhEventControl(NULL)
54 , fhTrackControl(NULL)
55 , fhNrTracksPerEvent(NULL)
59 , fParticleProperties(NULL)
69 const char* AliDxHFEParticleSelection::fgkEventControlBinNames[]={
75 const char* AliDxHFEParticleSelection::fgkTrackControlBinNames[]={
80 AliDxHFEParticleSelection::~AliDxHFEParticleSelection()
83 if (fParticleProperties) delete[] fParticleProperties;
84 fParticleProperties=NULL;
85 if (fSelectedTracks) delete fSelectedTracks;
87 if (fControlObjects) delete fControlObjects;
91 fhNrTracksPerEvent=NULL;
94 int AliDxHFEParticleSelection::Init()
97 // Init part sel. Calls InitControlObjects()
100 InitControlObjects();
105 int AliDxHFEParticleSelection::InitControlObjects()
108 // init control objects
109 // TODO: Change to private now that have Init()?
112 AliInfo("Setting up control objects");
115 /// init the control objects, can be overloaded by childs which should
116 /// call AliDxHFEParticleSelection::InitControlObjects() explicitly
118 name.Form("hEventControl%s", GetName());
119 fhEventControl=CreateControlHistogram(name.Data(), name.Data(),kNEventPropertyLabels,fgkEventControlBinNames);
121 name.Form("hTrackControl%s", GetName());
122 fhTrackControl=CreateControlHistogram(name.Data(), name.Data(),kNTrackPropertyLabels,fgkTrackControlBinNames);
124 name.Form("hNrTracksPerEvent%s", GetName());
125 fhNrTracksPerEvent=CreateControlHistogram(name.Data(), name.Data(),100);
127 AddControlObject(fhEventControl);
128 AddControlObject(fhTrackControl);
129 AddControlObject(fhNrTracksPerEvent);
135 TH1* AliDxHFEParticleSelection::CreateControlHistogram(const char* name,
140 const char** binLabels) const
142 /// create control histogram
143 std::auto_ptr<TH1> h(new TH1D(name, title, nBins, min, max));
144 if (!h.get()) return NULL;
146 for (int iLabel=0; iLabel<nBins; iLabel++) {
147 h->GetXaxis()->SetBinLabel(iLabel+1, binLabels[iLabel]);
155 TH2* AliDxHFEParticleSelection::CreateControl2DHistogram(const char* name,
162 /// create control 2D histogram. Requires as input:
163 // name = name of histogram
164 // title = title of histogram
165 // nBins (array with 6 elements) containing apropriate binning and range for x and y axis
166 // xaxis = title of x axis
167 // yaxis = title of y axis
169 std::auto_ptr<TH2> h(new TH2D(name, title, (Int_t)nBins[0], nBins[1], nBins[2], (Int_t)nBins[3], nBins[4],nBins[5]));
170 if (!h.get()) return NULL;
171 h->GetXaxis()->SetTitle(xaxis);
172 h->GetYaxis()->SetTitle(yaxis);
177 THnSparse* AliDxHFEParticleSelection::CreateControlTHnSparse(const char* name,
182 const char** binLabels) const
185 // Creates THnSparse.
188 AliInfo("Setting up THnSparse");
190 std::auto_ptr<THnSparseF> th(new THnSparseF(name, name, thnSize, thnBins, thnMin, thnMax));
191 if (th.get()==NULL) {
194 for (int iLabel=0; iLabel<thnSize; iLabel++) {
195 th->GetAxis(iLabel)->SetTitle(binLabels[iLabel]);
202 THnSparse* AliDxHFEParticleSelection::DefineTHnSparse()
205 // Defines the THnSparse. For now, only calls CreatControlTHnSparse
207 //TODO: Should make it more general. Or maybe one can use this here, and skip in PartSelEl?
209 // here is the only place to change the dimension
210 const int thnSize = 3;
211 InitTHnSparseArray(thnSize);
213 const double Pi=TMath::Pi();
217 int thnBins [thnSize] = { 1000, 200, 500};
218 double thnMin [thnSize] = { 0, 0, -1.};
219 double thnMax [thnSize] = { 100, 2*Pi, 1.};
220 const char* thnNames[thnSize] = { "Pt","Phi","Eta"};
222 name.Form("%s info", GetName());
224 return CreateControlTHnSparse(name,thnSize,thnBins,thnMin,thnMax,thnNames);
227 int AliDxHFEParticleSelection::AddControlObject(TObject* pObj)
229 /// add control object to list, the base class becomes owner of the object
230 if (!pObj) return -EINVAL;
231 if (!fControlObjects) {
232 fControlObjects=new TList;
233 if (!fControlObjects) return -ENOMEM;
234 fControlObjects->SetOwner();
236 if (fControlObjects->FindObject(pObj->GetName())) {
237 AliError(Form("ignoring duplicate object '%s' of type %s", pObj->GetName(), pObj->ClassName()));
240 if (GetVerbosity()>0) {
241 AliInfo(Form("Adding object '%s' of type %s",pObj->GetName(),pObj->ClassName()));
243 fControlObjects->Add(pObj);
247 int AliDxHFEParticleSelection::HistogramEventProperties(int histonr,int bin)
249 /// histogram event properties
250 if (!fControlObjects) return 0;
252 // TODO: use enums for the bins of the control histogram
253 // for now: 0=all, 1=events with D0s, 2=events with correlated D0s
254 if(histonr==kHistoEvent){
255 fhEventControl->Fill(bin);
257 if(histonr==kHistoNrTracksPrEvent){
258 fhNrTracksPerEvent->Fill(bin);
263 int AliDxHFEParticleSelection::HistogramParticleProperties(AliVParticle* p, int selected)
265 /// histogram particle properties
267 if (!p) return -EINVAL;
268 if (!fControlObjects) return 0;
270 // TODO: use enums for the bins of the control histogram
271 fhTrackControl->Fill(kTrackAll);
272 if (selected) fhTrackControl->Fill(kTrackSel);
276 int AliDxHFEParticleSelection::FillParticleProperties(AliVParticle* p, Double_t* data, int dimension) const
278 // fill the data array from the particle data
279 if (!data) return -EINVAL;
281 if (dimension!=GetDimTHnSparse()) {
282 // TODO: think about filling only the available data and throwing a warning
291 TObjArray* AliDxHFEParticleSelection::Select(const AliVEvent* pEvent)
293 /// create selection from 'Tracks' member of the event,
294 /// array contains only pointers but does not own the objects
295 /// object array needs to be deleted by caller
296 if (!pEvent) return NULL;
297 TObjArray* selectedTracks=new TObjArray;
298 if (!selectedTracks) return NULL;
299 selectedTracks->SetOwner(kFALSE); // creating new track objects below
300 int nofTracks=pEvent->GetNumberOfTracks();
301 for (int itrack=0; itrack<nofTracks; itrack++) {
302 AliVParticle* track=pEvent->GetTrack(itrack);
303 int selectionCode=IsSelected(track,pEvent);
304 HistogramParticleProperties(track, selectionCode);
305 if (selectionCode==0) continue;
306 selectedTracks->Add(CreateParticle(track));
308 return selectedTracks;
311 TObjArray* AliDxHFEParticleSelection::Select(TObjArray* pParticles, const AliVEvent* pEvent)
313 /// create selection from the array of particles,
314 /// array contains only pointers but does not own the objects
315 /// object array needs to be deleted by caller
316 if (!pParticles) return NULL;
317 TObjArray* selectedTracks=new TObjArray;
318 if (!selectedTracks) return NULL;
319 selectedTracks->SetOwner(kFALSE); // creating new track objects below
320 TIter next(pParticles);
322 while ((pObj=next())) {
323 AliVParticle* track=dynamic_cast<AliVParticle*>(pObj);
324 if (!track) continue;
325 int selectionCode=IsSelected(track, pEvent);
326 HistogramParticleProperties(track, selectionCode);
327 if (selectionCode ==0) continue;
328 selectedTracks->Add(CreateParticle(track));
330 return selectedTracks;
333 int AliDxHFEParticleSelection::CheckAndAdd(AliVParticle* /*p*/)
335 /// check and add track to internal array
336 /// TODO: check if needed
340 int AliDxHFEParticleSelection::IsSelected(AliVParticle* /*p*/, const AliVEvent* /*e*/)
342 /// check particle if it passes the selection criteria
343 /// childs can overload, by default all tracks are selected
347 void AliDxHFEParticleSelection::AliDxHFEParticleSelection::Clear(Option_t * /*option*/)
349 /// inherited from TObject: cleanup
352 void AliDxHFEParticleSelection::Print(Option_t */*option*/) const
354 /// inherited from TObject: print info
355 cout << "====================================================================" << endl;
357 if (fControlObjects) fControlObjects->Print();
360 void AliDxHFEParticleSelection::SaveAs(const char* filename, Option_t */*option*/) const
362 /// inherited from TObject: save selection criteria
364 // TODO: options recreate
365 fileoption="RECREATE";
366 //else fileoption="UPDATE";
368 std::auto_ptr<TFile> output(TFile::Open(filename,fileoption));
369 if (!output.get() || output->IsZombie()) {
370 AliError(Form("can not open file %s from writing", filename));
374 if (fControlObjects) fControlObjects->Write();
378 void AliDxHFEParticleSelection::Draw(Option_t* /*option*/)
380 /// inherited from TObject: draw content
382 // TODO: implement drawing code
383 // - create canvas objects
384 // - plot internal objects
385 // - optionally save canvases to file
387 // It might be appropriate to have another Draw function taking a
388 // TList as argument and implementing the actual drawing. If this
389 // function is 'static', it can be used stand-alone also from macros
392 TObject* AliDxHFEParticleSelection::FindObject(const char* name) const
394 /// inherited from TObject: find object by name
396 if (fControlObjects) {
397 return fControlObjects->FindObject(name);
402 TObject* AliDxHFEParticleSelection::FindObject(const TObject* obj) const
404 /// inherited from TObject: find object by pointer
405 if (fControlObjects) {
406 return fControlObjects->FindObject(obj);
411 AliVParticle *AliDxHFEParticleSelection::CreateParticle(AliVParticle* track)
413 // Creating object with reduced particle properties
414 AliReducedParticle *part = new AliReducedParticle(track->Eta(), track->Phi(), track->Pt(), track->Charge(), 0);
420 int AliDxHFEParticleSelection::ParseArguments(const char* arguments)
422 // parse arguments and set internal flags
423 const char* key=NULL;
424 const TString delimiter(" ");
425 TStringToken token(arguments, delimiter);
426 while (token.NextToken()) {
427 TString argument=token;
429 if (argument.BeginsWith(key)) {
430 argument.ReplaceAll(key, "");
431 if (argument.CompareTo("pp")==0) fSystem=0;
432 else if (argument.CompareTo("Pb-Pb")==0) fSystem=1;
433 else if (argument.CompareTo("p-Pb")==0) fSystem=2;
435 AliWarning(Form("can not set collision system, unknown parameter '%s'", argument.Data()));
436 // TODO: check what makes sense
442 AliWarning(Form("unknown argument '%s'", argument.Data()));