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