1 /**************************************************************************
2 * Copyright(c) 1998-1999, 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 **************************************************************************/
17 #include "AliRICHClusterFinder.h"
18 #include "AliRICHMap.h"
20 #include <TParticle.h>
22 #include <AliLoader.h>
25 #include "AliRICHParam.h"
27 void RICHMinMathieson(Int_t &npar, Double_t *gin, Double_t &chi2, Double_t *par, Int_t iflag);
29 ClassImp(AliRICHClusterFinder)
30 //__________________________________________________________________________________________________
31 AliRICHClusterFinder::AliRICHClusterFinder(AliRICH *pRICH)
34 AliDebug(1,"main ctor Start.");
38 fResolvedCluster.Reset();
39 AliDebug(1,"main ctor Stop.");
41 //__________________________________________________________________________________________________
42 void AliRICHClusterFinder::Exec()
44 //Main method of cluster finder. Loops on events and chambers, everything else is done in FindClusters()
45 AliDebug(1,"Exec Start.");
47 R()->GetLoader() ->LoadDigits();
48 // R()->GetLoader()->GetRunLoader()->LoadHeader();
49 R()->GetLoader()->GetRunLoader()->LoadKinematics(); //header is already loaded
51 for(Int_t iEventN=0;iEventN<gAlice->GetEventsPerRun();iEventN++){//events loop
52 AliDebug(1,Form("Processing event %i...",iEventN));
53 R()->GetLoader()->GetRunLoader()->GetEvent(iEventN);
55 R()->GetLoader()->MakeTree("R"); R()->MakeBranch("R");
56 R()->ResetDigits(); R()->ResetClusters();
58 R()->GetLoader()->TreeD()->GetEntry(0);
59 for(Int_t iChamber=1;iChamber<=kNchambers;iChamber++){//chambers loop
60 FindClusters(iChamber);
62 R()->GetLoader()->TreeR()->Fill(); R()->GetLoader()->WriteRecPoints("OVERWRITE");//write out clusters for current event
65 R()->ResetDigits();//reset and unload everything
67 R()->GetLoader() ->UnloadDigits();
68 R()->GetLoader() ->UnloadRecPoints();
69 // R()->GetLoader()->GetRunLoader()->UnloadHeader();
70 R()->GetLoader()->GetRunLoader()->UnloadKinematics();
74 //__________________________________________________________________________________________________
75 void AliRICHClusterFinder::FindClusters(Int_t iChamber)
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();
79 AliDebug(1,Form("Start for chamber %i with %i digits.",iChamber,iNdigits));
81 if(iNdigits==0)return;//no digits for a given chamber, nothing to do
83 fDigitMap=new AliRICHMap(R()->Digits(iChamber));//create digit map for the given chamber
85 for(Int_t iDigN=0;iDigN<iNdigits;iDigN++){//digits loop for a given chamber
86 AliRICHdigit *dig=(AliRICHdigit*)R()->Digits(iChamber)->At(iDigN);
87 Int_t i=dig->X(); Int_t j=dig->Y();
88 if(fDigitMap->TestHit(i,j)==kUsed) continue;//this digit is already taken, go after next digit
90 FormRawCluster(i,j);//form raw cluster starting from (i,j) pad
91 AliDebug(1,"After FormRawCluster:");ToAliDebug(1,fRawCluster.Print());
92 FindLocalMaxima(); //find number of local maxima and initial center of gravity
93 AliDebug(1,"After FindLocalMaxima:");ToAliDebug(1,fRawCluster.Print());
95 if(AliRICHParam::IsResolveClusters()&&fNlocals<=6&&fRawCluster.Size()>1){
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
100 fRawCluster.Reset(); fResolvedCluster.Reset();
101 }//digits loop for a given chamber
107 //__________________________________________________________________________________________________
108 void AliRICHClusterFinder::FindClusterContribs(AliRICHcluster *pCluster)
110 //Finds cerenkov-feedback-mip mixture for a given cluster
111 AliDebug(1,"Start.");ToAliDebug(1,pCluster->Print());
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!!
115 AliStack *pStack = R()->GetLoader()->GetRunLoader()->Stack();
117 {AliInfo("No Stack found!!! No contrib to cluster found.");return;}
119 TObjArray *pDigits = pCluster->Digits();
120 if(!pDigits) return; //??????????
121 Int_t iNmips=0,iNckovs=0,iNfeeds=0;
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
125 contribs[3*iDigN] =((AliRICHdigit*)pDigits->At(iDigN))->GetTrack(0);
126 if (contribs[3*iDigN] >= 10000000) contribs[3*iDigN] = 0;
127 contribs[3*iDigN+1]=((AliRICHdigit*)pDigits->At(iDigN))->GetTrack(1);
128 if (contribs[3*iDigN+1] >= 10000000) contribs[3*iDigN+1] = 0;
129 contribs[3*iDigN+2]=((AliRICHdigit*)pDigits->At(iDigN))->GetTrack(2);
130 if (contribs[3*iDigN+2] >= 10000000) contribs[3*iDigN+2] = 0;
131 }//loop on digits of a given cluster
132 TMath::Sort(contribs.GetSize(),contribs.GetArray(),pindex);
133 for(Int_t iDigN=0;iDigN<3*pCluster->Size()-1;iDigN++) {//loop on digits to sort tids
134 AliDebug(1,Form("%4i for digit n. %4i",contribs[pindex[iDigN]],iDigN));
135 if(contribs[pindex[iDigN]]!=contribs[pindex[iDigN+1]]) {
136 TParticle* particle = pStack->Particle(contribs[pindex[iDigN]]);
137 Int_t code = particle->GetPdgCode();
139 if(particle->GetPDG()) charge=particle->GetPDG()->Charge();
140 AliDebug(1,Form(" charge of particle %f",charge));
142 if(code==50000050) iNckovs++;
143 if(code==50000051) iNfeeds++;
144 if(charge!=0) iNmips++;
146 }//loop on digits to sort Tid
148 if (contribs[pindex[3*pCluster->Size()-1]]!=kBad) {
150 TParticle* particle = pStack->Particle(contribs[pindex[3*pCluster->Size()-1]]);
151 Int_t code = particle->GetPdgCode();
153 if(particle->GetPDG()) charge=particle->GetPDG()->Charge();
154 AliDebug(1,Form(" charge of particle %f",charge));
155 if(code==50000050) iNckovs++;
156 if(code==50000051) iNfeeds++;
157 if(charge!=0) iNmips++;
160 pCluster->CFM(iNckovs,iNfeeds,iNmips);
163 ToAliDebug(1,pCluster->Print());
165 }//FindClusterContribs()
166 //__________________________________________________________________________________________________
167 void AliRICHClusterFinder::FormRawCluster(Int_t i, Int_t j)
169 //Builds the raw cluster (before deconvolution). Starts from the first pad (i,j) then calls itself recursevly for all neighbours.
170 AliDebug(1,Form("Start with digit(%i,%i) Q=%f",i,j,((AliRICHdigit*)fDigitMap->GetHit(i,j))->Q()));
172 fRawCluster.AddDigit((AliRICHdigit*) fDigitMap->GetHit(i,j));//take this pad in cluster
173 fDigitMap->FlagHit(i,j);//flag this pad as taken
175 Int_t listX[4], listY[4]; // Now look recursively for all neighbours
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]);
179 //__________________________________________________________________________________________________
180 void AliRICHClusterFinder::FindLocalMaxima()
182 //find number of local maxima in the current raw cluster and then calculates initial center of gravity
184 AliDebug(1,Form("Cluster size of the Raw cluster ---> %i",fRawCluster.Size()));
185 for(Int_t iDig1=0;iDig1<fRawCluster.Size();iDig1++) {
187 AliRICHdigit *pDig1 = (AliRICHdigit *)fRawCluster.Digits()->At(iDig1);
188 if(!pDig1) {fNlocals=0;return;}
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);
194 if(!pDig2) {fNlocals=0;return;}
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++;
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;
212 AliDebug(1,Form("Number of local maxima found ---> %i",fNlocals));
213 fRawCluster.CoG(fNlocals); //first initial approximation of the CoG...to start minimization.
215 //__________________________________________________________________________________________________
216 void AliRICHClusterFinder::WriteRawCluster()
218 //Add the current raw cluster to the list of clusters
219 AliDebug(1,"Start.");
221 FindClusterContribs(&fRawCluster);
222 R()->AddCluster(fRawCluster);
224 ToAliDebug(1,fRawCluster.Print()); AliDebug(1,"Stop.");
226 //__________________________________________________________________________________________________
227 void AliRICHClusterFinder::WriteResolvedCluster()
229 //Add the current resolved cluster to the list of clusters
230 AliDebug(1,"Start.");
232 FindClusterContribs(&fResolvedCluster);
233 R()->AddCluster(fResolvedCluster);
235 ToAliDebug(1,fResolvedCluster.Print()); AliDebug(1,"Stop.");
236 }//WriteResolvedCluster()
237 //__________________________________________________________________________________________________
238 void AliRICHClusterFinder::FitCoG()
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
242 AliDebug(1,"Start with:"); ToAliDebug(1,fRawCluster.Print());
247 AliDebug(1,Form("MINUIT Started with %i parameters and %i local maxima",3*fNlocals-1,fNlocals));
249 // TMinuit *pMinuit = new TMinuit(3*fNlocals-1);
250 TMinuit *pMinuit = new TMinuit(100);
251 pMinuit->mninit(5,10,7);
254 pMinuit->mnexcm("SET PRI",&arglist, 1, ierflag);
255 pMinuit->mnexcm("SET NOW",&arglist, 0, ierflag);
260 pMinuit->SetObjectFit((TObject*)this);
261 pMinuit->SetFCN(RICHMinMathieson);
263 Double_t vstart,lower, upper;
264 Double_t stepX= 0.01;
265 Double_t stepY= 0.01;
266 Double_t stepQ= 0.01;
268 for(Int_t i=0;i<fNlocals;i++) {
269 AliDebug(1,Form(" local minimum n. %i with Xstart %f and Ystart %f",i,fLocalX[i],fLocalY[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);
274 AliDebug(1,Form("xCoG %i vstart %f lower %f upper %f ",i,vstart,lower,upper));
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);
280 AliDebug(1,Form("yCoG %i vstart %f lower %f upper %f ",i,vstart,lower,upper));
281 if(i==fNlocals-1) break; // last parameter is constrained
282 vstart = fLocalQ[i]/fRawCluster.Q();
285 pMinuit->mnparm(3*i+2,Form("qfrac %i",i),vstart,stepQ,lower,upper,ierflag);
286 AliDebug(1,Form("qfrac %i vstart %f lower %f upper %f ",i,vstart,lower,upper));
290 pMinuit->mnexcm("SET NOGR",&arglist, 1, ierflag);
292 pMinuit->mnexcm("SET ERR", &arglist, 1,ierflg);
294 pMinuit->mnexcm("SIMPLEX",&arglist, 0, ierflag);
295 pMinuit->mnexcm("MIGRAD",&arglist, 0, ierflag);
296 // pMinuit->mnexcm("EXIT" ,&arglist, 0, ierflag);
298 Double_t xCoG[50],yCoG[50],qfracCoG[50];
299 Double_t eps, b1, b2;
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];
309 qfracCoG[fNlocals-1] = 1 - qfraclast;
313 for(Int_t i=0;i<fNlocals;i++){//resolved positions loop
314 fResolvedCluster.Fill(&fRawCluster,xCoG[i],yCoG[i],qfracCoG[i],fLocalC[i]);
315 WriteResolvedCluster();
322 //__________________________________________________________________________________________________
323 void RICHMinMathieson(Int_t &npar, Double_t *, Double_t &chi2, Double_t *par, Int_t )
325 //Mathieson minimization function
327 AliRICHcluster *pRawCluster = ((AliRICHClusterFinder*)gMinuit->GetObjectFit())->GetRawCluster();
329 TVector2 centroid[50];
331 Int_t nFunctions = (npar+1)/3;
333 for(Int_t i=0;i<nFunctions;i++) {
334 centroid[i].Set(par[3*i],par[3*i+1]);
335 if(i==nFunctions-1) break;
339 q[nFunctions-1] = 1 - qfract;
342 Int_t qtot = pRawCluster->Q();
343 for(Int_t i=0;i<pRawCluster->Size();i++) {
344 TVector pad=((AliRICHdigit *)pRawCluster->Digits()->At(i))->Pad();
345 Double_t padQ = ((AliRICHdigit *)pRawCluster->Digits()->At(i))->Q();
347 for(Int_t j=0;j<nFunctions;j++) {
348 qfracpar += q[j]*AliRICHParam::FracQdc(centroid[j],pad);
350 chi2 += TMath::Power((qtot*qfracpar-padQ),2)/padQ;
352 }//RICHMinMathieson()
353 //__________________________________________________________________________________________________