]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSReconstructor.cxx
Run type changed: PHYSICS only
[u/mrichter/AliRoot.git] / PHOS / AliPHOSReconstructor.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 "AliLog.h"
30 #include "AliAltroMapping.h"
31 #include "AliESDEvent.h"
32 #include "AliESDCaloCluster.h"
33 #include "AliESDCaloCells.h"
34 #include "AliPHOSReconstructor.h"
35 #include "AliPHOSClusterizerv1.h"
36 #include "AliPHOSTrackSegmentMakerv1.h"
37 #include "AliPHOSPIDv1.h"
38 #include "AliPHOSTracker.h"
39 #include "AliRawReader.h"
40 #include "AliPHOSTrigger.h"
41 #include "AliPHOSGeometry.h"
42 #include "AliPHOSRecoParam.h"
43 #include "AliPHOSRecoParamEmc.h"
44 #include "AliPHOSRecoParamCpv.h"
45 #include "AliPHOSDigit.h"
46 #include "AliPHOSTrackSegment.h"
47 #include "AliPHOSEmcRecPoint.h"
48 #include "AliPHOSRecParticle.h"
49 #include "AliPHOSRawDecoder.h"
50 #include "AliPHOSRawDecoderv1.h"
51 #include "AliPHOSRawDecoderv2.h"
52 #include "AliPHOSRawDigiProducer.h"
53 #include "AliPHOSPulseGenerator.h"
54
55 ClassImp(AliPHOSReconstructor)
56
57 Bool_t AliPHOSReconstructor::fgDebug = kFALSE ; 
58 AliPHOSRecoParam* AliPHOSReconstructor::fgkRecoParamEmc =0;  // EMC rec. parameters
59 AliPHOSRecoParam* AliPHOSReconstructor::fgkRecoParamCpv =0;  // CPV rec. parameters
60 TClonesArray*     AliPHOSReconstructor::fgDigitsArray = 0;   // Array of PHOS digits
61 TObjArray*        AliPHOSReconstructor::fgEMCRecPoints = 0;   // Array of EMC rec.points
62
63 //____________________________________________________________________________
64 AliPHOSReconstructor::AliPHOSReconstructor() :
65   fGeom(NULL),fClusterizer(NULL),fTSM(NULL),fPID(NULL)
66 {
67   // ctor
68
69   if (!fgkRecoParamEmc) {
70     AliWarning("The Reconstruction parameters for EMC nonitialized - Used default one");
71     fgkRecoParamEmc = AliPHOSRecoParamEmc::GetEmcDefaultParameters();
72   }
73
74   if (!fgkRecoParamCpv) {
75     AliWarning("The Reconstruction parameters for CPV nonitialized - Used default one");
76     fgkRecoParamCpv = AliPHOSRecoParamCpv::GetCpvDefaultParameters();
77   }
78
79   fGeom        = AliPHOSGeometry::GetInstance("IHEP","");
80   fClusterizer = new AliPHOSClusterizerv1      (fGeom);
81   fTSM         = new AliPHOSTrackSegmentMakerv1(fGeom);
82   fPID         = new AliPHOSPIDv1              (fGeom);
83   fgDigitsArray = new TClonesArray("AliPHOSDigit",100);
84   fgEMCRecPoints= new TObjArray(100) ;
85 }
86
87 //____________________________________________________________________________
88   AliPHOSReconstructor::~AliPHOSReconstructor()
89 {
90   // dtor
91   delete fGeom;
92   delete fClusterizer;
93   delete fTSM;
94   delete fPID;
95   delete fgDigitsArray;
96   delete fgEMCRecPoints;
97
98
99 //____________________________________________________________________________
100 void AliPHOSReconstructor::Reconstruct(TTree* digitsTree, TTree* clustersTree) const
101 {
102   // 'single-event' local reco method called by AliReconstruction; 
103   // Only the clusterization is performed,; the rest of the reconstruction is done in FillESD because the track
104   // segment maker needs access to the AliESDEvent object to retrieve the tracks reconstructed by 
105   // the global tracking.
106
107   fClusterizer->SetInput(digitsTree);
108   fClusterizer->SetOutput(clustersTree);
109   if ( Debug() ) 
110     fClusterizer->Digits2Clusters("deb all") ; 
111   else 
112     fClusterizer->Digits2Clusters("") ;
113 }
114
115 //____________________________________________________________________________
116 void AliPHOSReconstructor::FillESD(TTree* digitsTree, TTree* clustersTree, 
117                                    AliESDEvent* esd) const
118 {
119   // This method produces PHOS rec-particles,
120   // then it creates AliESDtracks out of them and
121   // write tracks to the ESD
122
123
124   // do current event; the loop over events is done by AliReconstruction::Run()
125   fTSM->SetESD(esd) ; 
126   fTSM->SetInput(clustersTree);
127   if ( Debug() ) 
128     fTSM->Clusters2TrackSegments("deb all") ;
129   else 
130     fTSM->Clusters2TrackSegments("") ;
131   
132   fPID->SetInput(clustersTree, fTSM->GetTrackSegments()) ; 
133   fPID->SetESD(esd) ; 
134   if ( Debug() ) 
135     fPID->TrackSegments2RecParticles("deb all") ;
136   else 
137     fPID->TrackSegments2RecParticles("") ;
138
139   TClonesArray *recParticles  = fPID->GetRecParticles();
140   Int_t nOfRecParticles = recParticles->GetEntriesFast();
141   
142   esd->SetNumberOfPHOSClusters(nOfRecParticles) ; 
143   esd->SetFirstPHOSCluster(esd->GetNumberOfCaloClusters()) ;
144   
145   AliDebug(2,Form("%d rec. particles, option %s",nOfRecParticles,GetOption()));
146   
147   // Read digits array
148
149   TBranch *branch = digitsTree->GetBranch("PHOS");
150   if (!branch) { 
151     AliError("can't get the branch with the PHOS digits !");
152     return;
153   }
154   branch->SetAddress(&fgDigitsArray);
155   branch->GetEntry(0);
156
157   // Get the clusters array
158
159   TBranch *emcbranch = clustersTree->GetBranch("PHOSEmcRP");
160   if (!emcbranch) { 
161     AliError("can't get the branch with the PHOS EMC clusters !");
162     return;
163   }
164
165   emcbranch->SetAddress(&fgEMCRecPoints);
166   emcbranch->GetEntry(0);
167
168   //#########Calculate trigger and set trigger info###########
169
170   AliPHOSTrigger tr ;
171   //   tr.SetPatchSize(1);//create 4x4 patches
172   tr.SetSimulation(kFALSE);
173   tr.Trigger(fgDigitsArray);
174   
175   Float_t maxAmp2x2  = tr.Get2x2MaxAmplitude();
176   Float_t maxAmpnxn  = tr.GetnxnMaxAmplitude();
177   Float_t ampOutOfPatch2x2  = tr.Get2x2AmpOutOfPatch() ;
178   Float_t ampOutOfPatchnxn  = tr.GetnxnAmpOutOfPatch() ;
179
180   Int_t iSM2x2      = tr.Get2x2SuperModule();
181   Int_t iSMnxn      = tr.GetnxnSuperModule();
182   Int_t iCrystalPhi2x2 = tr.Get2x2CrystalPhi();
183   Int_t iCrystalPhinxn = tr.GetnxnCrystalPhi();
184   Int_t iCrystalEta2x2 = tr.Get2x2CrystalEta();
185   Int_t iCrystalEtanxn = tr.GetnxnCrystalEta();
186
187   AliDebug(2, Form("Trigger 2x2 max amp %f, out amp %f, SM %d, iphi %d ieta %d",  
188                    maxAmp2x2, ampOutOfPatch2x2, iSM2x2,iCrystalPhi2x2, iCrystalEta2x2));
189   AliDebug(2, Form("Trigger 4x4 max amp %f , out amp %f, SM %d, iphi %d, ieta %d",
190                    maxAmpnxn, ampOutOfPatchnxn, iSMnxn,iCrystalPhinxn, iCrystalEtanxn));
191
192   // Attention! PHOS modules in order to calculate AbsId need to be 1-5 not 0-4 as returns trigger.
193   Int_t iRelId2x2 []= {iSM2x2+1,0,iCrystalPhi2x2,iCrystalEta2x2};
194   Int_t iAbsId2x2 =-1;
195   Int_t iRelIdnxn []= {iSMnxn+1,0,iCrystalPhinxn,iCrystalEtanxn};
196   Int_t iAbsIdnxn =-1;
197   TVector3    pos2x2(-1,-1,-1);
198   TVector3    posnxn(-1,-1,-1);
199   fGeom->RelToAbsNumbering(iRelId2x2, iAbsId2x2);
200   fGeom->RelToAbsNumbering(iRelIdnxn, iAbsIdnxn);
201   fGeom->RelPosInAlice(iAbsId2x2, pos2x2);
202   fGeom->RelPosInAlice(iAbsIdnxn, posnxn);
203
204   TArrayF triggerPosition(6);
205   triggerPosition[0] = pos2x2(0) ;   
206   triggerPosition[1] = pos2x2(1) ;   
207   triggerPosition[2] = pos2x2(2) ;  
208   triggerPosition[3] = posnxn(0) ;   
209   triggerPosition[4] = posnxn(1) ;   
210   triggerPosition[5] = posnxn(2) ;  
211
212   TArrayF triggerAmplitudes(4);
213   triggerAmplitudes[0] = maxAmp2x2 ;   
214   triggerAmplitudes[1] = ampOutOfPatch2x2 ;    
215   triggerAmplitudes[2] = maxAmpnxn ;   
216   triggerAmplitudes[3] = ampOutOfPatchnxn ;   
217
218   //esd->SetPHOSTriggerCells(triggerPosition);
219   esd->AddPHOSTriggerPosition(triggerPosition);
220   esd->AddPHOSTriggerAmplitudes(triggerAmplitudes);
221   
222
223   //########################################
224   //############# Fill CaloCells ###########
225   //########################################
226
227   Int_t nDigits = fgDigitsArray->GetEntries();
228   Int_t idignew = 0 ;
229   AliDebug(1,Form("%d digits",nDigits));
230
231   const Int_t knEMC = fGeom->GetNModules()*fGeom->GetNPhi()*fGeom->GetNZ();
232   AliESDCaloCells &phsCells = *(esd->GetPHOSCells());
233   phsCells.CreateContainer(nDigits);
234   phsCells.SetType(AliESDCaloCells::kPHOSCell);
235
236   // Add to CaloCells only EMC digits with non-zero energy 
237   for (Int_t idig = 0 ; idig < nDigits ; idig++) {
238     const AliPHOSDigit * dig = (const AliPHOSDigit*)fgDigitsArray->At(idig);
239     if(dig->GetId() <= knEMC && dig->GetEnergy() > 0 ){
240       //printf("i %d; id %d; amp %f; time %e\n",
241       //idignew,dig->GetId(),dig->GetEnergy(), dig->GetTime());
242       phsCells.SetCell(idignew,dig->GetId(), dig->GetEnergy(), dig->GetTime());   
243       idignew++;
244     }
245   }
246   phsCells.SetNumberOfCells(idignew);
247   phsCells.Sort();
248
249   //########################################
250   //############## Fill CaloClusters #######
251   //########################################
252
253   for (Int_t recpart = 0 ; recpart < nOfRecParticles ; recpart++) {
254     AliPHOSRecParticle  *rp    = dynamic_cast<AliPHOSRecParticle*>(recParticles->At(recpart));
255     if (Debug()) 
256       rp->Print();
257     // Get track segment and EMC rec.point associated with this rec.particle
258     AliPHOSTrackSegment *ts    = static_cast<AliPHOSTrackSegment *>(fTSM->GetTrackSegments()
259                                                                     ->At(rp->GetPHOSTSIndex()));
260
261     AliPHOSEmcRecPoint  *emcRP = static_cast<AliPHOSEmcRecPoint *>(fgEMCRecPoints->At(ts->GetEmcIndex()));
262     AliESDCaloCluster   *ec    = new AliESDCaloCluster() ; 
263     
264     Float_t xyz[3];
265     for (Int_t ixyz=0; ixyz<3; ixyz++) 
266       xyz[ixyz] = rp->GetPos()[ixyz];
267     
268     AliDebug(2,Form("Global position xyz=(%f,%f,%f)",xyz[0],xyz[1],xyz[2]));
269    
270     // Create cell lists
271
272     Int_t     cellMult   = emcRP->GetDigitsMultiplicity();
273     Int_t    *digitsList = emcRP->GetDigitsList();
274     Float_t  *rpElist    = emcRP->GetEnergiesList() ;
275     UShort_t *absIdList  = new UShort_t[cellMult];
276     Double_t *fracList   = new Double_t[cellMult];
277
278     for (Int_t iCell=0; iCell<cellMult; iCell++) {
279       AliPHOSDigit *digit = static_cast<AliPHOSDigit *>(fgDigitsArray->At(digitsList[iCell]));
280       absIdList[iCell] = (UShort_t)(digit->GetId());
281       if (digit->GetEnergy() > 0)
282         fracList[iCell] = rpElist[iCell]/digit->GetEnergy();
283       else
284         fracList[iCell] = 0;
285     }
286
287     //Primaries
288     Int_t  primMult  = 0;
289     Int_t *primList =  emcRP->GetPrimaries(primMult);
290
291     Float_t energy;
292     if (fgkRecoParamEmc->Ecore2ESD())
293       energy = emcRP->GetCoreEnergy();
294     else
295       energy = rp->Energy();
296
297     // fills the ESDCaloCluster
298     ec->SetClusterType(AliESDCaloCluster::kPHOSCluster);
299     ec->SetPosition(xyz);                       //rec.point position in MARS
300     ec->SetE(energy);                           //total or core particle energy
301     ec->SetClusterDisp(emcRP->GetDispersion()); //cluster dispersion
302     ec->SetPid(rp->GetPID()) ;                  //array of particle identification
303     ec->SetM02(emcRP->GetM2x()) ;               //second moment M2x
304     ec->SetM20(emcRP->GetM2z()) ;               //second moment M2z
305     ec->SetNExMax(emcRP->GetNExMax());          //number of local maxima
306     ec->SetEmcCpvDistance(ts->GetCpvDistance("r")); //Only radius, what about separate x,z????
307     ec->SetClusterChi2(-1);                     //not yet implemented
308     ec->SetTOF(emcRP->GetTime()); //Time of flight
309
310     //Cells contributing to clusters
311     ec->SetNCells(cellMult);
312     ec->SetCellsAbsId(absIdList);
313     ec->SetCellsAmplitudeFraction(fracList);
314
315     //Distance to the nearest bad crystal
316     ec->SetDistanceToBadChannel(emcRP->GetDistanceToBadCrystal()); 
317   
318     //Array of MC indeces
319     TArrayI arrayPrim(primMult,primList);
320     ec->AddLabels(arrayPrim);
321
322     //Array of tracks uncomment when available in future
323     //TArrayS arrayTrackMatched(1);// Only one track, temporal solution.
324     //arrayTrackMatched[0]= (Short_t)(matchedTrack[iClust]);
325     //ec->AddTracksMatched(arrayTrackMatched);
326     
327     // add the track to the esd object
328
329     esd->AddCaloCluster(ec);
330     delete ec;   
331     delete [] fracList;
332     delete [] absIdList;
333   }
334   fgDigitsArray ->Delete();
335   fgEMCRecPoints->Delete();
336   recParticles  ->Delete();
337 }
338
339 //____________________________________________________________________________
340 AliTracker* AliPHOSReconstructor::CreateTracker() const
341 {
342   // creates the PHOS tracker
343   return new AliPHOSTracker();
344 }
345
346 //____________________________________________________________________________
347 void  AliPHOSReconstructor::ConvertDigits(AliRawReader* rawReader, TTree* digitsTree) const
348 {
349   // Converts raw data to
350   // PHOS digits
351   // Works on a single-event basis
352
353   rawReader->Reset() ; 
354
355   AliPHOSRawDecoder * dc ;
356
357   const TObjArray* maps = AliPHOSRecoParamEmc::GetMappings();
358   if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
359
360   AliAltroMapping *mapping[4];
361   for(Int_t i = 0; i < 4; i++) {
362     mapping[i] = (AliAltroMapping*)maps->At(i);
363   }
364
365   if(strcmp(fgkRecoParamEmc->DecoderVersion(),"v1")==0) 
366     dc=new AliPHOSRawDecoderv1(rawReader,mapping);
367   else 
368     if(strcmp(fgkRecoParamEmc->DecoderVersion(),"v2")==0) 
369       dc=new AliPHOSRawDecoderv2(rawReader,mapping);
370     else
371       dc=new AliPHOSRawDecoder(rawReader,mapping);
372
373   dc->SubtractPedestals(fgkRecoParamEmc->SubtractPedestals());
374   
375   TClonesArray *digits = new TClonesArray("AliPHOSDigit",1);
376   digits->SetName("DIGITS");
377   Int_t bufsize = 32000;
378   digitsTree->Branch("PHOS", &digits, bufsize);
379
380   AliPHOSRawDigiProducer pr(fgkRecoParamEmc,fgkRecoParamCpv);
381   pr.MakeDigits(digits,dc);
382
383   delete dc ;
384
385   //!!!!for debug!!!
386 /*
387   Int_t modMax=-111;
388   Int_t colMax=-111;
389   Int_t rowMax=-111;
390   Float_t eMax=-333;
391   //!!!for debug!!!
392
393   Int_t relId[4];
394   for(Int_t iDigit=0; iDigit<digits->GetEntries(); iDigit++) {
395     AliPHOSDigit* digit = (AliPHOSDigit*)digits->At(iDigit);
396     if(digit->GetEnergy()>eMax) {
397       fGeom->AbsToRelNumbering(digit->GetId(),relId);
398       eMax=digit->GetEnergy();
399       modMax=relId[0];
400       rowMax=relId[2];
401       colMax=relId[3];
402     }
403   }
404
405   AliDebug(1,Form("Digit with max. energy:  modMax %d colMax %d rowMax %d  eMax %f\n\n",
406                   modMax,colMax,rowMax,eMax));
407 */
408   digitsTree->Fill();
409   digits->Delete();
410   delete digits;
411 }