]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALReconstructor.cxx
delete digits and clusters array before finishing FillESD
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALReconstructor.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 /* $Id$ */
17
18 //_________________________________________________________________________
19 //--
20 //-- Yves Schutz (SUBATECH) 
21 // Reconstruction class. Redesigned from the old AliReconstructionner class and 
22 // derived from STEER/AliReconstructor. 
23 // 
24 // --- ROOT system ---
25
26 // --- Standard library ---
27
28 // --- AliRoot header files ---
29 #include "AliEMCALReconstructor.h"
30
31 #include "AliCodeTimer.h"
32 #include "AliESDEvent.h"
33 #include "AliESDCaloCluster.h"
34 #include "AliESDCaloCells.h"
35 #include "AliESDtrack.h"
36 #include "AliEMCALLoader.h"
37 #include "AliEMCALRawUtils.h"
38 #include "AliEMCALDigit.h"
39 #include "AliEMCALClusterizerv1.h"
40 #include "AliEMCALRecPoint.h"
41 #include "AliEMCALPID.h"
42 #include "AliEMCALTrigger.h"
43 #include "AliRawReader.h"
44 #include "AliCDBEntry.h"
45 #include "AliCDBManager.h"
46 #include "AliEMCALRecParam.h"
47 #include "AliEMCALGeometry.h"
48
49 #include "AliRunLoader.h"
50 #include "AliRun.h"
51
52 ClassImp(AliEMCALReconstructor)
53
54 AliEMCALRecParam* AliEMCALReconstructor::fgkRecParam = 0;  // EMCAL rec. parameters
55 AliEMCALRawUtils* AliEMCALReconstructor::fgRawUtils = 0;   // EMCAL raw utilities class
56 //____________________________________________________________________________
57 AliEMCALReconstructor::AliEMCALReconstructor() 
58   : fDebug(kFALSE),fGeom(0) 
59 {
60   // ctor
61   InitRecParam();
62
63   fgRawUtils = new AliEMCALRawUtils;
64
65   //To make sure we match with the geometry in a simulation file,
66   //let's try to get it first.  If not, take the default geometry
67   AliRunLoader *rl = AliRunLoader::GetRunLoader();
68   if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL")) {
69     fGeom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
70   } else {
71     AliInfo(Form("Using default geometry in reconstruction"));
72     fGeom =  AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
73   }
74
75   if(!fGeom) AliFatal(Form("Could not get geometry!"));
76
77
78
79 //____________________________________________________________________________
80 AliEMCALReconstructor::AliEMCALReconstructor(const AliEMCALReconstructor & rec)
81   : AliReconstructor(rec),
82     fDebug(rec.fDebug),
83     fGeom(rec.fGeom)
84 {
85   //copy ctor
86 }
87
88 //____________________________________________________________________________
89 AliEMCALReconstructor::~AliEMCALReconstructor()
90 {
91   // dtor
92   delete fGeom;
93   AliCodeTimer::Instance()->Print();
94
95
96 //____________________________________________________________________________
97 void AliEMCALReconstructor::InitRecParam() const
98 {
99   // Check if the instance of AliEMCALRecParam exists, 
100   // if not, get it from OCDB if available, otherwise create a default one
101
102  if (!fgkRecParam  && (AliCDBManager::Instance()->IsDefaultStorageSet())) {
103     AliCDBEntry *entry = (AliCDBEntry*) 
104       AliCDBManager::Instance()->Get("EMCAL/Config/RecParam");
105     if (entry) fgkRecParam =  (AliEMCALRecParam*) entry->GetObject();
106   }
107   
108   if(!fgkRecParam){
109     AliWarning("The Reconstruction parameters for EMCAL nonitialized - Used default one");
110     fgkRecParam = new AliEMCALRecParam;
111   }
112 }
113
114 //____________________________________________________________________________
115 void AliEMCALReconstructor::Reconstruct(TTree* digitsTree, TTree* clustersTree) const
116 {
117   // method called by AliReconstruction; 
118   // Only the clusterization is performed,; the rest of the reconstruction is done in FillESD because the track
119   // segment maker needs access to the AliESD object to retrieve the tracks reconstructed by 
120   // the global tracking.
121   // Works on the current event.
122
123   AliCodeTimerAuto("")
124
125   AliEMCALClusterizerv1 clu;
126   clu.SetInput(digitsTree);
127   clu.SetOutput(clustersTree);
128   if(Debug())
129     clu.Digits2Clusters("deb all") ;
130   else
131     clu.Digits2Clusters("") ;
132   
133 }
134
135 //____________________________________________________________________________
136 void AliEMCALReconstructor::ConvertDigits(AliRawReader* rawReader, TTree* digitsTree) const
137
138 {
139   // Conversion from raw data to
140   // EMCAL digits.
141   // Works on a single-event basis
142
143   rawReader->Reset() ; 
144
145   TClonesArray *digitsArr = new TClonesArray("AliEMCALDigit",100);
146   Int_t bufsize = 32000;
147   digitsTree->Branch("EMCAL", &digitsArr, bufsize);
148
149   //must be done here because, in constructor, option is not yet known
150   fgRawUtils->SetOption(GetOption());
151
152   fgRawUtils->SetRawFormatHighLowGainFactor(fgkRecParam->GetHighLowGainFactor());
153   fgRawUtils->SetRawFormatOrder(fgkRecParam->GetOrderParameter());
154   fgRawUtils->SetRawFormatTau(fgkRecParam->GetTau());
155   fgRawUtils->SetNoiseThreshold(fgkRecParam->GetNoiseThreshold());
156   fgRawUtils->SetNPedSamples(fgkRecParam->GetNPedSamples());
157
158   fgRawUtils->Raw2Digits(rawReader,digitsArr);
159
160   digitsTree->Fill();
161   digitsArr->Delete();
162   delete digitsArr;
163
164 }
165
166 //____________________________________________________________________________
167 void AliEMCALReconstructor::FillESD(TTree* digitsTree, TTree* clustersTree, 
168                                     AliESDEvent* esd) const
169 {
170   // Called by AliReconstruct after Reconstruct() and global tracking and vertexing 
171   // Works on the current event
172   // Creates AliESDCaloCluster from AliEMCALRecPoints 
173   // and AliESDCaloCells from AliEMCALDigits
174   // Also, fills ESD with calorimeter trigger information
175
176   //######################################################
177   //#########Calculate trigger and set trigger info###########
178   //######################################################
179
180   AliEMCALTrigger tr ;
181   //   tr.SetPatchSize(1);//create 4x4 patches
182   tr.Trigger();
183   
184   Float_t maxAmp2x2  = tr.Get2x2MaxAmplitude();
185   Float_t maxAmpnxn  = tr.GetnxnMaxAmplitude();
186   Float_t ampOutOfPatch2x2  = tr.Get2x2AmpOutOfPatch() ;
187   Float_t ampOutOfPatchnxn  = tr.GetnxnAmpOutOfPatch() ;
188
189   Int_t iSM2x2      = tr.Get2x2SuperModule();
190   Int_t iSMnxn      = tr.GetnxnSuperModule();
191   Int_t iCellPhi2x2 = tr.Get2x2CellPhi();
192   Int_t iCellPhinxn = tr.GetnxnCellPhi();
193   Int_t iCellEta2x2 = tr.Get2x2CellEta();
194   Int_t iCellEtanxn = tr.GetnxnCellEta();
195
196   AliDebug(2, Form("Trigger 2x2 max amp %f, out amp %f, SM %d, iphi %d ieta %d",  maxAmp2x2, ampOutOfPatch2x2, iSM2x2,iCellPhi2x2, iCellEta2x2));
197   AliDebug(2, Form("Trigger 4x4 max amp %f , out amp %f, SM %d, iphi %d, ieta %d",  maxAmpnxn, ampOutOfPatchnxn, iSMnxn,iCellPhinxn, iCellEtanxn));
198
199   TVector3    pos2x2(-1,-1,-1);
200   TVector3    posnxn(-1,-1,-1);
201
202   Int_t iAbsId2x2 = fGeom->GetAbsCellIdFromCellIndexes( iSM2x2, iCellPhi2x2, iCellEta2x2) ;
203   Int_t iAbsIdnxn = fGeom->GetAbsCellIdFromCellIndexes( iSMnxn, iCellPhinxn, iCellEtanxn) ;
204   fGeom->GetGlobal(iAbsId2x2, pos2x2);
205   fGeom->GetGlobal(iAbsIdnxn, posnxn);
206   
207   TArrayF triggerPosition(6);
208   triggerPosition[0] = pos2x2(0) ;   
209   triggerPosition[1] = pos2x2(1) ;   
210   triggerPosition[2] = pos2x2(2) ;  
211   triggerPosition[3] = posnxn(0) ;   
212   triggerPosition[4] = posnxn(1) ;   
213   triggerPosition[5] = posnxn(2) ;  
214
215   TArrayF triggerAmplitudes(4);
216   triggerAmplitudes[0] = maxAmp2x2 ;   
217   triggerAmplitudes[1] = ampOutOfPatch2x2 ;    
218   triggerAmplitudes[2] = maxAmpnxn ;   
219   triggerAmplitudes[3] = ampOutOfPatchnxn ;   
220
221   esd->AddEMCALTriggerPosition(triggerPosition);
222   esd->AddEMCALTriggerAmplitudes(triggerAmplitudes);
223
224   //########################################
225   //##############Fill CaloCells###############
226   //########################################
227
228   TClonesArray *digits = new TClonesArray("AliEMCALDigit",1000);
229   TBranch *branchdig = digitsTree->GetBranch("EMCAL");
230   if (!branchdig) { 
231     AliError("can't get the branch with the PHOS digits !");
232     return;
233   }
234   branchdig->SetAddress(&digits);
235   digitsTree->GetEvent(0);
236   Int_t nDigits = digits->GetEntries(), idignew = 0 ;
237   AliDebug(1,Form("%d digits",nDigits));
238
239   AliESDCaloCells &emcCells = *(esd->GetEMCALCells());
240   emcCells.CreateContainer(nDigits);
241   emcCells.SetType(AliESDCaloCells::kEMCALCell);
242   for (Int_t idig = 0 ; idig < nDigits ; idig++) {
243     const AliEMCALDigit * dig = (const AliEMCALDigit*)digits->At(idig);
244     if(dig->GetAmp() > 0 ){
245       emcCells.SetCell(idignew,dig->GetId(),dig->GetAmp(), dig->GetTime());   
246       idignew++;
247     }
248   }
249   emcCells.SetNumberOfCells(idignew);
250   emcCells.Sort();
251
252   //------------------------------------------------------------
253   //-----------------CLUSTERS-----------------------------
254   //------------------------------------------------------------
255   TObjArray *clusters = new TObjArray(100);
256   TBranch *branch = clustersTree->GetBranch("EMCALECARP");
257   branch->SetAddress(&clusters);
258   clustersTree->GetEvent(0);
259
260   Int_t nClusters = clusters->GetEntries(),  nClustersNew=0;
261   AliDebug(1,Form("%d clusters",nClusters));
262   esd->SetFirstEMCALCluster(esd->GetNumberOfCaloClusters()); // Put after Phos clusters 
263
264   //######################################################
265   //#######################TRACK MATCHING###############
266   //######################################################
267   //Fill list of integers, each one is index of track to which the cluster belongs.
268
269   // step 1 - initialize array of matched track indexes
270   Int_t *matchedTrack = new Int_t[nClusters];
271   for (Int_t iclus = 0; iclus < nClusters; iclus++)
272     matchedTrack[iclus] = -1;  // neg. index --> no matched track
273   
274   // step 2, change the flag for all matched clusters found in tracks
275   Int_t iemcalMatch = -1;
276   Int_t endtpc = esd->GetNumberOfTracks();
277   for (Int_t itrack = 0; itrack < endtpc; itrack++) {
278     AliESDtrack * track = esd->GetTrack(itrack) ; // retrieve track
279     iemcalMatch = track->GetEMCALcluster();
280     if(iemcalMatch >= 0) matchedTrack[iemcalMatch] = itrack;
281   } 
282
283   //########################################
284   //##############Fill CaloClusters############
285   //########################################
286
287   esd->SetNumberOfEMCALClusters(nClusters);
288   for (Int_t iClust = 0 ; iClust < nClusters ; iClust++) {
289     const AliEMCALRecPoint * clust = (const AliEMCALRecPoint*)clusters->At(iClust);
290     //if(clust->GetClusterType()== AliESDCaloCluster::kEMCALClusterv1) nRP++; else nPC++;
291     if (Debug()) clust->Print();
292     // Get information from EMCAL reconstruction points
293     Float_t xyz[3];
294     TVector3 gpos;
295     clust->GetGlobalPosition(gpos);
296     for (Int_t ixyz=0; ixyz<3; ixyz++) 
297       xyz[ixyz] = gpos[ixyz];
298     Float_t elipAxis[2];
299     clust->GetElipsAxis(elipAxis);
300     
301      //Create digits lists
302     Int_t cellMult = clust->GetMultiplicity();
303     //TArrayS digiList(digitMult);
304     Float_t *amplFloat = clust->GetEnergiesList();
305     Int_t   *digitInts = clust->GetAbsId();
306     TArrayS absIdList(cellMult);
307     //Uncomment when unfolding is done
308     //TArrayD fracList(cellMult);
309
310     Int_t newCellMult = 0; 
311     for (Int_t iCell=0; iCell<cellMult; iCell++) {
312       if (amplFloat[iCell] > 0) {
313         absIdList[newCellMult] = (UShort_t)(digitInts[iCell]);
314         //Uncomment when unfolding is done
315         //fracList[newCellMult] = amplFloat[iCell]/emcCells.GetCellAmplitude(digitInts[iCell]);
316         newCellMult++;
317       }
318     }
319     absIdList.Set(newCellMult);
320     //Uncomment when unfolding is done
321     //fracList.Set(newCellMult);
322  
323     if(newCellMult > 0) { // accept cluster if it has some digit
324       nClustersNew++;
325       //Primaries
326       Int_t  parentMult  = 0;
327       Int_t *parentList =  clust->GetParents(parentMult);
328       
329       // fills the ESDCaloCluster
330       AliESDCaloCluster * ec = new AliESDCaloCluster() ; 
331       ec->SetClusterType(AliESDCaloCluster::kEMCALClusterv1);
332       ec->SetPosition(xyz);
333       ec->SetE(clust->GetEnergy());
334       ec->SetNCells(newCellMult);
335       //Change type of list from short to ushort
336       UShort_t *newAbsIdList  = new UShort_t[newCellMult];
337       //Uncomment when unfolding is done
338       //Double_t *newFracList  = new Double_t[newCellMult];
339       for(Int_t i = 0; i < newCellMult ; i++) {
340         newAbsIdList[i]=absIdList[i];
341         //Uncomment when unfolding is done
342       //newFracList[i]=fracList[i];
343       }
344       ec->SetCellsAbsId(newAbsIdList);
345       //Uncomment when unfolding is done
346       //ec->SetCellsAmplitudeFraction(newFracList);
347       
348       ec->SetClusterDisp(clust->GetDispersion());
349       ec->SetClusterChi2(-1); //not yet implemented
350       ec->SetM02(elipAxis[0]*elipAxis[0]) ;
351       ec->SetM20(elipAxis[1]*elipAxis[1]) ;
352       ec->SetM11(-1) ;        //not yet implemented
353       
354       TArrayI arrayTrackMatched(1);// Only one track, temporal solution. 
355       arrayTrackMatched[0]= matchedTrack[iClust]; 
356       ec->AddTracksMatched(arrayTrackMatched); 
357       
358       TArrayI arrayParents(parentMult,parentList); 
359       ec->AddLabels(arrayParents);
360       
361       
362       // add the cluster to the esd object
363       esd->AddCaloCluster(ec);
364       delete ec;
365       //delete [] newAbsIdList ;
366       //delete [] newFracList ;
367     }
368   } // cycle on clusters
369   
370   delete [] matchedTrack;
371   
372   esd->SetNumberOfEMCALClusters(nClustersNew);
373   //if(nClustersNew != nClusters) 
374   //printf(" ##### nClusters %i -> new %i ##### \n", nClusters, nClustersNew );
375   
376   //Fill ESDCaloCluster with PID weights
377    AliEMCALPID *pid = new AliEMCALPID;
378    //pid->SetPrintInfo(kTRUE);
379    pid->SetReconstructor(kTRUE);
380    pid->RunPID(esd);
381    delete pid;
382
383    delete digits;
384    delete clusters;
385
386 }
387
388