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