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