Missing AliPID init.
[u/mrichter/AliRoot.git] / RICH / AliRICHClusterFinder.cxx
CommitLineData
8265fa96 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
8265fa96 16
17#include "AliRICHClusterFinder.h"
543d5224 18#include "AliRICHMap.h"
9d6f9427 19#include <TMinuit.h>
ed3ceb24 20#include <TParticle.h>
9d6f9427 21#include <TVector3.h>
cbaf35fb 22#include <AliLoader.h>
ed3ceb24 23#include <AliStack.h>
cbaf35fb 24#include <AliRun.h>
781ec278 25#include "AliRICHParam.h"
8265fa96 26
9d6f9427 27void RICHMinMathieson(Int_t &npar, Double_t *gin, Double_t &chi2, Double_t *par, Int_t iflag);
8265fa96 28
29ClassImp(AliRICHClusterFinder)
543d5224 30//__________________________________________________________________________________________________
0fe8fa07 31AliRICHClusterFinder::AliRICHClusterFinder(AliRunLoader *pRunLoader)
543d5224 32{//main ctor
0fe8fa07 33 fRICH = (AliRICH*) pRunLoader->GetAliRun()->GetDetector("RICH");
34
998b831f 35 AliDebug(1,"main ctor Start.");
543d5224 36
e42a7b46 37 fDigitMap = 0;
6865fb8d 38 fRawCluster.Reset();
39 fResolvedCluster.Reset();
998b831f 40 AliDebug(1,"main ctor Stop.");
543d5224 41}//main ctor
42//__________________________________________________________________________________________________
d3eb6079 43void AliRICHClusterFinder::Exec(const Option_t *)
237c933d 44{
e42a7b46 45//Main method of cluster finder. Loops on events and chambers, everything else is done in FindClusters()
998b831f 46 AliDebug(1,"Exec Start.");
e42a7b46 47
48 R()->GetLoader() ->LoadDigits();
998b831f 49// R()->GetLoader()->GetRunLoader()->LoadHeader();
d3eb6079 50 if(!R()->GetLoader()->GetRunLoader()->TreeK()) R()->GetLoader()->GetRunLoader()->LoadKinematics();
ed3ceb24 51
543d5224 52 for(Int_t iEventN=0;iEventN<gAlice->GetEventsPerRun();iEventN++){//events loop
998b831f 53 AliDebug(1,Form("Processing event %i...",iEventN));
e42a7b46 54 R()->GetLoader()->GetRunLoader()->GetEvent(iEventN);
237c933d 55
e42a7b46 56 R()->GetLoader()->MakeTree("R"); R()->MakeBranch("R");
0fe8fa07 57 R()->ResetDigits(); R()->ClustersReset();
543d5224 58
e42a7b46 59 R()->GetLoader()->TreeD()->GetEntry(0);
998b831f 60 for(Int_t iChamber=1;iChamber<=kNchambers;iChamber++){//chambers loop
ed3ceb24 61 FindClusters(iChamber);
543d5224 62 }//chambers loop
e42a7b46 63 R()->GetLoader()->TreeR()->Fill(); R()->GetLoader()->WriteRecPoints("OVERWRITE");//write out clusters for current event
543d5224 64 }//events loop
ed3ceb24 65
e42a7b46 66 R()->ResetDigits();//reset and unload everything
0fe8fa07 67 R()->ClustersReset();
e42a7b46 68 R()->GetLoader() ->UnloadDigits();
69 R()->GetLoader() ->UnloadRecPoints();
998b831f 70// R()->GetLoader()->GetRunLoader()->UnloadHeader();
e42a7b46 71 R()->GetLoader()->GetRunLoader()->UnloadKinematics();
ed3ceb24 72
998b831f 73 AliDebug(1,"Stop.");
543d5224 74}//Exec()
75//__________________________________________________________________________________________________
ed3ceb24 76void AliRICHClusterFinder::FindClusters(Int_t iChamber)
77{
e42a7b46 78//Loops on digits for a given chamber, forms raw clusters, then tries to resolve them if requested
79 Int_t iNdigits=R()->Digits(iChamber)->GetEntriesFast();
998b831f 80 AliDebug(1,Form("Start for chamber %i with %i digits.",iChamber,iNdigits));
e42a7b46 81
82 if(iNdigits==0)return;//no digits for a given chamber, nothing to do
cbaf35fb 83
e42a7b46 84 fDigitMap=new AliRICHMap(R()->Digits(iChamber));//create digit map for the given chamber
cbaf35fb 85
e42a7b46 86 for(Int_t iDigN=0;iDigN<iNdigits;iDigN++){//digits loop for a given chamber
0fe8fa07 87 AliRICHDigit *dig=(AliRICHDigit*)R()->Digits(iChamber)->At(iDigN);
cbaf35fb 88 Int_t i=dig->X(); Int_t j=dig->Y();
e42a7b46 89 if(fDigitMap->TestHit(i,j)==kUsed) continue;//this digit is already taken, go after next digit
cbaf35fb 90
e42a7b46 91 FormRawCluster(i,j);//form raw cluster starting from (i,j) pad
998b831f 92 AliDebug(1,"After FormRawCluster:");ToAliDebug(1,fRawCluster.Print());
e42a7b46 93 FindLocalMaxima(); //find number of local maxima and initial center of gravity
998b831f 94 AliDebug(1,"After FindLocalMaxima:");ToAliDebug(1,fRawCluster.Print());
6865fb8d 95
77bc1a05 96 if(AliRICHParam::IsResolveClusters()&&fNlocals<=6&&fRawCluster.Size()>1){
e42a7b46 97 FitCoG(); //serialization of resolved clusters will happen inside
98 }else{//cluster size=1 or resolving is switched off
99 WriteRawCluster();//simply output the formed raw cluster without deconvolution
ed3ceb24 100 }
e42a7b46 101 fRawCluster.Reset(); fResolvedCluster.Reset();
102 }//digits loop for a given chamber
cbaf35fb 103
e42a7b46 104 delete fDigitMap;
cbaf35fb 105
998b831f 106 AliDebug(1,"Stop.");
ed3ceb24 107}//FindClusters()
108//__________________________________________________________________________________________________
0fe8fa07 109void AliRICHClusterFinder::FindClusterContribs(AliRICHCluster *pCluster)
ed3ceb24 110{
e42a7b46 111//Finds cerenkov-feedback-mip mixture for a given cluster
998b831f 112 AliDebug(1,"Start.");ToAliDebug(1,pCluster->Print());
e0b2e10c 113
781ec278 114// R()->GetLoader()->GetRunLoader()->LoadHeader(); //...message from AliRunLoader...hopefully will disappear in future...
115 // sometimes no stack found if the above line is commented out!!
e0b2e10c 116 AliStack *pStack = R()->GetLoader()->GetRunLoader()->Stack();
117 if(!pStack)
118 {AliInfo("No Stack found!!! No contrib to cluster found.");return;}
ed3ceb24 119
6865fb8d 120 TObjArray *pDigits = pCluster->Digits();
e42a7b46 121 if(!pDigits) return; //??????????
ed3ceb24 122 Int_t iNmips=0,iNckovs=0,iNfeeds=0;
6865fb8d 123 TArrayI contribs(3*pCluster->Size());
124 Int_t *pindex = new Int_t[3*pCluster->Size()];
125 for(Int_t iDigN=0;iDigN<pCluster->Size();iDigN++) {//loop on digits of a given cluster
0fe8fa07 126 contribs[3*iDigN] =((AliRICHDigit*)pDigits->At(iDigN))->GetTrack(0);
89924db8 127 if (contribs[3*iDigN] >= 10000000) contribs[3*iDigN] = 0;
0fe8fa07 128 contribs[3*iDigN+1]=((AliRICHDigit*)pDigits->At(iDigN))->GetTrack(1);
89924db8 129 if (contribs[3*iDigN+1] >= 10000000) contribs[3*iDigN+1] = 0;
0fe8fa07 130 contribs[3*iDigN+2]=((AliRICHDigit*)pDigits->At(iDigN))->GetTrack(2);
89924db8 131 if (contribs[3*iDigN+2] >= 10000000) contribs[3*iDigN+2] = 0;
ed3ceb24 132 }//loop on digits of a given cluster
133 TMath::Sort(contribs.GetSize(),contribs.GetArray(),pindex);
e42a7b46 134 for(Int_t iDigN=0;iDigN<3*pCluster->Size()-1;iDigN++) {//loop on digits to sort tids
998b831f 135 AliDebug(1,Form("%4i for digit n. %4i",contribs[pindex[iDigN]],iDigN));
ed3ceb24 136 if(contribs[pindex[iDigN]]!=contribs[pindex[iDigN+1]]) {
08999534 137 Int_t thecontrib = contribs[pindex[iDigN]];
138 if (thecontrib>=pStack->GetNtrack()) continue;//PH this should not happen
139 TParticle* particle = pStack->Particle(thecontrib);
140 if (!particle) continue;//PH this should not happen
89924db8 141 Int_t code = particle->GetPdgCode();
142 Double_t charge = 0;
e42a7b46 143 if(particle->GetPDG()) charge=particle->GetPDG()->Charge();
998b831f 144 AliDebug(1,Form(" charge of particle %f",charge));
ed3ceb24 145
146 if(code==50000050) iNckovs++;
e42a7b46 147 if(code==50000051) iNfeeds++;
148 if(charge!=0) iNmips++;
ed3ceb24 149 }
150 }//loop on digits to sort Tid
e42a7b46 151
0fe8fa07 152 if (contribs[pindex[3*pCluster->Size()-1]]!=-1) {
e42a7b46 153
e0b2e10c 154 TParticle* particle = pStack->Particle(contribs[pindex[3*pCluster->Size()-1]]);
e42a7b46 155 Int_t code = particle->GetPdgCode();
156 Double_t charge = 0;
157 if(particle->GetPDG()) charge=particle->GetPDG()->Charge();
998b831f 158 AliDebug(1,Form(" charge of particle %f",charge));
e42a7b46 159 if(code==50000050) iNckovs++;
160 if(code==50000051) iNfeeds++;
161 if(charge!=0) iNmips++;
162 }
163
164 pCluster->CFM(iNckovs,iNfeeds,iNmips);
165//
ed3ceb24 166 delete [] pindex;
998b831f 167 ToAliDebug(1,pCluster->Print());
168 AliDebug(1,"Stop.");
3582c1f9 169}//FindClusterContribs()
cbaf35fb 170//__________________________________________________________________________________________________
9d6f9427 171void AliRICHClusterFinder::FormRawCluster(Int_t i, Int_t j)
ed3ceb24 172{
e42a7b46 173//Builds the raw cluster (before deconvolution). Starts from the first pad (i,j) then calls itself recursevly for all neighbours.
0fe8fa07 174 AliDebug(1,Form("Start with digit(%i,%i) Q=%f",i,j,((AliRICHDigit*)fDigitMap->GetHit(i,j))->Q()));
cbaf35fb 175
0fe8fa07 176 fRawCluster.AddDigit((AliRICHDigit*) fDigitMap->GetHit(i,j));//take this pad in cluster
e42a7b46 177 fDigitMap->FlagHit(i,j);//flag this pad as taken
cbaf35fb 178
179 Int_t listX[4], listY[4]; // Now look recursively for all neighbours
e42a7b46 180 for (Int_t iNei=0;iNei<R()->P()->PadNeighbours(i,j,listX,listY);iNei++)
181 if(fDigitMap->TestHit(listX[iNei],listY[iNei])==kUnused) FormRawCluster(listX[iNei],listY[iNei]);
3582c1f9 182}//FormRawCluster()
cbaf35fb 183//__________________________________________________________________________________________________
e42a7b46 184void AliRICHClusterFinder::FindLocalMaxima()
185{
186//find number of local maxima in the current raw cluster and then calculates initial center of gravity
187 fNlocals=0;
781ec278 188 AliDebug(1,Form("Cluster size of the Raw cluster ---> %i",fRawCluster.Size()));
e42a7b46 189 for(Int_t iDig1=0;iDig1<fRawCluster.Size();iDig1++) {
190 Int_t iNotMax = 0;
0fe8fa07 191 AliRICHDigit *pDig1 = (AliRICHDigit *)fRawCluster.Digits()->At(iDig1);
781ec278 192 if(!pDig1) {fNlocals=0;return;}
e42a7b46 193 TVector pad1 = pDig1->Pad();
194 Int_t padQ1 = (Int_t)(pDig1->Q()+0.1);
195 Int_t padC1 = pDig1->ChFbMi();
196 for(Int_t iDig2=0;iDig2<fRawCluster.Size();iDig2++) {
0fe8fa07 197 AliRICHDigit *pDig2 = (AliRICHDigit *)fRawCluster.Digits()->At(iDig2);
781ec278 198 if(!pDig2) {fNlocals=0;return;}
e42a7b46 199 TVector pad2 = pDig2->Pad();
200 Int_t padQ2 = (Int_t)(pDig2->Q()+0.1);
201 if(iDig1==iDig2) continue;
202 Int_t diffx = TMath::Sign(Int_t(pad1[0]-pad2[0]),1);
203 Int_t diffy = TMath::Sign(Int_t(pad1[1]-pad2[1]),1);
204 if((diffx+diffy)<=1) {
205 if(padQ2>=padQ1) iNotMax++;
206 }
207 }
208 if(iNotMax==0) {
209 TVector2 x2=AliRICHParam::Pad2Loc(pad1);
210 fLocalX[fNlocals]=x2.X();fLocalY[fNlocals]=x2.Y();
211 fLocalQ[fNlocals] = (Double_t)padQ1;
212 fLocalC[fNlocals] = padC1;
213 fNlocals++;
214 }
215 }
781ec278 216 AliDebug(1,Form("Number of local maxima found ---> %i",fNlocals));
e42a7b46 217 fRawCluster.CoG(fNlocals); //first initial approximation of the CoG...to start minimization.
218}//FindLocalMaxima()
cbaf35fb 219//__________________________________________________________________________________________________
9d6f9427 220void AliRICHClusterFinder::WriteRawCluster()
6865fb8d 221{
e42a7b46 222//Add the current raw cluster to the list of clusters
998b831f 223 AliDebug(1,"Start.");
cbaf35fb 224
e42a7b46 225 FindClusterContribs(&fRawCluster);
0fe8fa07 226 R()->ClusterAdd(fRawCluster);
e42a7b46 227
998b831f 228 ToAliDebug(1,fRawCluster.Print()); AliDebug(1,"Stop.");
cbaf35fb 229}//WriteRawCluster()
230//__________________________________________________________________________________________________
6865fb8d 231void AliRICHClusterFinder::WriteResolvedCluster()
232{
e42a7b46 233//Add the current resolved cluster to the list of clusters
998b831f 234 AliDebug(1,"Start.");
6865fb8d 235
781ec278 236 FindClusterContribs(&fResolvedCluster);
0fe8fa07 237 R()->ClusterAdd(fResolvedCluster);
6865fb8d 238
998b831f 239 ToAliDebug(1,fResolvedCluster.Print()); AliDebug(1,"Stop.");
6865fb8d 240}//WriteResolvedCluster()
241//__________________________________________________________________________________________________
9d6f9427 242void AliRICHClusterFinder::FitCoG()
e42a7b46 243{
244//Fits cluster of size by the corresponding number of Mathieson shapes.
245//This methode is only invoked in case everything is ok to start deconvolution
998b831f 246 AliDebug(1,"Start with:"); ToAliDebug(1,fRawCluster.Print());
6865fb8d 247
0f72f306 248 Double_t arglist;
249 Int_t ierflag = 0;
6865fb8d 250
781ec278 251 AliDebug(1,Form("MINUIT Started with %i parameters and %i local maxima",3*fNlocals-1,fNlocals));
252
253// TMinuit *pMinuit = new TMinuit(3*fNlocals-1);
254 TMinuit *pMinuit = new TMinuit(100);
0f72f306 255 pMinuit->mninit(5,10,7);
256
257 arglist = -1;
258 pMinuit->mnexcm("SET PRI",&arglist, 1, ierflag);
19dd5b2f 259 pMinuit->mnexcm("SET NOW",&arglist, 0, ierflag);
9d6f9427 260
9d6f9427 261 TString chname;
262 Int_t ierflg;
263
264 pMinuit->SetObjectFit((TObject*)this);
265 pMinuit->SetFCN(RICHMinMathieson);
6865fb8d 266
267 Double_t vstart,lower, upper;
77bc1a05 268 Double_t stepX= 0.01;
269 Double_t stepY= 0.01;
270 Double_t stepQ= 0.01;
6865fb8d 271
272 for(Int_t i=0;i<fNlocals;i++) {
781ec278 273 AliDebug(1,Form(" local minimum n. %i with Xstart %f and Ystart %f",i,fLocalX[i],fLocalY[i]));
6865fb8d 274 vstart = fLocalX[i];
275 lower = vstart - 2*AliRICHParam::PadSizeX();
276 upper = vstart + 2*AliRICHParam::PadSizeX();
277 pMinuit->mnparm(3*i ,Form("xCoG %i",i),vstart,stepX,lower,upper,ierflag);
781ec278 278 AliDebug(1,Form("xCoG %i vstart %f lower %f upper %f ",i,vstart,lower,upper));
279
6865fb8d 280 vstart = fLocalY[i];
281 lower = vstart - 2*AliRICHParam::PadSizeY();
282 upper = vstart + 2*AliRICHParam::PadSizeY();
283 pMinuit->mnparm(3*i+1,Form("yCoG %i",i),vstart,stepY,lower,upper,ierflag);
781ec278 284 AliDebug(1,Form("yCoG %i vstart %f lower %f upper %f ",i,vstart,lower,upper));
6865fb8d 285 if(i==fNlocals-1) break; // last parameter is constrained
286 vstart = fLocalQ[i]/fRawCluster.Q();
287 lower = 0;
288 upper = 1;
289 pMinuit->mnparm(3*i+2,Form("qfrac %i",i),vstart,stepQ,lower,upper,ierflag);
781ec278 290 AliDebug(1,Form("qfrac %i vstart %f lower %f upper %f ",i,vstart,lower,upper));
6865fb8d 291 }
292
9d6f9427 293 arglist = -1;
9d6f9427 294 pMinuit->mnexcm("SET NOGR",&arglist, 1, ierflag);
9d6f9427 295 arglist = 1;
296 pMinuit->mnexcm("SET ERR", &arglist, 1,ierflg);
297 arglist = -1;
298 pMinuit->mnexcm("SIMPLEX",&arglist, 0, ierflag);
299 pMinuit->mnexcm("MIGRAD",&arglist, 0, ierflag);
781ec278 300// pMinuit->mnexcm("EXIT" ,&arglist, 0, ierflag);
301
6865fb8d 302 Double_t xCoG[50],yCoG[50],qfracCoG[50];
9d6f9427 303 Double_t eps, b1, b2;
6865fb8d 304
305 Double_t qfraclast=0;
306 for(Int_t i=0;i<fNlocals;i++) {
307 pMinuit->mnpout(3*i ,chname, xCoG[i], eps , b1, b2, ierflg);
308 pMinuit->mnpout(3*i+1,chname, yCoG[i], eps , b1, b2, ierflg);
309 if(i==fNlocals-1) break;
310 pMinuit->mnpout(3*i+2,chname, qfracCoG[i], eps , b1, b2, ierflg);
311 qfraclast+=qfracCoG[i];
312 }
313 qfracCoG[fNlocals-1] = 1 - qfraclast;
781ec278 314
9d6f9427 315 delete pMinuit;
6865fb8d 316
e42a7b46 317 for(Int_t i=0;i<fNlocals;i++){//resolved positions loop
6865fb8d 318 fResolvedCluster.Fill(&fRawCluster,xCoG[i],yCoG[i],qfracCoG[i],fLocalC[i]);
6865fb8d 319 WriteResolvedCluster();
320 }
781ec278 321
322
998b831f 323 AliDebug(1,"Stop.");
781ec278 324
09c52ebc 325}//FitCoG()
9d6f9427 326//__________________________________________________________________________________________________
6865fb8d 327void RICHMinMathieson(Int_t &npar, Double_t *, Double_t &chi2, Double_t *par, Int_t )
09c52ebc 328{
e42a7b46 329//Mathieson minimization function
9d6f9427 330
0fe8fa07 331 AliRICHCluster *pRawCluster = ((AliRICHClusterFinder*)gMinuit->GetObjectFit())->GetRawCluster();
9d6f9427 332
3582c1f9 333 TVector2 centroid[50];
6865fb8d 334 Double_t q[50];
335 Int_t nFunctions = (npar+1)/3;
336 Double_t qfract = 0;
337 for(Int_t i=0;i<nFunctions;i++) {
3582c1f9 338 centroid[i].Set(par[3*i],par[3*i+1]);
6865fb8d 339 if(i==nFunctions-1) break;
340 q[i]=par[3*i+2];
341 qfract+=q[i];
342 }
343 q[nFunctions-1] = 1 - qfract;
344
9d6f9427 345 chi2 = 0;
346 Int_t qtot = pRawCluster->Q();
347 for(Int_t i=0;i<pRawCluster->Size();i++) {
0fe8fa07 348 TVector pad=((AliRICHDigit *)pRawCluster->Digits()->At(i))->Pad();
349 Double_t padQ = ((AliRICHDigit *)pRawCluster->Digits()->At(i))->Q();
6865fb8d 350 Double_t qfracpar=0;
351 for(Int_t j=0;j<nFunctions;j++) {
e42a7b46 352 qfracpar += q[j]*AliRICHParam::FracQdc(centroid[j],pad);
9d6f9427 353 }
6865fb8d 354 chi2 += TMath::Power((qtot*qfracpar-padQ),2)/padQ;
6865fb8d 355 }
9d6f9427 356}//RICHMinMathieson()
e42a7b46 357//__________________________________________________________________________________________________