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