]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/TenderSupplies/AliEMCALTenderSupply.cxx
1c7521792c0a0222ce2a62d69bac8aa93fbcb1b1
[u/mrichter/AliRoot.git] / ANALYSIS / TenderSupplies / AliEMCALTenderSupply.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 //                                                                           //
18 //  EMCAL tender, apply corrections to EMCAL clusters and do track matching. //
19 //                                                                           //
20 //  Author: Deepa Thomas (Utrecht University)                                // 
21 //  Later mods/rewrite: Jiri Kral (University of Jyvaskyla)                  //
22 //  S. Aiola, C. Loizides : Make it work for AODs                            //
23 //                                                                           //
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #include <TROOT.h>
27 #include <TFile.h>
28 #include <TTree.h>
29 #include <TInterpreter.h>
30 #include <TObjArray.h>
31 #include <TClonesArray.h>
32 #include <TGeoGlobalMagField.h>
33 #include "AliEMCALAfterBurnerUF.h"
34 #include "AliEMCALClusterizer.h"
35 #include "AliEMCALClusterizerNxN.h"
36 #include "AliEMCALClusterizerv1.h"
37 #include "AliEMCALClusterizerv2.h"
38 #include "AliEMCALDigit.h"
39 #include "AliEMCALGeometry.h"
40 #include "AliEMCALRecParam.h"
41 #include "AliEMCALRecParam.h"
42 #include "AliEMCALRecPoint.h"
43 #include "AliEMCALRecoUtils.h"
44 #include "AliEMCALTenderSupply.h"
45 #include "AliESDCaloCluster.h"
46 #include "AliMagF.h"
47 #include "AliOADBContainer.h"
48 #include "AliAODEvent.h"
49 #include "AliAnalysisManager.h"
50 #include "AliESDEvent.h"
51 #include "AliLog.h"
52 #include "AliTender.h"
53
54 ClassImp(AliEMCALTenderSupply)
55
56 AliEMCALTenderSupply::AliEMCALTenderSupply() :
57 AliTenderSupply()
58 ,fTask(0)
59 ,fRun(0)
60 ,fEMCALGeo(0x0)
61 ,fEMCALGeoName("")
62 ,fEMCALRecoUtils(0)
63 ,fConfigName("")
64 ,fDebugLevel(0)
65 ,fNonLinearFunc(-1) 
66 ,fNonLinearThreshold(-1)
67 ,fReCalibCluster(kFALSE)
68 ,fUpdateCell(kFALSE)  
69 ,fCalibrateEnergy(kFALSE)
70 ,fCalibrateTime(kFALSE)
71 ,fDoNonLinearity(kFALSE)
72 ,fBadCellRemove(kFALSE)
73 ,fRejectExoticCells(kFALSE)
74 ,fRejectExoticClusters(kFALSE)
75 ,fClusterBadChannelCheck(kFALSE)
76 ,fRecalClusPos(kFALSE)
77 ,fFiducial(kFALSE) 
78 ,fNCellsFromEMCALBorder(-1)
79 ,fRecalDistToBadChannels(kFALSE)
80 ,fRecalShowerShape(kFALSE)
81 ,fInputTree(0)
82 ,fInputFile(0)
83 ,fFilepass(0) 
84 ,fMass(-1)
85 ,fStep(-1)
86 ,fCutEtaPhiSum(kTRUE)
87 ,fCutEtaPhiSeparate(kFALSE)
88 ,fRcut(-1)
89 ,fEtacut(-1)
90 ,fPhicut(-1)
91 ,fBasePath("")
92 ,fReClusterize(kFALSE)
93 ,fClusterizer(0)
94 ,fGeomMatrixSet(kFALSE)
95 ,fLoadGeomMatrices(kFALSE)
96 ,fRecParam(0x0)
97 ,fDoTrackMatch(kFALSE)
98 ,fDoUpdateOnly(kFALSE)
99 ,fUnfolder(0)
100 ,fDigitsArr(0)
101 ,fClusterArr(0)
102 ,fMisalignSurvey(kdefault)  
103 ,fExoticCellFraction(-1)
104 ,fExoticCellDiffTime(-1)
105 ,fExoticCellMinAmplitude(-1)
106 {
107   // Default constructor.
108
109   for(Int_t i = 0; i < 12; i++) fEMCALMatrix[i] = 0 ;
110 }
111
112 //_____________________________________________________
113 AliEMCALTenderSupply::AliEMCALTenderSupply(const char *name, const AliTender *tender) :
114 AliTenderSupply(name,tender)
115 ,fTask(0)
116 ,fRun(0)
117 ,fEMCALGeo(0x0)
118 ,fEMCALGeoName("")
119 ,fEMCALRecoUtils(0)
120 ,fConfigName("")
121 ,fDebugLevel(0)
122 ,fNonLinearFunc(-1) 
123 ,fNonLinearThreshold(-1)        
124 ,fReCalibCluster(kFALSE)  
125 ,fUpdateCell(kFALSE)  
126 ,fCalibrateEnergy(kFALSE)
127 ,fCalibrateTime(kFALSE)
128 ,fDoNonLinearity(kFALSE)
129 ,fBadCellRemove(kFALSE)
130 ,fRejectExoticCells(kFALSE)
131 ,fRejectExoticClusters(kFALSE)
132 ,fClusterBadChannelCheck(kFALSE)
133 ,fRecalClusPos(kFALSE)
134 ,fFiducial(kFALSE) 
135 ,fNCellsFromEMCALBorder(-1)  
136 ,fRecalDistToBadChannels(kFALSE)  
137 ,fRecalShowerShape(kFALSE)
138 ,fInputTree(0)  
139 ,fInputFile(0)
140 ,fFilepass(0) 
141 ,fMass(-1)
142 ,fStep(-1)
143 ,fCutEtaPhiSum(kTRUE)
144 ,fCutEtaPhiSeparate(kFALSE)
145 ,fRcut(-1)  
146 ,fEtacut(-1)  
147 ,fPhicut(-1)  
148 ,fBasePath("")
149 ,fReClusterize(kFALSE)
150 ,fClusterizer(0)
151 ,fGeomMatrixSet(kFALSE)
152 ,fLoadGeomMatrices(kFALSE)
153 ,fRecParam(0x0)
154 ,fDoTrackMatch(kFALSE)
155 ,fDoUpdateOnly(kFALSE)
156 ,fUnfolder(0)
157 ,fDigitsArr(0)
158 ,fClusterArr(0)
159 ,fMisalignSurvey(kdefault)  
160 ,fExoticCellFraction(-1)
161 ,fExoticCellDiffTime(-1)
162 ,fExoticCellMinAmplitude(-1)
163 {
164   // Named constructor
165   
166   for(Int_t i = 0; i < 12; i++) fEMCALMatrix[i] = 0 ;
167 }
168
169 //_____________________________________________________
170 AliEMCALTenderSupply::AliEMCALTenderSupply(const char *name, AliAnalysisTaskSE *task) :
171 AliTenderSupply(name)
172 ,fTask(task)
173 ,fRun(0)
174 ,fEMCALGeo(0x0)
175 ,fEMCALGeoName("")
176 ,fEMCALRecoUtils(0)
177 ,fConfigName("")
178 ,fDebugLevel(0)
179 ,fNonLinearFunc(-1) 
180 ,fNonLinearThreshold(-1)        
181 ,fReCalibCluster(kFALSE)  
182 ,fUpdateCell(kFALSE)  
183 ,fCalibrateEnergy(kFALSE)
184 ,fCalibrateTime(kFALSE)
185 ,fDoNonLinearity(kFALSE)
186 ,fBadCellRemove(kFALSE)
187 ,fRejectExoticCells(kFALSE)
188 ,fRejectExoticClusters(kFALSE)
189 ,fClusterBadChannelCheck(kFALSE)
190 ,fRecalClusPos(kFALSE)
191 ,fFiducial(kFALSE) 
192 ,fNCellsFromEMCALBorder(-1)  
193 ,fRecalDistToBadChannels(kFALSE)  
194 ,fRecalShowerShape(kFALSE)
195 ,fInputTree(0)  
196 ,fInputFile(0)
197 ,fFilepass(0) 
198 ,fMass(-1)
199 ,fStep(-1)
200 ,fCutEtaPhiSum(kTRUE)
201 ,fCutEtaPhiSeparate(kFALSE)
202 ,fRcut(-1)  
203 ,fEtacut(-1)  
204 ,fPhicut(-1)  
205 ,fBasePath("")
206 ,fReClusterize(kFALSE)
207 ,fClusterizer(0)
208 ,fGeomMatrixSet(kFALSE)
209 ,fLoadGeomMatrices(kFALSE)
210 ,fRecParam(0x0)
211 ,fDoTrackMatch(kFALSE)
212 ,fDoUpdateOnly(kFALSE)
213 ,fUnfolder(0)
214 ,fDigitsArr(0)
215 ,fClusterArr(0)
216 ,fMisalignSurvey(kdefault)  
217 ,fExoticCellFraction(-1)
218 ,fExoticCellDiffTime(-1)
219 ,fExoticCellMinAmplitude(-1)
220 {
221   // Named constructor.
222   
223   for(Int_t i = 0; i < 12; i++) fEMCALMatrix[i] = 0 ;
224 }
225
226 //_____________________________________________________
227 AliEMCALTenderSupply::~AliEMCALTenderSupply()
228 {
229   //Destructor
230   
231   if (!AliAnalysisManager::GetAnalysisManager()->IsProofMode()) 
232   {
233     delete fEMCALRecoUtils;
234     delete fRecParam;
235     delete fUnfolder;
236     if (!fClusterizer) 
237     {
238       fDigitsArr->Clear("C");
239       delete fDigitsArr; 
240     } else 
241     {
242       delete fClusterizer;
243       fDigitsArr = 0;
244     }
245   }
246 }
247
248 //_____________________________________________________
249 void AliEMCALTenderSupply::SetDefaults()
250 {
251   // Set default settings.
252
253   SwitchOnBadCellRemove();
254   SwitchOnExoticCellRemove();
255   SwitchOnCalibrateEnergy();
256   SwitchOnCalibrateTime();
257   SwitchOnUpdateCell();
258   SwitchOnReclustering();
259   SwitchOnClusterBadChannelCheck();
260   SwitchOnClusterExoticChannelCheck();
261   SwitchOnTrackMatch();
262 }
263
264 //_____________________________________________________
265 Bool_t AliEMCALTenderSupply::RunChanged() const
266 {
267   // Get run number.
268
269   return (fTender && fTender->RunChanged()) || (fTask && fRun != fTask->InputEvent()->GetRunNumber()); 
270 }
271
272 //_____________________________________________________
273 void AliEMCALTenderSupply::Init()
274 {
275   // Initialise EMCAL tender.
276
277   if (fDebugLevel>0) 
278     AliWarning("Init EMCAL Tender supply"); 
279   
280   if (fConfigName.Length()>0 && gROOT->LoadMacro(fConfigName) >=0) {
281     AliDebug(1, Form("Loading settings from macro %s", fConfigName.Data()));
282     AliEMCALTenderSupply *tender = (AliEMCALTenderSupply*)gInterpreter->ProcessLine("ConfigEMCALTenderSupply()");
283     fDebugLevel             = tender->fDebugLevel;
284     fEMCALGeoName           = tender->fEMCALGeoName; 
285     fEMCALRecoUtils         = tender->fEMCALRecoUtils; 
286     fConfigName             = tender->fConfigName;
287     fNonLinearFunc          = tender->fNonLinearFunc;
288     fNonLinearThreshold     = tender->fNonLinearThreshold;
289     fReCalibCluster         = tender->fReCalibCluster;
290     fUpdateCell             = tender->fUpdateCell;
291     fRecalClusPos           = tender->fRecalClusPos;
292     fCalibrateEnergy        = tender->fCalibrateEnergy;
293     fCalibrateTime          = tender->fCalibrateTime;
294     fFiducial               = tender->fFiducial;
295     fNCellsFromEMCALBorder  = tender->fNCellsFromEMCALBorder;
296     fRecalDistToBadChannels = tender->fRecalDistToBadChannels;    
297     fRecalShowerShape       = tender->fRecalShowerShape;
298     fClusterBadChannelCheck = tender->fClusterBadChannelCheck;
299     fBadCellRemove          = tender->fBadCellRemove;
300     fRejectExoticCells      = tender->fRejectExoticCells;
301     fRejectExoticClusters   = tender->fRejectExoticClusters;
302     fMass                   = tender->fMass;
303     fStep                   = tender->fStep;
304     fCutEtaPhiSum           = tender->fCutEtaPhiSum;
305     fCutEtaPhiSeparate      = tender->fCutEtaPhiSeparate;
306     fRcut                   = tender->fRcut;
307     fEtacut                 = tender->fEtacut;
308     fPhicut                 = tender->fPhicut;
309     fReClusterize           = tender->fReClusterize;
310     fLoadGeomMatrices       = tender->fLoadGeomMatrices;
311     fRecParam               = tender->fRecParam;
312     fDoNonLinearity         = tender->fDoNonLinearity;
313     fDoTrackMatch           = tender->fDoTrackMatch;
314     fDoUpdateOnly           = tender->fDoUpdateOnly;
315     fMisalignSurvey         = tender->fMisalignSurvey;
316     fExoticCellFraction     = tender->fExoticCellFraction;
317     fExoticCellDiffTime     = tender->fExoticCellDiffTime;
318     fExoticCellMinAmplitude = tender->fExoticCellMinAmplitude;
319
320     for(Int_t i = 0; i < 12; i++) 
321       fEMCALMatrix[i] = tender->fEMCALMatrix[i] ;
322   }
323   
324   if (fDebugLevel>0){
325     AliInfo( "Emcal Tender settings: ======================================" ); 
326     AliInfo( "------------ Switches --------------------------" ); 
327     AliInfo( Form( "BadCellRemove : %d", fBadCellRemove )); 
328     AliInfo( Form( "ExoticCellRemove : %d", fRejectExoticCells )); 
329     AliInfo( Form( "CalibrateEnergy : %d", fCalibrateEnergy )); 
330     AliInfo( Form( "CalibrateTime : %d", fCalibrateTime )); 
331     AliInfo( Form( "UpdateCell : %d", fUpdateCell )); 
332     AliInfo( Form( "DoUpdateOnly : %d", fDoUpdateOnly )); 
333     AliInfo( Form( "Reclustering : %d", fReClusterize )); 
334     AliInfo( Form( "ClusterBadChannelCheck : %d", fClusterBadChannelCheck )); 
335     AliInfo( Form( "ClusterExoticChannelCheck : %d", fRejectExoticClusters )); 
336     AliInfo( Form( "CellFiducialRegion : %d", fFiducial )); 
337     AliInfo( Form( "ReCalibrateCluster : %d", fReCalibCluster )); 
338     AliInfo( Form( "RecalculateClusPos : %d", fRecalClusPos )); 
339     AliInfo( Form( "RecalShowerShape : %d", fRecalShowerShape )); 
340     AliInfo( Form( "NonLinearityCorrection : %d", fDoNonLinearity )); 
341     AliInfo( Form( "RecalDistBadChannel : %d", fRecalDistToBadChannels )); 
342     AliInfo( Form( "TrackMatch : %d", fDoTrackMatch )); 
343     AliInfo( "------------ Variables -------------------------" ); 
344     AliInfo( Form( "DebugLevel : %d", fDebugLevel )); 
345     AliInfo( Form( "BasePath : %s", fBasePath.Data() )); 
346     AliInfo( Form( "ConfigFileName : %s", fConfigName.Data() )); 
347     AliInfo( Form( "EMCALGeometryName : %s", fEMCALGeoName.Data() )); 
348     AliInfo( Form( "NonLinearityFunction : %d", fNonLinearFunc )); 
349     AliInfo( Form( "NonLinearityThreshold : %d", fNonLinearThreshold )); 
350     AliInfo( Form( "MisalignmentMatrixSurvey : %d", fMisalignSurvey )); 
351     AliInfo( Form( "NumberOfCellsFromEMCALBorder : %d", fNCellsFromEMCALBorder )); 
352     AliInfo( Form( "RCut : %f", fRcut )); 
353     AliInfo( Form( "Mass : %f", fMass )); 
354     AliInfo( Form( "Step : %f", fStep )); 
355     AliInfo( Form( "EtaCut : %f", fEtacut )); 
356     AliInfo( Form( "PhiCut : %f", fPhicut )); 
357     AliInfo( Form( "ExoticCellFraction : %f", fExoticCellFraction )); 
358     AliInfo( Form( "ExoticCellDiffTime : %f", fExoticCellDiffTime )); 
359     AliInfo( Form( "ExoticCellMinAmplitude : %f", fExoticCellMinAmplitude )); 
360     AliInfo( "=============================================================" ); 
361   }
362
363   // init reco utils
364   
365   if(!fEMCALRecoUtils)
366     fEMCALRecoUtils  = new AliEMCALRecoUtils;
367
368   // init geometry if requested
369   if (fEMCALGeoName.Length()>0) 
370     fEMCALGeo = AliEMCALGeometry::GetInstance(fEMCALGeoName) ;
371
372   // digits array
373   fDigitsArr       = new TClonesArray("AliEMCALDigit",1000);
374
375   // initialising non-linearity parameters
376   if( fNonLinearThreshold != -1 )
377     fEMCALRecoUtils->SetNonLinearityThreshold(fNonLinearThreshold);
378   if( fNonLinearFunc != -1 )
379     fEMCALRecoUtils->SetNonLinearityFunction(fNonLinearFunc);
380
381   // missalignment function
382   fEMCALRecoUtils->SetPositionAlgorithm(AliEMCALRecoUtils::kPosTowerGlobal);
383
384   // fiducial cut
385   // do not do the eta0 fiducial cut
386   if( fNCellsFromEMCALBorder != -1 )
387     fEMCALRecoUtils->SetNumberOfCellsFromEMCALBorder(fNCellsFromEMCALBorder);
388   fEMCALRecoUtils->SwitchOnNoFiducialBorderInEMCALEta0();
389     
390   // exotic cell rejection
391   if( fExoticCellFraction != -1 )
392     fEMCALRecoUtils->SetExoticCellFractionCut( fExoticCellFraction );
393   if( fExoticCellDiffTime != -1 )
394     fEMCALRecoUtils->SetExoticCellDiffTimeCut( fExoticCellDiffTime );
395   if( fExoticCellMinAmplitude != -1 )
396     fEMCALRecoUtils->SetExoticCellMinAmplitudeCut( fExoticCellMinAmplitude );
397
398   // setting track matching parameters ... mass, step size and residual cut
399   if( fMass != -1 )
400     fEMCALRecoUtils->SetMass(fMass);
401   if( fStep != -1 )
402     fEMCALRecoUtils->SetStep(fStep);
403   
404   // spatial cut based on separate eta/phi or common processing
405   if(fCutEtaPhiSum){ 
406     fEMCALRecoUtils->SwitchOnCutEtaPhiSum(); 
407     if( fRcut != -1 )
408       fEMCALRecoUtils->SetCutR(fRcut);
409   } else if (fCutEtaPhiSeparate) {
410     fEMCALRecoUtils->SwitchOnCutEtaPhiSeparate();
411     if( fEtacut != -1 )
412       fEMCALRecoUtils->SetCutEta(fEtacut);
413     if( fPhicut != -1 )
414       fEMCALRecoUtils->SetCutPhi(fPhicut);
415   }
416 }
417
418 //_____________________________________________________
419 AliVEvent* AliEMCALTenderSupply::GetEvent()
420 {
421   // Return the event pointer.
422   
423   if (fTender) {
424     return fTender->GetEvent();
425   }
426   else if (fTask) {
427     return fTask->InputEvent();
428   }
429   
430   return 0;
431 }
432
433 //_____________________________________________________
434 void AliEMCALTenderSupply::ProcessEvent()
435 {
436   // Event loop.
437   
438   AliVEvent *event = GetEvent();
439   
440   if (!event) {
441     AliError("Event ptr = 0, returning");
442     return;
443   }
444   
445  // Initialising parameters once per run number
446   if (RunChanged()) { 
447
448     AliWarning( "Run changed, initializing parameters" );
449     fRun = event->GetRunNumber();
450
451     // init geometry if not already done
452     if (fEMCALGeoName.Length()==0) {
453       fEMCALGeoName = "EMCAL_FIRSTYEARV1";
454       if (fRun>139517) {
455         fEMCALGeoName = "EMCAL_COMPLETEV1";
456       } 
457       if (fRun>170593) {
458         fEMCALGeoName = "EMCAL_COMPLETE12SMV1";
459       }
460       fEMCALGeo = AliEMCALGeometry::GetInstance(fEMCALGeoName);
461       if (!fEMCALGeo) {
462         AliFatal(Form("Can not create geometry: %s", fEMCALGeoName.Data()));
463         return;
464       }
465     } 
466
467     // get pass
468     GetPass();
469
470     // define what recalib parameters are needed for various switches
471     // this is based on implementation in AliEMCALRecoUtils
472     Bool_t needRecoParam   = fReClusterize;
473     Bool_t needBadChannels = fBadCellRemove   | fClusterBadChannelCheck | fRecalDistToBadChannels | fReClusterize;
474     Bool_t needRecalib     = fCalibrateEnergy | fReClusterize;
475     Bool_t needTimecalib   = fCalibrateTime   | fReClusterize;
476     Bool_t needMisalign    = fRecalClusPos    | fReClusterize;
477     Bool_t needClusterizer = fReClusterize;
478
479     // initiate reco params with some defaults 
480     // will not overwrite, if those have been provided by user
481     if( needRecoParam ){
482       Int_t initRC = InitRecParam();
483       
484       if( initRC == 0 )
485         AliInfo("Defaults reco params loaded.");
486       if( initRC > 1 )
487         AliWarning("User defined reco params.");
488     }
489
490     // init bad channels
491     if( needBadChannels ){
492       Int_t fInitBC = InitBadChannels();
493       if (fInitBC==0)
494         AliError("InitBadChannels returned false, returning");
495       if (fInitBC==1)
496         AliWarning("InitBadChannels OK");
497       if (fInitBC>1)
498         AliWarning(Form("No external hot channel set: %d - %s", event->GetRunNumber(), fFilepass.Data()));
499     }
500
501     // init recalibration factors
502     if( needRecalib ) 
503     { 
504       Int_t fInitRecalib = InitRecalib();
505       if (fInitRecalib==0)
506         AliError("InitRecalib returned false, returning");
507       if (fInitRecalib==1)
508         AliWarning("InitRecalib OK");
509       if (fInitRecalib>1)
510         AliWarning(Form("No recalibration available: %d - %s", event->GetRunNumber(), fFilepass.Data()));
511       
512       Int_t fInitRunDepRecalib = InitRunDepRecalib();
513       if (fInitRunDepRecalib==0)
514         AliError("InitrunDepRecalib returned false, returning");
515       if (fInitRunDepRecalib==1)
516         AliWarning("InitRecalib OK");
517       if (fInitRunDepRecalib>1)
518         AliWarning(Form("No Temperature recalibration available: %d - %s", event->GetRunNumber(), fFilepass.Data()));
519       
520     }
521     
522     
523     
524     // init time calibration
525     if( needTimecalib ){
526       Int_t initTC = InitTimeCalibration();
527       if ( !initTC ) 
528         AliError("InitTimeCalibration returned false, returning");
529       if (initTC==1)
530         AliWarning("InitTimeCalib OK");
531       if( initTC > 1 )
532         AliWarning(Form("No external time calibration set: %d - %s", event->GetRunNumber(), fFilepass.Data()));
533     }
534
535     // init misalignment matrix
536     if( needMisalign ) { 
537       if (!InitMisalignMatrix())
538         AliError("InitMisalignmentMatrix returned false, returning");
539       else
540         AliWarning("InitMisalignMatrix OK");
541     }
542     
543     // init clusterizer
544     if( needClusterizer ) {
545       if (!InitClusterization()) 
546         AliError("InitClusterization returned false, returning");
547       else
548         AliWarning("InitClusterization OK");
549     }
550     
551     if(fDebugLevel>1) 
552       fEMCALRecoUtils->Print("");
553   }
554   
555   // disable implied switches -------------------------------------------------
556   // AliEMCALRecoUtils or clusterizer functions alredy include some
557   // recalibration so based on those implied callibration te switches
558   // here are disabled to avoid duplication
559     
560   // clusterizer does cluster energy recalibration, position recomputation
561   // and shower shape
562   if( fReClusterize ){
563     fReCalibCluster   = kFALSE;
564     fRecalClusPos     = kFALSE;
565     fRecalShowerShape = kFALSE;
566   }
567   
568   // CONFIGURE THE RECO UTILS -------------------------------------------------
569   // configure the reco utils
570   // this option does energy recalibration
571   if( fCalibrateEnergy )
572     fEMCALRecoUtils->SwitchOnRecalibration();
573   else
574     fEMCALRecoUtils->SwitchOffRecalibration();
575   
576   // allows time calibration
577   if( fCalibrateTime )
578     fEMCALRecoUtils->SwitchOnTimeRecalibration();
579   else
580     fEMCALRecoUtils->SwitchOffTimeRecalibration();
581
582   // allows to zero bad cells
583   if( fBadCellRemove )
584     fEMCALRecoUtils->SwitchOnBadChannelsRemoval();
585   else
586     fEMCALRecoUtils->SwitchOffBadChannelsRemoval();
587   
588   // distance to bad channel recalibration
589   if( fRecalDistToBadChannels )
590     fEMCALRecoUtils->SwitchOnDistToBadChannelRecalculation();
591   else
592     fEMCALRecoUtils->SwitchOffDistToBadChannelRecalculation();
593
594   // exclude exotic cells
595   if( fRejectExoticCells )
596     fEMCALRecoUtils->SwitchOnRejectExoticCell();
597   else
598     fEMCALRecoUtils->SwitchOffRejectExoticCell();
599   
600   // exclude clusters with exotic cells
601   if( fRejectExoticClusters )
602     fEMCALRecoUtils->SwitchOnRejectExoticCluster();
603   else
604     fEMCALRecoUtils->SwitchOffRejectExoticCluster();
605
606   // TODO: not implemented switches
607   // SwitchOnClusterEnergySmearing
608   // SwitchOnRunDepCorrection
609
610   // START PROCESSING ---------------------------------------------------------
611   // Test if cells present
612   AliVCaloCells *cells= event->GetEMCALCells();
613   if (cells->GetNumberOfCells()<=0) 
614   {
615     if(fDebugLevel>1) 
616       AliWarning(Form("Number of EMCAL cells = %d, returning", cells->GetNumberOfCells()));
617     return;
618   }
619   
620   if (fDebugLevel>2)
621     AliInfo(Form("Re-calibrate cluster %d\n",fReCalibCluster));
622
623   // mark the cells not recalibrated in case of selected
624   // time, energy recalibration or bad channel removal
625   if( fCalibrateEnergy || fCalibrateTime || fBadCellRemove )
626     fEMCALRecoUtils->ResetCellsCalibrated();
627   
628  // CELL RECALIBRATION -------------------------------------------------------
629   // cell objects will be updated
630   // the cell calibrations are also processed locally any time those are needed
631   // in case that the cell objects are not to be updated here for later use
632   if( fUpdateCell )
633   {
634     // do the update
635     // includes exotic cell check in the UpdateCells function - is not provided
636     // by the reco utils
637     UpdateCells();
638
639     // switch off recalibrations so those are not done multiple times
640     // this is just for safety, the recalibrated flag of cell object
641     // should not allow for farther processing anyways
642     fEMCALRecoUtils->SwitchOffRecalibration();
643     fEMCALRecoUtils->SwitchOffTimeRecalibration();  
644   
645     if (fDoUpdateOnly)
646       return;
647   }
648
649   // RECLUSTERIZATION ---------------------------------------------------------
650   if (fReClusterize)
651   {
652     FillDigitsArray();
653     Clusterize();
654     UpdateClusters();
655   }
656
657   // Store good clusters
658   TClonesArray *clusArr = dynamic_cast<TClonesArray*>(event->FindListObject("caloClusters"));
659   if (!clusArr) 
660     clusArr = dynamic_cast<TClonesArray*>(event->FindListObject("CaloClusters"));
661   if (!clusArr) {
662     AliWarning(Form("No cluster array, number of cells in event = %d, returning", cells->GetNumberOfCells()));
663     return;
664   }
665
666   // PROCESS SINGLE CLUSTER RECALIBRATION -------------------------------------
667   // now go through clusters one by one and process separate correction
668   // as those were defined or not
669   Int_t nclusters = clusArr->GetEntriesFast();
670   for (Int_t icluster=0; icluster < nclusters; ++icluster) 
671   { 
672     AliVCluster *clust = static_cast<AliVCluster*>(clusArr->At(icluster));
673     if (!clust) 
674       continue;
675     if  (!clust->IsEMCAL()) 
676       continue;
677
678     // REMOVE CLUSTERS WITH BAD CELLS -----------------------------
679     if( fClusterBadChannelCheck )
680     {
681       // careful, the the ClusterContainsBadChannel is dependent on
682       // SwitchOnBadChannelsRemoval, switching it ON automatically
683       // and returning to original value after processing
684       Bool_t badRemoval = fEMCALRecoUtils->IsBadChannelsRemovalSwitchedOn();
685       fEMCALRecoUtils->SwitchOnBadChannelsRemoval();
686       
687       Bool_t badResult = fEMCALRecoUtils->ClusterContainsBadChannel(fEMCALGeo, clust->GetCellsAbsId(), clust->GetNCells());
688
689       // switch the bad channels removal back
690       if( ! badRemoval )
691         fEMCALRecoUtils->SwitchOffBadChannelsRemoval();
692       
693       if( badResult )
694       {
695         delete clusArr->RemoveAt(icluster);
696         continue; //TODO is it really needed to remove it? Or should we flag it?
697       }
698     }
699     
700     // REMOVE EXOTIC CLUSTERS -------------------------------------
701     // does process local cell recalibration energy and time without replacing
702     // the global cell values, in case of no cell recalib done yet
703     if( fRejectExoticClusters )
704     {
705       // careful, the IsExoticCluster is dependent on
706       // SwitchOnRejectExoticCell, switching it ON automatically
707       // and returning to original value after processing
708       Bool_t exRemoval = fEMCALRecoUtils->IsRejectExoticCell();
709       fEMCALRecoUtils->SwitchOnRejectExoticCell();
710
711       // get bunch crossing
712       Int_t bunchCrossNo = event->GetBunchCrossNumber();
713
714       Bool_t exResult = fEMCALRecoUtils->IsExoticCluster(clust, cells, bunchCrossNo );
715
716       // switch the exotic channels removal back
717       if( ! exRemoval )
718         fEMCALRecoUtils->SwitchOffRejectExoticCell();
719       
720       if( exResult )
721       {
722         delete clusArr->RemoveAt(icluster);
723         continue; //TODO is it really needed to remove it? Or should we flag it?
724       }
725     }
726     
727     // FIDUCIAL CUT -----------------------------------------------
728     if (fFiducial)
729     {
730       // depends on SetNumberOfCellsFromEMCALBorder
731       // SwitchOnNoFiducialBorderInEMCALEta0
732       if (!fEMCALRecoUtils->CheckCellFiducialRegion(fEMCALGeo, clust, cells)){
733         delete clusArr->RemoveAt(icluster);
734         continue; //TODO it would be nice to store the distance
735       }
736     }
737     
738     // CLUSTER ENERGY ---------------------------------------------
739     // does process local cell recalibration energy and time without replacing
740     // the global cell values, in case of no cell recalib done yet
741     if( fReCalibCluster ) {
742       fEMCALRecoUtils->RecalibrateClusterEnergy(fEMCALGeo, clust, cells);
743       if (clust->E() < 1e-9) {
744         delete clusArr->RemoveAt(icluster);
745         continue;
746       }
747     }
748     
749     // CLUSTER POSITION -------------------------------------------
750     // does process local cell energy recalibration, if enabled and cells
751     // not calibrated yet
752     if( fRecalClusPos ) 
753       fEMCALRecoUtils->RecalculateClusterPosition(fEMCALGeo, cells, clust);
754     
755     // SHOWER SHAPE -----------------------------------------------
756     if( fRecalShowerShape )
757       fEMCALRecoUtils->RecalculateClusterShowerShapeParameters(fEMCALGeo, cells, clust);  
758
759     // NONLINEARITY -----------------------------------------------
760     if( fDoNonLinearity )
761     {
762       Float_t correctedEnergy = fEMCALRecoUtils->CorrectClusterEnergyLinearity(clust);
763       clust->SetE(correctedEnergy);
764     }
765
766     // DISTANCE TO BAD CHANNELS -----------------------------------
767     if( fRecalDistToBadChannels )
768       fEMCALRecoUtils->RecalculateClusterDistanceToBadChannel(fEMCALGeo, cells, clust);  
769   }
770
771   clusArr->Compress();
772
773   if (!fDoTrackMatch)
774     return;
775
776   // TRACK MATCHING -----------------------------------------------------------
777   if (!TGeoGlobalMagField::Instance()->GetField()) {
778     AliESDEvent *esd = dynamic_cast<AliESDEvent*>(event);
779     if (esd)
780       esd->InitMagneticField();
781     else {
782       AliAODEvent *aod = dynamic_cast<AliAODEvent*>(event);
783       Double_t curSol = 30000*aod->GetMagneticField()/5.00668;
784       Double_t curDip = 6000 *aod->GetMuonMagFieldScale();
785       AliMagF *field  = AliMagF::CreateFieldMap(curSol,curDip);
786       TGeoGlobalMagField::Instance()->SetField(field);
787     }
788   }
789
790   fEMCALRecoUtils->FindMatches(event,0x0,fEMCALGeo);
791   fEMCALRecoUtils->SetClusterMatchedToTrack(event);
792   fEMCALRecoUtils->SetTracksMatchedToCluster(event);
793 }
794
795 //_____________________________________________________
796 Bool_t AliEMCALTenderSupply::InitMisalignMatrix()
797 {
798   // Initialising misalignment matrices
799
800   AliVEvent *event = GetEvent();
801   
802   if (!event) 
803     return kFALSE;
804   
805   if (fGeomMatrixSet) 
806   {
807     AliInfo("Misalignment matrix already set");  
808     return kTRUE;
809   }
810   
811   if (fDebugLevel>0) 
812     AliInfo("Initialising misalignment matrix");  
813   
814   if (fLoadGeomMatrices) {
815     for(Int_t mod=0; mod < fEMCALGeo->GetNumberOfSuperModules(); ++mod)
816     {
817       if (fEMCALMatrix[mod]){
818         if(fDebugLevel > 2) 
819           fEMCALMatrix[mod]->Print();
820         fEMCALGeo->SetMisalMatrix(fEMCALMatrix[mod],mod);  
821       }
822     }
823     fGeomMatrixSet = kTRUE;
824     return kTRUE;
825   }
826   
827   Int_t runGM = event->GetRunNumber();
828   TObjArray *mobj = 0;
829
830   if(fMisalignSurvey == kdefault)
831   { //take default alignment corresponding to run no
832     AliOADBContainer emcalgeoCont(Form("emcal"));
833     emcalgeoCont.InitFromFile("$ALICE_ROOT/OADB/EMCAL/EMCALlocal2master.root",Form("AliEMCALgeo"));
834     mobj=(TObjArray*)emcalgeoCont.GetObject(runGM,"EmcalMatrices");
835   }
836   
837   if(fMisalignSurvey == kSurveybyS)
838   { //take alignment at sector level
839     if (runGM <= 140000) { //2010 data
840       AliOADBContainer emcalgeoCont(Form("emcal2010"));
841       emcalgeoCont.InitFromFile("$ALICE_ROOT/OADB/EMCAL/EMCALlocal2master.root",Form("AliEMCALgeo"));
842       mobj=(TObjArray*)emcalgeoCont.GetObject(100,"survey10");
843     } 
844     else if (runGM>140000)
845     { // 2011 LHC11a pass1 data
846       AliOADBContainer emcalgeoCont(Form("emcal2011"));
847       emcalgeoCont.InitFromFile("$ALICE_ROOT/OADB/EMCAL/EMCALlocal2master.root",Form("AliEMCALgeo"));
848       mobj=(TObjArray*)emcalgeoCont.GetObject(100,"survey11byS");      
849     }
850   }
851
852   if(fMisalignSurvey == kSurveybyM)
853   { //take alignment at module level
854     if (runGM <= 140000) { //2010 data
855       AliOADBContainer emcalgeoCont(Form("emcal2010"));
856       emcalgeoCont.InitFromFile("$ALICE_ROOT/OADB/EMCAL/EMCALlocal2master.root",Form("AliEMCALgeo"));
857       mobj=(TObjArray*)emcalgeoCont.GetObject(100,"survey10");
858     } 
859     else if (runGM>140000) 
860     { // 2011 LHC11a pass1 data
861       AliOADBContainer emcalgeoCont(Form("emcal2011"));
862       emcalgeoCont.InitFromFile("$ALICE_ROOT/OADB/EMCAL/EMCALlocal2master.root",Form("AliEMCALgeo"));
863       mobj=(TObjArray*)emcalgeoCont.GetObject(100,"survey11byM");      
864     }
865   }
866
867   if(!mobj)
868   {
869     AliFatal("Geometry matrix array not found");
870     return kFALSE;
871   }
872   
873   for(Int_t mod=0; mod < (fEMCALGeo->GetEMCGeometry())->GetNumberOfSuperModules(); mod++)
874   {
875     fEMCALMatrix[mod] = (TGeoHMatrix*) mobj->At(mod);
876     fEMCALGeo->SetMisalMatrix(fEMCALMatrix[mod],mod); 
877     fEMCALMatrix[mod]->Print();
878   }
879   
880   return kTRUE;
881 }
882
883 //_____________________________________________________
884 Int_t AliEMCALTenderSupply::InitBadChannels()
885 {
886   // Initialising bad channel maps
887
888   AliVEvent *event = GetEvent();
889
890   if (!event) 
891     return 0;
892   
893   if (fDebugLevel>0) 
894     AliInfo("Initialising Bad channel map");
895   
896   // init default maps first
897   if( !fEMCALRecoUtils->GetEMCALBadChannelStatusMapArray() )
898     fEMCALRecoUtils->InitEMCALBadChannelStatusMap() ;
899   
900   Int_t runBC = event->GetRunNumber();
901   
902   AliOADBContainer *contBC = new AliOADBContainer("");
903   if (fBasePath!="")
904   { //if fBasePath specified in the ->SetBasePath()
905     if (fDebugLevel>0) AliInfo(Form("Loading Bad Channels OADB from given path %s",fBasePath.Data()));
906     
907     TFile *fbad=new TFile(Form("%s/EMCALBadChannels.root",fBasePath.Data()),"read");
908     if (!fbad || fbad->IsZombie())
909     {
910       AliFatal(Form("EMCALBadChannels.root was not found in the path provided: %s",fBasePath.Data()));
911       return 0;
912     }  
913     
914     if (fbad) delete fbad;
915     
916     contBC->InitFromFile(Form("%s/EMCALBadChannels.root",fBasePath.Data()),"AliEMCALBadChannels");    
917   } 
918   else 
919   { // Else choose the one in the $ALICE_ROOT directory
920     if (fDebugLevel>0) AliInfo("Loading Bad Channels OADB from $ALICE_ROOT/OADB/EMCAL");
921     
922     TFile *fbad=new TFile("$ALICE_ROOT/OADB/EMCAL/EMCALBadChannels.root","read");
923     if (!fbad || fbad->IsZombie())
924     {
925       AliFatal("$ALICE_ROOT/OADB/EMCAL/EMCALBadChannels.root was not found");
926       return 0;
927     }  
928       
929     if (fbad) delete fbad;
930     
931     contBC->InitFromFile("$ALICE_ROOT/OADB/EMCAL/EMCALBadChannels.root","AliEMCALBadChannels"); 
932   }
933   
934   TObjArray *arrayBC=(TObjArray*)contBC->GetObject(runBC);
935   if (!arrayBC)
936   {
937     AliError(Form("No external hot channel set for run number: %d", runBC));
938     return 2; 
939   }
940
941   Int_t sms = fEMCALGeo->GetEMCGeometry()->GetNumberOfSuperModules();
942   for (Int_t i=0; i<sms; ++i) 
943   {
944     TH2I *h = fEMCALRecoUtils->GetEMCALChannelStatusMap(i);
945     if (h)
946       delete h;
947     h=(TH2I*)arrayBC->FindObject(Form("EMCALBadChannelMap_Mod%d",i));
948
949     if (!h) 
950     {
951       AliError(Form("Can not get EMCALBadChannelMap_Mod%d",i));
952       continue;
953     }
954     h->SetDirectory(0);
955     fEMCALRecoUtils->SetEMCALChannelStatusMap(i,h);
956   }
957   return 1;  
958 }
959
960 //_____________________________________________________
961 Int_t AliEMCALTenderSupply::InitRecalib()
962 {
963   // Initialising recalibration factors.
964   
965   AliVEvent *event = GetEvent();
966
967   if (!event) 
968     return 0;
969   
970   if (fDebugLevel>0) 
971     AliInfo("Initialising recalibration factors");
972   
973   // init default maps first
974   if( !fEMCALRecoUtils->GetEMCALRecalibrationFactorsArray() )
975     fEMCALRecoUtils->InitEMCALRecalibrationFactors() ;
976
977   Int_t runRC = event->GetRunNumber();
978       
979   AliOADBContainer *contRF=new AliOADBContainer("");
980   if (fBasePath!="") 
981   { //if fBasePath specified in the ->SetBasePath()
982     if (fDebugLevel>0)  AliInfo(Form("Loading Recalib OADB from given path %s",fBasePath.Data()));
983     
984     TFile *fRecalib= new TFile(Form("%s/EMCALRecalib.root",fBasePath.Data()),"read");
985     if (!fRecalib || fRecalib->IsZombie()) 
986     {
987       AliFatal(Form("EMCALRecalib.root not found in %s",fBasePath.Data()));
988       return 0;
989     }
990     
991     if (fRecalib) delete fRecalib;
992     
993     contRF->InitFromFile(Form("%s/EMCALRecalib.root",fBasePath.Data()),"AliEMCALRecalib");
994   }
995   else
996   { // Else choose the one in the $ALICE_ROOT directory
997     if (fDebugLevel>0)  AliInfo("Loading Recalib OADB from $ALICE_ROOT/OADB/EMCAL");
998     
999     TFile *fRecalib= new TFile("$ALICE_ROOT/OADB/EMCAL/EMCALRecalib.root","read");
1000     if (!fRecalib || fRecalib->IsZombie()) 
1001     {
1002       AliFatal("$ALICE_ROOT/OADB/EMCAL/EMCALRecalib.root was not found");
1003       return 0;
1004     }
1005     
1006     if (fRecalib) delete fRecalib;
1007       
1008     contRF->InitFromFile("$ALICE_ROOT/OADB/EMCAL/EMCALRecalib.root","AliEMCALRecalib");     
1009   }
1010
1011   TObjArray *recal=(TObjArray*)contRF->GetObject(runRC);
1012   if (!recal)
1013   {
1014     AliError(Form("No Objects for run: %d",runRC));
1015     return 2;
1016   } 
1017
1018   TObjArray *recalpass=(TObjArray*)recal->FindObject(fFilepass);
1019   if (!recalpass)
1020   {
1021     AliError(Form("No Objects for run: %d - %s",runRC,fFilepass.Data()));
1022     return 2;
1023   }
1024
1025   TObjArray *recalib=(TObjArray*)recalpass->FindObject("Recalib");
1026   if (!recalib)
1027   {
1028     AliError(Form("No Recalib histos found for  %d - %s",runRC,fFilepass.Data())); 
1029     return 2;
1030   }
1031
1032   if (fDebugLevel>0) recalib->Print();
1033
1034   Int_t sms = fEMCALGeo->GetEMCGeometry()->GetNumberOfSuperModules();
1035   for (Int_t i=0; i<sms; ++i) 
1036   {
1037     TH2F *h = fEMCALRecoUtils->GetEMCALChannelRecalibrationFactors(i);
1038     if (h)
1039       delete h;
1040     h = (TH2F*)recalib->FindObject(Form("EMCALRecalFactors_SM%d",i));
1041     if (!h) 
1042     {
1043       AliError(Form("Could not load EMCALRecalFactors_SM%d",i));
1044       continue;
1045     }
1046     h->SetDirectory(0);
1047     fEMCALRecoUtils->SetEMCALChannelRecalibrationFactors(i,h);
1048   }
1049   return 1;
1050 }
1051
1052 //_____________________________________________________
1053 Int_t AliEMCALTenderSupply::InitRunDepRecalib()
1054 {
1055   // Initialising recalibration factors.
1056   
1057   AliVEvent *event = GetEvent();
1058   
1059   if (!event) 
1060     return 0;
1061   
1062   if (fDebugLevel>0) 
1063     AliInfo("Initialising recalibration factors");
1064   
1065   // init default maps first
1066   if( !fEMCALRecoUtils->GetEMCALRecalibrationFactorsArray() )
1067     fEMCALRecoUtils->InitEMCALRecalibrationFactors() ;
1068   
1069   Int_t runRC = event->GetRunNumber();
1070   
1071   AliOADBContainer *contRF=new AliOADBContainer("");
1072   if (fBasePath!="") 
1073   { //if fBasePath specified in the ->SetBasePath()
1074     if (fDebugLevel>0)  AliInfo(Form("Loading Recalib OADB from given path %s",fBasePath.Data()));
1075     
1076     TFile *fRunDepRecalib= new TFile(Form("%s/EMCALTemperatureCorrCalib.root",fBasePath.Data()),"read");
1077     if (!fRunDepRecalib || fRunDepRecalib->IsZombie()) 
1078     {
1079       AliFatal(Form("EMCALTemperatureCorrCalib.root not found in %s",fBasePath.Data()));
1080       return 0;
1081     }
1082     
1083     if (fRunDepRecalib) delete fRunDepRecalib;
1084     
1085     contRF->InitFromFile(Form("%s/EMCALTemperatureCorrCalib.root",fBasePath.Data()),"AliEMCALRunDepTempCalibCorrections");
1086   }
1087   else
1088   { // Else choose the one in the $ALICE_ROOT directory
1089     if (fDebugLevel>0)  AliInfo("Loading Recalib OADB from $ALICE_ROOT/OADB/EMCAL");
1090     
1091     TFile *fRunDepRecalib= new TFile("$ALICE_ROOT/OADB/EMCAL/EMCALTemperatureCorrCalib.root","read");
1092     if (!fRunDepRecalib || fRunDepRecalib->IsZombie()) 
1093     {
1094       AliFatal("$ALICE_ROOT/OADB/EMCAL/EMCALTemperatureCorrCalib.root was not found");
1095       return 0;
1096     }
1097     
1098     if (fRunDepRecalib) delete fRunDepRecalib;
1099     
1100     contRF->InitFromFile("$ALICE_ROOT/OADB/EMCAL/EMCALTemperatureCorrCalib.root","AliEMCALRunDepTempCalibCorrections");     
1101   }
1102   
1103   TH1S *rundeprecal=(TH1S*)contRF->GetObject(runRC);
1104   if (!rundeprecal)
1105   {
1106     AliWarning(Form("No TemperatureCorrCalib Objects for run: %d",runRC));
1107     // let's get the closest runnumber instead then..
1108     Int_t lower = 0;
1109     Int_t ic = 0;
1110     Int_t maxEntry = contRF->GetNumberOfEntries();
1111
1112     while ( (ic < maxEntry) && (contRF->UpperLimit(ic) < runRC) ) {
1113       lower = ic;
1114       ic++; 
1115     }
1116
1117     Int_t closest = lower;
1118     if ( (ic<maxEntry) && 
1119          (contRF->LowerLimit(ic)-runRC) < (runRC - contRF->UpperLimit(lower)) ) {
1120          closest = ic;
1121     }
1122
1123     AliWarning(Form("TemperatureCorrCalib Objects found closest id %d from run: %d", closest, contRF->LowerLimit(closest)));
1124     rundeprecal = (TH1S*) contRF->GetObjectByIndex(closest);  
1125   } 
1126   
1127   if (fDebugLevel>0) rundeprecal->Print();
1128   
1129   Int_t nSM = fEMCALGeo->GetEMCGeometry()->GetNumberOfSuperModules();
1130   
1131   for (Int_t ism=0; ism<nSM; ++ism) 
1132   {        
1133     for (Int_t icol=0; icol<48; ++icol) 
1134     {        
1135       for (Int_t irow=0; irow<24; ++irow) 
1136       {
1137         Float_t factor = fEMCALRecoUtils->GetEMCALChannelRecalibrationFactor(ism,icol,irow);
1138         
1139         Int_t absID = fEMCALGeo->GetAbsCellIdFromCellIndexes(ism, irow, icol); // original calibration factor
1140         factor *= rundeprecal->GetBinContent(absID) / 10000. ; // correction dependent on T
1141         //printf("\t ism %d, icol %d, irow %d,absID %d, corrA %2.3f, corrB %2.3f, corrAB %2.3f\n",ism, icol, irow, absID, 
1142         //      GetEMCALChannelRecalibrationFactor(ism,icol,irow) , rundeprecal->GetBinContent(absID) / 10000., factor);
1143         fEMCALRecoUtils->SetEMCALChannelRecalibrationFactor(ism,icol,irow,factor);
1144       } // columns
1145     } // rows 
1146   } // SM loop
1147   
1148   return 1;
1149 }
1150
1151
1152 //_____________________________________________________
1153 Int_t AliEMCALTenderSupply::InitTimeCalibration()
1154 {
1155   // Initialising bad channel maps
1156   AliVEvent *event = GetEvent();
1157
1158   if (!event) 
1159     return 0;
1160   
1161   if (fDebugLevel>0) 
1162     AliInfo("Initialising time calibration map");
1163   
1164   // init default maps first
1165   if ( !fEMCALRecoUtils->GetEMCALTimeRecalibrationFactorsArray() )
1166     fEMCALRecoUtils->InitEMCALTimeRecalibrationFactors() ;
1167
1168   Int_t runBC = event->GetRunNumber();
1169   
1170   AliOADBContainer *contBC = new AliOADBContainer("");
1171   if (fBasePath!="")
1172   { //if fBasePath specified in the ->SetBasePath()
1173     if (fDebugLevel>0) AliInfo(Form("Loading time calibration OADB from given path %s",fBasePath.Data()));
1174     
1175     TFile *fbad=new TFile(Form("%s/EMCALTimeCalib.root",fBasePath.Data()),"read");
1176     if (!fbad || fbad->IsZombie())
1177     {
1178       AliFatal(Form("EMCALTimeCalib.root was not found in the path provided: %s",fBasePath.Data()));
1179       return 0;
1180     }  
1181     
1182     if (fbad) delete fbad;
1183     
1184     contBC->InitFromFile(Form("%s/EMCALTimeCalib.root",fBasePath.Data()),"AliEMCALTimeCalib");    
1185   } 
1186   else 
1187   { // Else choose the one in the $ALICE_ROOT directory
1188     if (fDebugLevel>0) AliInfo("Loading time calibration OADB from $ALICE_ROOT/OADB/EMCAL");
1189     
1190     TFile *fbad=new TFile("$ALICE_ROOT/OADB/EMCAL/EMCALTimeCalib.root","read");
1191     if (!fbad || fbad->IsZombie())
1192     {
1193       AliFatal("$ALICE_ROOT/OADB/EMCAL/EMCALTimeCalib.root was not found");
1194       return 0;
1195     }  
1196       
1197     if (fbad) delete fbad;
1198     
1199     contBC->InitFromFile("$ALICE_ROOT/OADB/EMCAL/EMCALTimeCalib.root","AliEMCALTimeCalib"); 
1200   }
1201   
1202   TObjArray *arrayBC=(TObjArray*)contBC->GetObject(runBC);
1203   if (!arrayBC)
1204   {
1205     AliError(Form("No external time calibration set for run number: %d", runBC));
1206     return 2; 
1207   }
1208   
1209   // Here, it looks for a specific pass
1210   TObjArray *arrayBCpass=(TObjArray*)arrayBC->FindObject(fFilepass); 
1211   if (!arrayBCpass)
1212   {
1213     AliError(Form("No external time calibration set for: %d -%s", runBC,fFilepass.Data()));
1214     return 2; 
1215   }
1216
1217   if (fDebugLevel>0) arrayBCpass->Print();
1218
1219   for( Int_t i = 0; i < 4; i++ )
1220   {
1221     TH1F *h = fEMCALRecoUtils->GetEMCALChannelTimeRecalibrationFactors( i );
1222     if( h )
1223       delete h;
1224     
1225     h = (TH1F*)arrayBCpass->FindObject(Form("hAllTimeAvBC%d",i));
1226     
1227     if (!h)
1228     {
1229       AliError(Form("Can not get hAllTimeAvBC%d",i));
1230       continue;
1231     }
1232     h->SetDirectory(0);
1233     fEMCALRecoUtils->SetEMCALChannelTimeRecalibrationFactors(i,h);
1234   }
1235   return 1;  
1236 }
1237
1238 //_____________________________________________________
1239 void AliEMCALTenderSupply::UpdateCells()
1240 {
1241   //Remove bad cells from the cell list
1242   //Recalibrate energy and time cells 
1243   //This is required for later reclusterization
1244
1245   AliVEvent *event = GetEvent();
1246
1247   if(!event) return ;
1248   
1249   AliVCaloCells *cells = event->GetEMCALCells();
1250   Int_t bunchCrossNo = event->GetBunchCrossNumber();
1251
1252   fEMCALRecoUtils->RecalibrateCells(cells, bunchCrossNo); 
1253   
1254   // remove exotic cells - loop through cells and zero the exotic ones
1255   // just like with bad cell rejection in reco utils (inside RecalibrateCells)
1256   if( fRejectExoticCells )
1257   {
1258     Short_t  absId  =-1;
1259     Double_t ecell = 0;
1260     Double_t tcell = 0;
1261     Double_t efrac = 0;
1262     Short_t  mclabel = -1;
1263     Bool_t   isExot = kFALSE;
1264   
1265     // loop through cells
1266     Int_t nEMcell  = cells->GetNumberOfCells() ;  
1267     for (Int_t iCell = 0; iCell < nEMcell; iCell++) 
1268     { 
1269       cells->GetCell( iCell, absId, ecell, tcell, mclabel, efrac );
1270     
1271       isExot = fEMCALRecoUtils->IsExoticCell( absId, cells, bunchCrossNo ); 
1272       // zero if exotic
1273       if( isExot )
1274         cells->SetCell( iCell, absId, 0.0, -1.0, mclabel, efrac );
1275     } // cell loop
1276   } // reject exotic cells
1277
1278   cells->Sort();
1279 }
1280
1281 //_____________________________________________________
1282 TString AliEMCALTenderSupply::GetBeamType()
1283 {
1284   // Get beam type : pp-AA-pA
1285   // ESDs have it directly, AODs get it from hardcoded run number ranges
1286   
1287   AliVEvent *event = GetEvent();
1288
1289   if (!event) { 
1290     AliError("Couldn't retrieve event!");
1291     return "";
1292   }
1293
1294   TString beamType;
1295
1296   AliESDEvent *esd = dynamic_cast<AliESDEvent*>(event);
1297   if (esd) {
1298     const AliESDRun *run = esd->GetESDRun();
1299     beamType = run->GetBeamType();
1300   }
1301   else
1302   {
1303     Int_t runNumber = event->GetRunNumber();
1304     if ((runNumber >= 136851 && runNumber <= 139517)  // LHC10h
1305   || (runNumber >= 166529 && runNumber <= 170593))  // LHC11h
1306     {
1307       beamType = "A-A";
1308     }
1309     else 
1310     {
1311       beamType = "p-p";
1312     }
1313   }
1314
1315   return beamType;    
1316 }
1317
1318 //_____________________________________________________
1319 Int_t AliEMCALTenderSupply::InitRecParam()
1320 {
1321   // Init reco params if not yet exist (probably shipped by the user already)
1322
1323   if( fRecParam != 0 )
1324     return 2;
1325
1326   TString beamType = GetBeamType();
1327
1328   // set some default reco params
1329   fRecParam = new AliEMCALRecParam();
1330   fRecParam->SetClusteringThreshold(0.100);
1331   fRecParam->SetMinECut(0.050);
1332   fRecParam->SetTimeCut(250);
1333   fRecParam->SetTimeMin(425);
1334   fRecParam->SetTimeMax(825);
1335   if ( beamType == "A-A") {
1336     fRecParam->SetClusterizerFlag(AliEMCALRecParam::kClusterizerv2);
1337   } 
1338   else {
1339     fRecParam->SetClusterizerFlag(AliEMCALRecParam::kClusterizerv1);
1340   }
1341
1342   return 0;
1343 }
1344
1345 //_____________________________________________________
1346 Bool_t AliEMCALTenderSupply::InitClusterization()
1347 {
1348   // Initialing clusterization/unfolding algorithm and set all the needed parameters.
1349   
1350   AliVEvent *event = GetEvent();
1351
1352   if (!event) 
1353     return kFALSE;
1354   
1355   if (fDebugLevel>0) 
1356     AliInfo(Form("Initialising reclustering parameters: Clusterizer type: %d",fRecParam->GetClusterizerFlag()));
1357   
1358   //---setup clusterizer
1359   delete fClusterizer;
1360   if     (fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerv1)
1361     fClusterizer = new AliEMCALClusterizerv1 (fEMCALGeo);
1362   else if (fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerv2) 
1363     fClusterizer = new AliEMCALClusterizerv2(fEMCALGeo);
1364   else if (fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerNxN) 
1365   {
1366     AliEMCALClusterizerNxN *clusterizer = new AliEMCALClusterizerNxN(fEMCALGeo);
1367     clusterizer->SetNRowDiff(fRecParam->GetNRowDiff());
1368     clusterizer->SetNColDiff(fRecParam->GetNColDiff());
1369     fClusterizer = clusterizer;
1370   } 
1371   else 
1372   {
1373     AliFatal(Form("Clusterizer < %d > not available", fRecParam->GetClusterizerFlag()));
1374     return kFALSE;
1375   }
1376   
1377   // Set the clustering parameters
1378   fClusterizer->SetECAClusteringThreshold( fRecParam->GetClusteringThreshold() );
1379   fClusterizer->SetECALogWeight          ( fRecParam->GetW0()                  );
1380   fClusterizer->SetMinECut               ( fRecParam->GetMinECut()             );    
1381   fClusterizer->SetUnfolding             ( fRecParam->GetUnfold()              );
1382   fClusterizer->SetECALocalMaxCut        ( fRecParam->GetLocMaxCut()           );
1383   fClusterizer->SetTimeCut               ( fRecParam->GetTimeCut()             );
1384   fClusterizer->SetTimeMin               ( fRecParam->GetTimeMin()             );
1385   fClusterizer->SetTimeMax               ( fRecParam->GetTimeMax()             );
1386   fClusterizer->SetInputCalibrated       ( kTRUE                               );
1387   fClusterizer->SetJustClusters          ( kTRUE                               );  
1388   
1389   // In case of unfolding after clusterization is requested, set the corresponding parameters
1390   if (fRecParam->GetUnfold()) 
1391   {
1392     for (Int_t i = 0; i < 8; ++i) 
1393     {
1394       fClusterizer->SetSSPars(i, fRecParam->GetSSPars(i));
1395     }
1396     for (Int_t i = 0; i < 3; ++i)
1397     {
1398       fClusterizer->SetPar5  (i, fRecParam->GetPar5(i));
1399       fClusterizer->SetPar6  (i, fRecParam->GetPar6(i));
1400     }
1401     fClusterizer->InitClusterUnfolding();
1402   }
1403   
1404   fClusterizer->SetDigitsArr(fDigitsArr);
1405   fClusterizer->SetOutput(0);
1406   fClusterArr = const_cast<TObjArray *>(fClusterizer->GetRecPoints());
1407   return kTRUE;
1408 }
1409
1410 //_____________________________________________________
1411 void AliEMCALTenderSupply::FillDigitsArray()
1412 {
1413   // Fill digits from cells to a TClonesArray.
1414   
1415   AliVEvent *event = GetEvent();
1416
1417  if (!event)
1418     return;
1419   
1420   fDigitsArr->Clear("C");
1421   AliVCaloCells *cells = event->GetEMCALCells();
1422   Int_t ncells = cells->GetNumberOfCells();
1423   for (Int_t icell = 0, idigit = 0; icell < ncells; ++icell) 
1424   {
1425     Double_t cellAmplitude=0, cellTime=0, efrac = 0;
1426     Short_t  cellNumber=0, mcLabel=-1;
1427
1428     if (cells->GetCell(icell, cellNumber, cellAmplitude, cellTime, mcLabel, efrac) != kTRUE)
1429       break;
1430
1431     // Do not add if energy already too low (some cells set to 0 if bad channels)
1432     if (cellAmplitude < fRecParam->GetMinECut())
1433       continue;
1434
1435     // If requested, do not include exotic cells
1436    if (fEMCALRecoUtils->IsExoticCell(cellNumber,cells,event->GetBunchCrossNumber())) 
1437       continue;
1438     
1439     new((*fDigitsArr)[idigit]) AliEMCALDigit(mcLabel, mcLabel, cellNumber,
1440                                              (Float_t)cellAmplitude, (Float_t)cellTime,
1441                                              AliEMCALDigit::kHG,idigit, 0, 0, 1);
1442     
1443 //    AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->New(idigit));
1444 //    digit->SetId(cellNumber);
1445 //    digit->SetTime((Float_t)cellTime);
1446 //    digit->SetTimeR((Float_t)cellTime);
1447 //    digit->SetIndexInList(idigit);
1448 //    digit->SetType(AliEMCALDigit::kHG);
1449 //    digit->SetAmplitude((Float_t)cellAmplitude);
1450     idigit++;
1451   }
1452 }
1453
1454 //_____________________________________________________
1455 void AliEMCALTenderSupply::Clusterize()
1456 {
1457   // Clusterize.
1458   
1459   fClusterizer->Digits2Clusters("");
1460 }
1461
1462 //_____________________________________________________
1463 void AliEMCALTenderSupply::UpdateClusters()
1464 {
1465   // Update ESD cluster list.
1466   
1467   AliVEvent *event = GetEvent();
1468
1469   if (!event)
1470     return;
1471   
1472   TClonesArray *clus = dynamic_cast<TClonesArray*>(event->FindListObject("caloClusters"));
1473   if (!clus) 
1474     clus = dynamic_cast<TClonesArray*>(event->FindListObject("CaloClusters"));
1475   if (!clus) 
1476   {
1477     AliError(" Null pointer to calo clusters array, returning");
1478     return;
1479   }
1480   
1481   Int_t nents = clus->GetEntriesFast();
1482   for (Int_t i=0; i < nents; ++i) 
1483   {
1484     AliVCluster *c = dynamic_cast<AliVCluster*>(clus->At(i));
1485     if (!c)
1486       continue;
1487     if (c->IsEMCAL())
1488       delete clus->RemoveAt(i);
1489   }
1490   
1491   clus->Compress();
1492   
1493   RecPoints2Clusters(clus);
1494 }
1495
1496 //_____________________________________________________
1497 void AliEMCALTenderSupply::RecPoints2Clusters(TClonesArray *clus)
1498 {
1499   // Convert AliEMCALRecoPoints to AliESDCaloClusters/AliAODCaloClusters.
1500   // Cluster energy, global position, cells and their amplitude fractions are restored.
1501   
1502   AliVEvent *event = GetEvent();
1503
1504   if (!event)
1505     return;
1506
1507   Int_t ncls = fClusterArr->GetEntriesFast();
1508   for(Int_t i=0, nout=clus->GetEntriesFast(); i < ncls; ++i) 
1509   {
1510     AliEMCALRecPoint *recpoint = static_cast<AliEMCALRecPoint*>(fClusterArr->At(i));
1511     
1512     Int_t ncellsTrue = 0;
1513     const Int_t ncells = recpoint->GetMultiplicity();
1514     UShort_t   absIds[ncells];  
1515     Double32_t ratios[ncells];
1516     Int_t *dlist = recpoint->GetDigitsList();
1517     Float_t *elist = recpoint->GetEnergiesList();
1518     for (Int_t c = 0; c < ncells; ++c) 
1519     {
1520       AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->At(dlist[c]));
1521       absIds[ncellsTrue] = digit->GetId();
1522       ratios[ncellsTrue] = elist[c]/digit->GetAmplitude();
1523       if (ratios[ncellsTrue] < 0.001) 
1524         continue;
1525       ++ncellsTrue;
1526     }
1527     
1528     if (ncellsTrue < 1) 
1529     {
1530       AliWarning("Skipping cluster with no cells");
1531       continue;
1532     }
1533     
1534     // calculate new cluster position
1535     TVector3 gpos;
1536     recpoint->GetGlobalPosition(gpos);
1537     Float_t g[3];
1538     gpos.GetXYZ(g);
1539     
1540     AliVCluster *c = static_cast<AliVCluster*>(clus->New(nout++));
1541     c->SetID(nout-1); 
1542     c->SetType(AliVCluster::kEMCALClusterv1);
1543     c->SetE(recpoint->GetEnergy());
1544     c->SetPosition(g);
1545     c->SetNCells(ncellsTrue);
1546     c->SetDispersion(recpoint->GetDispersion());
1547     c->SetEmcCpvDistance(-1);            //not yet implemented
1548     c->SetChi2(-1);                      //not yet implemented
1549     c->SetTOF(recpoint->GetTime()) ;     //time-of-flight
1550     c->SetNExMax(recpoint->GetNExMax()); //number of local maxima
1551     Float_t elipAxis[2];
1552     recpoint->GetElipsAxis(elipAxis);
1553     c->SetM02(elipAxis[0]*elipAxis[0]) ;
1554     c->SetM20(elipAxis[1]*elipAxis[1]) ;
1555     c->SetCellsAbsId(absIds);
1556     c->SetCellsAmplitudeFraction(ratios);
1557   }
1558 }
1559
1560 //_____________________________________________________
1561 void AliEMCALTenderSupply::GetPass()
1562 {
1563   // Get passx from filename.
1564   
1565   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
1566   fInputTree = mgr->GetTree();
1567   
1568   if (!fInputTree) 
1569   {
1570     AliError("Pointer to tree = 0, returning");
1571     return;
1572   }
1573   
1574   fInputFile = fInputTree->GetCurrentFile();
1575   if (!fInputFile) {
1576     AliError("Null pointer input file, returning");
1577     return;
1578   }
1579   
1580   TString fname(fInputFile->GetName());
1581   if      (fname.Contains("pass1")) fFilepass = TString("pass1");
1582   else if (fname.Contains("pass2")) fFilepass = TString("pass2");
1583   else if (fname.Contains("pass3")) fFilepass = TString("pass3");
1584   else if (fname.Contains("pass4")) fFilepass = TString("pass4");
1585   else 
1586   {
1587     AliError(Form("Pass number string not found: %s", fname.Data()));
1588     return;            
1589   }
1590 }