]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALReconstructor.cxx
Fixes for memory leaks (Christian)
[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 "AliRun.h"
32 #include "AliESDEvent.h"
33 #include "AliESDCaloCluster.h"
34 #include "AliESDtrack.h"
35 #include "AliRunLoader.h"
36 #include "AliEMCALLoader.h"
37 #include "AliEMCALRawUtils.h"
38 #include "AliEMCALClusterizerv1.h"
39 #include "AliEMCALRecPoint.h"
40 #include "AliEMCALPID.h"
41 #include "AliEMCALTrigger.h"
42 #include "AliRawReader.h"
43
44 ClassImp(AliEMCALReconstructor)
45
46 //____________________________________________________________________________
47 AliEMCALReconstructor::AliEMCALReconstructor() 
48   : fDebug(kFALSE) 
49 {
50   // ctor
51
52
53 //____________________________________________________________________________
54 AliEMCALReconstructor::AliEMCALReconstructor(const AliEMCALReconstructor & rec)
55   : AliReconstructor(rec),
56     fDebug(rec.fDebug)
57 {
58   //copy ctor
59 }
60
61 //____________________________________________________________________________
62 AliEMCALReconstructor::~AliEMCALReconstructor()
63 {
64   // dtor
65
66
67 //____________________________________________________________________________
68 void AliEMCALReconstructor::Reconstruct(AliRunLoader* runLoader) const 
69 {
70   // method called by AliReconstruction; 
71   // Only the clusterization is performed,; the rest of the reconstruction is done in FillESD because the track
72   // segment maker needs access to the AliESD object to retrieve the tracks reconstructed by 
73   // the global tracking.
74  
75   TString headerFile(runLoader->GetFileName()) ; 
76   TString branchName(runLoader->GetEventFolder()->GetName() ) ;  
77   
78   AliEMCALClusterizerv1 clu(headerFile, branchName);
79   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(runLoader->GetDetectorLoader("EMCAL"));    
80
81   Int_t nEvents   = runLoader->GetNumberOfEvents();
82   runLoader->LoadDigits("EMCAL");
83   for (Int_t ievent = 0; ievent < nEvents; ievent++) {
84     runLoader->GetEvent(ievent);
85     if ( Debug() ) 
86       clu.ExecuteTask("deb all") ; 
87     else 
88       clu.ExecuteTask("pseudo") ;  
89   }   
90   // Unload the Digits and RecPoints
91   emcalLoader->UnloadDigits() ; 
92   emcalLoader->UnloadRecPoints() ; 
93 }
94
95 //____________________________________________________________________________
96 void AliEMCALReconstructor::Reconstruct(AliRunLoader* runLoader, AliRawReader* rawReader) const 
97
98 {
99   // Reconstruction loop for Raw Data processing
100   //
101   // Only the clusterization is performed
102   // Track-cluster matching is done in FillESD because the track
103   // segment maker needs access to the AliESD object to retrieve 
104   // the tracks reconstructed by the global tracking.
105   
106   TString headerFile(runLoader->GetFileName()) ; 
107   TString branchName(runLoader->GetEventFolder()->GetName()) ;  
108
109   static AliEMCALRawUtils rawUtils;
110   AliEMCALClusterizerv1 clu(headerFile, branchName);
111   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(runLoader->GetDetectorLoader("EMCAL"));    
112  
113   Int_t iEvent = 0;
114   rawReader->Reset() ; 
115   while (rawReader->NextEvent()) {  
116     runLoader->GetEvent(iEvent++);
117
118     rawUtils.Raw2Digits(rawReader);
119
120     if ( Debug() ) 
121       clu.ExecuteTask("deb pseudo all") ; 
122     else 
123       clu.ExecuteTask("pseudo") ;  
124     // Unload the RecPoints
125     emcalLoader->UnloadRecPoints() ; 
126   }
127 }
128
129 //____________________________________________________________________________
130 void AliEMCALReconstructor::FillESD(AliRunLoader* runLoader, AliESDEvent* esd) const
131 {
132   // Called by AliReconstruct after Reconstruct() and global tracking and vertexing 
133   const double timeScale = 1.e+11; // transition constant from sec to 0.01 ns 
134
135   Int_t eventNumber = runLoader->GetEventNumber() ;
136
137   // Creates AliESDCaloCluster from AliEMCALRecPoints 
138   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(runLoader->GetDetectorLoader("EMCAL"));
139   runLoader->LoadRecPoints("EMCAL");
140   runLoader->GetEvent(eventNumber);
141   TObjArray *clusters = emcalLoader->RecPoints();
142   Int_t nClusters = clusters->GetEntries(), nClustersNew=0;
143   AliDebug(1,Form("Event %d: %d clusters",eventNumber,nClusters));
144   //  Int_t nRP=0, nPC=0; // in input
145   esd->SetFirstEMCALCluster(esd->GetNumberOfCaloClusters()); // Put after Phos clusters 
146
147   //######################################################
148   //#########Calculate trigger and set trigger info###########
149   //######################################################
150  
151   AliEMCALTrigger tr ;
152   //   tr.SetPatchSize(1);//create 4x4 patches
153   tr.Trigger();
154   
155   Float_t maxAmp2x2  = tr.Get2x2MaxAmplitude();
156   Float_t maxAmpnxn  = tr.GetnxnMaxAmplitude();
157   Float_t ampOutOfPatch2x2  = tr.Get2x2AmpOutOfPatch() ;
158   Float_t ampOutOfPatchnxn  = tr.GetnxnAmpOutOfPatch() ;
159
160   AliEMCALGeometry * geom = 0;
161   if (runLoader->GetAliRun() && runLoader->GetAliRun()->GetDetector("EMCAL"))
162     geom = dynamic_cast<AliEMCAL*>(runLoader->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
163   if (geom == 0) 
164     geom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaulGeometryName());
165
166   Int_t iSM2x2      = tr.Get2x2SuperModule();
167   Int_t iSMnxn      = tr.GetnxnSuperModule();
168   Int_t iCellPhi2x2 = tr.Get2x2CellPhi();
169   Int_t iCellPhinxn = tr.GetnxnCellPhi();
170   Int_t iCellEta2x2 = tr.Get2x2CellEta();
171   Int_t iCellEtanxn = tr.GetnxnCellEta();
172
173   AliDebug(2, Form("Trigger 2x2 max amp %f, out amp %f, SM %d, iphi %d ieta %d",  maxAmp2x2, ampOutOfPatch2x2, iSM2x2,iCellPhi2x2, iCellEta2x2));
174   AliDebug(2, Form("Trigger 4x4 max amp %f , out amp %f, SM %d, iphi %d, ieta %d",  maxAmpnxn, ampOutOfPatchnxn, iSMnxn,iCellPhinxn, iCellEtanxn));
175
176   TVector3    pos2x2(-1,-1,-1);
177   TVector3    posnxn(-1,-1,-1);
178
179   Int_t iAbsId2x2 = geom->GetAbsCellIdFromCellIndexes( iSM2x2, iCellPhi2x2, iCellEta2x2) ;
180   Int_t iAbsIdnxn = geom->GetAbsCellIdFromCellIndexes( iSMnxn, iCellPhinxn, iCellEtanxn) ;
181   geom->GetGlobal(iAbsId2x2, pos2x2);
182   geom->GetGlobal(iAbsIdnxn, posnxn);
183   
184   TArrayF triggerPosition(6);
185   triggerPosition[0] = pos2x2(0) ;   
186   triggerPosition[1] = pos2x2(1) ;   
187   triggerPosition[2] = pos2x2(2) ;  
188   triggerPosition[3] = posnxn(0) ;   
189   triggerPosition[4] = posnxn(1) ;   
190   triggerPosition[5] = posnxn(2) ;  
191
192   TArrayF triggerAmplitudes(4);
193   triggerAmplitudes[0] = maxAmp2x2 ;   
194   triggerAmplitudes[1] = ampOutOfPatch2x2 ;    
195   triggerAmplitudes[2] = maxAmpnxn ;   
196   triggerAmplitudes[3] = ampOutOfPatchnxn ;   
197
198   esd->AddEMCALTriggerPosition(triggerPosition);
199   esd->AddEMCALTriggerAmplitudes(triggerAmplitudes);
200   
201   //######################################################
202   //#######################TRACK MATCHING###############
203   //######################################################
204   //Fill list of integers, each one is index of track to which the cluster belongs.
205
206   // step 1 - initialize array of matched track indexes
207   Int_t *matchedTrack = new Int_t[nClusters];
208   for (Int_t iclus = 0; iclus < nClusters; iclus++)
209     matchedTrack[iclus] = -1;  // neg. index --> no matched track
210   
211   // step 2, change the flag for all matched clusters found in tracks
212   Int_t iemcalMatch = -1;
213   Int_t endtpc = esd->GetNumberOfTracks();
214   for (Int_t itrack = 0; itrack < endtpc; itrack++) {
215     AliESDtrack * track = esd->GetTrack(itrack) ; // retrieve track
216     iemcalMatch = track->GetEMCALcluster();
217     if(iemcalMatch >= 0) matchedTrack[iemcalMatch] = itrack;
218   } 
219   
220   //########################################
221   //##############Fill CaloClusters#############
222   //########################################
223
224
225   for (Int_t iClust = 0 ; iClust < nClusters ; iClust++) {
226     const AliEMCALRecPoint * clust = emcalLoader->RecPoint(iClust);
227     //if(clust->GetClusterType()== AliESDCaloCluster::kClusterv1) nRP++; else nPC++;
228     if (Debug()) clust->Print();
229     // Get information from EMCAL reconstruction points
230     Float_t xyz[3];
231     TVector3 gpos;
232     clust->GetGlobalPosition(gpos);
233     for (Int_t ixyz=0; ixyz<3; ixyz++) 
234       xyz[ixyz] = gpos[ixyz];
235     
236     Int_t digitMult = clust->GetMultiplicity();
237     Short_t *amplList = new Short_t[digitMult];
238     Short_t *timeList = new Short_t[digitMult];
239     Short_t *digiList = new Short_t[digitMult];
240     Float_t *amplFloat = clust->GetEnergiesList();
241     Float_t *timeFloat = clust->GetTimeList();
242     Int_t   *digitInts = clust->GetAbsId();
243     Float_t elipAxis[2];
244     clust->GetElipsAxis(elipAxis);
245     
246     // Convert Float_t* and Int_t* to Short_t* to save memory
247     // Problem : we should recalculate a cluster characteristics when discard digit(s)
248     Int_t newdigitMult = 0; 
249     for (Int_t iDigit=0; iDigit<digitMult; iDigit++) {
250       if (amplFloat[iDigit] > 0) {
251         amplList[newdigitMult] = (UShort_t)(amplFloat[iDigit]*500);
252         // Time in units of 0.01 ns = 10 ps
253         if(timeFloat[iDigit] < 65536./timeScale) 
254           timeList[newdigitMult] = (UShort_t)(timeFloat[iDigit]*timeScale);
255         else
256           timeList[newdigitMult] = 65535;
257         digiList[newdigitMult] = (UShort_t)(digitInts[iDigit]);
258         newdigitMult++;
259       }
260       else if (clust->GetClusterType() != AliESDCaloCluster::kPseudoCluster)
261         Warning("FillESD()","Negative or 0 digit amplitude in cluster");
262     }
263     
264     if(newdigitMult > 0) { // accept cluster if it has some digit
265       nClustersNew++;
266       if(newdigitMult != digitMult) { // some digits were deleted
267         Short_t *amplListNew = new Short_t[newdigitMult];
268         Short_t *timeListNew = new Short_t[newdigitMult];
269         Short_t *digiListNew = new Short_t[newdigitMult];
270         for (Int_t iDigit=0; iDigit<newdigitMult; iDigit++) {
271           amplListNew[iDigit] = amplList[iDigit];
272           timeListNew[iDigit] = timeList[iDigit];
273           digiListNew[iDigit] = digiList[iDigit];
274         }
275         
276         delete [] amplList;
277         delete [] timeList;
278         delete [] digiList;
279         
280         amplList = amplListNew;
281         timeList = timeListNew;
282         digiList = digiListNew;
283       }
284       
285       //Primaries
286       Int_t  parentMult  = 0;
287       Int_t *parentInts =  clust->GetParents(parentMult);
288       Short_t *parentList = new Short_t[parentMult];
289       for (Int_t ipr=0; ipr<parentMult; ipr++) 
290         parentList[ipr] = (Short_t)(parentInts[ipr]);    
291       
292     
293       // fills the ESDCaloCluster
294       AliESDCaloCluster * ec = new AliESDCaloCluster() ; 
295       ec->SetEMCAL(kTRUE);
296       ec->SetClusterType(clust->GetClusterType());
297       ec->SetPosition(xyz);
298       ec->SetE(clust->GetEnergy());
299       TArrayS arrayAmpList(newdigitMult,amplList);
300       TArrayS arrayTimeList(newdigitMult,timeList);
301       TArrayS arrayIndexList(newdigitMult,digiList);
302       ec->AddDigitAmplitude(arrayAmpList);
303       ec->AddDigitTime(arrayTimeList);
304       ec->AddDigitIndex(arrayIndexList);
305     
306       if(clust->GetClusterType()== AliESDCaloCluster::kClusterv1){
307
308         ec->SetClusterDisp(clust->GetDispersion());
309         ec->SetClusterChi2(-1); //not yet implemented
310         ec->SetM02(elipAxis[0]*elipAxis[0]) ;
311         ec->SetM20(elipAxis[1]*elipAxis[1]) ;
312         ec->SetM11(-1) ;        //not yet implemented
313         
314        TArrayS arrayTrackMatched(1);// Only one track, temporal solution.
315        arrayTrackMatched[0]= (Short_t)(matchedTrack[iClust]);
316        ec->AddTracksMatched(arrayTrackMatched);
317         
318        TArrayS arrayParents(parentMult,parentList);
319        ec->AddLabels(arrayParents);
320       } 
321       
322       // add the cluster to the esd object
323       esd->AddCaloCluster(ec);
324       delete ec;
325       delete [] parentList;
326     } else { // no new ESD cluster
327       
328     }
329     delete [] amplList;
330     delete [] timeList;
331     delete [] digiList;
332
333   } // cycle on clusters
334
335   delete [] matchedTrack;
336
337   esd->SetNumberOfEMCALClusters(nClustersNew);
338   //if(nClustersNew != nClusters) 
339   //printf(" ##### nClusters %i -> new %i ##### \n", nClusters, nClustersNew );
340   
341   //Fill ESDCaloCluster with PID weights
342   AliEMCALPID *pid = new AliEMCALPID;
343   //pid->SetPrintInfo(kTRUE);
344   pid->SetReconstructor(kTRUE);
345   pid->RunPID(esd);
346 }
347
348