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>
26 void RICHMinMathieson(Int_t &npar, Double_t *gin, Double_t &chi2, Double_t *par, Int_t iflag);
28 ClassImp(AliRICHClusterFinder)
29 //__________________________________________________________________________________________________
30 AliRICHClusterFinder::AliRICHClusterFinder(AliRICH *pRICH)
32 // Info("main ctor","Start.");
38 fResolvedCluster.Reset();
41 //__________________________________________________________________________________________________
42 void AliRICHClusterFinder::FindLocalMaxima()
44 // Find number of local maxima in the cluster
45 // Info("FindLocalMaxima","Start.");
48 for(Int_t iDig1=0;iDig1<fRawCluster.Size();iDig1++) {
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++;
68 if (fNlocals >= fLocalX.GetSize()) {
69 fLocalX.Set(fNlocals+100);
70 fLocalY.Set(fNlocals+100);
71 fLocalQ.Set(fNlocals+100);
72 fLocalC.Set(fNlocals+100);
74 TVector2 x2=AliRICHParam::Pad2Loc(padX1,padY1);
75 fLocalX[fNlocals]=x2.X();fLocalY[fNlocals]=x2.Y();
76 fLocalQ[fNlocals] = (Double_t)padQ1;
77 fLocalC[fNlocals] = padC1;
82 //__________________________________________________________________________________________________
83 void AliRICHClusterFinder::Exec()
85 if(GetDebug()) Info("Exec","Start.");
88 Rich()->GetLoader()->LoadDigits();
90 Rich()->GetLoader()->GetRunLoader()->LoadHeader();
91 Rich()->GetLoader()->GetRunLoader()->LoadKinematics();
93 for(Int_t iEventN=0;iEventN<gAlice->GetEventsPerRun();iEventN++){//events loop
94 if(GetDebug()) Info("Exec","Event %i processed.",iEventN+1);
95 // gAlice->GetRunLoader()->GetEvent(iEventN);
96 Rich()->GetLoader()->GetRunLoader()->GetEvent(iEventN);
98 Rich()->GetLoader()->MakeTree("R"); Rich()->MakeBranch("R");
99 Rich()->ResetDigits(); Rich()->ResetClusters();
101 Rich()->GetLoader()->TreeD()->GetEntry(0);
102 for(Int_t iChamber=1;iChamber<=kNCH;iChamber++){//chambers loop
103 FindClusters(iChamber);
105 Rich()->GetLoader()->TreeR()->Fill();
106 Rich()->GetLoader()->WriteRecPoints("OVERWRITE");
108 Rich()->GetLoader()->UnloadDigits(); Rich()->GetLoader()->UnloadRecPoints();
109 Rich()->ResetDigits(); Rich()->ResetClusters();
111 Rich()->GetLoader()->GetRunLoader()->UnloadHeader();
112 Rich()->GetLoader()->GetRunLoader()->UnloadKinematics();
114 if(GetDebug()) Info("Exec","Stop.");
116 //__________________________________________________________________________________________________
117 void AliRICHClusterFinder::FindClusters(Int_t iChamber)
119 //finds neighbours and fill the tree with raw clusters
120 Int_t nDigits=Rich()->Digits(iChamber)->GetEntriesFast();
121 // Info("FindClusters","Start for Chamber %i with %i digits.",iChamber,nDigits);
122 if(nDigits==0)return;
124 fHitMap=new AliRICHMap(Rich()->Digits(iChamber));//create digit map for the given chamber
126 for(Int_t iDig=0;iDig<nDigits;iDig++){
127 AliRICHdigit *dig=(AliRICHdigit*)Rich()->Digits(iChamber)->At(iDig);
128 Int_t i=dig->X(); Int_t j=dig->Y();
129 if(fHitMap->TestHit(i,j)==kUsed) continue;
133 // cout << " fNlocals in FindCluster " << fNlocals << endl;
134 fRawCluster.CoG(fNlocals); // first initial approxmation of the CoG...to start minimization.
136 if(AliRICHParam::IsResolveClusters()) {
137 ResolveCluster(); // ResolveCluster serialization will happen inside
139 WriteRawCluster(); // simply output of the RawCluster found without deconvolution
142 fResolvedCluster.Reset();
146 // Info("FindClusters","Stop.");
149 //__________________________________________________________________________________________________
150 void AliRICHClusterFinder::FindClusterContribs(AliRICHcluster *pCluster)
152 // finds CombiPid for a given cluster
153 // Info("FindClusterContribs","Start");
155 TObjArray *pDigits = pCluster->Digits();
156 Int_t iNmips=0,iNckovs=0,iNfeeds=0;
157 TArrayI contribs(3*pCluster->Size());
158 Int_t *pindex = new Int_t[3*pCluster->Size()];
159 for(Int_t iDigN=0;iDigN<pCluster->Size();iDigN++) {//loop on digits of a given cluster
160 contribs[3*iDigN] =((AliRICHdigit*)pDigits->At(iDigN))->Tid(0);
161 if (contribs[3*iDigN] >= 10000000) contribs[3*iDigN] = 0;
162 contribs[3*iDigN+1]=((AliRICHdigit*)pDigits->At(iDigN))->Tid(1);
163 if (contribs[3*iDigN+1] >= 10000000) contribs[3*iDigN+1] = 0;
164 contribs[3*iDigN+2]=((AliRICHdigit*)pDigits->At(iDigN))->Tid(2);
165 if (contribs[3*iDigN+2] >= 10000000) contribs[3*iDigN+2] = 0;
166 }//loop on digits of a given cluster
167 TMath::Sort(contribs.GetSize(),contribs.GetArray(),pindex);
168 for(Int_t iDigN=0;iDigN<3*pCluster->Size()-1;iDigN++) {//loop on digits to sort Tid
169 if(contribs[pindex[iDigN]]!=contribs[pindex[iDigN+1]]) {
170 TParticle* particle = Rich()->GetLoader()->GetRunLoader()->Stack()->Particle(contribs[pindex[iDigN]]);
171 Int_t code = particle->GetPdgCode();
173 if (particle->GetPDG()) particle->GetPDG()->Charge();
175 if(code==50000050) iNckovs++;
176 else if(code==50000051) iNfeeds++;
177 else if(charge!=0) iNmips++;
178 if (contribs[pindex[iDigN+1]]==kBad) break;
180 }//loop on digits to sort Tid
181 pCluster->SetCombiPid(iNckovs,iNfeeds,iNmips);
182 // pCluster->Print();
184 }//FindClusterContribs()
185 //__________________________________________________________________________________________________
186 void AliRICHClusterFinder::FormRawCluster(Int_t i, Int_t j)
188 // Builder of the final Raw Cluster (before deconvolution)
189 if(GetDebug()) Info("FormRawCluster","Start with digit(%i,%i)",i,j);
191 fRawCluster.AddDigit((AliRICHdigit*) fHitMap->GetHit(i,j));
192 fHitMap->FlagHit(i,j);// Flag hit as taken
194 Int_t listX[4], listY[4]; // Now look recursively for all neighbours
195 for (Int_t iNeighbour=0;iNeighbour<Rich()->P()->PadNeighbours(i,j,listX,listY);iNeighbour++)
196 if(fHitMap->TestHit(listX[iNeighbour],listY[iNeighbour])==kUnused)
197 FormRawCluster(listX[iNeighbour],listY[iNeighbour]);
199 //__________________________________________________________________________________________________
200 void AliRICHClusterFinder::ResolveCluster()
201 {// Decluster algorithm
202 if(GetDebug()) {Info("ResolveCluster","Start."); fRawCluster.Print();}
204 switch (fRawCluster.Size()) {
206 case 1: // nothing to decluster: cluster size = 1
207 WriteRawCluster();break;
208 default: // cluster size > 1: if=2 FitCoG; if>2 Resolve and FitCoG
212 //__________________________________________________________________________________________________
213 void AliRICHClusterFinder::WriteRawCluster()
215 // out the current raw cluster
216 if(GetDebug()) Info("WriteRawCluster","Start.");
218 FindClusterContribs(&fRawCluster);
219 if(GetDebug()) fRawCluster.Dump();
220 Rich()->AddCluster(fRawCluster);
221 // fRawCluster.Print();
223 //__________________________________________________________________________________________________
224 void AliRICHClusterFinder::WriteResolvedCluster()
226 // out the current resolved cluster
227 if(GetDebug()) Info("WriteResolvedCluster","Start.");
229 // FindClusterContribs(&fResolvedCluster);
230 Rich()->AddCluster(fResolvedCluster);
232 }//WriteResolvedCluster()
233 //__________________________________________________________________________________________________
234 void AliRICHClusterFinder::FitCoG()
235 {// Fit cluster size 2 by single Mathieson
236 if(GetDebug()) Info("FitCoG","Start with size %3i and local maxima %3i",fRawCluster.Size(),fNlocals);
241 // fRawCluster.Print();
242 // if(fNlocals==0) fRawCluster.Print();
243 if(fNlocals==0||fNlocals>3) {WriteRawCluster();return;}
245 TMinuit *pMinuit = new TMinuit(3*fNlocals-1);
246 pMinuit->mninit(5,10,7);
249 pMinuit->mnexcm("SET PRI",&arglist, 1, ierflag);
250 pMinuit->mnexcm("SET NOW",&arglist, 0, ierflag);
255 pMinuit->SetObjectFit((TObject*)this);
256 pMinuit->SetFCN(RICHMinMathieson);
258 Double_t vstart,lower, upper;
259 Double_t stepX= 0.001;
260 Double_t stepY= 0.001;
261 Double_t stepQ= 0.0001;
263 for(Int_t i=0;i<fNlocals;i++) {
265 lower = vstart - 2*AliRICHParam::PadSizeX();
266 upper = vstart + 2*AliRICHParam::PadSizeX();
267 pMinuit->mnparm(3*i ,Form("xCoG %i",i),vstart,stepX,lower,upper,ierflag);
268 // cout << Form("xCoG %i",i) << "start " << vstart << "lower " << lower << "upper " << upper << endl;
270 lower = vstart - 2*AliRICHParam::PadSizeY();
271 upper = vstart + 2*AliRICHParam::PadSizeY();
272 pMinuit->mnparm(3*i+1,Form("yCoG %i",i),vstart,stepY,lower,upper,ierflag);
273 // cout << Form("yCoG %i",i) << "start " << vstart << "lower " << lower << "upper " << upper << endl;
274 if(i==fNlocals-1) break; // last parameter is constrained
275 vstart = fLocalQ[i]/fRawCluster.Q();
278 pMinuit->mnparm(3*i+2,Form("qfrac %i",i),vstart,stepQ,lower,upper,ierflag);
279 // cout << Form("qCoG %i",i) << "start " << vstart << "lower " << lower << "upper " << upper << endl;
284 pMinuit->mnexcm("SET NOGR",&arglist, 1, ierflag);
286 pMinuit->mnexcm("SET ERR", &arglist, 1,ierflg);
288 pMinuit->mnexcm("SIMPLEX",&arglist, 0, ierflag);
289 pMinuit->mnexcm("MIGRAD",&arglist, 0, ierflag);
290 pMinuit->mnexcm("EXIT" ,&arglist, 0, ierflag);
292 Double_t xCoG[50],yCoG[50],qfracCoG[50];
293 Double_t eps, b1, b2;
295 Double_t qfraclast=0;
296 for(Int_t i=0;i<fNlocals;i++) {
297 pMinuit->mnpout(3*i ,chname, xCoG[i], eps , b1, b2, ierflg);
298 pMinuit->mnpout(3*i+1,chname, yCoG[i], eps , b1, b2, ierflg);
299 if(i==fNlocals-1) break;
300 pMinuit->mnpout(3*i+2,chname, qfracCoG[i], eps , b1, b2, ierflg);
301 qfraclast+=qfracCoG[i];
303 qfracCoG[fNlocals-1] = 1 - qfraclast;
306 for(Int_t i=0;i<fNlocals;i++) {
309 cout << " Start writing deconvolved cluster n." << i << endl;
310 cout << " fRawCluster " << &fRawCluster << " xCoG " << xCoG[i] << " yCoG " << yCoG[i] << " qfrac " << qfracCoG[i] << endl;
311 cout << " Combipid " << fLocalC[i] << " for maximum n. " << i << endl;
313 fResolvedCluster.Fill(&fRawCluster,xCoG[i],yCoG[i],qfracCoG[i],fLocalC[i]);
314 // fResolvedCluster.Print();
315 WriteResolvedCluster();
317 if(fNlocals==5) Info("CoG","Stop.");
319 //__________________________________________________________________________________________________
320 void RICHMinMathieson(Int_t &npar, Double_t *, Double_t &chi2, Double_t *par, Int_t )
322 // Mathieson minimization function
324 AliRICHcluster *pRawCluster = ((AliRICHClusterFinder*)gMinuit->GetObjectFit())->GetRawCluster();
326 TVector2 centroid[50];
328 Int_t nFunctions = (npar+1)/3;
330 for(Int_t i=0;i<nFunctions;i++) {
331 centroid[i].Set(par[3*i],par[3*i+1]);
332 if(i==nFunctions-1) break;
336 q[nFunctions-1] = 1 - qfract;
339 Int_t qtot = pRawCluster->Q();
340 for(Int_t i=0;i<pRawCluster->Size();i++) {
341 Int_t padX = ((AliRICHdigit *)pRawCluster->Digits()->At(i))->X();
342 Int_t padY = ((AliRICHdigit *)pRawCluster->Digits()->At(i))->Y();
343 Double_t padQ = ((AliRICHdigit *)pRawCluster->Digits()->At(i))->Q();
345 for(Int_t j=0;j<nFunctions;j++) {
346 qfracpar += q[j]*AliRICHParam::FracQdc(centroid[j],padX,padY);
347 // cout << " function n. " << j+1 << " q " << q[j] << " fracMat " << AliRICHParam::FracQdc(centroid[j],padX,padY)
348 // << " xpar " << centroid[j].X() << " ypar " << centroid[j].Y() << endl;
350 chi2 += TMath::Power((qtot*qfracpar-padQ),2)/padQ;
351 // cout << " chi2 " << chi2 << " pad n. " << i << " qfracpar " << qfracpar << endl;
354 // cout << " chi2 final " << chi2 << endl;
356 }//RICHMinMathieson()