]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGDQ/dielectron/AliDielectronMixingHandler.cxx
ync gsi and aliroot trunk
[u/mrichter/AliRoot.git] / PWGDQ / dielectron / AliDielectronMixingHandler.cxx
1 /*************************************************************************
2 * Copyright(c) 1998-2009, 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 //                Dielectron MixingHandler                                  //
18 //                                                                       //
19 //                                                                       //
20 /*
21 Detailed description
22
23
24 */
25 //                                                                       //
26 ///////////////////////////////////////////////////////////////////////////
27
28 #include <TVectorD.h>
29 #include <TH1.h>
30 #include <TAxis.h>
31
32 #include <AliLog.h>
33 #include <AliVTrack.h>
34
35 #include "AliDielectron.h"
36 #include "AliDielectronHelper.h"
37 #include "AliDielectronHistos.h"
38 #include "AliDielectronEvent.h"
39
40 #include "AliDielectronMixingHandler.h"
41
42 ClassImp(AliDielectronMixingHandler)
43
44 AliDielectronMixingHandler::AliDielectronMixingHandler() :
45   TNamed(),
46   fDepth(10),
47   fArrPools("TClonesArray"),
48   fAxes(kMaxCuts),
49   fMixType(kOSonly),
50   fMixIncomplete(kTRUE),
51   fMoveToSameVertex(kFALSE)
52 {
53   //
54   // Default Constructor
55   //
56   for (Int_t i=0; i<kMaxCuts; ++i){
57     fEventCuts[i]=0;
58   }
59   fAxes.SetOwner(kTRUE);
60 }
61
62 //______________________________________________
63 AliDielectronMixingHandler::AliDielectronMixingHandler(const char* name, const char* title) :
64   TNamed(name, title),
65   fDepth(10),
66   fArrPools("TClonesArray"),
67   fAxes(kMaxCuts),
68   fMixType(kOSonly),
69   fMixIncomplete(kTRUE),
70   fMoveToSameVertex(kFALSE)
71 {
72   //
73   // Named Constructor
74   //
75   for (Int_t i=0; i<kMaxCuts; ++i){
76     fEventCuts[i]=0;
77   }
78   fAxes.SetOwner(kTRUE);
79 }
80
81 //______________________________________________
82 AliDielectronMixingHandler::~AliDielectronMixingHandler()
83 {
84   //
85   // Default Destructor
86   //
87   fAxes.Delete();
88 }
89
90 //________________________________________________________________
91 void AliDielectronMixingHandler::AddVariable(AliDielectronVarManager::ValueTypes type,
92                                              Int_t nbins, Double_t min, Double_t max, Bool_t log)
93 {
94   //
95   // Add a variable to the mixing handler
96   //
97
98   // limit number of variables to kMaxCuts
99   if (fAxes.GetEntriesFast()>=kMaxCuts) return;
100   
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;
105
106   Int_t size=fAxes.GetEntriesFast();
107   fEventCuts[size]=(UShort_t)type;
108   fAxes.Add(binLimits);
109 }
110
111 //________________________________________________________________
112 void AliDielectronMixingHandler::AddVariable(AliDielectronVarManager::ValueTypes type,
113                                              const char* binLimitStr)
114 {
115   //
116   // Add a variable to the mixing handler with arbitrary binning
117   //
118
119   // limit number of variables to kMaxCuts
120   if (fAxes.GetEntriesFast()>=kMaxCuts) return;
121   
122   TVectorD *binLimits=AliDielectronHelper::MakeArbitraryBinning(binLimitStr);
123   if (!binLimits) return;
124   
125   Int_t size=fAxes.GetEntriesFast();
126   fEventCuts[size]=(UShort_t)type;
127   fAxes.Add(binLimits);
128 }
129
130 //______________________________________________
131 void AliDielectronMixingHandler::Fill(const AliVEvent *ev, AliDielectron *diele)
132 {
133   //
134   // fill event buffers and perform mixing if the pool depth is reached
135   //
136
137   //check if there are tracks available
138   if (diele->GetTrackArray(0)->GetEntriesFast()==0 && diele->GetTrackArray(1)->GetEntriesFast()==0) return;
139
140   //find mixing bin
141   Double_t values[AliDielectronVarManager::kNMaxValues]={0.};
142   AliDielectronVarManager::Fill(ev,values);
143
144   TString dim;
145   Int_t bin=FindBin(values,&dim);
146
147   //add mixing bin to event data
148   values[AliDielectronVarManager::kMixingBin] = bin;
149
150   if (bin<0){
151     AliDebug(5,Form("Bin outside range: %s",dim.Data()));
152     return;
153   }
154
155   // get mixing pool, create it if it does not yet exist.
156   TClonesArray *poolp=static_cast<TClonesArray*>(fArrPools.At(bin));
157   if (!poolp){
158     AliDebug(10,Form("New pool at %d (%s)\n",bin,dim.Data()));
159     poolp=new(fArrPools[bin]) TClonesArray("AliDielectronEvent",fDepth);
160   }
161   TClonesArray &pool=*poolp;
162
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   else event->SetESD();
167
168   event->SetTracks(*diele->GetTrackArray(0), *diele->GetTrackArray(1), *diele->GetPairArray(1));
169   event->SetEventData(values);
170
171   // check if pool depth is reached.
172   if (pool.GetEntriesFast()<fDepth) return;
173
174   // pool depth reached, do mixing
175   DoMixing(pool,diele);
176
177   // increase counter for full bins
178   if (diele->fHistos) {
179     diele->fHistos->Fill("Mixing","Stats",0);
180     diele->fHistos->Fill("Mixing","CompletePools",bin);
181   }
182   
183   //clear the current pool
184   pool.Clear("C");
185 }
186
187 //______________________________________________
188 void AliDielectronMixingHandler::DoMixing(TClonesArray &pool, AliDielectron *diele)
189 {
190   //
191   // perform the mixing
192   //
193
194   //buffer track arrays and copy them back afterwards
195   TObjArray arrTrDummy[4];
196   for (Int_t i=0; i<4; ++i) arrTrDummy[i]=diele->fTracks[i];
197
198   //buffer also global event data
199   Double_t values[AliDielectronVarManager::kNMaxValues]={0};
200   for (Int_t i=AliDielectronVarManager::kPairMax; i<AliDielectronVarManager::kNMaxValues; ++i)
201     values[i]=AliDielectronVarManager::GetValue((AliDielectronVarManager::ValueTypes)i);
202
203   for (Int_t i1=0; i1<pool.GetEntriesFast(); ++i1){
204     AliDielectronEvent *ev1=static_cast<AliDielectronEvent*>(pool.At(i1));
205     //use event data from the first event
206     //both events are in the same mixing bin anyhow...
207     AliDielectronVarManager::SetEventData(ev1->GetEventData());
208
209     TObject *o=0x0;
210     TIter ev1P(ev1->GetTrackArrayP());
211     TIter ev1N(ev1->GetTrackArrayN());
212     
213     for (Int_t i2=i1+1; i2<pool.GetEntriesFast(); ++i2){
214       //clear arryas
215       diele->fTracks[0].Clear();
216       diele->fTracks[1].Clear();
217       diele->fTracks[2].Clear();
218       diele->fTracks[3].Clear();
219       
220       //setup track arrays
221       AliDielectronEvent *ev2=static_cast<AliDielectronEvent*>(pool.At(i2));
222       ev1P.Reset();
223       ev1N.Reset();
224       TIter ev2P(ev2->GetTrackArrayP());
225       TIter ev2N(ev2->GetTrackArrayN());
226
227       //
228       //move tracks to the same vertex (vertex of the first event), if requested
229       //
230       if (fMoveToSameVertex){
231         const Double_t *varsFirst=ev1->GetEventData();
232         const Double_t *varsMix=ev2->GetEventData();
233
234         const Double_t vFirst[3]={varsFirst[AliDielectronVarManager::kXvPrim],
235                                   varsFirst[AliDielectronVarManager::kYvPrim],
236                                   varsFirst[AliDielectronVarManager::kZvPrim]};
237
238         const Double_t vMix[3]  ={varsMix[AliDielectronVarManager::kXvPrim],
239                                   varsMix[AliDielectronVarManager::kYvPrim],
240                                   varsMix[AliDielectronVarManager::kZvPrim]};
241                                   
242         //loop over all tracks from the second event and move them to the vertex of the first
243         AliVTrack *vtrack=0x0;
244         while ( ( vtrack=(AliVTrack*)ev2P() ) ){
245           MoveToSameVertex(vtrack, vFirst, vMix);
246         }
247
248         
249         while ( ( vtrack=(AliVTrack*)ev2N() ) ){
250           MoveToSameVertex(vtrack, vFirst, vMix);
251         }
252
253         
254         ev2P.Reset();
255         ev2N.Reset();
256       }
257
258       //mixing of ev1- ev2+ (pair type4). This is common for all mixing types
259       while ( (o=ev1N()) ) diele->fTracks[1].Add(o); 
260       while ( (o=ev2P()) ) diele->fTracks[2].Add(o);
261       diele->FillPairArrays(1,2);
262       
263       if (fMixType==kAll || fMixType==kOSandLS){
264         // all 4 pair arrays will be filled
265         while ( (o=ev1P()) ) diele->fTracks[0].Add(o);
266         while ( (o=ev2N()) ) diele->fTracks[3].Add(o);
267         diele->FillPairArrays(0,2);
268         diele->FillPairArrays(1,3);
269         if (fMixType==kAll) diele->FillPairArrays(0,3);
270       }
271       
272       if (fMixType==kOSonly || fMixType==kOSandLS){
273         //use the pair type of ev1- ev1+ also for ev1+ ev1-
274         diele->fTracks[1].Clear();
275         diele->fTracks[2].Clear();
276         while ( (o=ev1P()) ) diele->fTracks[1].Add(o);
277         while ( (o=ev2N()) ) diele->fTracks[2].Add(o);
278         diele->FillPairArrays(1,2);
279       }
280
281     }
282   }
283
284   //copy back the tracks
285   for (Int_t i=0; i<4; ++i) {
286     diele->fTracks[i].Clear();
287     diele->fTracks[i]=arrTrDummy[i];
288   }
289
290   //set back global event values
291   AliDielectronVarManager::SetEventData(values);
292 }
293
294 //______________________________________________
295 void AliDielectronMixingHandler::MixRemaining(AliDielectron *diele)
296 {
297   //
298   // mix all pools even if they are incomplete
299   //
300
301   //Check if there was any processed data and it is requested to mix incomplete bins
302   if (!diele || !diele->PairArray(0) || !fMixIncomplete ) return;
303
304   AliDielectronVarManager::SetEvent(0x0);
305   for (Int_t ipool=0; ipool<fArrPools.GetSize(); ++ipool){
306     TClonesArray *poolp=static_cast<TClonesArray*>(fArrPools.At(ipool));
307     if (!poolp || !poolp->GetEntriesFast() || !poolp->At(0)) continue;
308     //clear the arrays before the final processing"
309     AliDebug(10,Form("Incomplete: Bin %d (%d)\n",ipool,poolp->GetEntriesFast()));
310     diele->ClearArrays();
311     DoMixing(*poolp,diele);
312
313     // increase counter for incomplete bins
314     if (diele->fHistos) {
315       //buffer event data and set event data using the first event in this pool
316       Double_t values[AliDielectronVarManager::kNMaxValues]={0};
317       for (Int_t i=AliDielectronVarManager::kPairMax; i<AliDielectronVarManager::kNMaxValues; ++i)
318         values[i]=AliDielectronVarManager::GetValue((AliDielectronVarManager::ValueTypes)i);
319       
320       AliDielectronEvent *ev1=static_cast<AliDielectronEvent*>(poolp->At(0));
321       //use event data from the first event all events are in the same mixing bin anyhow...
322       AliDielectronVarManager::SetEventData(ev1->GetEventData());
323       
324       // fill the histograms
325       diele->FillHistograms(0x0, kTRUE);
326       diele->fHistos->Fill("Mixing","Stats",1);
327       diele->fHistos->Fill("Mixing","InCompletePools",ipool);
328       diele->fHistos->Fill("Mixing","Entries_InCompletePools",poolp->GetEntriesFast());
329       
330       //set back global event values
331       AliDielectronVarManager::SetEventData(values);
332     }
333     
334   }
335 }
336
337
338 //______________________________________________
339 void AliDielectronMixingHandler::Init(const AliDielectron *diele)
340 {
341   //
342   // initialise event buffers
343   //
344
345   Int_t size=GetNumberOfBins();
346
347   AliDebug(10,Form("Creating a pool array with size %d \n",size));
348
349   fArrPools.Expand(size);
350
351   //add statics histogram if we have a histogram manager
352   if (diele && diele->fHistos) {
353     diele->fHistos->AddClass("Mixing");
354     diele->fHistos->UserHistogram("Mixing","Stats","Mixing Statistics;;#called bins",2,0,2);
355     TH1* h=diele->fHistos->GetHistogram("Mixing","Stats");
356     h->GetXaxis()->SetBinLabel(1,"Complete");
357     h->GetXaxis()->SetBinLabel(2,"Incomplete");
358
359     diele->fHistos->UserHistogram("Mixing","CompletePools","Mixing Statistics compete pools;bin;#fills",size,0,size);
360     diele->fHistos->UserHistogram("Mixing","InCompletePools","Mixing Statistics incomplete pools;bin;#fills",size,0,size);
361     diele->fHistos->UserHistogram("Mixing","Entries_InCompletePools","#entries in incomplete pools;entries;#fills",fDepth,0,fDepth);
362   }
363
364   TString values;
365   for (Int_t i=0; i<fAxes.GetEntriesFast(); ++i){
366     TVectorD *bins=static_cast<TVectorD*>(fAxes.At(i));
367     Int_t nRows=bins->GetNrows();
368     values+=Form("%s: ",AliDielectronVarManager::GetValueName(fEventCuts[i]));
369     for (Int_t irow=0; irow<nRows; ++irow){
370       values+=Form("%.2f, ",(*bins)[irow]);
371     }
372   }
373
374   AliDebug(10,values.Data());
375 }
376
377 //______________________________________________
378 Int_t AliDielectronMixingHandler::GetNumberOfBins() const
379 {
380   //
381   // return the number of bins this mixing handler has
382   //
383   Int_t size=1;
384   for (Int_t i=0; i<fAxes.GetEntriesFast(); ++i)
385     size*=((static_cast<TVectorD*>(fAxes.At(i)))->GetNrows()-1);
386   return size;
387 }
388
389 //______________________________________________
390 Int_t AliDielectronMixingHandler::FindBin(const Double_t values[], TString *dim)
391 {
392   //
393   // bin bin in mixing stack described by 'values'
394   // if the values are outside the binning range -1 is returned
395   // if dim is non NULL debug info will be stored in the variable
396   //
397
398   if (fAxes.GetEntriesFast()==0) {
399     if (dim) (*dim)="single bin";
400     return 0;
401   }
402   if (dim) (*dim)="";
403   Int_t sizeAdd=1;
404   Int_t bin=0;
405   for (Int_t i=0; i<fAxes.GetEntriesFast(); ++i){
406     Double_t val=values[fEventCuts[i]];
407     TVectorD *bins=static_cast<TVectorD*>(fAxes.At(i));
408     Int_t nRows=bins->GetNrows();
409     if ( (val<(*bins)[0]) || (val>(*bins)[nRows-1]) ) {
410       return -1;
411     }
412
413     Int_t pos=TMath::BinarySearch(nRows,bins->GetMatrixArray(),val);
414     bin+=sizeAdd*pos;
415     if (dim) (*dim)+=Form("%s: %f (%d); ",AliDielectronVarManager::GetValueName(fEventCuts[i]),val,pos);
416     sizeAdd*=(nRows-1);
417   }
418
419   return bin;
420 }
421
422 //______________________________________________
423 void AliDielectronMixingHandler::MoveToSameVertex(AliVTrack * const vtrack, const Double_t *vFirst, const Double_t* vMix)
424 {
425   //
426   // move 'track' which belongs to the vertex information of vMix to the vertex of vFirst
427   //
428
429   static Bool_t printed=kFALSE;
430   
431   if (vtrack->IsA()==AliESDtrack::Class()){
432     AliESDtrack *track=(AliESDtrack*)vtrack;
433
434     //get track information
435     Double_t x        = track->GetX();
436     Double_t alpha    = track->GetAlpha();
437     Double_t param[5] = {0};
438     Double_t cov[15]  = {0};
439
440     for (Int_t i=0; i<5;  ++i) param[i]=track->GetParameter()[i];
441     for (Int_t i=0; i<15; ++i) cov[i]  =track->GetCovariance()[i];
442
443     //translation
444     Double_t vt[3] = {vMix[0]-vFirst[0],vMix[1]-vFirst[1],vMix[2]-vFirst[2]};
445     //rotate to the track frame
446 //     track->Global2LocalPosition(vt,track->GetAlpha());
447
448     //add to track position
449 //     x        = x       -vt[0];
450 //     param[0] = param[0]-vt[1];
451 //     param[1] = param[1]-vt[2];
452     param[1] = param[1]-vt[2];
453     
454     //set updated track information
455     track->Set(x, alpha, param, cov);
456   } else {
457     //             AliAODTrack *track=(AliAODTrack*)vtrack;
458     //             Double_t pos[3]={0};
459     //             track->GetPosition(pos);
460     //             if (pos[0]>-999.){
461       //               pos[0]=pos[0]-vMix[AliDielectronVarManager::kXvPrim]+vFirst[AliDielectronVarManager::kXvPrim];
462       //               pos[1]=pos[1]-vMix[AliDielectronVarManager::kYvPrim]+vFirst[AliDielectronVarManager::kYvPrim];
463       //               pos[2]=pos[2]-vMix[AliDielectronVarManager::kZvPrim]+vFirst[AliDielectronVarManager::kZvPrim];
464       //               track->SetPosition(pos);
465 //       AliError("Move To same vertex not yet implemented for AOD!");
466     if (!printed) {
467 //       Error("AliDielectronMixingHandler::MoveToSameVertex","Move To same vertex not yet implemented for AOD!");
468       printed=kTRUE;
469     }
470       //
471       // Not that clear how to do it. In AOD track there is fPosition, fPositionAtDCA and "TRef fProdVertex"
472       // where Xv(), Yv(), Zv() returns the position of fProdVertex
473       //
474     }
475     
476 }