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(AliRunLoader *pRunLoader)
33 fRICH = (AliRICH*) pRunLoader->GetAliRun()->GetDetector("RICH");
35 AliDebug(1,"main ctor Start.");
39 fResolvedCluster.Reset();
40 AliDebug(1,"main ctor Stop.");
42 //__________________________________________________________________________________________________
43 void AliRICHClusterFinder::Exec(const Option_t *)
45 //Main method of cluster finder. Loops on events and chambers, everything else is done in FindClusters()
46 AliDebug(1,"Exec Start.");
48 R()->GetLoader() ->LoadDigits();
49 // R()->GetLoader()->GetRunLoader()->LoadHeader();
50 if(!R()->GetLoader()->GetRunLoader()->TreeK()) R()->GetLoader()->GetRunLoader()->LoadKinematics();
52 for(Int_t iEventN=0;iEventN<gAlice->GetEventsPerRun();iEventN++){//events loop
53 AliDebug(1,Form("Processing event %i...",iEventN));
54 R()->GetLoader()->GetRunLoader()->GetEvent(iEventN);
56 R()->GetLoader()->MakeTree("R"); R()->MakeBranch("R");
57 R()->DigitsReset(); R()->ClustersReset();
59 R()->GetLoader()->TreeD()->GetEntry(0);
60 for(Int_t iChamber=1;iChamber<=kNchambers;iChamber++){//chambers loop
61 FindClusters(iChamber);
63 R()->GetLoader()->TreeR()->Fill(); R()->GetLoader()->WriteRecPoints("OVERWRITE");//write out clusters for current event
66 R()->DigitsReset();//reset and unload everything
68 R()->GetLoader() ->UnloadDigits();
69 R()->GetLoader() ->UnloadRecPoints();
70 // R()->GetLoader()->GetRunLoader()->UnloadHeader();
71 R()->GetLoader()->GetRunLoader()->UnloadKinematics();
75 //__________________________________________________________________________________________________
76 void AliRICHClusterFinder::FindClusters(Int_t iChamber)
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();
80 AliDebug(1,Form("Start for chamber %i with %i digits.",iChamber,iNdigits));
82 if(iNdigits==0)return;//no digits for a given chamber, nothing to do
84 fDigitMap=new AliRICHMap(R()->Digits(iChamber));//create digit map for the given chamber
86 for(Int_t iDigN=0;iDigN<iNdigits;iDigN++){//digits loop for a given chamber
87 AliRICHDigit *dig=(AliRICHDigit*)R()->Digits(iChamber)->At(iDigN);
88 Int_t i=dig->PadX(); Int_t j=dig->PadY();
89 if(fDigitMap->TestHit(i,j)==kUsed) continue;//this digit is already taken, go after next digit
91 FormRawCluster(i,j);//form raw cluster starting from (i,j) pad
92 AliDebug(1,"After FormRawCluster:");ToAliDebug(1,fRawCluster.Print());
93 FindLocalMaxima(); //find number of local maxima and initial center of gravity
94 AliDebug(1,"After FindLocalMaxima:");ToAliDebug(1,fRawCluster.Print());
96 if(AliRICHParam::IsResolveClusters()&&fNlocals<=6&&fRawCluster.Size()>1){
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
101 fRawCluster.Reset(); fResolvedCluster.Reset();
102 }//digits loop for a given chamber
108 //__________________________________________________________________________________________________
109 void AliRICHClusterFinder::FindClusterContribs(AliRICHCluster *pCluster)
111 //Finds cerenkov-feedback-mip mixture for a given cluster
112 AliDebug(1,"Start.");ToAliDebug(1,pCluster->Print());
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!!
116 AliStack *pStack = R()->GetLoader()->GetRunLoader()->Stack();
118 {AliInfo("No Stack found!!! No contrib to cluster found.");return;}
120 TObjArray *pDigits = pCluster->Digits();
121 if(!pDigits) return; //??????????
122 Int_t iNmips=0,iNckovs=0,iNfeeds=0;
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
126 contribs[3*iDigN] =((AliRICHDigit*)pDigits->At(iDigN))->GetTrack(0);
127 if (contribs[3*iDigN] >= 10000000) contribs[3*iDigN] = 0;
128 contribs[3*iDigN+1]=((AliRICHDigit*)pDigits->At(iDigN))->GetTrack(1);
129 if (contribs[3*iDigN+1] >= 10000000) contribs[3*iDigN+1] = 0;
130 contribs[3*iDigN+2]=((AliRICHDigit*)pDigits->At(iDigN))->GetTrack(2);
131 if (contribs[3*iDigN+2] >= 10000000) contribs[3*iDigN+2] = 0;
132 }//loop on digits of a given cluster
133 TMath::Sort(contribs.GetSize(),contribs.GetArray(),pindex);
134 for(Int_t iDigN=0;iDigN<3*pCluster->Size()-1;iDigN++) {//loop on digits to sort tids
135 AliDebug(1,Form("%4i for digit n. %4i",contribs[pindex[iDigN]],iDigN));
136 if(contribs[pindex[iDigN]]!=contribs[pindex[iDigN+1]]) {
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
141 Int_t code = particle->GetPdgCode();
143 if(particle->GetPDG()) charge=particle->GetPDG()->Charge();
144 AliDebug(1,Form(" charge of particle %f",charge));
146 if(code==50000050) iNckovs++;
147 if(code==50000051) iNfeeds++;
148 if(charge!=0) iNmips++;
150 }//loop on digits to sort Tid
152 if (contribs[pindex[3*pCluster->Size()-1]]!=-1) {
153 Int_t thecontrib = contribs[pindex[3*pCluster->Size()-1]];
154 if (thecontrib<pStack->GetNtrack()){
155 //PH the opposite should not happen
157 TParticle* particle = pStack->Particle(thecontrib);
159 //PH the opposite should not happen
160 Int_t code = particle->GetPdgCode();
162 if(particle->GetPDG()) charge=particle->GetPDG()->Charge();
163 AliDebug(1,Form(" charge of particle %f",charge));
164 if(code==50000050) iNckovs++;
165 if(code==50000051) iNfeeds++;
166 if(charge!=0) iNmips++;
171 pCluster->CFM(iNckovs,iNfeeds,iNmips);
174 ToAliDebug(1,pCluster->Print());
176 }//FindClusterContribs()
177 //__________________________________________________________________________________________________
178 void AliRICHClusterFinder::FormRawCluster(Int_t i, Int_t j)
180 //Builds the raw cluster (before deconvolution). Starts from the first pad (i,j) then calls itself recursevly for all neighbours.
181 AliDebug(1,Form("Start with digit(%i,%i) Q=%f",i,j,((AliRICHDigit*)fDigitMap->GetHit(i,j))->Qdc()));
183 fRawCluster.AddDigit((AliRICHDigit*) fDigitMap->GetHit(i,j));//take this pad in cluster
184 fDigitMap->FlagHit(i,j);//flag this pad as taken
186 Int_t listX[4], listY[4]; // Now look recursively for all neighbours
187 for (Int_t iNei=0;iNei<R()->P()->PadNeighbours(i,j,listX,listY);iNei++)
188 if(fDigitMap->TestHit(listX[iNei],listY[iNei])==kUnused) FormRawCluster(listX[iNei],listY[iNei]);
190 //__________________________________________________________________________________________________
191 void AliRICHClusterFinder::FindLocalMaxima()
193 //find number of local maxima in the current raw cluster and then calculates initial center of gravity
195 AliDebug(1,Form("Cluster size of the Raw cluster ---> %i",fRawCluster.Size()));
196 for(Int_t iDig1=0;iDig1<fRawCluster.Size();iDig1++) {
198 AliRICHDigit *pDig1 = (AliRICHDigit *)fRawCluster.Digits()->At(iDig1);
199 if(!pDig1) {fNlocals=0;return;}
200 TVector pad1 = pDig1->Pad();
201 Int_t padQ1 = (Int_t)(pDig1->Qdc()+0.1);
202 Int_t padC1 = pDig1->Cfm();
203 for(Int_t iDig2=0;iDig2<fRawCluster.Size();iDig2++) {
204 AliRICHDigit *pDig2 = (AliRICHDigit *)fRawCluster.Digits()->At(iDig2);
205 if(!pDig2) {fNlocals=0;return;}
206 TVector pad2 = pDig2->Pad();
207 Int_t padQ2 = (Int_t)(pDig2->Qdc()+0.1);
208 if(iDig1==iDig2) continue;
209 Int_t diffx = TMath::Sign(Int_t(pad1[0]-pad2[0]),1);
210 Int_t diffy = TMath::Sign(Int_t(pad1[1]-pad2[1]),1);
211 if((diffx+diffy)<=1) {
212 if(padQ2>=padQ1) iNotMax++;
217 TVector2 x2=AliRICHParam::Pad2Loc(pad1);
218 fLocalX[fNlocals]=x2.X();fLocalY[fNlocals]=x2.Y();
219 fLocalQ[fNlocals] = (Double_t)padQ1;
220 fLocalC[fNlocals] = padC1;
225 AliDebug(1,Form("Number of local maxima found ---> %i",fNlocals));
226 fRawCluster.CoG(fNlocals); //first initial approximation of the CoG...to start minimization.
228 //__________________________________________________________________________________________________
229 void AliRICHClusterFinder::WriteRawCluster()
231 //Add the current raw cluster to the list of clusters
232 AliDebug(1,"Start.");
234 FindClusterContribs(&fRawCluster);
235 R()->ClusterAdd(fRawCluster);
237 ToAliDebug(1,fRawCluster.Print()); AliDebug(1,"Stop.");
239 //__________________________________________________________________________________________________
240 void AliRICHClusterFinder::WriteResolvedCluster()
242 //Add the current resolved cluster to the list of clusters
243 AliDebug(1,"Start.");
245 FindClusterContribs(&fResolvedCluster);
246 R()->ClusterAdd(fResolvedCluster);
248 ToAliDebug(1,fResolvedCluster.Print()); AliDebug(1,"Stop.");
249 }//WriteResolvedCluster()
250 //__________________________________________________________________________________________________
251 void AliRICHClusterFinder::FitCoG()
253 //Fits cluster of size by the corresponding number of Mathieson shapes.
254 //This methode is only invoked in case everything is ok to start deconvolution
255 AliDebug(1,"Start with:"); ToAliDebug(1,fRawCluster.Print());
260 AliDebug(1,Form("MINUIT Started with %i parameters and %i local maxima",3*fNlocals-1,fNlocals));
262 // TMinuit *pMinuit = new TMinuit(3*fNlocals-1);
263 TMinuit *pMinuit = new TMinuit(100);
264 pMinuit->mninit(5,10,7);
267 pMinuit->mnexcm("SET PRI",&arglist, 1, ierflag);
268 pMinuit->mnexcm("SET NOW",&arglist, 0, ierflag);
273 pMinuit->SetObjectFit((TObject*)this);
274 pMinuit->SetFCN(RICHMinMathieson);
276 Double_t vstart,lower, upper;
277 Double_t stepX= 0.01;
278 Double_t stepY= 0.01;
279 Double_t stepQ= 0.01;
281 for(Int_t i=0;i<fNlocals;i++) {
282 AliDebug(1,Form(" local minimum n. %i with Xstart %f and Ystart %f",i,fLocalX[i],fLocalY[i]));
284 lower = vstart - 2*AliRICHParam::PadSizeX();
285 upper = vstart + 2*AliRICHParam::PadSizeX();
286 pMinuit->mnparm(3*i ,Form("xCoG %i",i),vstart,stepX,lower,upper,ierflag);
287 AliDebug(1,Form("xCoG %i vstart %f lower %f upper %f ",i,vstart,lower,upper));
290 lower = vstart - 2*AliRICHParam::PadSizeY();
291 upper = vstart + 2*AliRICHParam::PadSizeY();
292 pMinuit->mnparm(3*i+1,Form("yCoG %i",i),vstart,stepY,lower,upper,ierflag);
293 AliDebug(1,Form("yCoG %i vstart %f lower %f upper %f ",i,vstart,lower,upper));
294 if(i==fNlocals-1) break; // last parameter is constrained
295 vstart = fLocalQ[i]/fRawCluster.Q();
298 pMinuit->mnparm(3*i+2,Form("qfrac %i",i),vstart,stepQ,lower,upper,ierflag);
299 AliDebug(1,Form("qfrac %i vstart %f lower %f upper %f ",i,vstart,lower,upper));
302 arglist = -1; pMinuit->mnexcm("SET NOGR",&arglist, 1,ierflag);
303 arglist = 1; pMinuit->mnexcm("SET ERR" ,&arglist, 1,ierflg);
304 arglist = -1; pMinuit->mnexcm("SIMPLEX" ,&arglist, 0,ierflag);
305 pMinuit->mnexcm("MIGRAD",&arglist, 0, ierflag);
306 // pMinuit->mnexcm("EXIT" ,&arglist, 0, ierflag);
308 Double_t xCoG[50],yCoG[50],qfracCoG[50];
309 Double_t eps, b1, b2;
311 Double_t qfraclast=0;
312 for(Int_t i=0;i<fNlocals;i++) {
313 pMinuit->mnpout(3*i ,chname, xCoG[i], eps , b1, b2, ierflg);
314 pMinuit->mnpout(3*i+1,chname, yCoG[i], eps , b1, b2, ierflg);
315 if(i==fNlocals-1) break;
316 pMinuit->mnpout(3*i+2,chname, qfracCoG[i], eps , b1, b2, ierflg);
317 qfraclast+=qfracCoG[i];
319 qfracCoG[fNlocals-1] = 1 - qfraclast;
323 for(Int_t i=0;i<fNlocals;i++){//resolved positions loop
324 fResolvedCluster.Fill(&fRawCluster,xCoG[i],yCoG[i],qfracCoG[i],fLocalC[i]);
325 WriteResolvedCluster();
332 //__________________________________________________________________________________________________
333 void RICHMinMathieson(Int_t &npar, Double_t *, Double_t &chi2, Double_t *par, Int_t )
335 //Mathieson minimization function
337 AliRICHCluster *pRawCluster = ((AliRICHClusterFinder*)gMinuit->GetObjectFit())->GetRawCluster();
339 TVector2 centroid[50];
341 Int_t nFunctions = (npar+1)/3;
343 for(Int_t i=0;i<nFunctions;i++) {
344 centroid[i].Set(par[3*i],par[3*i+1]);
345 if(i==nFunctions-1) break;
349 q[nFunctions-1] = 1 - qfract;
352 Int_t qtot = pRawCluster->Q();
353 for(Int_t i=0;i<pRawCluster->Size();i++) {
354 TVector pad=((AliRICHDigit *)pRawCluster->Digits()->At(i))->Pad();
355 Double_t padQ = ((AliRICHDigit *)pRawCluster->Digits()->At(i))->Qdc();
357 for(Int_t j=0;j<nFunctions;j++) {
358 qfracpar += q[j]*AliRICHParam::FracQdc(centroid[j],pad);
360 chi2 += TMath::Power((qtot*qfracpar-padQ),2)/padQ;
362 }//RICHMinMathieson()
363 //__________________________________________________________________________________________________