]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALRawUtils.cxx
Changing to centrality dependent corrections
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALRawUtils.cxx
1 // -*- mode: c++ -*-
2 /**************************************************************************
3  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4  *                                                                        *
5  * Author: The ALICE Off-line Project.                                    *
6  * Contributors are mentioned in the code where appropriate.              *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          *
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 /* $Id$ */
18
19
20 //_________________________________________________________________________
21 //  Utility Class for handling Raw data
22 //  Does all transitions from Digits to Raw and vice versa, 
23 //  for simu and reconstruction
24 //
25 //  Note: the current version is still simplified. Only 
26 //    one raw signal per digit is generated; either high-gain or low-gain
27 //    Need to add concurrent high and low-gain info in the future
28 //    No pedestal is added to the raw signal.
29 //*-- Author: Marco van Leeuwen (LBL)
30 //*-- Major refactoring by Per Thomas Hille
31
32 #include "AliEMCALRawUtils.h"
33 #include "AliRun.h"
34 #include "AliRunLoader.h"
35 #include "AliAltroBuffer.h"
36 #include "AliRawReader.h"
37 #include "AliCaloRawStreamV3.h"
38 #include "AliDAQ.h"
39 #include "AliEMCALRecParam.h"
40 #include "AliEMCALLoader.h"
41 #include "AliEMCALGeometry.h"
42 #include "AliEMCALDigit.h"
43 #include "AliEMCALRawDigit.h"
44 #include "AliEMCAL.h"
45 #include "AliCaloCalibPedestal.h"  
46 #include "AliCaloBunchInfo.h"
47 #include "AliCaloFitResults.h"
48 #include "AliEMCALTriggerRawDigitMaker.h"
49 #include "AliEMCALTriggerSTURawStream.h"
50 #include "AliEMCALTriggerData.h"
51 #include "AliCaloConstants.h"
52 #include "AliCaloRawAnalyzer.h"
53 #include "AliCaloRawAnalyzerFactory.h"
54 #include "AliEMCALRawResponse.h"
55
56 using namespace CALO;
57 using namespace EMCAL;
58
59 using std::vector;
60 ClassImp(AliEMCALRawUtils)
61
62
63 AliEMCALRawUtils::AliEMCALRawUtils( Algo::fitAlgorithm fitAlgo) : fNoiseThreshold(3),
64                                                                   fNPedSamples(4), 
65                                                                   fGeom(0), 
66                                                                   fOption(""),
67                                                                   fRemoveBadChannels(kFALSE),
68                                                                   fFittingAlgorithm(fitAlgo),  
69                                                                   fTimeMin(-1.),
70                                                                   fTimeMax(1.),
71                                                                   fUseFALTRO(kTRUE),
72                                                                   fRawAnalyzer(0),
73                                                                   fTriggerRawDigitMaker(0x0)
74 { // ctor; set up fit algo etc
75   SetFittingAlgorithm(fitAlgo);
76   const TObjArray* maps = AliEMCALRecParam::GetMappings();
77   if(!maps) AliFatal("Cannot retrieve ALTRO mappings!!");
78   for(Int_t i = 0; i < 4; i++) 
79     {
80       fMapping[i] = (AliAltroMapping*)maps->At(i);
81     }
82   
83   AliRunLoader *rl = AliRunLoader::Instance();
84   if (rl && rl->GetAliRun()) 
85     {
86     AliEMCAL * emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
87     if(emcal)
88       {
89         fGeom = emcal->GetGeometry();
90       }
91     else 
92       {
93         AliDebug(1, Form("Using default geometry in raw reco"));
94         fGeom =  AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
95       }
96     } 
97   else 
98     {
99       AliDebug(1, Form("Using default geometry in raw reco"));
100       fGeom =  AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
101     }
102   
103   if(!fGeom) AliFatal(Form("Could not get geometry!"));
104   fTriggerRawDigitMaker = new AliEMCALTriggerRawDigitMaker();
105 }
106
107
108 AliEMCALRawUtils::~AliEMCALRawUtils() 
109 {
110   //dtor
111   delete fRawAnalyzer;
112   delete fTriggerRawDigitMaker;
113 }
114
115
116 void AliEMCALRawUtils::Digits2Raw()
117 {
118   // convert digits of the current event to raw data
119   AliRunLoader *rl = AliRunLoader::Instance();
120   AliEMCALLoader *loader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
121   loader->LoadDigits("EMCAL");
122   loader->GetEvent();
123   TClonesArray* digits = loader->Digits() ;
124   
125   if (!digits) {
126     Warning("Digits2Raw", "no digits found !");
127     return;
128   }
129   
130   static const Int_t nDDL = 12*2; // 12 SM hardcoded for now. Buffers allocated dynamically, when needed, so just need an upper limit here
131   AliAltroBuffer* buffers[nDDL];
132   for (Int_t i=0; i < nDDL; i++)
133     buffers[i] = 0;
134   
135   TArrayI adcValuesLow( TIMEBINS );
136   TArrayI adcValuesHigh( TIMEBINS );
137   
138   // loop over digits (assume ordered digits)
139   for (Int_t iDigit = 0; iDigit < digits->GetEntries(); iDigit++) 
140     {
141       AliEMCALDigit* digit = dynamic_cast<AliEMCALDigit *>(digits->At(iDigit)) ;
142       if(!digit)
143         {
144           AliFatal("NULL Digit");
145         }
146       else
147         {
148           if (digit->GetAmplitude() <  AliEMCALRawResponse::GetRawFormatThreshold() ) 
149             {
150               continue;
151             }
152           //get cell indices
153           Int_t nSM = 0;
154           Int_t nIphi = 0;
155           Int_t nIeta = 0;
156           Int_t iphi = 0;
157           Int_t ieta = 0;
158           Int_t nModule = 0;
159           fGeom->GetCellIndex(digit->GetId(), nSM, nModule, nIphi, nIeta);
160           fGeom->GetCellPhiEtaIndexInSModule(nSM, nModule, nIphi, nIeta,iphi, ieta) ;
161       
162           //Check which is the RCU, 0 or 1, of the cell.
163           Int_t iRCU = -111;
164           if (0<=iphi&&iphi<8) iRCU=0; // first cable row
165           else if (8<=iphi&&iphi<16 && 0<=ieta&&ieta<24) iRCU=0; // first half; 
166           else if(8<=iphi&&iphi<16 && 24<=ieta&&ieta<48) iRCU=1; // second half; 
167           //second cable row
168           else if(16<=iphi&&iphi<24) iRCU=1; // third cable row
169           
170           if (nSM%2==1) iRCU = 1 - iRCU; // swap for odd=C side, to allow us to cable both sides the same
171           
172           if (iRCU<0) 
173             Fatal("Digits2Raw()","Non-existent RCU number: %d", iRCU);
174       
175           //Which DDL?
176           Int_t iDDL = NRCUSPERMODULE*nSM + iRCU;
177           if (iDDL < 0 || iDDL >= nDDL){
178             Fatal("Digits2Raw()","Non-existent DDL board number: %d", iDDL);
179           }
180           else{
181             if (buffers[iDDL] == 0) 
182               {      
183                 // open new file and write dummy header
184                 TString fileName = AliDAQ::DdlFileName("EMCAL",iDDL);
185                 //Select mapping file RCU0A, RCU0C, RCU1A, RCU1C
186           Int_t iRCUside=iRCU+(nSM%2)*2;
187           //iRCU=0 and even (0) SM -> RCU0A.data   0
188           //iRCU=1 and even (0) SM -> RCU1A.data   1
189           //iRCU=0 and odd  (1) SM -> RCU0C.data   2
190           //iRCU=1 and odd  (1) SM -> RCU1C.data   3
191           buffers[iDDL] = new AliAltroBuffer(fileName.Data(),fMapping[iRCUside]);
192           buffers[iDDL]->WriteDataHeader(kTRUE, kFALSE);  //Dummy;
193         }
194         
195         // out of time range signal (?)
196         if (digit->GetTimeR() >  TIMEBINMAX  ) {
197           AliInfo("Signal is out of time range.\n");
198           buffers[iDDL]->FillBuffer((Int_t)digit->GetAmplitude());
199           buffers[iDDL]->FillBuffer( TIMEBINS );  // time bin
200           buffers[iDDL]->FillBuffer(3);          // bunch length      
201           buffers[iDDL]->WriteTrailer(3, ieta, iphi, nSM);  // trailer
202           // calculate the time response function
203         } else {
204           Bool_t lowgain = AliEMCALRawResponse::RawSampledResponse(digit->GetTimeR(), digit->GetAmplitude(), 
205                                                                    adcValuesHigh.GetArray(), adcValuesLow.GetArray()) ; 
206           
207           if (lowgain) 
208             buffers[iDDL]->WriteChannel(ieta, iphi, 0, TIMEBINS, adcValuesLow.GetArray(),  AliEMCALRawResponse::GetRawFormatThreshold()  );
209           else 
210             buffers[iDDL]->WriteChannel(ieta,iphi, 1, TIMEBINS, adcValuesHigh.GetArray(),  AliEMCALRawResponse::GetRawFormatThreshold()  );
211         }
212       }// iDDL under the limits
213     }//digit exists
214   }//Digit loop
215   
216   // write headers and close files
217   for (Int_t i=0; i < nDDL; i++) {
218     if (buffers[i]) {
219       buffers[i]->Flush();
220       buffers[i]->WriteDataHeader(kFALSE, kFALSE);
221       delete buffers[i];
222     }
223   }
224   
225   loader->UnloadDigits();
226 }
227
228
229
230 void AliEMCALRawUtils::AddDigit(TClonesArray *digitsArr, Int_t id, Int_t lowGain, Float_t amp, Float_t time, Float_t chi2, Int_t ndf) 
231 {
232   // comment
233   AliEMCALDigit *digit = 0, *tmpdigit = 0;
234   TIter nextdigit(digitsArr);
235  
236   while (digit == 0 && (tmpdigit = (AliEMCALDigit*) nextdigit())) 
237     {
238       if (tmpdigit->GetId() == id) digit = tmpdigit;
239     }
240   
241   if (!digit) { // no digit existed for this tower; create one
242     Int_t type = AliEMCALDigit::kHG; // use enum in AliEMCALDigit
243     if (lowGain) 
244       { 
245         amp *= HGLGFACTOR;
246         type = AliEMCALDigit::kLGnoHG;
247       } 
248     
249     Int_t idigit = digitsArr->GetEntries();
250     new((*digitsArr)[idigit]) AliEMCALDigit( -1, -1, id, amp, time, type, idigit, chi2, ndf); 
251     AliDebug(2,Form("Add digit Id %d for the first time, type %d", id, type));
252   }//digit added first time
253   else 
254     { // a digit already exists, check range 
255                 // (use high gain if signal < cut value, otherwise low gain)
256       if (lowGain) 
257         { // new digit is low gain
258           if (digit->GetAmplitude() >  OVERFLOWCUT ) 
259             {  // use if previously stored (HG) digit is out of range
260               digit->SetAmplitude( HGLGFACTOR * amp);
261               digit->SetTime(time);
262               digit->SetType(AliEMCALDigit::kLG);
263               AliDebug(2,Form("Add LG digit ID %d for the second time, type %d", digit->GetId(), digit->GetType()));
264             }
265         }//new low gain digit
266       else { // new digit is high gain 
267         
268         if (amp <  OVERFLOWCUT  ) 
269           { // new digit is high gain; use if not out of range
270             digit->SetAmplitude(amp);
271             digit->SetTime(time);
272             digit->SetType(AliEMCALDigit::kHG);
273             AliDebug(2,Form("Add HG digit ID %d for the second time, type %d", digit->GetId(), digit->GetType()));
274           }
275         else 
276           { // HG out of range, just change flag value to show that HG did exist
277             digit->SetType(AliEMCALDigit::kLG);
278             AliDebug(2,Form("Change LG digit to HG, ID %d, type %d", digit->GetId(), digit->GetType()));
279           }
280       }//new high gain digit
281     }//digit existed replace it
282 }
283
284
285 void AliEMCALRawUtils::Raw2Digits(AliRawReader* reader,TClonesArray *digitsArr, const AliCaloCalibPedestal* pedbadmap, TClonesArray *digitsTRG, AliEMCALTriggerData* trgData)
286 {
287   //conversion of raw data to digits
288   if(digitsArr) digitsArr->Clear("C"); 
289   if (!digitsArr) { Error("Raw2Digits", "no digits found !");return;}
290   if (!reader) {Error("Raw2Digits", "no raw reader found !");return;}
291   AliEMCALTriggerSTURawStream inSTU(reader);
292   AliCaloRawStreamV3 in(reader,"EMCAL",fMapping);       
293   reader->Select("EMCAL",0,43); // 43 = AliEMCALGeoParams::fgkLastAltroDDL
294   fTriggerRawDigitMaker->Reset();       
295   fTriggerRawDigitMaker->SetIO(reader, in, inSTU, digitsTRG, trgData);
296   fRawAnalyzer->SetIsZeroSuppressed(true); // TMP - should use stream->IsZeroSuppressed(), or altro cfg registers later
297     
298   Int_t lowGain  = 0;
299   Int_t caloFlag = 0; // low, high gain, or TRU, or LED ref.
300   
301   Float_t bcTimePhaseCorr = 0; // for BC-based L1 phase correction
302   Int_t bcMod4 = (reader->GetBCID() % 4); // LHC uses 40 MHz, EMCal uses 10 MHz clock
303   if (bcMod4==0 || bcMod4==1) { 
304     bcTimePhaseCorr = -1e-7; // subtract 100 ns for certain BC values
305   } 
306
307   while (in.NextDDL()) 
308     {
309       while (in.NextChannel()) 
310         {
311           caloFlag = in.GetCaloFlag();
312           if (caloFlag > 2) continue; // Work with ALTRO and FALTRO 
313           if(caloFlag < 2 && fRemoveBadChannels && pedbadmap->IsBadChannel(in.GetModule(),in.GetColumn(),in.GetRow()))
314             {
315               continue;
316             }  
317           vector<AliCaloBunchInfo> bunchlist; 
318           while (in.NextBunch()) 
319             {
320               bunchlist.push_back( AliCaloBunchInfo(in.GetStartTimeBin(), in.GetBunchLength(), in.GetSignals() ) );
321             } 
322           if (bunchlist.size() == 0) continue;
323           if ( caloFlag < 2 )
324             { // ALTRO
325               Int_t id = fGeom->GetAbsCellIdFromCellIndexes(in.GetModule(), in.GetRow(), in.GetColumn()) ;
326               lowGain  = in.IsLowGain();
327               fRawAnalyzer->SetL1Phase( in.GetL1Phase() );
328               AliCaloFitResults res =  fRawAnalyzer->Evaluate( bunchlist, in.GetAltroCFG1(), in.GetAltroCFG2());  
329               if(res.GetAmp() >= fNoiseThreshold )
330                 {
331                   AddDigit(digitsArr, id, lowGain, res.GetAmp(),  res.GetTime()+bcTimePhaseCorr, res.GetChi2(),  res.GetNdf() ); 
332                 }
333             }//ALTRO
334           else if(fUseFALTRO)
335             {// Fake ALTRO
336               fTriggerRawDigitMaker->Add( bunchlist );
337             }//Fake ALTRO
338         } // end while over channel   
339     } //end while over DDL's, of input stream 
340   fTriggerRawDigitMaker->PostProcess(); 
341   TrimDigits(digitsArr);
342 }
343
344
345 void AliEMCALRawUtils::TrimDigits(TClonesArray *digitsArr) 
346 { // rm entries with LGnoHG (unphysical), out of time window, and too bad chi2
347   AliEMCALDigit *digit = 0;
348   Int_t n = 0;
349   Int_t nDigits = digitsArr->GetEntriesFast();
350   TIter nextdigit(digitsArr);
351   while ((digit = (AliEMCALDigit*) nextdigit())) {
352     if (digit->GetType() == AliEMCALDigit::kLGnoHG) {
353       AliDebug(1,Form("Remove digit with id %d, LGnoHG",digit->GetId()));
354       digitsArr->Remove(digit);
355     }
356     else if(fTimeMin > digit->GetTime() || fTimeMax < digit->GetTime()) {
357       digitsArr->Remove(digit);
358       AliDebug(1,Form("Remove digit with id %d, Bad Time %e",digit->GetId(), digit->GetTime()));
359     }
360     else if (0 > digit->GetChi2()) {
361       digitsArr->Remove(digit);
362       AliDebug(1,Form("Remove digit with id %d, Bad Chi2 %e",digit->GetId(), digit->GetChi2()));
363     }
364     else {
365       digit->SetIndexInList(n); 
366       n++;
367     }    
368   }//while
369   
370   digitsArr->Compress();
371   AliDebug(1,Form("N Digits before trimming : %d; after array compression %d",nDigits,digitsArr->GetEntriesFast()));
372 }
373
374
375 void AliEMCALRawUtils::SetFittingAlgorithm(Int_t fitAlgo)              
376 { // select which fitting algo should be used
377   delete fRawAnalyzer; // delete doesn't do anything if the pointer is 0x0
378   fRawAnalyzer = AliCaloRawAnalyzerFactory::CreateAnalyzer( fitAlgo );
379   fRawAnalyzer->SetNsampleCut(5); // requirement for fits to be done, for the new methods
380   fRawAnalyzer->SetOverflowCut ( OVERFLOWCUT );
381   fRawAnalyzer->SetAmpCut(fNoiseThreshold);
382   fRawAnalyzer->SetFitArrayCut(fNoiseThreshold);
383 }
384
385
386