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