]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RICH/AliRICHClusterFinder.cxx
Macro to plot pathlengths of back-to-back jets. (A. Dainese)
[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 //  Info("main ctor","Start.");
33   
34   fRICH = pRICH;
35   
36   fHitMap = 0;
37   fRawCluster.Reset();
38   fResolvedCluster.Reset();
39   
40 }//main ctor
41 //__________________________________________________________________________________________________
42 void AliRICHClusterFinder::FindLocalMaxima()
43 {
44 //  Find number of local maxima in the cluster
45 //  Info("FindLocalMaxima","Start.");
46   
47   fNlocals = 0;
48   for(Int_t iDig1=0;iDig1<fRawCluster.Size();iDig1++) {
49     Int_t iNotMax = 0;
50     AliRICHdigit *pDig1 = (AliRICHdigit *)fRawCluster.Digits()->At(iDig1);
51     Int_t padX1 = pDig1->X();
52     Int_t padY1 = pDig1->Y();
53     Int_t padQ1 = (Int_t)(pDig1->Q()+0.1);
54     Int_t padC1 = pDig1->ChFbMi();
55     for(Int_t iDig2=0;iDig2<fRawCluster.Size();iDig2++) {
56       AliRICHdigit *pDig2 = (AliRICHdigit *)fRawCluster.Digits()->At(iDig2);
57       Int_t padX2 = pDig2->X();
58       Int_t padY2 = pDig2->Y();
59       Int_t padQ2 = (Int_t)(pDig2->Q()+0.1);
60       if(iDig1==iDig2) continue;
61       Int_t diffx = TMath::Sign(padX1-padX2,1);
62       Int_t diffy = TMath::Sign(padY1-padY2,1);
63       if((diffx+diffy)<=1) {
64          if(padQ2>=padQ1) iNotMax++;
65       }
66     }
67     if(iNotMax==0) {
68       TVector2 x2=AliRICHParam::Pad2Loc(padX1,padY1);
69       fLocalX[fNlocals]=x2.X();fLocalY[fNlocals]=x2.Y();
70       fLocalQ[fNlocals] = (Double_t)padQ1;
71       fLocalC[fNlocals] = padC1;
72       fNlocals++;
73     }
74   }
75 }//FindLocalMaxima()
76 //__________________________________________________________________________________________________
77 void AliRICHClusterFinder::Exec()
78 {
79   if(GetDebug()) Info("Exec","Start.");
80   
81   
82   Rich()->GetLoader()->LoadDigits(); 
83   
84   Rich()->GetLoader()->GetRunLoader()->LoadHeader();
85   Rich()->GetLoader()->GetRunLoader()->LoadKinematics();
86
87   for(Int_t iEventN=0;iEventN<gAlice->GetEventsPerRun();iEventN++){//events loop
88     if(GetDebug()) Info("Exec","Event %i processed.",iEventN+1);
89 //    gAlice->GetRunLoader()->GetEvent(iEventN);
90     Rich()->GetLoader()->GetRunLoader()->GetEvent(iEventN);
91     
92     Rich()->GetLoader()->MakeTree("R");  Rich()->MakeBranch("R");
93     Rich()->ResetDigits();  Rich()->ResetClusters();
94     
95     Rich()->GetLoader()->TreeD()->GetEntry(0);
96     for(Int_t iChamber=1;iChamber<=kNCH;iChamber++){//chambers loop
97       FindClusters(iChamber);
98     }//chambers loop
99     Rich()->GetLoader()->TreeR()->Fill();
100     Rich()->GetLoader()->WriteRecPoints("OVERWRITE");
101   }//events loop  
102   Rich()->GetLoader()->UnloadDigits(); Rich()->GetLoader()->UnloadRecPoints();  
103   Rich()->ResetDigits();  Rich()->ResetClusters();
104   
105   Rich()->GetLoader()->GetRunLoader()->UnloadHeader();
106   Rich()->GetLoader()->GetRunLoader()->UnloadKinematics();
107
108   if(GetDebug()) Info("Exec","Stop.");      
109 }//Exec()
110 //__________________________________________________________________________________________________
111 void AliRICHClusterFinder::FindClusters(Int_t iChamber)
112 {
113   //finds neighbours and fill the tree with raw clusters
114   Int_t nDigits=Rich()->Digits(iChamber)->GetEntriesFast();
115 //  Info("FindClusters","Start for Chamber %i with %i digits.",iChamber,nDigits);  
116   if(nDigits==0)return;
117
118   fHitMap=new AliRICHMap(Rich()->Digits(iChamber));//create digit map for the given chamber
119
120   for(Int_t iDig=0;iDig<nDigits;iDig++){    
121     AliRICHdigit *dig=(AliRICHdigit*)Rich()->Digits(iChamber)->At(iDig);
122     Int_t i=dig->X();   Int_t j=dig->Y();
123     if(fHitMap->TestHit(i,j)==kUsed) continue;
124         
125     FormRawCluster(i,j);
126     FindLocalMaxima();
127 //    cout << " fNlocals in FindCluster " << fNlocals << endl;
128     fRawCluster.CoG(fNlocals); // first initial approxmation of the CoG...to start minimization.
129     
130     if(AliRICHParam::IsResolveClusters()) {
131       ResolveCluster(); // ResolveCluster serialization will happen inside
132     } else {
133       WriteRawCluster(); // simply output of the RawCluster found without deconvolution
134     }
135     fRawCluster.Reset();
136     fResolvedCluster.Reset();
137   }//digits loop
138
139   delete fHitMap;
140 //  Info("FindClusters","Stop.");
141   
142 }//FindClusters()
143 //__________________________________________________________________________________________________
144 void AliRICHClusterFinder::FindClusterContribs(AliRICHcluster *pCluster)
145 {
146 // finds CombiPid for a given cluster
147 // Info("FindClusterContribs","Start");
148   
149   TObjArray *pDigits = pCluster->Digits();
150   Int_t iNmips=0,iNckovs=0,iNfeeds=0;
151   TArrayI contribs(3*pCluster->Size());
152   Int_t *pindex = new Int_t[3*pCluster->Size()];
153   for(Int_t iDigN=0;iDigN<pCluster->Size();iDigN++) {//loop on digits of a given cluster
154     contribs[3*iDigN]  =((AliRICHdigit*)pDigits->At(iDigN))->Tid(0);
155     if (contribs[3*iDigN] >= 10000000) contribs[3*iDigN] = 0;
156     contribs[3*iDigN+1]=((AliRICHdigit*)pDigits->At(iDigN))->Tid(1);
157     if (contribs[3*iDigN+1] >= 10000000) contribs[3*iDigN+1] = 0;
158     contribs[3*iDigN+2]=((AliRICHdigit*)pDigits->At(iDigN))->Tid(2);
159     if (contribs[3*iDigN+2] >= 10000000) contribs[3*iDigN+2] = 0;
160   }//loop on digits of a given cluster
161   TMath::Sort(contribs.GetSize(),contribs.GetArray(),pindex);
162   for(Int_t iDigN=0;iDigN<3*pCluster->Size()-1;iDigN++) {//loop on digits to sort Tid
163     if(contribs[pindex[iDigN]]!=contribs[pindex[iDigN+1]]) {
164       TParticle* particle = Rich()->GetLoader()->GetRunLoader()->Stack()->Particle(contribs[pindex[iDigN]]);
165       Int_t code   = particle->GetPdgCode();
166       Double_t charge = 0;
167       if (particle->GetPDG()) particle->GetPDG()->Charge();
168
169       if(code==50000050) iNckovs++;
170       else if(code==50000051) iNfeeds++;
171       else if(charge!=0) iNmips++;
172       if (contribs[pindex[iDigN+1]]==kBad) break;
173     }
174   }//loop on digits to sort Tid
175   pCluster->SetCombiPid(iNckovs,iNfeeds,iNmips);
176 //  pCluster->Print();
177   delete [] pindex; 
178 }//FindClusterContribs()
179 //__________________________________________________________________________________________________
180 void  AliRICHClusterFinder::FormRawCluster(Int_t i, Int_t j)
181 {
182 // Builder of the final Raw Cluster (before deconvolution)  
183   if(GetDebug()) Info("FormRawCluster","Start with digit(%i,%i)",i,j);
184   
185   fRawCluster.AddDigit((AliRICHdigit*) fHitMap->GetHit(i,j));
186   fHitMap->FlagHit(i,j);// Flag hit as taken  
187
188   Int_t listX[4], listY[4];    //  Now look recursively for all neighbours
189   for (Int_t iNeighbour=0;iNeighbour<Rich()->P()->PadNeighbours(i,j,listX,listY);iNeighbour++)
190     if(fHitMap->TestHit(listX[iNeighbour],listY[iNeighbour])==kUnused) 
191                       FormRawCluster(listX[iNeighbour],listY[iNeighbour]);    
192 }//FormRawCluster()
193 //__________________________________________________________________________________________________
194 void AliRICHClusterFinder::ResolveCluster()
195 {// Decluster algorithm
196   if(GetDebug()) {Info("ResolveCluster","Start."); fRawCluster.Print();}
197   
198   switch (fRawCluster.Size()) {
199   
200   case 1:                     // nothing to decluster: cluster size = 1
201     WriteRawCluster();break; 
202   default:                     // cluster size > 1: if=2 FitCoG; if>2 Resolve and FitCoG
203     FitCoG();break;
204   }     
205 }//ResolveCluster()
206 //__________________________________________________________________________________________________
207 void AliRICHClusterFinder::WriteRawCluster()
208 {
209 // out the current raw cluster
210   if(GetDebug()) Info("WriteRawCluster","Start.");
211   
212   FindClusterContribs(&fRawCluster);
213   if(GetDebug()) fRawCluster.Dump();
214   Rich()->AddCluster(fRawCluster);
215 //  fRawCluster.Print();
216 }//WriteRawCluster()
217 //__________________________________________________________________________________________________
218 void AliRICHClusterFinder::WriteResolvedCluster()
219 {
220 // out the current resolved cluster
221   if(GetDebug()) Info("WriteResolvedCluster","Start.");
222   
223 //  FindClusterContribs(&fResolvedCluster);
224   Rich()->AddCluster(fResolvedCluster);
225   
226 }//WriteResolvedCluster()
227 //__________________________________________________________________________________________________
228 void AliRICHClusterFinder::FitCoG()
229 {// Fit cluster size 2 by single Mathieson
230   if(GetDebug()) Info("FitCoG","Start with size %3i and local maxima %3i",fRawCluster.Size(),fNlocals);
231   
232   Double_t arglist;
233   Int_t ierflag = 0;
234
235 //  fRawCluster.Print();
236 //  if(fNlocals==0) fRawCluster.Print();
237   if(fNlocals==0||fNlocals>3) {WriteRawCluster();return;}
238   
239   TMinuit *pMinuit = new TMinuit(3*fNlocals-1);
240   pMinuit->mninit(5,10,7);
241   
242   arglist = -1;
243   pMinuit->mnexcm("SET PRI",&arglist, 1, ierflag);
244   pMinuit->mnexcm("SET NOW",&arglist, 0, ierflag);
245   
246   TString chname;
247   Int_t ierflg;
248   
249   pMinuit->SetObjectFit((TObject*)this);
250   pMinuit->SetFCN(RICHMinMathieson);
251   
252   Double_t vstart,lower, upper;
253   Double_t stepX= 0.001;
254   Double_t stepY= 0.001;
255   Double_t stepQ= 0.0001;
256   
257   for(Int_t i=0;i<fNlocals;i++) {
258     vstart   = fLocalX[i];
259     lower    = vstart - 2*AliRICHParam::PadSizeX();
260     upper    = vstart + 2*AliRICHParam::PadSizeX();
261     pMinuit->mnparm(3*i  ,Form("xCoG  %i",i),vstart,stepX,lower,upper,ierflag);
262 //    cout << Form("xCoG  %i",i) << "start " << vstart << "lower " << lower << "upper " << upper << endl;
263     vstart   = fLocalY[i];
264     lower    = vstart - 2*AliRICHParam::PadSizeY();
265     upper    = vstart + 2*AliRICHParam::PadSizeY();
266     pMinuit->mnparm(3*i+1,Form("yCoG  %i",i),vstart,stepY,lower,upper,ierflag);
267 //    cout << Form("yCoG  %i",i) << "start " << vstart << "lower " << lower << "upper " << upper << endl;
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 //    cout << Form("qCoG  %i",i) << "start " << vstart << "lower " << lower << "upper " << upper << endl;
274
275   }
276   
277   arglist = -1;
278   pMinuit->mnexcm("SET NOGR",&arglist, 1, ierflag);
279   arglist = 1;
280   pMinuit->mnexcm("SET ERR", &arglist, 1,ierflg);
281   arglist = -1;
282   pMinuit->mnexcm("SIMPLEX",&arglist, 0, ierflag);
283   pMinuit->mnexcm("MIGRAD",&arglist, 0, ierflag);
284   pMinuit->mnexcm("EXIT" ,&arglist, 0, ierflag);
285
286   Double_t xCoG[50],yCoG[50],qfracCoG[50];
287   Double_t eps, b1, b2;
288
289   Double_t qfraclast=0;  
290   for(Int_t i=0;i<fNlocals;i++) {
291     pMinuit->mnpout(3*i  ,chname,     xCoG[i], eps , b1, b2, ierflg);
292     pMinuit->mnpout(3*i+1,chname,     yCoG[i], eps , b1, b2, ierflg);
293     if(i==fNlocals-1) break;
294     pMinuit->mnpout(3*i+2,chname, qfracCoG[i], eps , b1, b2, ierflg);
295     qfraclast+=qfracCoG[i];
296    }
297   qfracCoG[fNlocals-1] = 1 - qfraclast;
298   delete pMinuit;
299
300   for(Int_t i=0;i<fNlocals;i++) {
301
302     if(fNlocals==5) {
303     cout << " Start writing  deconvolved cluster n." << i << endl;
304     cout << " fRawCluster " << &fRawCluster << " xCoG " << xCoG[i] << " yCoG " << yCoG[i] << " qfrac " << qfracCoG[i] << endl;
305     cout << " Combipid " << fLocalC[i] << " for maximum n. " << i << endl; 
306   }
307     fResolvedCluster.Fill(&fRawCluster,xCoG[i],yCoG[i],qfracCoG[i],fLocalC[i]);
308 //    fResolvedCluster.Print();
309     WriteResolvedCluster();
310   }
311 if(fNlocals==5)  Info("CoG","Stop.");
312 }//FitCoG()
313 //__________________________________________________________________________________________________
314 void RICHMinMathieson(Int_t &npar, Double_t *, Double_t &chi2, Double_t *par, Int_t )
315 {
316 // Mathieson minimization function 
317   
318   AliRICHcluster *pRawCluster = ((AliRICHClusterFinder*)gMinuit->GetObjectFit())->GetRawCluster();
319
320   TVector2 centroid[50];
321   Double_t q[50];
322   Int_t nFunctions = (npar+1)/3;
323   Double_t qfract = 0;
324   for(Int_t i=0;i<nFunctions;i++) {
325     centroid[i].Set(par[3*i],par[3*i+1]);
326     if(i==nFunctions-1) break;
327     q[i]=par[3*i+2];
328     qfract+=q[i];
329   }
330   q[nFunctions-1] = 1 - qfract;
331     
332   chi2 = 0;
333   Int_t qtot = pRawCluster->Q();
334   for(Int_t i=0;i<pRawCluster->Size();i++) {
335     Int_t    padX = ((AliRICHdigit *)pRawCluster->Digits()->At(i))->X();
336     Int_t    padY = ((AliRICHdigit *)pRawCluster->Digits()->At(i))->Y();
337     Double_t padQ = ((AliRICHdigit *)pRawCluster->Digits()->At(i))->Q();
338     Double_t qfracpar=0;
339     for(Int_t j=0;j<nFunctions;j++) {
340       qfracpar += q[j]*AliRICHParam::FracQdc(centroid[j],padX,padY);
341 //      cout << " function n. " << j+1 << " q " << q[j] << " fracMat " << AliRICHParam::FracQdc(centroid[j],padX,padY) 
342 //           << " xpar " << centroid[j].X() << " ypar " << centroid[j].Y() << endl;
343     }
344     chi2 += TMath::Power((qtot*qfracpar-padQ),2)/padQ;
345 //    cout << " chi2 " << chi2 << " pad n. " << i << " qfracpar " << qfracpar << endl;
346   }     
347 //  if(iflag==3) {
348 //    cout << " chi2 final " << chi2 << endl;
349 //  }
350 }//RICHMinMathieson()