]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGGA/EMCALTasks/AliAnalysisTaskEMCALClusterizeFast.cxx
411320c6ed5d22d4cb40bb3587648f4d810b00f1
[u/mrichter/AliRoot.git] / PWGGA / EMCALTasks / AliAnalysisTaskEMCALClusterizeFast.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 // This analysis provides a new list of clusters to be used in other analysis
18 //
19 // Author: Constantin Loizides, Salvatore Aiola
20 //         Adapted from analysis class from Deepa Thomas
21 //
22 // $Id$
23 //_________________________________________________________________________
24
25 // --- Root ---
26 #include <TClonesArray.h>
27 #include <TGeoManager.h>
28 #include <TObjArray.h>
29 #include <TString.h>
30 #include <TTree.h>
31
32 // --- AliRoot ---
33 #include "AliAODCaloCluster.h"
34 #include "AliAODEvent.h"
35 #include "AliAnalysisManager.h"
36 #include "AliCDBEntry.h"
37 #include "AliCDBManager.h"
38 #include "AliCaloCalibPedestal.h"
39 #include "AliEMCALAfterBurnerUF.h"
40 #include "AliEMCALCalibData.h"
41 #include "AliEMCALClusterizerNxN.h"
42 #include "AliEMCALClusterizerv1.h"
43 #include "AliEMCALClusterizerv2.h"
44 #include "AliEMCALClusterizerFixedWindow.h"
45 #include "AliEMCALDigit.h"
46 #include "AliEMCALGeometry.h"
47 #include "AliEMCALRecParam.h"
48 #include "AliEMCALRecPoint.h"
49 #include "AliEMCALRecoUtils.h"
50 #include "AliESDEvent.h"
51 #include "AliInputEventHandler.h"
52 #include "AliLog.h"
53
54 #include "AliAnalysisTaskEMCALClusterizeFast.h"
55
56 ClassImp(AliAnalysisTaskEMCALClusterizeFast)
57
58 //________________________________________________________________________
59 AliAnalysisTaskEMCALClusterizeFast::AliAnalysisTaskEMCALClusterizeFast() 
60   : AliAnalysisTaskSE(), 
61     fRun(-1),
62     fDigitsArr(0),       
63     fClusterArr(0),       
64     fRecParam(0),
65     fClusterizer(0),
66     fUnfolder(0),
67     fJustUnfold(kFALSE),
68     fGeomName(),
69     fGeomMatrixSet(kFALSE), 
70     fLoadGeomMatrices(kFALSE),
71     fOCDBpath(),
72     fCalibData(0),
73     fPedestalData(0),
74     fOutputAODBranch(0),
75     fOutputAODBrName(),
76     fRecoUtils(0),
77     fLoadCalib(0),
78     fLoadPed(0),
79     fAttachClusters(1),
80     fRecalibOnly(0),
81     fSubBackground(0),
82     fCreatePattern(0),
83     fOverwrite(0),
84     fNewClusterArrayName("newCaloClusters"),
85     fNPhi(4),
86     fNEta(4),
87     fShiftPhi(2),
88     fShiftEta(2),
89     fTRUShift(0),
90     fClusterizeFastORs(0),
91     fTrackName(),
92     fCutL0Times(kTRUE)
93
94   // Constructor
95
96   for(Int_t i = 0; i < 12; ++i) 
97     fGeomMatrix[i] = 0;
98 }
99
100 //________________________________________________________________________
101 AliAnalysisTaskEMCALClusterizeFast::AliAnalysisTaskEMCALClusterizeFast(const char *name) 
102   : AliAnalysisTaskSE(name), 
103     fRun(-1),
104     fDigitsArr(0),       
105     fClusterArr(0),       
106     fRecParam(new AliEMCALRecParam),
107     fClusterizer(0),
108     fUnfolder(0),
109     fJustUnfold(kFALSE),
110     fGeomName("EMCAL_FIRSTYEARV1"),
111     fGeomMatrixSet(kFALSE), 
112     fLoadGeomMatrices(kFALSE),
113     fOCDBpath(),
114     fCalibData(0),
115     fPedestalData(0),
116     fOutputAODBranch(0),
117     fOutputAODBrName(),
118     fRecoUtils(0),
119     fLoadCalib(0),
120     fLoadPed(0),
121     fAttachClusters(1),
122     fRecalibOnly(0),
123     fSubBackground(0),
124     fCreatePattern(0),
125     fOverwrite(0),
126     fNewClusterArrayName("newCaloClusters"),
127     fNPhi(4),
128     fNEta(4),
129     fShiftPhi(2),
130     fShiftEta(2),
131     fTRUShift(0),
132     fClusterizeFastORs(0),
133     fTrackName(),
134     fCutL0Times(kTRUE)
135
136   // Constructor
137
138   fBranchNames     = "ESD:AliESDHeader.,AliESDRun.,EMCALCells.,EMCALTrigger. AOD:header,emcalCells";
139   for(Int_t i = 0; i < 12; ++i) 
140     fGeomMatrix[i] = 0;
141 }
142
143 //________________________________________________________________________
144 AliAnalysisTaskEMCALClusterizeFast::~AliAnalysisTaskEMCALClusterizeFast()
145 {
146   // Destructor.
147
148   delete fClusterizer;
149   delete fUnfolder;   
150   delete fRecoUtils;
151 }
152
153 //-------------------------------------------------------------------
154 void AliAnalysisTaskEMCALClusterizeFast::UserCreateOutputObjects()
155 {
156   // Create output objects.
157
158   if (!fOutputAODBrName.IsNull()) {
159     fOutputAODBranch = new TClonesArray("AliAODCaloCluster", 0);
160     fOutputAODBranch->SetName(fOutputAODBrName);
161     AddAODBranch("TClonesArray", &fOutputAODBranch);
162     AliInfo(Form("Created Branch: %s",fOutputAODBrName.Data()));
163   }
164 }
165
166 //________________________________________________________________________
167 void AliAnalysisTaskEMCALClusterizeFast::UserExec(Option_t *) 
168 {
169   // Main loop, called for each event
170
171   // remove the contents of output list set in the previous event 
172   if (fOutputAODBranch)
173     fOutputAODBranch->Clear("C");
174
175   AliESDEvent *esdevent = dynamic_cast<AliESDEvent*>(InputEvent());
176   AliAODEvent *aodevent = dynamic_cast<AliAODEvent*>(InputEvent());
177
178   if (!esdevent&&!aodevent) {
179     Error("UserExec","Event not available");
180     return;
181   }
182
183   LoadBranches();
184
185   UInt_t offtrigger = 0;
186   if (esdevent) {
187     UInt_t mask1 = esdevent->GetESDRun()->GetDetectorsInDAQ();
188     UInt_t mask2 = esdevent->GetESDRun()->GetDetectorsInReco();
189     Bool_t desc1 = (mask1 >> 18) & 0x1;
190     Bool_t desc2 = (mask2 >> 18) & 0x1;
191     if (desc1==0 || desc2==0) { //AliDAQ::OfflineModuleName(180=="EMCAL"
192       AliError(Form("EMCAL not in DAQ/RECO: %u (%u)/%u (%u)", 
193                     mask1, esdevent->GetESDRun()->GetDetectorsInReco(),
194                     mask2, esdevent->GetESDRun()->GetDetectorsInDAQ()));
195       return;
196     }
197     AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
198     offtrigger = ((AliInputEventHandler*)(am->GetInputEventHandler()))->IsEventSelected();
199   } else if (aodevent) {
200     offtrigger =  aodevent->GetHeader()->GetOfflineTrigger();
201   }
202
203   if (esdevent) {
204     AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
205     Bool_t mcmode = 0;
206     if (am->GetMCtruthEventHandler())
207       mcmode = 1;
208     if (!mcmode) {
209       if (offtrigger & AliVEvent::kFastOnly) {
210         AliWarning(Form("EMCAL not in fast only partition"));
211         return;
212       }
213     }
214   }
215   
216   Init();
217
218   if (fJustUnfold) {
219     AliWarning("Unfolding not implemented");
220     return;
221   }
222
223   FillDigitsArray();
224
225   if (fRecalibOnly) {
226     UpdateCells();
227     return; // not requested to run clusterizer
228   }
229
230   Clusterize();
231   UpdateCells();
232   UpdateClusters();
233
234   if (fOutputAODBranch)
235     RecPoints2Clusters(fOutputAODBranch);
236 }
237
238 //________________________________________________________________________
239 void AliAnalysisTaskEMCALClusterizeFast::Clusterize()
240 {
241   // Clusterize
242
243   if (fSubBackground) {
244     fClusterizer->SetInputCalibrated(kTRUE);   
245     fClusterizer->SetCalibrationParameters(0);
246   }
247
248   fClusterizer->Digits2Clusters("");
249   if (fSubBackground) {
250     if (fCalibData) {
251       fClusterizer->SetInputCalibrated(kFALSE);   
252       fClusterizer->SetCalibrationParameters(fCalibData);
253     }
254   }
255 }
256
257 //________________________________________________________________________
258 void AliAnalysisTaskEMCALClusterizeFast::FillDigitsArray()
259 {
260   // Fill digits array
261
262   fDigitsArr->Clear("C");
263   
264   if (fCreatePattern) {  // Fill digits from a pattern
265     AliEMCALGeometry *fGeom = AliEMCALGeometry::GetInstance(fGeomName);
266     Int_t maxd = fGeom->GetNCells() / 4;
267     for (Int_t idigit = 0; idigit < maxd; idigit++){
268       if (idigit % 24 == 12) idigit += 12;
269       AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->New(idigit));
270       digit->SetId(idigit * 4);
271       digit->SetTime(600);
272       digit->SetTimeR(600);
273       digit->SetIndexInList(idigit);
274       digit->SetType(AliEMCALDigit::kHG);
275       digit->SetAmplitude(0.1);
276     }
277
278   } else if (fClusterizeFastORs) { // Fill digits from FastORs
279     
280     AliEMCALGeometry *fGeom = AliEMCALGeometry::GetInstance(fGeomName);
281   
282     AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());
283     if (!esd){
284       AliError("Cannot get the ESD event");
285       return;
286     } 
287     
288     AliESDCaloTrigger *triggers = esd->GetCaloTrigger("EMCAL");
289     
290     if (!triggers || !(triggers->GetEntries() > 0))
291       return;
292   
293     Int_t idigit = 0;
294     triggers->Reset();
295     
296     while ((triggers->Next())) {
297       Float_t triggerAmplitude = 0;
298       triggers->GetAmplitude(triggerAmplitude);
299       if (triggerAmplitude <= 0)
300         continue;
301       
302       Int_t triggerTime = 0;
303       Int_t ntimes = 0;
304       triggers->GetNL0Times(ntimes);
305       if (!(ntimes > 0) && fCutL0Times)
306         continue;
307
308       Int_t trgtimes[25];
309       triggers->GetL0Times(trgtimes);
310       triggerTime = trgtimes[0];
311      
312       Int_t triggerCol = 0, triggerRow = 0;
313       triggers->GetPosition(triggerCol, triggerRow);
314       
315       Int_t find = -1;
316       fGeom->GetAbsFastORIndexFromPositionInEMCAL(triggerCol, triggerRow, find);
317       
318       if (find<0)
319         continue;
320       
321       Int_t cidx[4] = {-1};
322       Bool_t ret = fGeom->GetCellIndexFromFastORIndex(find, cidx);
323       
324       if (!ret)
325         continue;
326       
327       for (Int_t idxpos = 0; idxpos < 4; idxpos++) {
328         Int_t triggerNumber = cidx[idxpos];
329         AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->New(idigit));
330         digit->SetId(triggerNumber);
331         digit->SetTime(triggerTime);
332         digit->SetTimeR(triggerTime);
333         digit->SetIndexInList(idigit);
334         digit->SetType(AliEMCALDigit::kHG);
335         digit->SetAmplitude(triggerAmplitude);
336         idigit++;
337       }
338     }
339
340   } else { // Fill digits from cells.
341     
342     AliVCaloCells *cells = InputEvent()->GetEMCALCells();
343     Double_t avgE        = 0; // for background subtraction
344     const Int_t ncells   = cells->GetNumberOfCells();
345     for (Int_t icell = 0, idigit = 0; icell < ncells; ++icell) {
346       Double_t cellAmplitude=0, cellTime=0, cellEFrac = 0;
347       Short_t  cellNumber=0, cellMCLabel=-1;
348       if (cells->GetCell(icell, cellNumber, cellAmplitude, cellTime, cellMCLabel, cellEFrac) != kTRUE)
349         break;
350       AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->New(idigit));
351       digit->SetId(cellNumber);
352       digit->SetTime(cellTime);
353       digit->SetTimeR(cellTime);
354       digit->SetIndexInList(idigit);
355       digit->SetType(AliEMCALDigit::kHG);
356       if (fRecalibOnly||fSubBackground) {
357         Float_t energy = cellAmplitude;
358         Float_t time   = cellTime;
359         fClusterizer->Calibrate(energy,time,cellNumber);
360         digit->SetAmplitude(energy);
361         avgE += energy;
362       } else {
363         digit->SetAmplitude(cellAmplitude);
364       }
365       idigit++;
366     }
367     
368     fDigitsArr->Sort();
369
370     if (fSubBackground) {
371       avgE /= AliEMCALGeometry::GetInstance(fGeomName)->GetNumberOfSuperModules()*48*24;
372       Int_t ndigis = fDigitsArr->GetEntries();
373       for (Int_t i = 0; i < ndigis; ++i) {
374         AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->At(i));
375         Double_t energy = digit->GetAmplitude() - avgE;
376         if (energy<=0.001) {
377           digit->SetAmplitude(0);
378         } else {
379           digit->SetAmplitude(energy);
380         }
381       }
382     }
383   }
384 }
385
386 //________________________________________________________________________________________
387 void AliAnalysisTaskEMCALClusterizeFast::RecPoints2Clusters(TClonesArray *clus)
388 {
389   // Cluster energy, global position, cells and their amplitude fractions are restored.
390
391   Bool_t esdobjects = 0;
392   if (strcmp(clus->GetClass()->GetName(),"AliESDCaloCluster")==0)
393     esdobjects = 1;
394
395   AliVCaloCells *cells = InputEvent()->GetEMCALCells();
396   AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(fGeomName);
397   AliVEvent *event = InputEvent();
398   if (!event) {
399     AliError("Cannot get the event");
400     return;
401   } 
402
403   // tracks array for track/cluster matching
404   TClonesArray *tarr = 0;
405   if (!fTrackName.IsNull()) {
406     tarr = dynamic_cast<TClonesArray*>(event->FindListObject(fTrackName));
407     if (!tarr) {
408       AliError(Form("Cannot get tracks named %s", fTrackName.Data()));
409     }
410   }
411   
412   const Int_t Ncls = fClusterArr->GetEntries();
413   AliDebug(1, Form("total no of clusters %d", Ncls)); 
414   for(Int_t i=0, nout=clus->GetEntries(); i < Ncls; ++i) {
415     AliEMCALRecPoint *recpoint = static_cast<AliEMCALRecPoint*>(fClusterArr->At(i));
416     Int_t ncells_true = 0;
417     const Int_t ncells = recpoint->GetMultiplicity();
418     UShort_t   absIds[ncells];  
419     Double32_t ratios[ncells];
420     Int_t *dlist = recpoint->GetDigitsList();
421     Float_t *elist = recpoint->GetEnergiesList();
422     for (Int_t c = 0; c < ncells; ++c) {
423       AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->At(dlist[c]));
424       absIds[ncells_true] = digit->GetId();
425       ratios[ncells_true] = elist[c]/digit->GetAmplitude();
426       ++ncells_true;
427     }
428     
429     if (ncells_true < 1) {
430       AliWarning("Skipping cluster with no cells");
431       continue;
432     }
433     
434     // calculate new cluster position
435     TVector3 gpos;
436     recpoint->GetGlobalPosition(gpos);
437     Float_t g[3];
438     gpos.GetXYZ(g);
439     
440     AliDebug(1, Form("energy %f", recpoint->GetEnergy()));
441     
442     AliVCluster *c = static_cast<AliVCluster*>(clus->New(nout++));
443     c->SetType(AliVCluster::kEMCALClusterv1);
444     c->SetE(recpoint->GetEnergy());
445     c->SetPosition(g);
446     c->SetNCells(ncells_true);
447     if (esdobjects) {
448       AliESDCaloCluster *cesd = static_cast<AliESDCaloCluster*>(c);
449       cesd->SetCellsAbsId(absIds);
450       cesd->SetCellsAmplitudeFraction(ratios);
451       cesd->SetID(recpoint->GetUniqueID());
452     } else {
453       AliAODCaloCluster *caod = static_cast<AliAODCaloCluster*>(c);
454       caod->SetCellsAbsId(absIds);
455       caod->SetCellsAmplitudeFraction(ratios);
456     }
457     c->SetDispersion(recpoint->GetDispersion());
458     c->SetEmcCpvDistance(-1);
459     c->SetChi2(-1);
460     c->SetTOF(recpoint->GetTime()) ;     //time-of-flight
461     c->SetNExMax(recpoint->GetNExMax()); //number of local maxima
462     Float_t elipAxis[2];
463     recpoint->GetElipsAxis(elipAxis);
464     c->SetM02(elipAxis[0]*elipAxis[0]);
465     c->SetM20(elipAxis[1]*elipAxis[1]);
466     if (fPedestalData) {
467       c->SetDistanceToBadChannel(recpoint->GetDistanceToBadTower()); 
468     } else {
469       if (fRecoUtils && fRecoUtils->IsBadChannelsRemovalSwitchedOn()) {
470         fRecoUtils->RecalculateClusterDistanceToBadChannel(geom, cells, c);
471       } 
472     }
473
474     if (tarr) {
475       Double_t dEtaMin  = 1e9;
476       Double_t dPhiMin  = 1e9;
477       Int_t    imin     = -1;
478       Double_t ceta     = gpos.Eta();
479       Double_t cphi     = gpos.Phi();
480       const Int_t ntrks = tarr->GetEntries();
481       for(Int_t t = 0; t<ntrks; ++t) {
482         AliVTrack *track = static_cast<AliVTrack*>(tarr->At(t));
483         if (!track)
484           continue;
485         const AliExternalTrackParam *outp = track->GetOuterParam();
486         if (!outp)
487           continue;
488         Double_t trkPos[3] = {0.,0.,0.};
489         if (!outp->GetXYZ(trkPos)) 
490           continue;
491         TVector3 vec(trkPos[0],trkPos[1],trkPos[2]);
492         Double_t veta = vec.Eta();
493         Double_t vphi = vec.Phi();
494         if(vphi<0)
495           vphi += 2*TMath::Pi();
496         if (TMath::Abs(veta)>0.75 || (vphi<70*TMath::DegToRad()) || (vphi>190*TMath::DegToRad()))
497           continue;
498         Double_t dR = vec.DeltaR(gpos);
499         if(dR > 25) 
500           continue;
501         Float_t tmpEta=0, tmpPhi=0;
502         if (0) {
503           AliExternalTrackParam trkParTemp(*outp); // retrieve the starting point every time before the extrapolation
504           Bool_t ret = fRecoUtils->ExtrapolateTrackToCluster(&trkParTemp, c, fRecoUtils->GetMass(), fRecoUtils->GetStep(), tmpEta, tmpPhi); 
505           if (!ret)
506             continue;
507         } else {
508           tmpEta = ceta - veta;
509           tmpPhi = cphi - vphi;
510         }
511         if (TMath::Abs(tmpEta)<TMath::Abs(dEtaMin) && TMath::Abs(tmpPhi)<TMath::Abs(dPhiMin)) {
512           dEtaMin = tmpEta;
513           dPhiMin = tmpPhi;
514           imin = t;
515         }
516       }
517       c->SetEmcCpvDistance(imin);
518       c->SetTrackDistance(dPhiMin, dEtaMin);
519     }
520   }
521 }
522
523 //________________________________________________________________________
524 void AliAnalysisTaskEMCALClusterizeFast::UpdateCells()
525 {
526   // Update cells in case re-calibration was done.
527
528   if (!fCalibData&&!fSubBackground)
529     return;
530
531   AliVCaloCells *cells = InputEvent()->GetEMCALCells();
532   const Int_t   ncells = cells->GetNumberOfCells();
533   const Int_t   ndigis = fDigitsArr->GetEntries();
534   if (ncells!=ndigis) {
535     cells->DeleteContainer();
536     cells->CreateContainer(ndigis);
537   }
538   for (Int_t idigit = 0; idigit < ndigis; ++idigit) {
539     AliEMCALDigit *digit = static_cast<AliEMCALDigit*>(fDigitsArr->At(idigit));
540     Double_t cellAmplitude = digit->GetCalibAmp();
541     Short_t cellNumber     = digit->GetId();
542     Double_t cellTime      = digit->GetTime();
543     cells->SetCell(idigit, cellNumber, cellAmplitude, cellTime);
544   }
545 }
546
547 //________________________________________________________________________
548 void AliAnalysisTaskEMCALClusterizeFast::UpdateClusters()
549 {
550   // Update cells in case re-calibration was done.
551   
552   if (!fAttachClusters)
553     return;
554   
555   TClonesArray *clus = 0;
556   
557   if (fOverwrite) {
558     clus = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject("caloClusters"));
559     if (!clus)
560       clus = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject("CaloClusters"));
561     if (!clus)
562       return;
563     
564     const Int_t nents = clus->GetEntries();
565     for (Int_t i=0;i<nents;++i) {
566       AliVCluster *c = static_cast<AliVCluster*>(clus->At(i));
567       if (!c)
568         continue;
569       if (c->IsEMCAL())
570         delete clus->RemoveAt(i);
571     }
572   } else {
573     clus = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fNewClusterArrayName));
574     if (!clus) {
575       clus = new TClonesArray("AliESDCaloCluster");
576       clus->SetName(fNewClusterArrayName);
577       InputEvent()->AddObject(clus);
578     } else {
579       clus->Delete();
580     }
581   }
582   
583   RecPoints2Clusters(clus);
584 }
585
586 //________________________________________________________________________________________
587 void AliAnalysisTaskEMCALClusterizeFast::Init()
588 {
589   //Select clusterization/unfolding algorithm and set all the needed parameters
590
591   AliVEvent * event = InputEvent();
592   if (!event) {
593     AliWarning("Event not available!!!");
594     return;
595   }
596
597   if (event->GetRunNumber()==fRun)
598     return;
599   fRun = event->GetRunNumber();
600
601   if (fJustUnfold){
602     // init the unfolding afterburner 
603     delete fUnfolder;
604     fUnfolder = new AliEMCALAfterBurnerUF(fRecParam->GetW0(),fRecParam->GetLocMaxCut(),fRecParam->GetMinECut());
605     return;
606   }
607
608   AliEMCALGeometry *geometry = AliEMCALGeometry::GetInstance(fGeomName);
609   if (!geometry) {
610     AliFatal("Geometry not available!!!");
611     return;
612   }
613
614   if (!fGeomMatrixSet) {
615     if (fLoadGeomMatrices) {
616       for(Int_t mod=0; mod < geometry->GetNumberOfSuperModules(); ++mod) {
617         if (fGeomMatrix[mod]){
618           if (DebugLevel() > 2) 
619             fGeomMatrix[mod]->Print();
620           geometry->SetMisalMatrix(fGeomMatrix[mod],mod);  
621         }
622       }
623     } else { // get matrix from file (work around bug in aliroot)
624       for(Int_t mod=0; mod < geometry->GetEMCGeometry()->GetNumberOfSuperModules(); ++mod) {
625         const TGeoHMatrix *gm = 0;
626         AliESDEvent *esdevent = dynamic_cast<AliESDEvent*>(event);
627         if (esdevent) {
628           gm = esdevent->GetEMCALMatrix(mod);
629         } else {
630           AliAODHeader *aodheader = dynamic_cast<AliAODHeader*>(event->GetHeader());
631           if (aodheader) {
632             gm = aodheader->GetEMCALMatrix(mod);
633           }
634         }
635         if (gm) {
636           if (DebugLevel() > 2) 
637             gm->Print();
638           geometry->SetMisalMatrix(gm,mod);
639         }
640       }
641     }
642     fGeomMatrixSet=kTRUE;
643   }
644   
645   // setup digit array if needed
646   if (!fDigitsArr) {
647     fDigitsArr = new TClonesArray("AliEMCALDigit", 1000);
648     fDigitsArr->SetOwner(1);
649   }
650
651   // then setup clusterizer
652   delete fClusterizer;
653   if     (fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerv1)
654     fClusterizer = new AliEMCALClusterizerv1(geometry);
655   else if (fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerNxN) {
656    AliEMCALClusterizerNxN *clusterizer = new AliEMCALClusterizerNxN(geometry);
657    clusterizer->SetNRowDiff(fRecParam->GetNRowDiff());
658    clusterizer->SetNColDiff(fRecParam->GetNColDiff());
659     fClusterizer = clusterizer;
660   } else if (fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerv2) 
661     fClusterizer = new AliEMCALClusterizerv2(geometry);
662   else if (fRecParam->GetClusterizerFlag() == AliEMCALRecParam::kClusterizerFW){
663     AliEMCALClusterizerFixedWindow *clusterizer = new AliEMCALClusterizerFixedWindow(geometry);
664     clusterizer->SetNphi(fNPhi);
665     clusterizer->SetNeta(fNEta);
666     clusterizer->SetShiftPhi(fShiftPhi);
667     clusterizer->SetShiftEta(fShiftEta);
668     clusterizer->SetTRUshift(fTRUShift);
669     fClusterizer = clusterizer;
670   }
671   else{
672     AliFatal(Form("Clusterizer < %d > not available", fRecParam->GetClusterizerFlag()));
673   }
674   fClusterizer->InitParameters(fRecParam);
675
676   if ((!fCalibData&&fLoadCalib) || (!fPedestalData&&fLoadPed)) {
677     AliCDBManager *cdb = AliCDBManager::Instance();
678     if (!cdb->IsDefaultStorageSet() && !fOCDBpath.IsNull())
679       cdb->SetDefaultStorage(fOCDBpath);
680     if (fRun!=cdb->GetRun())
681       cdb->SetRun(fRun);
682   }
683   if (!fCalibData&&fLoadCalib&&fRun>0) {
684     AliCDBEntry *entry = static_cast<AliCDBEntry*>(AliCDBManager::Instance()->Get("EMCAL/Calib/Data"));
685     if (entry) 
686       fCalibData =  static_cast<AliEMCALCalibData*>(entry->GetObject());
687     if (!fCalibData)
688       AliFatal("Calibration parameters not found in CDB!");
689   }
690   if (!fPedestalData&&fLoadPed&&fRun>0) {
691     AliCDBEntry *entry = static_cast<AliCDBEntry*>(AliCDBManager::Instance()->Get("EMCAL/Calib/Pedestals"));
692     if (entry) 
693       fPedestalData =  static_cast<AliCaloCalibPedestal*>(entry->GetObject());
694   }
695   if (fCalibData) {
696     fClusterizer->SetInputCalibrated(kFALSE);   
697     fClusterizer->SetCalibrationParameters(fCalibData);
698   } else {
699     fClusterizer->SetInputCalibrated(kTRUE);   
700   }
701   fClusterizer->SetCaloCalibPedestal(fPedestalData);
702   fClusterizer->SetJustClusters(kTRUE);
703   fClusterizer->SetDigitsArr(fDigitsArr);
704   fClusterizer->SetOutput(0);
705   fClusterArr = const_cast<TObjArray *>(fClusterizer->GetRecPoints());
706 }