1 /*************************************************************************
2 * Copyright(c) 1998-2009, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 ///////////////////////////////////////////////////////////////////////////
17 // Dielectron MixingHandler //
26 ///////////////////////////////////////////////////////////////////////////
33 #include <AliVTrack.h>
35 #include "AliDielectron.h"
36 #include "AliDielectronHelper.h"
37 #include "AliDielectronHistos.h"
38 #include "AliDielectronEvent.h"
40 #include "AliDielectronMixingHandler.h"
42 ClassImp(AliDielectronMixingHandler)
44 AliDielectronMixingHandler::AliDielectronMixingHandler() :
47 fArrPools("TClonesArray"),
50 fMixIncomplete(kTRUE),
51 fMoveToSameVertex(kFALSE)
54 // Default Constructor
56 for (Int_t i=0; i<kMaxCuts; ++i){
59 fAxes.SetOwner(kTRUE);
62 //______________________________________________
63 AliDielectronMixingHandler::AliDielectronMixingHandler(const char* name, const char* title) :
66 fArrPools("TClonesArray"),
69 fMixIncomplete(kTRUE),
70 fMoveToSameVertex(kFALSE)
75 for (Int_t i=0; i<kMaxCuts; ++i){
78 fAxes.SetOwner(kTRUE);
81 //______________________________________________
82 AliDielectronMixingHandler::~AliDielectronMixingHandler()
90 //________________________________________________________________
91 void AliDielectronMixingHandler::AddVariable(AliDielectronVarManager::ValueTypes type,
92 Int_t nbins, Double_t min, Double_t max, Bool_t log)
95 // Add a variable to the mixing handler
98 // limit number of variables to kMaxCuts
99 if (fAxes.GetEntriesFast()>=kMaxCuts) return;
101 TVectorD *binLimits=0x0;
102 if (!log) binLimits=AliDielectronHelper::MakeLinBinning(nbins,min,max);
103 else binLimits=AliDielectronHelper::MakeLogBinning(nbins,min,max);
104 if (!binLimits) return;
106 Int_t size=fAxes.GetEntriesFast();
107 fEventCuts[size]=(UShort_t)type;
108 fAxes.Add(binLimits);
111 //________________________________________________________________
112 void AliDielectronMixingHandler::AddVariable(AliDielectronVarManager::ValueTypes type,
113 const char* binLimitStr)
116 // Add a variable to the mixing handler with arbitrary binning
119 // limit number of variables to kMaxCuts
120 if (fAxes.GetEntriesFast()>=kMaxCuts) return;
122 TVectorD *binLimits=AliDielectronHelper::MakeArbitraryBinning(binLimitStr);
123 if (!binLimits) return;
125 Int_t size=fAxes.GetEntriesFast();
126 fEventCuts[size]=(UShort_t)type;
127 fAxes.Add(binLimits);
130 //______________________________________________
131 void AliDielectronMixingHandler::Fill(const AliVEvent *ev, AliDielectron *diele)
134 // fill event buffers and perform mixing if the pool depth is reached
137 //check if there are tracks available
138 if (diele->GetTrackArray(0)->GetEntriesFast()==0 && diele->GetTrackArray(1)->GetEntriesFast()==0) return;
141 Double_t values[AliDielectronVarManager::kNMaxValues]={0.};
142 AliDielectronVarManager::Fill(ev,values);
145 Int_t bin=FindBin(values,&dim);
147 //add mixing bin to event data
148 values[AliDielectronVarManager::kMixingBin] = bin;
151 AliDebug(5,Form("Bin outside range: %s",dim.Data()));
155 // get mixing pool, create it if it does not yet exist.
156 TClonesArray *poolp=static_cast<TClonesArray*>(fArrPools.At(bin));
158 AliDebug(10,Form("New pool at %d (%s)\n",bin,dim.Data()));
159 poolp=new(fArrPools[bin]) TClonesArray("AliDielectronEvent",fDepth);
161 TClonesArray &pool=*poolp;
163 AliDebug(10,Form("new event at %d: %d",bin,pool.GetEntriesFast()));
164 AliDielectronEvent *event=new(pool[pool.GetEntriesFast()]) AliDielectronEvent();
165 if(ev->IsA() == AliAODEvent::Class()) event->SetAOD();
166 event->SetTracks(*diele->GetTrackArray(0), *diele->GetTrackArray(1), *diele->GetPairArray(1));
167 event->SetEventData(values);
169 // check if pool depth is reached.
170 if (pool.GetEntriesFast()<fDepth) return;
172 // pool depth reached, do mixing
173 DoMixing(pool,diele);
175 // increase counter for full bins
176 if (diele->fHistos) {
177 diele->fHistos->Fill("Mixing","Stats",0);
178 diele->fHistos->Fill("Mixing","CompletePools",bin);
181 //clear the current pool
185 //______________________________________________
186 void AliDielectronMixingHandler::DoMixing(TClonesArray &pool, AliDielectron *diele)
189 // perform the mixing
192 //buffer track arrays and copy them back afterwards
193 TObjArray arrTrDummy[4];
194 for (Int_t i=0; i<4; ++i) arrTrDummy[i]=diele->fTracks[i];
196 //buffer also global event data
197 Double_t values[AliDielectronVarManager::kNMaxValues]={0};
198 for (Int_t i=AliDielectronVarManager::kPairMax; i<AliDielectronVarManager::kNMaxValues; ++i)
199 values[i]=AliDielectronVarManager::GetValue((AliDielectronVarManager::ValueTypes)i);
201 for (Int_t i1=0; i1<pool.GetEntriesFast(); ++i1){
202 AliDielectronEvent *ev1=static_cast<AliDielectronEvent*>(pool.At(i1));
203 //use event data from the first event
204 //both events are in the same mixing bin anyhow...
205 AliDielectronVarManager::SetEventData(ev1->GetEventData());
208 TIter ev1P(ev1->GetTrackArrayP());
209 TIter ev1N(ev1->GetTrackArrayN());
211 for (Int_t i2=i1+1; i2<pool.GetEntriesFast(); ++i2){
213 diele->fTracks[0].Clear();
214 diele->fTracks[1].Clear();
215 diele->fTracks[2].Clear();
216 diele->fTracks[3].Clear();
219 AliDielectronEvent *ev2=static_cast<AliDielectronEvent*>(pool.At(i2));
222 TIter ev2P(ev2->GetTrackArrayP());
223 TIter ev2N(ev2->GetTrackArrayN());
226 //move tracks to the same vertex (vertex of the first event), if requested
228 if (fMoveToSameVertex){
229 const Double_t *varsFirst=ev1->GetEventData();
230 const Double_t *varsMix=ev2->GetEventData();
232 const Double_t vFirst[3]={varsFirst[AliDielectronVarManager::kXvPrim],
233 varsFirst[AliDielectronVarManager::kYvPrim],
234 varsFirst[AliDielectronVarManager::kZvPrim]};
236 const Double_t vMix[3] ={varsMix[AliDielectronVarManager::kXvPrim],
237 varsMix[AliDielectronVarManager::kYvPrim],
238 varsMix[AliDielectronVarManager::kZvPrim]};
240 //loop over all tracks from the second event and move them to the vertex of the first
241 AliVTrack *vtrack=0x0;
242 while ( ( vtrack=(AliVTrack*)ev2P() ) ){
243 MoveToSameVertex(vtrack, vFirst, vMix);
247 while ( ( vtrack=(AliVTrack*)ev2N() ) ){
248 MoveToSameVertex(vtrack, vFirst, vMix);
256 // set the vertex to the track in case (AOD only)
257 if ( ev1->IsAOD() && ev2->IsAOD() ) {
260 while ( (o=ev1N()) ) {
261 idx = ev1->GetTrackArrayN()->IndexOf(o);
262 ((AliAODTrack *)o)->SetProdVertex(ev1->GetVertexArrayN()->At(idx));
264 while ( (o=ev1P()) ) {
265 idx = ev1->GetTrackArrayP()->IndexOf(o);
266 ((AliAODTrack *)o)->SetProdVertex(ev1->GetVertexArrayP()->At(idx));
268 while ( (o=ev2N()) ) {
269 idx = ev2->GetTrackArrayN()->IndexOf(o);
270 ((AliAODTrack *)o)->SetProdVertex(ev2->GetVertexArrayN()->At(idx));
272 while ( (o=ev2P()) ) {
273 idx = ev2->GetTrackArrayP()->IndexOf(o);
274 ((AliAODTrack *)o)->SetProdVertex(ev2->GetVertexArrayP()->At(idx));
277 // reset the iterators
284 //mixing of ev1- ev2+ (pair type4). This is common for all mixing types
285 while ( (o=ev1N()) ) diele->fTracks[1].Add(o);
286 while ( (o=ev2P()) ) diele->fTracks[2].Add(o);
287 diele->FillPairArrays(1,2);
289 if (fMixType==kAll || fMixType==kOSandLS){
290 // all 4 pair arrays will be filled
291 while ( (o=ev1P()) ) diele->fTracks[0].Add(o);
292 while ( (o=ev2N()) ) diele->fTracks[3].Add(o);
293 diele->FillPairArrays(0,2);
294 diele->FillPairArrays(1,3);
295 if (fMixType==kAll) diele->FillPairArrays(0,3);
298 if (fMixType==kOSonly || fMixType==kOSandLS){
299 //use the pair type of ev1- ev1+ also for ev1+ ev1-
300 diele->fTracks[1].Clear();
301 diele->fTracks[2].Clear();
302 while ( (o=ev1P()) ) diele->fTracks[1].Add(o);
303 while ( (o=ev2N()) ) diele->fTracks[2].Add(o);
304 diele->FillPairArrays(1,2);
310 //copy back the tracks
311 for (Int_t i=0; i<4; ++i) {
312 diele->fTracks[i].Clear();
313 diele->fTracks[i]=arrTrDummy[i];
316 //set back global event values
317 AliDielectronVarManager::SetEventData(values);
320 //______________________________________________
321 void AliDielectronMixingHandler::MixRemaining(AliDielectron *diele)
324 // mix all pools even if they are incomplete
327 //Check if there was any processed data and it is requested to mix incomplete bins
328 if (!diele || !diele->PairArray(0) || !fMixIncomplete ) return;
330 AliDielectronVarManager::SetEvent(0x0);
331 for (Int_t ipool=0; ipool<fArrPools.GetSize(); ++ipool){
332 TClonesArray *poolp=static_cast<TClonesArray*>(fArrPools.At(ipool));
333 if (!poolp || !poolp->GetEntriesFast() || !poolp->At(0)) continue;
334 //clear the arrays before the final processing"
335 AliDebug(10,Form("Incomplete: Bin %d (%d)\n",ipool,poolp->GetEntriesFast()));
336 diele->ClearArrays();
337 DoMixing(*poolp,diele);
339 // increase counter for incomplete bins
340 if (diele->fHistos) {
341 //buffer event data and set event data using the first event in this pool
342 Double_t values[AliDielectronVarManager::kNMaxValues]={0};
343 for (Int_t i=AliDielectronVarManager::kPairMax; i<AliDielectronVarManager::kNMaxValues; ++i)
344 values[i]=AliDielectronVarManager::GetValue((AliDielectronVarManager::ValueTypes)i);
346 AliDielectronEvent *ev1=static_cast<AliDielectronEvent*>(poolp->At(0));
347 //use event data from the first event all events are in the same mixing bin anyhow...
348 AliDielectronVarManager::SetEventData(ev1->GetEventData());
350 // fill the histograms
351 diele->FillHistograms(0x0, kTRUE);
352 diele->fHistos->Fill("Mixing","Stats",1);
353 diele->fHistos->Fill("Mixing","InCompletePools",ipool);
354 diele->fHistos->Fill("Mixing","Entries_InCompletePools",poolp->GetEntriesFast());
356 //set back global event values
357 AliDielectronVarManager::SetEventData(values);
364 //______________________________________________
365 void AliDielectronMixingHandler::Init(const AliDielectron *diele)
368 // initialise event buffers
371 Int_t size=GetNumberOfBins();
373 AliDebug(10,Form("Creating a pool array with size %d \n",size));
375 fArrPools.Expand(size);
377 //add statics histogram if we have a histogram manager
378 if (diele && diele->fHistos) {
379 diele->fHistos->AddClass("Mixing");
380 diele->fHistos->UserHistogram("Mixing","Stats","Mixing Statistics;;#called bins",2,0,2);
381 TH1* h=diele->fHistos->GetHistogram("Mixing","Stats");
382 h->GetXaxis()->SetBinLabel(1,"Complete");
383 h->GetXaxis()->SetBinLabel(2,"Incomplete");
385 diele->fHistos->UserHistogram("Mixing","CompletePools","Mixing Statistics compete pools;bin;#fills",size,0,size);
386 diele->fHistos->UserHistogram("Mixing","InCompletePools","Mixing Statistics incomplete pools;bin;#fills",size,0,size);
387 diele->fHistos->UserHistogram("Mixing","Entries_InCompletePools","#entries in incomplete pools;entries;#fills",fDepth,0,fDepth);
391 for (Int_t i=0; i<fAxes.GetEntriesFast(); ++i){
392 TVectorD *bins=static_cast<TVectorD*>(fAxes.At(i));
393 Int_t nRows=bins->GetNrows();
394 values+=Form("%s: ",AliDielectronVarManager::GetValueName(fEventCuts[i]));
395 for (Int_t irow=0; irow<nRows; ++irow){
396 values+=Form("%.2f, ",(*bins)[irow]);
400 AliDebug(10,values.Data());
403 //______________________________________________
404 Int_t AliDielectronMixingHandler::GetNumberOfBins() const
407 // return the number of bins this mixing handler has
410 for (Int_t i=0; i<fAxes.GetEntriesFast(); ++i)
411 size*=((static_cast<TVectorD*>(fAxes.At(i)))->GetNrows()-1);
415 //______________________________________________
416 Int_t AliDielectronMixingHandler::FindBin(const Double_t values[], TString *dim)
419 // bin bin in mixing stack described by 'values'
420 // if the values are outside the binning range -1 is returned
421 // if dim is non NULL debug info will be stored in the variable
424 if (fAxes.GetEntriesFast()==0) {
425 if (dim) (*dim)="single bin";
431 for (Int_t i=0; i<fAxes.GetEntriesFast(); ++i){
432 Double_t val=values[fEventCuts[i]];
433 TVectorD *bins=static_cast<TVectorD*>(fAxes.At(i));
434 Int_t nRows=bins->GetNrows();
435 if ( (val<(*bins)[0]) || (val>(*bins)[nRows-1]) ) {
439 Int_t pos=TMath::BinarySearch(nRows,bins->GetMatrixArray(),val);
441 if (dim) (*dim)+=Form("%s: %f (%d); ",AliDielectronVarManager::GetValueName(fEventCuts[i]),val,pos);
448 //______________________________________________
449 void AliDielectronMixingHandler::MoveToSameVertex(AliVTrack * const vtrack, const Double_t *vFirst, const Double_t* vMix)
452 // move 'track' which belongs to the vertex information of vMix to the vertex of vFirst
455 static Bool_t printed=kFALSE;
457 if (vtrack->IsA()==AliESDtrack::Class()){
458 AliESDtrack *track=(AliESDtrack*)vtrack;
460 //get track information
461 Double_t x = track->GetX();
462 Double_t alpha = track->GetAlpha();
463 Double_t param[5] = {0};
464 Double_t cov[15] = {0};
466 for (Int_t i=0; i<5; ++i) param[i]=track->GetParameter()[i];
467 for (Int_t i=0; i<15; ++i) cov[i] =track->GetCovariance()[i];
470 Double_t vt[3] = {vMix[0]-vFirst[0],vMix[1]-vFirst[1],vMix[2]-vFirst[2]};
471 //rotate to the track frame
472 // track->Global2LocalPosition(vt,track->GetAlpha());
474 //add to track position
476 // param[0] = param[0]-vt[1];
477 // param[1] = param[1]-vt[2];
478 param[1] = param[1]-vt[2];
480 //set updated track information
481 track->Set(x, alpha, param, cov);
483 // AliAODTrack *track=(AliAODTrack*)vtrack;
484 // Double_t pos[3]={0};
485 // track->GetPosition(pos);
486 // if (pos[0]>-999.){
487 // pos[0]=pos[0]-vMix[AliDielectronVarManager::kXvPrim]+vFirst[AliDielectronVarManager::kXvPrim];
488 // pos[1]=pos[1]-vMix[AliDielectronVarManager::kYvPrim]+vFirst[AliDielectronVarManager::kYvPrim];
489 // pos[2]=pos[2]-vMix[AliDielectronVarManager::kZvPrim]+vFirst[AliDielectronVarManager::kZvPrim];
490 // track->SetPosition(pos);
491 // AliError("Move To same vertex not yet implemented for AOD!");
493 // Error("AliDielectronMixingHandler::MoveToSameVertex","Move To same vertex not yet implemented for AOD!");
497 // Not that clear how to do it. In AOD track there is fPosition, fPositionAtDCA and "TRef fProdVertex"
498 // where Xv(), Yv(), Zv() returns the position of fProdVertex