]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGGA/PHOSTasks/ClusterSelection/AliPHOSClusterSelection.cxx
minor fix, removed fCoreRadius
[u/mrichter/AliRoot.git] / PWGGA / PHOSTasks / ClusterSelection / AliPHOSClusterSelection.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, 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 /* $Id$ */
17
18 #include "AliAnalysisManager.h"
19 #include "AliInputEventHandler.h"
20 #include "AliMultiInputEventHandler.h"
21 #include "AliVEvent.h"
22 #include "AliESDEvent.h"
23 #include "AliAODEvent.h"
24 #include "AliESDCaloCluster.h"
25 #include "AliVParticle.h"
26 #include "AliVTrack.h"
27 #include "AliAODCluster.h"
28 #include "AliVCaloCells.h"
29 #include "AliOADBContainer.h"
30 #include "AliPHOSGeometry.h"
31 #include "AliAnalysisManager.h"
32
33
34
35 #include "AliPHOSClusterSelection.h"
36
37 AliPHOSClusterSelection::AliPHOSClusterSelection()
38   : fMinChargedParticleTrackDistance(-1.), 
39     fNotUnfolded(false),
40     fMaxDispR2(-1.),
41     fIsCore(-1.),
42     fMaxTOF(-1.)
43 {
44   // Defaults to the most lenient selection allowable
45         return;
46 }
47
48 AliPHOSClusterSelection::~AliPHOSClusterSelection()
49 {
50 }
51
52 Bool_t AliPHOSClusterSelection::IsSelected(AliVCluster* cluster) const
53 {
54   return IsSelectedCPV(cluster)
55     && IsSelectedUnfolded(cluster)
56     && IsSelectedDisp(cluster)
57     && IsSelectedTOF(cluster);
58 }
59
60 Bool_t AliPHOSClusterSelection::IsSelectedCPV(AliVCluster* cluster) const
61 {
62   if( 0 > fMinChargedParticleTrackDistance )//No selection on CPV
63     return true; 
64
65   else{
66     Bool_t cpvBit = true; //Changed to false if there is a matching track in the requested radius.
67
68     Double_t dx=cluster->GetTrackDx();//Position for Track matching
69     Double_t dz=cluster->GetTrackDz();
70
71
72     AliESDEvent* EventESD = dynamic_cast<AliESDEvent*> (AliPHOSClusterSelection::GetCurrentEvent());//dynamic cast to test for ESD or AOD
73     AliAODEvent* EventAOD = dynamic_cast<AliAODEvent*> (AliPHOSClusterSelection::GetCurrentEvent());
74     Double_t mf = 0.; //
75     if(EventAOD) mf = EventAOD->GetMagneticField(); //Positive for ++ and negative for -- 
76     else if(EventESD) mf = EventESD->GetMagneticField(); //Positive for ++ and negative for --
77
78
79     if(EventESD){//if ESD
80       AliESDCaloCluster * ESDcluster = static_cast<AliESDCaloCluster*> (cluster);//Know its ESD so static cast is fine?
81       TArrayI * itracks = ESDcluster-> GetTracksMatched() ;
82       if(itracks->GetSize()>0){
83         Int_t iTr = itracks->At(0);
84         if(iTr>=0 && iTr<EventESD->GetNumberOfTracks()){
85           AliVParticle* track = EventESD->GetTrack(iTr);
86           Double_t pt = track->Pt() ;
87           Short_t charge = track->Charge() ;
88           Double_t r = AliPHOSClusterSelection::TestCPV(dx, dz, pt, charge, mf);
89           cpvBit=(r>fMinChargedParticleTrackDistance) ;
90         }
91       }
92     }
93
94
95     if(EventAOD){//if AOD
96       AliAODCluster * AODcluster = static_cast<AliAODCluster*> (cluster);
97       int nTracksMatched = AODcluster->GetNTracksMatched();
98       if(nTracksMatched > 0) {
99         AliVTrack* track = dynamic_cast<AliVTrack*> (cluster->GetTrackMatched(0));
100         if ( track ) {
101           Double_t pt = track->Pt();
102           Short_t charge = track->Charge();
103           Double_t r = AliPHOSClusterSelection::TestCPV(dx, dz, pt, charge, mf);
104           cpvBit=(r>fMinChargedParticleTrackDistance) ;
105         }
106       }
107     }
108     return cpvBit;
109   }
110 }
111
112
113 Bool_t AliPHOSClusterSelection::IsSelectedUnfolded(AliVCluster* cluster) const
114 {
115   if(!fNotUnfolded)
116     return true;
117   else{
118     Bool_t NotUnfolded = cluster->GetNExMax()<2;//True if it was not unfolded
119     return !NotUnfolded;
120   }
121 }
122
123 Bool_t AliPHOSClusterSelection::IsSelectedDisp(AliVCluster* cluster) const
124 {
125   if(0 > fMaxDispR2)
126     return true;
127   else{
128     Double_t m02 = 0.,m20 = 0.;
129     if(fIsCore<0){//No core calculation
130       m02 = cluster->GetM02();
131       m20 = cluster->GetM20();
132       return AliPHOSClusterSelection::TestLambda(cluster->E(),m20,m02) ;
133     }
134     else{//DispCore
135       AliVCaloCells* cells = static_cast<AliVCaloCells*> (AliPHOSClusterSelection::GetCurrentEvent()->GetPHOSCells());//Need the cells
136       AliPHOSClusterSelection::EvalCoreLambdas(cluster, cells, m02, m20);
137       return AliPHOSClusterSelection::TestLambda(cluster->E(),m20,m02);
138     }
139   }
140
141 }
142
143 Bool_t AliPHOSClusterSelection::IsSelectedTOF(AliVCluster* cluster) const
144 {
145   if(0 > fMaxTOF)
146     return true;
147   else{ 
148     // Time of Flight (TOF)
149     Double_t tof = cluster->GetTOF();//Time of Flight for the cluster
150     return  TMath::Abs(tof) < fMaxTOF;//True if the cut is passed
151   }
152 }
153
154 AliPHOSClusterSelection* AliPHOSClusterSelection::SetMinChargedParticleTrackDistance(Float_t distance)
155 {
156   // 'distance' set the minimal allowable distance between the cluster
157   // and the nearest extrapolated track.
158   // If 'distance' is negative, then all clusters are sellected, the selection
159   // being "not applied" or "disabled".
160   
161   fMinChargedParticleTrackDistance = distance;
162   return this;
163 }
164
165 AliPHOSClusterSelection* AliPHOSClusterSelection::SetNotUnfolded(Bool_t notUnfolded)
166 {
167   //if notUnfolded true, it rejects Unfolded Clusters
168   fNotUnfolded = notUnfolded;
169   return this;
170 }
171
172 AliPHOSClusterSelection* AliPHOSClusterSelection::SetMaxDispR2(Float_t maxR2)
173 {
174   // 'maxR2' sets the maximum allowed dispersion.
175   // If 'maxR2' is negative, then all clusters are selected, the selection
176   // being "not applied" or "disabled".
177   
178   fMaxDispR2 = maxR2;
179   return this;
180 }
181
182 AliPHOSClusterSelection* AliPHOSClusterSelection::SetIsCore(Bool_t isCore)
183 {
184   // 'isCore' sets wether core version of Disp is used. -1 gives no core.
185   
186   fIsCore = isCore;
187   return this;
188 }
189
190 AliPHOSClusterSelection* AliPHOSClusterSelection::SetMaxTOF(Float_t maxTOF)
191 {
192   // 'maxTOF' sets the maximum allowed time of flight for the cluster.
193   // If 'maxTOF' is negative, all clusters are selected and the selection is "disabled".
194   
195   fMaxTOF = maxTOF;
196   return this;
197 }
198
199 TString AliPHOSClusterSelection::ToString() const
200 {
201   // returns a string an quasi-unique string for whatever selection 
202   // parameters the instance contains. The uniqueness of the string
203   // is limited by the precision given in the formatting of the string.
204   // Take care that the precision is sufficient for your needs.
205
206   return TString::Format("%.1f_%i_%.1f_%.1f_%.1f",
207                          fMinChargedParticleTrackDistance,
208                          fNotUnfolded,
209                          fMaxDispR2,
210                          fMaxTOF
211                          );
212 }
213
214
215 Float_t AliPHOSClusterSelection::GetMinChargedParticleTrackDistance(const TString& string)
216 {
217   TString * s =&(static_cast<TObjString*>(string.Tokenize("_")->At(0))->String());
218   Float_t flt= s->Atof();
219   //delete s; //s is defined in the stack, no delete necessary
220   return flt;
221 }
222
223 Bool_t AliPHOSClusterSelection::GetUnfolded(const TString& string)
224 {
225   TString * s =&(static_cast<TObjString*>(string.Tokenize("_")->At(1))->String());
226   Bool_t bl = s->Atoi(); 
227   return bl;
228 }
229
230 Float_t AliPHOSClusterSelection::GetMaxDispR2(const TString& string)
231 {
232   TString * s =&(static_cast<TObjString*>(string.Tokenize("_")->At(2))->String());
233   Float_t flt = s->Atof(); 
234   return flt;
235 }
236
237 Bool_t AliPHOSClusterSelection::GetIsCore(const TString& string)
238 {
239   TString * s =&(static_cast<TObjString*>(string.Tokenize("_")->At(3))->String());
240   Bool_t blt = s->Atoi(); 
241   return blt;
242 }
243
244 Float_t AliPHOSClusterSelection::GetMaxTOF(const TString& string)
245 {
246   TString * s =&(static_cast<TObjString*>(string.Tokenize("_")->At(4))->String());
247   Float_t flt = s->Atof(); 
248   return flt;
249 }
250
251
252 Double_t AliPHOSClusterSelection::TestCPV(Double_t dx, Double_t dz, Double_t pt, Int_t charge, Double_t mf) const {
253   //Parameterization of LHC10h period
254   //_true if neutral_
255   //Copied from Pi0Flow task
256
257   Double_t meanX=0;
258   Double_t meanZ=0.;
259   Double_t sx=TMath::Min(5.4,2.59719e+02*TMath::Exp(-pt/1.02053e-01)+
260                          6.58365e-01*5.91917e-01*5.91917e-01/((pt-9.61306e-01)*(pt-9.61306e-01)+5.91917e-01*5.91917e-01)+1.59219);
261   Double_t sz=TMath::Min(2.75,4.90341e+02*1.91456e-02*1.91456e-02/(pt*pt+1.91456e-02*1.91456e-02)+1.60) ;
262  
263   if(mf<0.){ //field --
264     meanZ = -0.468318 ;
265     if(charge>0)
266       meanX=TMath::Min(7.3, 3.89994*1.20679*1.20679/(pt*pt+1.20679*1.20679)+0.249029+2.49088e+07*TMath::Exp(-pt*3.33650e+01)) ;
267     else
268       meanX=-TMath::Min(7.7,3.86040*0.912499*0.912499/(pt*pt+0.912499*0.912499)+1.23114+4.48277e+05*TMath::Exp(-pt*2.57070e+01)) ;
269   }
270   else{ //Field ++
271     meanZ= -0.468318;
272     if(charge>0)
273       meanX=-TMath::Min(8.0,3.86040*1.31357*1.31357/(pt*pt+1.31357*1.31357)+0.880579+7.56199e+06*TMath::Exp(-pt*3.08451e+01)) ;
274     else
275       meanX= TMath::Min(6.85, 3.89994*1.16240*1.16240/(pt*pt+1.16240*1.16240)-0.120787+2.20275e+05*TMath::Exp(-pt*2.40913e+01)) ;
276   }
277
278   Double_t rz=(dz-meanZ)/sz ;
279   Double_t rx=(dx-meanX)/sx ;
280   return TMath::Sqrt(rx*rx+rz*rz) ;
281 }
282
283 //____________________________________________________________________________
284 void  AliPHOSClusterSelection::EvalCoreLambdas(AliVCluster  * clu, AliVCaloCells * cells,Double_t &m02, Double_t &m20) const
285
286   //calculate dispecrsion of the cluster in the circle with radius distanceCut around the maximum
287   //Copied from pi0flowtask
288   Int_t fRunNumber;
289   const Double32_t * elist = clu->GetCellsAmplitudeFraction() ;  
290   // Calculates the center of gravity in the local PHOS-module coordinates
291   Float_t wtot = 0;
292   const Int_t mulDigit=clu->GetNCells() ;
293   Double_t xc[mulDigit] ;
294   Double_t zc[mulDigit] ;
295   Double_t wi[mulDigit] ;
296   Double_t x = 0 ;
297   Double_t z = 0 ;
298   const Double_t logWeight=4.5 ;
299   for(Int_t iDigit=0; iDigit<mulDigit; iDigit++) {
300     Int_t relid[4] ;
301     Float_t xi=0. ;
302     Float_t zi=0. ;
303     Int_t absId = clu->GetCellAbsId(iDigit) ;
304
305
306     AliESDEvent* EventESD = dynamic_cast<AliESDEvent*> (AliPHOSClusterSelection::GetCurrentEvent());//dynamic cast to test for ESD or AOD
307
308     AliAODEvent* EventAOD = dynamic_cast<AliAODEvent*> (AliPHOSClusterSelection::GetCurrentEvent());
309     if(EventESD)
310       fRunNumber = EventESD->GetRunNumber() ;
311     if(EventAOD)
312       fRunNumber = EventESD->GetRunNumber() ;
313
314     AliOADBContainer geomContainer("phosGeo");//Initialize Geometry
315     geomContainer.InitFromFile("$ALICE_ROOT/OADB/PHOS/PHOSGeometry.root","PHOSRotationMatrixes");
316     TObjArray *matrixes = (TObjArray*)geomContainer.GetObject(fRunNumber,"PHOSRotationMatrixes");
317     AliPHOSGeometry * fPHOSGeo =  AliPHOSGeometry::GetInstance("IHEP") ;
318     for(Int_t mod=0; mod<5; mod++) {
319       if(!matrixes->At(mod)) {
320         AliInfo(Form("No PHOS Matrix for mod:%d, geo=%p\n", mod, fPHOSGeo));
321         continue;
322       }
323
324
325       fPHOSGeo->AbsToRelNumbering(absId, relid) ;
326       fPHOSGeo->RelPosInModule(relid, xi, zi);
327       xc[iDigit]=xi ;
328       zc[iDigit]=zi ;
329       Double_t ei = elist[iDigit]*cells->GetCellAmplitude(absId) ;
330       wi[iDigit]=0. ;
331       if (clu->E()>0 && ei>0) {
332         wi[iDigit] = TMath::Max( 0., logWeight + TMath::Log( ei / clu->E() ) ) ;
333         Double_t w=wi[iDigit];
334         x    += xc[iDigit] * w ;
335         z    += zc[iDigit] * w ;
336         wtot += w ;
337       }
338     }
339     if (wtot>0) {
340       x /= wtot ;
341       z /= wtot ;
342     }
343      
344     wtot = 0. ;
345     Double_t dxx  = 0.;
346     Double_t dzz  = 0.;
347     Double_t dxz  = 0.;
348     Double_t xCut = 0. ;
349     Double_t zCut = 0. ;
350     for(Int_t iDigit1=0; iDigit1<mulDigit; iDigit1++) {
351       Double_t w=wi[iDigit1];
352       if (w>0.) {
353         Double_t xi1= xc[iDigit1] ;
354         Double_t zi1= zc[iDigit1] ;
355         if((xi1-x)*(xi1-x)+(zi1-z)*(zi1-z) < 4.5*4.5){
356           xCut += w * xi1 ;
357           zCut += w * zi1 ; 
358           dxx  += w * xi1 * xi1 ;
359           dzz  += w * zi1 * zi1 ;
360           dxz  += w * xi1 * zi1 ; 
361           wtot += w ;
362         }
363       }
364     
365     }
366     if (wtot>0) {
367       xCut/= wtot ;
368       zCut/= wtot ;
369       dxx /= wtot ;
370       dzz /= wtot ;
371       dxz /= wtot ;
372       dxx -= xCut * xCut ;
373       dzz -= zCut * zCut ;
374       dxz -= xCut * zCut ;
375
376       m02 =  0.5 * (dxx + dzz) + TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
377       m20 =  0.5 * (dxx + dzz) - TMath::Sqrt( 0.25 * (dxx - dzz) * (dxx - dzz) + dxz * dxz )  ;
378     }
379     else {
380       m20=m02=0.;
381     }
382
383   }
384 }
385
386 //_____________________________________________________________________________
387 Bool_t AliPHOSClusterSelection::TestLambda(Double_t pt,Double_t l1,Double_t l2) const 
388 {
389   //Evaluates if lambdas correspond to photon cluster
390   //Tuned using pp data 
391   //copied from Pi0FlowTask
392   Double_t l2Mean, l1Mean, l2Sigma, l1Sigma, c, R2;
393   if(fIsCore<0){
394     l2Mean  = 1.53126+9.50835e+06/(1.+1.08728e+07*pt+1.73420e+06*pt*pt) ;
395     l1Mean  = 1.12365+0.123770*TMath::Exp(-pt*0.246551)+5.30000e-03*pt ;
396     l2Sigma = 6.48260e-02+7.60261e+10/(1.+1.53012e+11*pt+5.01265e+05*pt*pt)+9.00000e-03*pt;
397     l1Sigma = 4.44719e-04+6.99839e-01/(1.+1.22497e+00*pt+6.78604e-07*pt*pt)+9.00000e-03*pt;
398     c=-0.35-0.550*TMath::Exp(-0.390730*pt) ;
399     R2=0.5*(l1-l1Mean)*(l1-l1Mean)/l1Sigma/l1Sigma +
400       0.5*(l2-l2Mean)*(l2-l2Mean)/l2Sigma/l2Sigma +
401       0.5*c*(l1-l1Mean)*(l2-l2Mean)/l1Sigma/l2Sigma ;
402   }
403   else{
404     //For core radius R=4.5
405     l1Mean  = 1.150200 + 0.097886/(1.+1.486645*pt+0.000038*pt*pt) ;
406     l2Mean = 1.574706 + 0.997966*exp(-0.895075*pt)-0.010666*pt ;
407     l1Sigma = 0.100255 + 0.337177*exp(-0.517684*pt)+0.001170*pt ;
408     l2Sigma = 0.232580 + 0.573401*exp(-0.735903*pt)-0.002325*pt ;
409     c = -0.110983 -0.017353/(1.-1.836995*pt+0.934517*pt*pt) ;
410     R2=0.5*(l1-l1Mean)*(l1-l1Mean)/l1Sigma/l1Sigma + 
411       0.5*(l2-l2Mean)*(l2-l2Mean)/l2Sigma/l2Sigma +
412       0.5*c*(l1-l1Mean)*(l2-l2Mean)/l1Sigma/l2Sigma ;
413   }
414   return (R2<fMaxDispR2*fMaxDispR2);//TODO: Check if this should be 2. order
415 }
416
417
418 AliVEvent* AliPHOSClusterSelection::GetCurrentEvent() const
419 {
420   // Hackish way of getting the current event.
421   // Its probably not appropriate to call this function outside of
422   // AliAnalysisTaskSE::UserExec
423   
424   AliAnalysisManager* analysisManager = dynamic_cast<AliAnalysisManager*>(AliAnalysisManager::GetAnalysisManager());
425   AliInputEventHandler* inputHandler = dynamic_cast<AliInputEventHandler*>(analysisManager->GetInputEventHandler());
426   AliMultiInputEventHandler *multiInputHandler = dynamic_cast<AliMultiInputEventHandler *>(inputHandler);
427   if (multiInputHandler)
428     inputHandler = dynamic_cast<AliInputEventHandler *>(multiInputHandler->GetFirstInputEventHandler());
429   
430   AliVEvent* inputEvent = dynamic_cast<AliVEvent*>(inputHandler->GetEvent());
431   if( ! inputEvent ) 
432     AliError("Was not able to retrieve event!");
433   
434   return inputEvent;
435 }