]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMDGainDA.cxx
ACORDEv1 is now the default
[u/mrichter/AliRoot.git] / FMD / AliFMDGainDA.cxx
1 /**************************************************************************
2  * Copyright(c) 2004, 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 /** @file    AliFMDGainDA.cxx
17     @author  Hans Hjersing Dalsgaard <canute@nbi.dk>
18     @date    Mon Mar 13 13:46:05 2008
19     @brief   Derived class for the pulse gain detector algorithm.
20 */
21 //____________________________________________________________________
22 //
23 // This class contains the implementation of the gain detector
24 // algorithms (DA) for the FMD.  The data is collected in histograms
25 // that are reset for each pulse length after the mean and standard
26 // deviation are put into a TGraphErrors object. After a certain
27 // number of pulses (usually 8) the graph is fitted to a straight
28 // line. The gain is then slope of this line as it combines the known
29 // pulse and the response of the detector.
30 //
31 #include "AliFMDGainDA.h"
32 #include "iostream"
33 #include "fstream"
34 #include "AliLog.h"
35 #include "TF1.h"
36 #include "TH1.h"
37 #include "TMath.h"
38 #include "TGraphErrors.h"
39 #include "AliFMDParameters.h"
40
41 //_____________________________________________________________________
42 ClassImp(AliFMDGainDA)
43 #if 0 // Do not delete - here to let Emacs indent properly
44 ;
45 #endif
46
47 //_____________________________________________________________________
48 AliFMDGainDA::AliFMDGainDA() 
49   : AliFMDBaseDA(),
50     fGainArray(),
51     fHighPulse(256), 
52     fEventsPerChannel(10),
53     fCurrentPulse(10),
54     fCurrentChannel(10),
55     fNumberOfStripsPerChip(128),
56     fSummaryGains("GainsSummary","Summary of gains",51200,0,51200),
57     fCurrentSummaryStrip(1)
58 {
59   fCurrentPulse.Reset(0);
60   fCurrentChannel.Reset(0);
61   fOutputFile.open("gains.csv");
62   fGainArray.SetOwner(); 
63 }
64
65 //_____________________________________________________________________
66 AliFMDGainDA::AliFMDGainDA(const AliFMDGainDA & gainDA) 
67   :  AliFMDBaseDA(gainDA),
68      fGainArray(gainDA.fGainArray),
69      fHighPulse(gainDA.fHighPulse),
70      fEventsPerChannel(gainDA.fEventsPerChannel),
71      fCurrentPulse(gainDA.fCurrentPulse),
72      fCurrentChannel(gainDA.fCurrentChannel),
73      fNumberOfStripsPerChip(gainDA.fNumberOfStripsPerChip),
74      fSummaryGains(gainDA.fSummaryGains),
75      fCurrentSummaryStrip(gainDA.fCurrentSummaryStrip)
76 {  
77   fCurrentPulse.Reset(0);
78   fCurrentChannel.Reset(0);
79 }
80
81 //_____________________________________________________________________
82 AliFMDGainDA::~AliFMDGainDA() 
83 {
84 }
85
86 //_____________________________________________________________________
87 void AliFMDGainDA::Init() 
88 {
89   
90  
91   Int_t nEventsRequired = 0;
92   
93   //for(UShort_t det=1; det<=3;det++) {
94   //  UShort_t firstring = (det == 1 ? 1 : 0);
95   //  for(UShort_t iring = firstring; iring <=1;iring++) {
96   //    Char_t ring = (iring == 1 ? 'I' : 'O');
97   //    for(UShort_t board =0 ; board <=1; board++) {
98   //    Int_t idx = GetHalfringIndex(det,ring,board);
99   for(Int_t idx = 0;idx<fEventsPerChannel.GetSize();idx++)
100     {
101       
102       Int_t nEvents = 0;
103       if(fPulseSize.At(idx))
104         nEvents = (fPulseLength.At(idx)*fHighPulse) / fPulseSize.At(idx);
105       fEventsPerChannel.AddAt(nEvents,idx);
106       if(nEvents>nEventsRequired) nEventsRequired = nEvents * fNumberOfStripsPerChip;
107       
108     }
109   //}
110   // }
111   
112   //8 pulser values * 128 strips * 100 samples
113   
114   
115   SetRequiredEvents(nEventsRequired); 
116   
117   TObjArray* detArray;
118   TObjArray* ringArray;
119   TObjArray* sectorArray;
120   
121   for(UShort_t det=1;det<=3;det++) {
122     detArray = new TObjArray();
123     detArray->SetOwner();
124     fGainArray.AddAtAndExpand(detArray,det);
125     for (UShort_t ir = 0; ir < 2; ir++) {
126       Char_t   ring = (ir == 0 ? 'O' : 'I');
127       UShort_t nsec = (ir == 0 ? 40  : 20);
128       UShort_t nstr = (ir == 0 ? 2 : 4);
129       ringArray = new TObjArray();
130       ringArray->SetOwner();
131       detArray->AddAtAndExpand(ringArray,ir);
132       for(UShort_t sec =0; sec < nsec;  sec++)  {
133         sectorArray = new TObjArray();
134         sectorArray->SetOwner();
135         ringArray->AddAtAndExpand(sectorArray,sec);
136         for(UShort_t strip = 0; strip < nstr; strip++) {
137           TH1S* hChannel = new TH1S(Form("hFMD%d%c_%d_%d",det,ring,sec,strip),
138                                     Form("hFMD%d%c_%d_%d",det,ring,sec,strip),
139                                     1024,0,1023);
140           hChannel->SetDirectory(0);
141           sectorArray->AddAtAndExpand(hChannel,strip);
142         }
143       }
144     }
145   }
146 }
147
148 //_____________________________________________________________________
149 void AliFMDGainDA::AddChannelContainer(TObjArray* sectorArray, 
150                                        UShort_t det  , 
151                                        Char_t   ring,  
152                                        UShort_t sec, 
153                                        UShort_t strip) 
154 {  
155   TGraphErrors* hChannel  = new TGraphErrors();
156   hChannel->SetName(Form("FMD%d%c[%02d,%03d]", det, ring, sec, strip));
157   hChannel->SetTitle(Form("FMD%d%c[%02d,%03d] ADC vs DAC", 
158                           det, ring, sec, strip));
159   sectorArray->AddAtAndExpand(hChannel,strip);
160 }
161
162 //_____________________________________________________________________
163 void AliFMDGainDA::FillChannels(AliFMDDigit* digit) {
164
165   UShort_t det   = digit->Detector();
166   Char_t   ring  = digit->Ring();
167   UShort_t sec   = digit->Sector();
168   UShort_t strip = digit->Strip();
169   
170   //Strip is always seen as the first in a VA chip. All other strips are junk
171   if(strip % fNumberOfStripsPerChip) return;
172   Int_t vaChip   = strip / fNumberOfStripsPerChip; 
173   TH1S* hChannel = GetChannelHistogram(det, ring, sec, vaChip);
174   
175   hChannel->Fill(digit->Counts());
176   UpdatePulseAndADC(det,ring,sec,strip);
177 }
178
179 //_____________________________________________________________________
180 void AliFMDGainDA::Analyse(UShort_t det, 
181                            Char_t   ring, 
182                            UShort_t sec, 
183                            UShort_t strip) {
184   TGraphErrors* grChannel = GetChannel(det,ring,sec,strip);
185   if(!grChannel->GetN()) {
186     // AliWarning(Form("No entries for FMD%d%c, sector %d, strip %d",
187     //                 det, ring , sec, strip));
188     return;
189   }
190   TF1 fitFunc("fitFunc","pol1",-10,280); 
191   fitFunc.SetParameters(100,3);
192   grChannel->Fit("fitFunc","Q0+","",0,fHighPulse);
193      
194   Float_t chi2ndf = 0;
195   if(fitFunc.GetNDF())
196     chi2ndf = fitFunc.GetChisquare() / fitFunc.GetNDF();
197    
198   fOutputFile << det                         << ','
199               << ring                        << ','
200               << sec                         << ','
201               << strip                       << ','
202               << fitFunc.GetParameter(1)     << ','
203               << fitFunc.GetParError(1)      << ','
204               << chi2ndf                     <<"\n";
205   
206   //due to RCU trouble, first strips on VAs are excluded
207   if(strip%128 != 0) {
208     
209     fSummaryGains.SetBinContent(fCurrentSummaryStrip,fitFunc.GetParameter(1));
210     fSummaryGains.SetBinError(fCurrentSummaryStrip,fitFunc.GetParError(1));
211     
212     fCurrentSummaryStrip++;
213   }
214   if(fSaveHistograms) {
215     gDirectory->cd(GetSectorPath(det,ring, sec, kTRUE));
216     
217     TH1F* summary = dynamic_cast<TH1F*>(gDirectory->Get("Summary"));
218     if (!summary) { 
219       Int_t nStr = (ring == 'I' ? 512 : 256);
220       summary = new TH1F("Summary", Form("Summary of gains in FMD%d%c[%02d]", 
221                                          det, ring, sec), 
222                          nStr, -.5, nStr-.5);
223       summary->SetXTitle("Strip");
224       summary->SetYTitle("Gain [ADC/DAC]");
225       summary->SetDirectory(gDirectory);
226     }
227     summary->SetBinContent(strip+1, fitFunc.GetParameter(1));
228     summary->SetBinError(strip+1, fitFunc.GetParError(1));
229     
230     gDirectory->cd(GetStripPath(det,ring,sec,strip, kTRUE));
231     grChannel->SetName(Form("FMD%d%c[%02d,%03d]",det,ring,sec,strip));
232     // grChannel->SetDirectory(gDirectory);
233     grChannel->Write();
234     // grChannel->Write(Form("grFMD%d%c_%d_%d",det,ring,sec,strip));
235   }  
236 }
237
238 //_____________________________________________________________________
239 void AliFMDGainDA::Terminate(TFile* diagFile)
240 {
241   diagFile->cd();
242   fSummaryGains.Write();
243 }
244
245 //_____________________________________________________________________
246 void AliFMDGainDA::WriteHeaderToFile() 
247 {
248   AliFMDParameters* pars       = AliFMDParameters::Instance();
249   fOutputFile.write(Form("# %s \n",pars->GetGainShuttleID()),9);
250   fOutputFile.write("# Detector, "
251                     "Ring, "
252                     "Sector, "
253                     "Strip, "
254                     "Gain, "
255                     "Error, "
256                     "Chi2/NDF \n",56);
257   
258 }
259
260 //_____________________________________________________________________
261 TH1S* AliFMDGainDA::GetChannelHistogram(UShort_t det, 
262                                         Char_t   ring, 
263                                         UShort_t sec, 
264                                         UShort_t strip) 
265 {
266   
267   UShort_t  Ring = 1;
268   if(ring == 'O')
269     Ring = 0;
270   
271   
272   TObjArray* detArray  = static_cast<TObjArray*>(fGainArray.At(det));
273   TObjArray* ringArray = static_cast<TObjArray*>(detArray->At(Ring));
274   TObjArray* secArray  = static_cast<TObjArray*>(ringArray->At(sec));
275   TH1S* hChannel       = static_cast<TH1S*>(secArray->At(strip));
276   
277   return hChannel;
278 }
279
280 //_____________________________________________________________________
281 TGraphErrors* AliFMDGainDA::GetChannel(UShort_t det, 
282                                        Char_t   ring, 
283                                        UShort_t sec, 
284                                        UShort_t strip) 
285 {  
286   UShort_t      iring     = (ring == 'O' ? 0 : 1);
287   TObjArray*    detArray  = static_cast<TObjArray*>(fDetectorArray.At(det));
288   TObjArray*    ringArray = static_cast<TObjArray*>(detArray->At(iring));
289   TObjArray*    secArray  = static_cast<TObjArray*>(ringArray->At(sec));
290   TGraphErrors* hChannel  = static_cast<TGraphErrors*>(secArray->At(strip));
291   
292   return hChannel;
293 }
294
295 //_____________________________________________________________________
296 void AliFMDGainDA::UpdatePulseAndADC(UShort_t det, 
297                                      Char_t ring, 
298                                      UShort_t sec, 
299                                      UShort_t strip) 
300 {
301   
302   AliFMDParameters* pars = AliFMDParameters::Instance();
303   UInt_t ddl, board,chip,ch;
304   pars->Detector2Hardware(det,ring,sec,strip,ddl,board,chip,ch);
305   Int_t halfring = GetHalfringIndex(det,ring,board/16);
306   
307   if(GetCurrentEvent()> (fNumberOfStripsPerChip*fEventsPerChannel.At(halfring)))
308     return;
309   if(strip % fNumberOfStripsPerChip) return;
310   if(((GetCurrentEvent()) % fPulseLength.At(halfring)) && GetCurrentEvent() > 0) return;
311      
312   Int_t vaChip = strip/fNumberOfStripsPerChip; 
313   TH1S* hChannel = GetChannelHistogram(det,ring,sec,vaChip);
314   
315   if(!hChannel->GetEntries()) {
316     AliWarning(Form("No entries for FMD%d%c, sector %d, strip %d",
317                     det, ring , sec, strip));
318     return;
319   }
320   Double_t mean      = hChannel->GetMean();
321   Double_t rms       = hChannel->GetRMS();
322   Double_t pulse     = Double_t(fCurrentPulse.At(halfring)) * fPulseSize.At(halfring);
323   Int_t    firstBin  = hChannel->GetXaxis()->GetFirst();
324   Int_t    lastBin   = hChannel->GetXaxis()->GetLast();
325   hChannel->GetXaxis()->SetRangeUser(mean-4*rms,mean+4*rms);
326   
327   mean               = hChannel->GetMean();
328   rms                = hChannel->GetRMS();
329   
330   hChannel->GetXaxis()->SetRange(firstBin,lastBin);
331   
332   Int_t channelNumber = strip + (GetCurrentEvent()-1)/((fPulseLength.At(halfring)*fHighPulse)/fPulseSize.At(halfring)); 
333   
334   TGraphErrors* channel = GetChannel(det,ring,sec,channelNumber);
335   
336   channel->SetPoint(fCurrentPulse.At(halfring),pulse,mean);
337   channel->SetPointError(fCurrentPulse.At(halfring),0,rms);
338   
339   if(fSaveHistograms) {
340     gDirectory->cd(GetStripPath(det,ring,sec,channelNumber));
341     hChannel->Write(Form("%s_pulse_%03d",hChannel->GetName(),(Int_t)pulse));
342     
343   }
344     
345   hChannel->Reset();
346   
347 }
348
349 //_____________________________________________________________________
350 void AliFMDGainDA::ResetPulseAndUpdateChannel() 
351 {  
352   fCurrentPulse.Reset(0); 
353 }
354
355 //_____________________________________________________________________
356 void AliFMDGainDA::FinishEvent() 
357 {
358   for(UShort_t det=1; det<=3;det++) {
359     UShort_t firstring = (det == 1 ? 1 : 0);
360     for(UShort_t iring = firstring; iring <=1;iring++) {
361       Char_t ring = (iring == 1 ? 'I' : 'O');
362       for(UShort_t board =0 ; board <=1; board++) {
363         Int_t idx = GetHalfringIndex(det,ring,board);
364         
365         if( !fPulseLength.At(idx) || !fEventsPerChannel.At(idx))
366           continue;
367         if(GetCurrentEvent()>0 && ((GetCurrentEvent() % fPulseLength.At(idx)) == 0))
368           fCurrentPulse.AddAt(fCurrentPulse.At(idx)+1,idx);
369         
370         if(GetCurrentEvent()>0 && ((GetCurrentEvent()) % fEventsPerChannel.At(idx)) == 0)
371           fCurrentPulse.AddAt(0,idx);
372       }
373     }
374   }
375 }
376 //_____________________________________________________________________
377 //
378 //EOF
379 //