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