]> git.uio.no Git - u/mrichter/AliRoot.git/blob - VZERO/AliVZEROReconstructor.cxx
Calibrated TDC thresholds extracted from the HV-scan data. Affects both simulated...
[u/mrichter/AliRoot.git] / VZERO / AliVZEROReconstructor.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 /// class for VZERO reconstruction                                           //
21 ///                                                                          //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include <TH1F.h>
25 #include <TF1.h>
26 #include <TParameter.h>
27
28 #include "AliRunLoader.h"
29 #include "AliRawReader.h"
30 #include "AliGRPObject.h"
31 #include "AliCDBManager.h"
32 #include "AliCDBStorage.h"
33 #include "AliCDBEntry.h"
34 #include "AliVZEROReconstructor.h"
35 #include "AliVZERORawStream.h"
36 #include "AliVZEROConst.h"
37 #include "AliESDEvent.h"
38 #include "AliVZEROTriggerMask.h"
39 #include "AliESDfriend.h"
40 #include "AliESDVZEROfriend.h"
41 #include "AliVZEROdigit.h"
42 #include "AliVZEROCalibData.h"
43 #include "AliRunInfo.h"
44 #include "AliCTPTimeParams.h"
45 #include "AliLHCClockPhase.h"
46
47 ClassImp(AliVZEROReconstructor)
48
49 //_____________________________________________________________________________
50 AliVZEROReconstructor:: AliVZEROReconstructor(): AliReconstructor(),
51                         fESDVZERO(0x0),
52                         fESD(0x0),
53                         fESDVZEROfriend(0x0),
54                         fCalibData(NULL),
55                         fTimeSlewing(NULL),
56                         fCollisionMode(0),
57                         fBeamEnergy(0.),
58                         fDigitsArray(0)
59 {
60   // Default constructor  
61   // Get calibration data
62   
63   fCalibData = GetCalibData();
64
65   AliCDBEntry *entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");
66   if (!entry) AliFatal("CTP timing parameters are not found in OCDB !");
67   AliCTPTimeParams *ctpParams = (AliCTPTimeParams*)entry->GetObject();
68   Float_t l1Delay = (Float_t)ctpParams->GetDelayL1L0()*25.0;
69
70   AliCDBEntry *entry1 = AliCDBManager::Instance()->Get("GRP/CTP/TimeAlign");
71   if (!entry1) AliFatal("CTP time-alignment is not found in OCDB !");
72   AliCTPTimeParams *ctpTimeAlign = (AliCTPTimeParams*)entry1->GetObject();
73   l1Delay += ((Float_t)ctpTimeAlign->GetDelayL1L0()*25.0);
74
75   AliCDBEntry *entry2 = AliCDBManager::Instance()->Get("VZERO/Calib/TimeDelays");
76   if (!entry2) AliFatal("VZERO time delays are not found in OCDB !");
77   TH1F *delays = (TH1F*)entry2->GetObject();
78
79   AliCDBEntry *entry3 = AliCDBManager::Instance()->Get("VZERO/Calib/TimeSlewing");
80   if (!entry3) AliFatal("VZERO time slewing function is not found in OCDB !");
81   fTimeSlewing = (TF1*)entry3->GetObject();
82
83   AliCDBEntry *entry4 = AliCDBManager::Instance()->Get("GRP/Calib/LHCClockPhase");
84   if (!entry4) AliFatal("LHC clock-phase shift is not found in OCDB !");
85   AliLHCClockPhase *phase = (AliLHCClockPhase*)entry4->GetObject();
86
87   for(Int_t i = 0 ; i < 64; ++i) {
88     Int_t board = AliVZEROCalibData::GetBoardNumber(i);
89     fTimeOffset[i] = (((Float_t)fCalibData->GetTriggerCountOffset(board)-
90                         (Float_t)fCalibData->GetRollOver(board))*25.0+
91                        fCalibData->GetTimeOffset(i)-
92                        l1Delay-
93                        phase->GetMeanPhase()+
94                        delays->GetBinContent(i+1)+
95                        kV0Offset);
96   }
97 }
98
99
100 //_____________________________________________________________________________
101 AliVZEROReconstructor& AliVZEROReconstructor::operator = 
102   (const AliVZEROReconstructor& /*reconstructor*/)
103 {
104 // assignment operator
105
106   Fatal("operator =", "assignment operator not implemented");
107   return *this;
108 }
109
110 //_____________________________________________________________________________
111 AliVZEROReconstructor::~AliVZEROReconstructor()
112 {
113 // destructor
114
115   if(fESDVZERO)
116     delete fESDVZERO;
117   if(fESDVZEROfriend)
118    delete fESDVZEROfriend;
119   if(fDigitsArray)
120    delete fDigitsArray;
121 }
122
123 //_____________________________________________________________________________
124 void AliVZEROReconstructor::Init()
125 {
126 // initializer
127
128   fESDVZERO  = new AliESDVZERO;
129   fESDVZEROfriend = new AliESDVZEROfriend;
130   
131   GetCollisionMode();  // fCollisionMode =1 for Pb-Pb simulated data
132 }
133
134 //______________________________________________________________________
135 void AliVZEROReconstructor::ConvertDigits(AliRawReader* rawReader, TTree* digitsTree) const
136 {
137 // converts RAW to digits 
138
139   if (!digitsTree) {
140     AliError("No digits tree!");
141     return;
142   }
143
144   if (!fDigitsArray)
145     fDigitsArray = new TClonesArray("AliVZEROdigit", 64);
146   digitsTree->Branch("VZERODigit", &fDigitsArray);
147
148   fESDVZEROfriend->Reset();
149
150   rawReader->Reset();
151   AliVZERORawStream rawStream(rawReader);
152   if (rawStream.Next()) { 
153
154     Int_t aBBflagsV0A = 0;
155     Int_t aBBflagsV0C = 0;
156     Int_t aBGflagsV0A = 0;
157     Int_t aBGflagsV0C = 0;
158
159     for(Int_t iChannel=0; iChannel < 64; ++iChannel) {
160       Int_t offlineCh = rawStream.GetOfflineChannel(iChannel);
161       // ADC charge samples
162       Short_t chargeADC[AliVZEROdigit::kNClocks];
163       for(Int_t iClock=0; iClock < AliVZEROdigit::kNClocks; ++iClock) {
164         chargeADC[iClock] = rawStream.GetPedestal(iChannel,iClock);
165       }
166       // Integrator flag
167       Bool_t integrator = rawStream.GetIntegratorFlag(iChannel,AliVZEROdigit::kNClocks/2);
168       // Beam-beam and beam-gas flags
169       if(offlineCh<32) {
170         if (rawStream.GetBBFlag(iChannel,AliVZEROdigit::kNClocks/2)) aBBflagsV0C |= (1 << offlineCh);
171         if (rawStream.GetBGFlag(iChannel,AliVZEROdigit::kNClocks/2)) aBGflagsV0C |= (1 << offlineCh);
172       } else {
173         if (rawStream.GetBBFlag(iChannel,AliVZEROdigit::kNClocks/2)) aBBflagsV0A |= (1 << (offlineCh-32));
174         if (rawStream.GetBGFlag(iChannel,AliVZEROdigit::kNClocks/2)) aBGflagsV0A |= (1 << (offlineCh-32));
175       }
176       // HPTDC data (leading time and width)
177       Int_t board = AliVZEROCalibData::GetBoardNumber(offlineCh);
178       Float_t time = rawStream.GetTime(iChannel)*fCalibData->GetTimeResolution(board);
179       Float_t width = rawStream.GetWidth(iChannel)*fCalibData->GetWidthResolution(board);
180       // Add a digit
181       if(!fCalibData->IsChannelDead(iChannel)){
182           new ((*fDigitsArray)[fDigitsArray->GetEntriesFast()])
183             AliVZEROdigit(offlineCh, time,
184                           width,integrator,
185                           chargeADC);
186       }
187
188       // Filling the part of esd friend object that is available only for raw data
189       fESDVZEROfriend->SetBBScalers(offlineCh,rawStream.GetBBScalers(iChannel));
190       fESDVZEROfriend->SetBGScalers(offlineCh,rawStream.GetBGScalers(iChannel));
191       for (Int_t iBunch = 0; iBunch < AliESDVZEROfriend::kNBunches; iBunch++) {
192         fESDVZEROfriend->SetChargeMB(offlineCh,iBunch,rawStream.GetChargeMB(iChannel,iBunch));
193         fESDVZEROfriend->SetIntMBFlag(offlineCh,iBunch,rawStream.GetIntMBFlag(iChannel,iBunch));
194         fESDVZEROfriend->SetBBMBFlag(offlineCh,iBunch,rawStream.GetBBMBFlag(iChannel,iBunch));
195         fESDVZEROfriend->SetBGMBFlag(offlineCh,iBunch,rawStream.GetBGMBFlag(iChannel,iBunch));
196       }
197       for (Int_t iEv = 0; iEv < AliESDVZEROfriend::kNEvOfInt; iEv++) {
198           fESDVZEROfriend->SetBBFlag(offlineCh,iEv,rawStream.GetBBFlag(iChannel,iEv));
199           fESDVZEROfriend->SetBGFlag(offlineCh,iEv,rawStream.GetBGFlag(iChannel,iEv));
200       }
201     }  
202
203     // Filling the global part of esd friend object that is available only for raw data
204     fESDVZEROfriend->SetTriggerInputs(rawStream.GetTriggerInputs());
205     fESDVZEROfriend->SetTriggerInputsMask(rawStream.GetTriggerInputsMask());
206
207     for(Int_t iScaler = 0; iScaler < AliESDVZEROfriend::kNScalers; iScaler++)
208       fESDVZEROfriend->SetTriggerScalers(iScaler,rawStream.GetTriggerScalers(iScaler));
209
210     for(Int_t iBunch = 0; iBunch < AliESDVZEROfriend::kNBunches; iBunch++)
211       fESDVZEROfriend->SetBunchNumbersMB(iBunch,rawStream.GetBunchNumbersMB(iBunch));
212      
213     // Store the BB and BG flags in the digits tree (user info)
214     digitsTree->GetUserInfo()->Add(new TParameter<int>("BBflagsV0A",aBBflagsV0A));
215     digitsTree->GetUserInfo()->Add(new TParameter<int>("BBflagsV0C",aBBflagsV0C));
216     digitsTree->GetUserInfo()->Add(new TParameter<int>("BGflagsV0A",aBGflagsV0A));
217     digitsTree->GetUserInfo()->Add(new TParameter<int>("BGflagsV0C",aBGflagsV0C));
218
219     digitsTree->Fill();
220   }
221
222   fDigitsArray->Clear();
223 }      
224
225 //______________________________________________________________________
226 void AliVZEROReconstructor::FillESD(TTree* digitsTree, TTree* /*clustersTree*/,
227                                     AliESDEvent* esd) const
228 {
229 // fills multiplicities to the ESD - pedestal is now subtracted
230     
231   if (!digitsTree) {
232       AliError("No digits tree!");
233       return;
234   }
235
236   TBranch* digitBranch = digitsTree->GetBranch("VZERODigit");
237   digitBranch->SetAddress(&fDigitsArray);
238
239   Float_t   mult[64];  
240   Float_t    adc[64]; 
241   Float_t   time[64]; 
242   Float_t  width[64];
243   Bool_t aBBflag[64];
244   Bool_t aBGflag[64];
245    
246   for (Int_t i=0; i<64; i++){
247        adc[i]    = 0.0;
248        mult[i]   = 0.0;
249        time[i]   = kInvalidTime;
250        width[i]  = 0.0;
251        aBBflag[i] = kFALSE;
252        aBGflag[i] = kFALSE;
253   }
254      
255   Int_t aBBflagsV0A = 0;
256   Int_t aBBflagsV0C = 0;
257   Int_t aBGflagsV0A = 0;
258   Int_t aBGflagsV0C = 0;
259
260   if (digitsTree->GetUserInfo()->FindObject("BBflagsV0A")) {
261     aBBflagsV0A = ((TParameter<int>*)digitsTree->GetUserInfo()->FindObject("BBflagsV0A"))->GetVal();
262   }
263   else
264     AliWarning("V0A beam-beam flags not found in digits tree UserInfo! The flags will not be written to the raw-data stream!");
265
266   if (digitsTree->GetUserInfo()->FindObject("BBflagsV0C")) {
267     aBBflagsV0C = ((TParameter<int>*)digitsTree->GetUserInfo()->FindObject("BBflagsV0C"))->GetVal();
268   }
269   else
270     AliWarning("V0C beam-beam flags not found in digits tree UserInfo! The flags will not be written to the raw-data stream!");
271
272   if (digitsTree->GetUserInfo()->FindObject("BGflagsV0A")) {
273     aBGflagsV0A = ((TParameter<int>*)digitsTree->GetUserInfo()->FindObject("BGflagsV0A"))->GetVal();
274   }
275   else
276     AliWarning("V0A beam-gas flags not found in digits tree UserInfo! The flags will not be written to the raw-data stream!");
277
278   if (digitsTree->GetUserInfo()->FindObject("BGflagsV0C")) {
279     aBGflagsV0C = ((TParameter<int>*)digitsTree->GetUserInfo()->FindObject("BGflagsV0C"))->GetVal();
280   }
281   else
282     AliWarning("V0C beam-gas flags not found in digits tree UserInfo! The flags will not be written to the raw-data stream!");
283   
284   // Beam-beam and beam-gas flags (hardware)
285   for (Int_t iChannel = 0; iChannel < 64; ++iChannel) {
286     if(iChannel < 32) {
287       aBBflag[iChannel] = (aBBflagsV0C >> iChannel) & 0x1;
288       aBGflag[iChannel] = (aBGflagsV0C >> iChannel) & 0x1;
289     }
290     else {
291       aBBflag[iChannel] = (aBBflagsV0A >> (iChannel-32)) & 0x1;
292       aBGflag[iChannel] = (aBGflagsV0A >> (iChannel-32)) & 0x1;
293     }
294   }
295
296   Int_t nEntries = (Int_t)digitsTree->GetEntries();
297   for (Int_t e=0; e<nEntries; e++) {
298     digitsTree->GetEvent(e);
299
300     Int_t nDigits = fDigitsArray->GetEntriesFast();
301     
302     for (Int_t d=0; d<nDigits; d++) {    
303         AliVZEROdigit* digit = (AliVZEROdigit*) fDigitsArray->At(d);      
304         Int_t  pmNumber = digit->PMNumber();
305
306         // Pedestal retrieval and suppression
307         Bool_t integrator = digit->Integrator();
308         Float_t maxadc = 0;
309         Int_t imax = -1;
310         Float_t adcPedSub[AliVZEROdigit::kNClocks];
311         for(Int_t iClock=0; iClock < AliVZEROdigit::kNClocks; ++iClock) {
312           Short_t charge = digit->ChargeADC(iClock);
313           Bool_t iIntegrator = (iClock%2 == 0) ? integrator : !integrator;
314           Int_t k = pmNumber + 64*iIntegrator;
315           adcPedSub[iClock] = (Float_t)charge - fCalibData->GetPedestal(k);
316           if(adcPedSub[iClock] <= GetRecoParam()->GetNSigmaPed()*fCalibData->GetSigma(k)) {
317             adcPedSub[iClock] = 0;
318             continue;
319           }
320           if(iClock < GetRecoParam()->GetStartClock() || iClock > GetRecoParam()->GetEndClock()) continue;
321           if(adcPedSub[iClock] > maxadc) {
322             maxadc = adcPedSub[iClock];
323             imax   = iClock;
324           }
325         }
326
327         if (imax != -1) {
328           Int_t start = imax - GetRecoParam()->GetNPreClocks();
329           if (start < 0) start = 0;
330           Int_t end = imax + GetRecoParam()->GetNPostClocks();
331           if (end > 20) end = 20;
332           for(Int_t iClock = start; iClock <= end; iClock++) {
333             adc[pmNumber] += adcPedSub[iClock];
334           }
335         }
336
337         // HPTDC leading time and width
338         // Correction for slewing and various time delays
339         time[pmNumber]  =  CorrectLeadingTime(pmNumber,digit->Time(),adc[pmNumber]);
340         width[pmNumber] =  digit->Width();
341
342         if (adc[pmNumber] > 0) {
343           AliDebug(1,Form("PM = %d ADC = %f TDC %f (%f)   Int %d (%d %d %d %d %d)    %f %f   %f %f    %d %d",pmNumber, adc[pmNumber],
344                           digit->Time(),time[pmNumber],
345                           integrator,
346                           digit->ChargeADC(8),digit->ChargeADC(9),digit->ChargeADC(10),
347                           digit->ChargeADC(11),digit->ChargeADC(12),
348                           fCalibData->GetPedestal(pmNumber),fCalibData->GetSigma(pmNumber),
349                           fCalibData->GetPedestal(pmNumber+64),fCalibData->GetSigma(pmNumber+64),
350                           aBBflag[pmNumber],aBGflag[pmNumber]));
351             };
352
353         mult[pmNumber] = adc[pmNumber]*fCalibData->GetMIPperADC(pmNumber);
354
355         // Fill ESD friend object
356         for (Int_t iEv = 0; iEv < AliESDVZEROfriend::kNEvOfInt; iEv++) {
357           fESDVZEROfriend->SetPedestal(pmNumber,iEv,(Float_t)digit->ChargeADC(iEv));
358           fESDVZEROfriend->SetIntegratorFlag(pmNumber,iEv,(iEv%2 == 0) ? integrator : !integrator);
359         }
360         fESDVZEROfriend->SetTime(pmNumber,digit->Time());
361         fESDVZEROfriend->SetWidth(pmNumber,digit->Width());
362
363     } // end of loop over digits
364   } // end of loop over events in digits tree
365          
366   fESDVZERO->SetBit(AliESDVZERO::kCorrectedLeadingTime,kTRUE);
367   fESDVZERO->SetMultiplicity(mult);
368   fESDVZERO->SetADC(adc);
369   fESDVZERO->SetTime(time);
370   fESDVZERO->SetWidth(width);
371   fESDVZERO->SetBit(AliESDVZERO::kOnlineBitsFilled,kTRUE);
372   fESDVZERO->SetBBFlag(aBBflag);
373   fESDVZERO->SetBGFlag(aBGflag);
374
375   // now fill the V0 decision and channel flags
376   {
377     AliVZEROTriggerMask triggerMask;
378     triggerMask.FillMasks(fESDVZERO, fCalibData, fTimeSlewing);
379   }
380
381   if (esd) { 
382      AliDebug(1, Form("Writing VZERO data to ESD tree"));
383      esd->SetVZEROData(fESDVZERO);
384   }
385
386   if (esd) {
387      AliESDfriend *fr = (AliESDfriend*)esd->FindListObject("AliESDfriend");
388      if (fr) {
389         AliDebug(1, Form("Writing VZERO friend data to ESD tree"));
390         fr->SetVZEROfriend(fESDVZEROfriend);
391     }
392   }
393
394   fDigitsArray->Clear();
395 }
396
397 //_____________________________________________________________________________
398 AliCDBStorage* AliVZEROReconstructor::SetStorage(const char *uri) 
399 {
400 // Sets the storage  
401
402   Bool_t deleteManager = kFALSE;
403   
404   AliCDBManager *manager = AliCDBManager::Instance();
405   AliCDBStorage *defstorage = manager->GetDefaultStorage();
406   
407   if(!defstorage || !(defstorage->Contains("VZERO"))){ 
408      AliWarning("No default storage set or default storage doesn't contain VZERO!");
409      manager->SetDefaultStorage(uri);
410      deleteManager = kTRUE;
411   }
412  
413   AliCDBStorage *storage = manager->GetDefaultStorage();
414
415   if(deleteManager){
416      AliCDBManager::Instance()->UnsetDefaultStorage();
417      defstorage = 0;   // the storage is killed by AliCDBManager::Instance()->Destroy()
418   }
419
420   return storage; 
421 }
422
423 //____________________________________________________________________________
424 void AliVZEROReconstructor::GetCollisionMode()
425 {
426   // Retrieval of collision mode 
427
428   TString beamType = GetRunInfo()->GetBeamType();
429   if(beamType==AliGRPObject::GetInvalidString()){
430      AliError("VZERO cannot retrieve beam type");
431      return;
432   }
433
434   if( (beamType.CompareTo("P-P") ==0)  || (beamType.CompareTo("p-p") ==0) ){
435     fCollisionMode=0;
436   }
437   else if( (beamType.CompareTo("Pb-Pb") ==0)  || (beamType.CompareTo("A-A") ==0) ){
438     fCollisionMode=1;
439   }
440     
441   fBeamEnergy = GetRunInfo()->GetBeamEnergy();
442   if(fBeamEnergy==AliGRPObject::GetInvalidFloat()) {
443      AliError("Missing value for the beam energy ! Using 0");
444      fBeamEnergy = 0.;
445   }
446   
447   AliDebug(1,Form("\n ++++++ Beam type and collision mode retrieved as %s %d @ %1.3f GeV ++++++\n\n",beamType.Data(), fCollisionMode, fBeamEnergy));
448
449 }
450
451 //_____________________________________________________________________________
452 AliVZEROCalibData* AliVZEROReconstructor::GetCalibData() const
453 {
454   // Gets calibration object for VZERO set
455
456   AliCDBManager *man = AliCDBManager::Instance();
457
458   AliCDBEntry *entry=0;
459
460   entry = man->Get("VZERO/Calib/Data");
461
462   AliVZEROCalibData *calibdata = 0;
463
464   if (entry) calibdata = (AliVZEROCalibData*) entry->GetObject();
465   if (!calibdata)  AliFatal("No calibration data from calibration database !");
466
467   return calibdata;
468 }
469
470 Float_t AliVZEROReconstructor::CorrectLeadingTime(Int_t i, Float_t time, Float_t adc) const
471 {
472   // Correct the leading time
473   // for slewing effect and
474   // misalignment of the channels
475   if (time < 1e-6) return kInvalidTime;
476
477   // Channel alignment and general offset subtraction
478   if (i < 32) time -= kV0CDelayCables;
479   time -= fTimeOffset[i];
480
481   // In case of pathological signals
482   if (adc < 1e-6) return time;
483
484   // Slewing correction
485   Float_t thr = fCalibData->GetCalibDiscriThr(i,kTRUE);
486   time -= fTimeSlewing->Eval(adc/thr);
487
488   return time;
489 }