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