]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALClusterizerNxN.cxx
be588f3948dfd450a4078e459558e75c13f497b8
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALClusterizerNxN.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 // This class derives from AliEMCALClustrerizer but also keeps the API of AliEMCALClusterizerv1
17 // Algorithm:
18 // 1. peek the most energetic cell
19 // 2. assign it as a center of the cluster and add cells surrounding it: 3x3, 5x5...
20 // 3. remove the cells contributing to the cluster
21 // 4. start from 1 for the remaining clusters
22 // 5. cluster splitting (not implemented yet) - use the shape analysis to resolve the energy sharing
23 // - for high energy clusters check the surrounding of the 3x3 clusters for extra energy 
24 // (merge 3x3 clusters and resolve the internal energy sharing - case for 2 clusters merged)
25 // Use Case:
26 //  root [0] AliEMCALClusterizerNxN * cl = new AliEMCALClusterizerNxN("galice.root")  
27 //  Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
28 //               //reads gAlice from header file "..."                      
29 //  root [1] cl->ExecuteTask()  
30 //               //finds RecPoints in all events stored in galice.root
31 //  root [2] cl->SetDigitsBranch("digits2") 
32 //               //sets another title for Digitis (input) branch
33 //  root [3] cl->SetRecPointsBranch("recp2")  
34 //               //sets another title four output branches
35 //  root [4] cl->SetTowerLocalMaxCut(0.03)  
36 //               //set clusterization parameters
37 //  root [5] cl->ExecuteTask("deb all time")  
38 //               //once more finds RecPoints options are 
39 //               // deb - print number of found rec points
40 //               // deb all - print number of found RecPoints and some their characteristics 
41 //               // time - print benchmarking results
42
43 // --- ROOT system ---
44 #include <TMath.h> 
45 #include <TMinuit.h>
46 #include <TTree.h> 
47 #include <TBenchmark.h>
48 #include <TBrowser.h>
49 #include <TROOT.h>
50 #include <TClonesArray.h>
51
52 // --- Standard library ---
53 #include <cassert>
54
55 // --- AliRoot header files ---
56 #include "AliLog.h"
57 #include "AliEMCALClusterizerNxN.h"
58 #include "AliEMCALRecPoint.h"
59 #include "AliEMCALDigit.h"
60 #include "AliEMCALGeometry.h"
61 #include "AliCaloCalibPedestal.h"
62 #include "AliEMCALCalibData.h"
63 #include "AliESDCaloCluster.h"
64
65 ClassImp(AliEMCALClusterizerNxN)
66
67 Bool_t AliEMCALClusterizerNxN::fgkIsInputCalibrated = kFALSE;
68
69 //____________________________________________________________________________
70 AliEMCALClusterizerNxN::AliEMCALClusterizerNxN()
71   : AliEMCALClusterizer()
72 {
73   // ctor with the indication of the file where header Tree and digits Tree are stored
74 }
75
76 //____________________________________________________________________________
77 AliEMCALClusterizerNxN::AliEMCALClusterizerNxN(AliEMCALGeometry* geometry)
78   : AliEMCALClusterizer(geometry)
79 {
80   // ctor with the indication of the file where header Tree and digits Tree are stored
81   // use this contructor to avoid usage of Init() which uses runloader
82   // change needed by HLT - MP
83
84 }
85
86 //____________________________________________________________________________
87 AliEMCALClusterizerNxN::AliEMCALClusterizerNxN(AliEMCALGeometry* geometry, AliEMCALCalibData * calib, AliCaloCalibPedestal * caloped)
88 : AliEMCALClusterizer(geometry, calib, caloped)
89 {
90         // ctor, geometry and calibration are initialized elsewhere.
91                                 
92 }
93
94
95 //____________________________________________________________________________
96   AliEMCALClusterizerNxN::~AliEMCALClusterizerNxN()
97 {
98   // dtor
99 }
100
101
102 //____________________________________________________________________________
103 void AliEMCALClusterizerNxN::Digits2Clusters(Option_t * option)
104 {
105   // Steering method to perform clusterization for the current event 
106   // in AliEMCALLoader
107   
108   if(strstr(option,"tim"))
109     gBenchmark->Start("EMCALClusterizer"); 
110   
111   if(strstr(option,"print"))
112     Print("") ; 
113   
114   //Get calibration parameters from file or digitizer default values.
115   GetCalibrationParameters() ;
116   
117   //Get dead channel map from file or digitizer default values.
118   GetCaloCalibPedestal() ;
119         
120   fNumberOfECAClusters = 0;
121   
122   MakeClusters() ;  //only the real clusters
123   
124   if(fToUnfold)
125     MakeUnfolding() ;
126   
127   Int_t index ;
128   
129   //Evaluate position, dispersion and other RecPoint properties for EC section                      
130   for(index = 0; index < fRecPoints->GetEntries(); index++) 
131   { 
132     AliEMCALRecPoint * rp = dynamic_cast<AliEMCALRecPoint *>(fRecPoints->At(index));
133     if(rp){
134       rp->EvalAll(fECAW0,fDigitsArr) ;
135       AliDebug(5, Form("MAX INDEX %d ", rp->GetMaximalEnergyIndex()));
136       //For each rec.point set the distance to the nearest bad crystal
137       rp->EvalDistanceToBadChannels(fCaloPed);
138     }
139   }
140   
141   fRecPoints->Sort() ;
142   
143   for(index = 0; index < fRecPoints->GetEntries(); index++) 
144   {
145     (dynamic_cast<AliEMCALRecPoint *>(fRecPoints->At(index)))->SetIndexInList(index) ;
146     (dynamic_cast<AliEMCALRecPoint *>(fRecPoints->At(index)))->Print();
147   }
148   
149   fTreeR->Fill();
150   
151   if(strstr(option,"deb") || strstr(option,"all"))  
152     PrintRecPoints(option) ;
153   
154   AliDebug(1,Form("EMCAL Clusterizer found %d Rec Points",fRecPoints->GetEntriesFast()));
155   
156   fRecPoints->Delete();
157   
158   if(strstr(option,"tim")){
159     gBenchmark->Stop("EMCALClusterizer");
160     printf("Exec took %f seconds for Clusterizing", 
161            gBenchmark->GetCpuTime("EMCALClusterizer"));
162   }    
163 }
164
165 //____________________________________________________________________________
166 Int_t AliEMCALClusterizerNxN::AreNeighbours(AliEMCALDigit * d1, AliEMCALDigit * d2, Bool_t & shared) const
167 {
168         // Gives the neighbourness of two digits = 0 are not neighbour ; continue searching 
169         //                                       = 1 are neighbour
170         //                                       = 2 is in different SM; continue searching 
171         // In case it is in different SM, but same phi rack, check if neigbours at eta=0
172         // neighbours are defined as digits having at least a common side 
173         // The order of d1 and d2 is important: first (d1) should be a digit already in a cluster 
174         //                                      which is compared to a digit (d2)  not yet in a cluster  
175         
176         static Int_t nSupMod1=0, nModule1=0, nIphi1=0, nIeta1=0, iphi1=0, ieta1=0;
177         static Int_t nSupMod2=0, nModule2=0, nIphi2=0, nIeta2=0, iphi2=0, ieta2=0;
178         static Int_t rowdiff=0, coldiff=0;
179
180         shared = kFALSE;
181         
182         fGeom->GetCellIndex(d1->GetId(), nSupMod1,nModule1,nIphi1,nIeta1);
183         fGeom->GetCellIndex(d2->GetId(), nSupMod2,nModule2,nIphi2,nIeta2);
184         fGeom->GetCellPhiEtaIndexInSModule(nSupMod1,nModule1,nIphi1,nIeta1, iphi1,ieta1);
185         fGeom->GetCellPhiEtaIndexInSModule(nSupMod2,nModule2,nIphi2,nIeta2, iphi2,ieta2);
186         
187         //If different SM, check if they are in the same phi, then consider cells close to eta=0 as neighbours; May 2010
188         if(nSupMod1 != nSupMod2 ) 
189           {
190             //Check if the 2 SM are in the same PHI position (0,1), (2,3), ...
191             Float_t smPhi1 = fGeom->GetEMCGeometry()->GetPhiCenterOfSM(nSupMod1);
192             Float_t smPhi2 = fGeom->GetEMCGeometry()->GetPhiCenterOfSM(nSupMod2);
193             
194             if(!TMath::AreEqualAbs(smPhi1, smPhi2, 1e-3)) return 2; //Not phi rack equal, not neighbours
195             
196             // In case of a shared cluster, index of SM in C side, columns start at 48 and ends at 48*2
197             // C Side impair SM, nSupMod%2=1; A side pair SM nSupMod%2=0
198             if(nSupMod1%2) ieta1+=AliEMCALGeoParams::fgkEMCALCols;
199             else           ieta2+=AliEMCALGeoParams::fgkEMCALCols;
200             
201             shared = kTRUE; // maybe a shared cluster, we know this later, set it for the moment.
202             
203         }//Different SM, same phi
204         
205         rowdiff = TMath::Abs(iphi1 - iphi2);  
206         coldiff = TMath::Abs(ieta1 - ieta2) ;  
207
208         // neighbours +-1 in col and row
209         if ( TMath::Abs(coldiff) < 2 && TMath::Abs(rowdiff) < 2)
210           {
211             
212             AliDebug(9, Form("AliEMCALClusterizerNxN::AreNeighbours(): id1=%d, (row %d, col %d) ; id2=%d, (row %d, col %d), shared %d \n",
213                              d1->GetId(), iphi1,ieta1, d2->GetId(), iphi2,ieta2, shared));
214             
215             return 1;
216           }//Neighbours
217         else 
218           {
219             AliDebug(9, Form("NOT AliEMCALClusterizerNxN::AreNeighbours(): id1=%d, (row %d, col %d) ; id2=%d, (row %d, col %d), shared %d \n",
220                              d1->GetId(), iphi1,ieta1, d2->GetId(), iphi2,ieta2, shared));
221             shared = kFALSE;
222             return 2 ; 
223           }//Not neighbours
224 }
225
226 //____________________________________________________________________________
227 void AliEMCALClusterizerNxN::MakeClusters()
228 {
229   // Steering method to construct the clusters stored in a list of Reconstructed Points
230   // A cluster is defined as a list of neighbour digits
231   // Mar 03, 2007 by PAI
232   
233   if (fGeom==0) AliFatal("Did not get geometry from EMCALLoader");
234   
235   fRecPoints->Clear();
236   
237   // Set up TObjArray with pointers to digits to work on 
238   //TObjArray *digitsC = new TObjArray();
239   TObjArray digitsC;
240   TIter nextdigit(fDigitsArr);
241   AliEMCALDigit *digit = 0;
242   while ( (digit = dynamic_cast<AliEMCALDigit*>(nextdigit())) ) {
243     digitsC.AddLast(digit);
244   }
245   
246   TIter nextdigitC(&digitsC);
247   
248   AliDebug(1,Form("MakeClusters: Number of digits %d  -> (e %f)\n",
249                   fDigitsArr->GetEntries(),fMinECut));
250   
251   Bool_t bDone = kFALSE;
252   while ( bDone != kTRUE )
253   {
254     //first sort the digits:
255     Int_t iMaxEnergyDigit = -1;
256     Float_t dMaxEnergyDigit = -1;
257     AliEMCALDigit *pMaxEnergyDigit = 0;
258     nextdigitC.Reset();
259     while ( (digit = dynamic_cast<AliEMCALDigit *>(nextdigitC())) ) 
260     { // scan over the list of digitsC
261       Float_t dEnergyCalibrated = Calibrate(digit->GetAmplitude(), digit->GetTime(),digit->GetId());
262       //AliDebug(5, Form("-> Digit ENERGY: %1.5f", dEnergyCalibrated));
263       
264       //if(fGeom->CheckAbsCellId(digit->GetId()) && dEnergyCalibrated > fECAClusteringThreshold  )
265       if(fGeom->CheckAbsCellId(digit->GetId()) && dEnergyCalibrated > 0.0) // no threshold!
266             {
267               if (dEnergyCalibrated > dMaxEnergyDigit)
268         {
269           dMaxEnergyDigit = dEnergyCalibrated;
270           iMaxEnergyDigit = digit->GetId();
271           pMaxEnergyDigit = digit;
272         }
273             }
274     }
275     
276     if (iMaxEnergyDigit < 0 || digitsC.GetEntries() <= 0) 
277     {
278       bDone = kTRUE;
279       continue;
280     }
281     
282     AliDebug (2, Form("Max digit found: %1.2f AbsId: %d", dMaxEnergyDigit, iMaxEnergyDigit));
283     AliDebug(5, Form("Max Digit ENERGY: %1.5f", dMaxEnergyDigit));
284     
285     // keep the candidate digits in a list
286     TList clusterDigitList;
287     clusterDigitList.SetOwner(kFALSE);
288     clusterDigitList.AddLast(pMaxEnergyDigit);   
289     
290     Double_t clusterCandidateEnergy = dMaxEnergyDigit;
291     
292     // now loop over the resto of the digits and cluster into NxN cluster 
293     // we do not actually cluster yet: we keep them in the list clusterDigitList
294     nextdigitC.Reset();
295     while ( (digit = dynamic_cast<AliEMCALDigit *>(nextdigitC())) ) 
296     { // scan over the list of digitsC
297       if (digit == pMaxEnergyDigit) continue;
298       Float_t dEnergyCalibrated = Calibrate(digit->GetAmplitude(), digit->GetTime(),digit->GetId());
299       AliDebug(5, Form("-> Digit ENERGY: %1.5f", dEnergyCalibrated));
300       //if(fGeom->CheckAbsCellId(digit->GetId()) && dEnergyCalibrated > fECAClusteringThreshold  )
301       if(fGeom->CheckAbsCellId(digit->GetId()) && dEnergyCalibrated > 0.0  )
302             {
303               Float_t time = pMaxEnergyDigit->GetTime(); //Time or TimeR?
304               if(TMath::Abs(time - digit->GetTime()) > fTimeCut ) continue; //Time or TimeR?
305               Bool_t shared = kFALSE; //cluster shared by 2 SuperModules?
306               if (AreNeighbours(pMaxEnergyDigit, digit, shared) == 1) // call (digit,digitN) in THAT order !!!!! 
307         {      
308           clusterDigitList.AddLast(digit) ;
309           clusterCandidateEnergy += dEnergyCalibrated;
310         }
311             }
312     }// loop over the next digits
313     
314     // start a cluster here only if a cluster energy is larger than clustering threshold
315     //if (clusterCandidateEnergy > 0.1)
316     AliDebug(5, Form("Clusterization threshold is %f MeV", fECAClusteringThreshold));
317     if (clusterCandidateEnergy > fECAClusteringThreshold)
318     {
319       if(fNumberOfECAClusters >= fRecPoints->GetSize()) fRecPoints->Expand(2*fNumberOfECAClusters+1) ;
320       
321       AliEMCALRecPoint *recPoint = new  AliEMCALRecPoint("") ; 
322       fRecPoints->AddAt(recPoint, fNumberOfECAClusters) ;
323       recPoint = dynamic_cast<AliEMCALRecPoint *>(fRecPoints->At(fNumberOfECAClusters)) ; 
324       if(recPoint){
325         fNumberOfECAClusters++ ;       
326         recPoint->SetClusterType(AliVCluster::kEMCALClusterv1);
327         
328         AliDebug(9, Form("Number of cells per cluster (max is 9!): %d", clusterDigitList.GetEntries()));
329         for (Int_t idig = 0; idig < clusterDigitList.GetEntries(); idig++)
330         {
331           
332           digit = (AliEMCALDigit*)clusterDigitList.At(idig);
333           Float_t dEnergyCalibrated = Calibrate(digit->GetAmplitude(), digit->GetTime(),digit->GetId());
334           AliDebug(5, Form(" Adding digit %d", digit->GetId()));
335           // note: this way the sharing info is lost!
336           recPoint->AddDigit(*digit, dEnergyCalibrated, kFALSE) ; //Time or TimeR?
337           digitsC.Remove(digit);                  
338         }
339       }// recpoint
340     }
341     else
342     {
343       // we do not want to start clustering in the same spot!
344       // but in this case we may NOT reuse this seed for another cluster!
345       // need a better bookeeping?
346       digitsC.Remove(pMaxEnergyDigit);
347     }
348     
349     AliDebug (2, Form("Number of digits left: %d", digitsC.GetEntries()));      
350   } // while ! done 
351   
352   //delete digitsC ; //nope we use an object
353   
354   AliDebug(1,Form("total no of clusters %d from %d digits",fNumberOfECAClusters,fDigitsArr->GetEntriesFast())); 
355 }
356
357 //____________________________________________________________________________
358 void AliEMCALClusterizerNxN::MakeUnfolding()
359 {
360   // Unfolds clusters using the shape of an ElectroMagnetic shower
361   // Performs unfolding of all clusters
362                 
363   if(fNumberOfECAClusters > 0){
364     if (fGeom==0)
365       AliFatal("Did not get geometry from EMCALLoader") ;
366     Int_t nModulesToUnfold = fGeom->GetNCells();
367
368     Int_t numberofNotUnfolded = fNumberOfECAClusters ;
369     Int_t index ;
370     for(index = 0 ; index < numberofNotUnfolded ; index++){
371
372       AliEMCALRecPoint * recPoint = dynamic_cast<AliEMCALRecPoint *>( fRecPoints->At(index) ) ;
373
374       TVector3 gpos;
375       Int_t absId;
376       recPoint->GetGlobalPosition(gpos);
377       fGeom->GetAbsCellIdFromEtaPhi(gpos.Eta(),gpos.Phi(),absId);
378       if(absId > nModulesToUnfold)
379         break ;
380
381       Int_t nMultipl = recPoint->GetMultiplicity() ;
382       AliEMCALDigit ** maxAt = new AliEMCALDigit*[nMultipl] ;
383       Float_t * maxAtEnergy = new Float_t[nMultipl] ;
384       Int_t nMax = recPoint->GetNumberOfLocalMax(maxAt, maxAtEnergy,fECALocMaxCut,fDigitsArr) ;
385
386       if( nMax > 1 ) {     // if cluster is very flat (no pronounced maximum) then nMax = 0
387         //UnfoldCluster(recPoint, nMax, maxAt, maxAtEnergy) ;
388         fRecPoints->Remove(recPoint);
389         fRecPoints->Compress() ;
390         index-- ;
391         fNumberOfECAClusters-- ;
392         numberofNotUnfolded-- ;
393       }
394       else{
395         recPoint->SetNExMax(1) ; //Only one local maximum
396       }
397
398       delete[] maxAt ;
399       delete[] maxAtEnergy ;
400     }
401   }
402   // End of Unfolding of clusters
403 }
404
405 //____________________________________________________________________________
406 Double_t  AliEMCALClusterizerNxN::ShowerShape(Double_t x, Double_t y)
407
408   // Shape of the shower
409   // If you change this function, change also the gradient evaluation in ChiSquare()
410   //
411   Double_t r = sqrt(x*x+y*y);
412   Double_t r133  = TMath::Power(r, 1.33) ;
413   Double_t r669  = TMath::Power(r, 6.69) ;
414   Double_t shape = TMath::Exp( -r133 * (1. / (1.57 + 0.0860 * r133) - 0.55 / (1 + 0.000563 * r669) ) ) ;
415   return shape ;
416 }
417
418 //____________________________________________________________________________
419 void  AliEMCALClusterizerNxN::UnfoldCluster(AliEMCALRecPoint * /*iniTower*/, 
420                                             Int_t /*nMax*/, 
421                                             AliEMCALDigit ** /*maxAt*/, 
422                                             Float_t * /*maxAtEnergy*/)
423 {
424   //
425   // Performs the unfolding of a cluster with nMax overlapping showers 
426   //
427   AliWarning("Not implemented. To be.");
428 }
429
430 //___________________________________________________________________
431 void   AliEMCALClusterizerNxN::SetInputCalibrated(Bool_t val)
432 {
433   //
434   // input is calibrated - the case when we run already on ESD
435   //
436   AliEMCALClusterizerNxN::fgkIsInputCalibrated = val;
437 }