CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TPedValues.cc
Go to the documentation of this file.
3 
4 #include <math.h>
5 #include <iostream>
6 #include <cassert>
7 #include "TGraphErrors.h"
8 #include "TAxis.h"
9 #include "TF1.h"
10 
11 void reset (double vett[256])
12 {
13  for (int i=0 ; i<256; ++i) vett[i] = 0. ;
14 }
15 
16 
17 TPedValues::TPedValues (double RMSmax, int bestPedestal) :
18  m_bestPedestal (bestPedestal) ,
19  m_RMSmax (RMSmax)
20 {
21  LogDebug ("EcalPedOffset") << "entering TPedValues ctor ..." ;
22  for(int i=0; i<1700;++i)
24 }
25 
26 
28 {
29  LogDebug ("EcalPedOffset") << "entering TPedValues copyctor ..." ;
31  m_RMSmax = orig.m_RMSmax ;
32 
33  for (int gain = 0 ; gain < 3 ; ++gain)
34  for (int crystal = 0 ; crystal < 1700 ; ++crystal)
35  for (int DAC = 0 ; DAC < 256 ; ++DAC)
36  m_entries[gain][crystal][DAC] = orig.m_entries[gain][crystal][DAC] ;
37 
38  for(int i=0; i<1700;++i)
40 }
41 
42 
43 
45 
46 
47 void TPedValues::insert (const int gainId,
48  const int crystal,
49  const int DAC,
50  const int pedestal,
51  const int endcapIndex)
52 {
53 // assert (gainId > 0) ;
54 // assert (gainId < 4) ;
55  if (gainId <= 0 || gainId >= 4)
56  {
57  edm::LogWarning ("EcalPedOffset") << "WARNING : TPedValues : gainId " << gainId
58  << " does not exist, entry skipped" ;
59  return ;
60  }
61 // assert (crystal > 0) ;
62 // assert (crystal <= 1700) ;
63  if (crystal <= 0 || crystal > 1700)
64  {
65  edm::LogWarning ("EcalPedOffset") << "WARNING : TPedValues : crystal " << crystal
66  << " does not exist, entry skipped" ;
67  return ;
68  }
69 // assert (DAC >= 0) ;
70 // assert (DAC < 256) ;
71  if (DAC < 0 || DAC >= 256)
72  {
73  edm::LogWarning ("EcalPedOffset") << "WARNING : TPedValues : DAC value " << DAC
74  << " is out range, entry skipped" ;
75  return ;
76  }
77  m_entries[gainId-1][crystal-1][DAC].insert (pedestal) ;
78  endcapCrystalNumbers[crystal-1] = endcapIndex;
79  return ;
80 }
81 
82 
83 TPedResult TPedValues::terminate (const int & DACstart, const int & DACend) const
84 {
85  assert (DACstart >= 0) ;
86  assert (DACend <= 256) ;
87 // checkEntries (DACstart, DACend) ;
88 
89  TPedResult bestDAC ;
91  for (int gainId = 1 ; gainId < 4 ; ++gainId)
92  {
94  for (int crystal = 0 ; crystal < 1700 ; ++crystal)
95  {
97  double delta = 1000 ;
98  int dummyBestDAC = -1 ;
99  bool hasDigis = false;
101  for (int DAC = DACstart ; DAC < DACend ; ++DAC)
102  {
103  double average = m_entries[gainId-1][crystal][DAC].average () ;
104  if (average == -1) continue ;
105  hasDigis = true;
106  if (m_entries[gainId-1][crystal][DAC].RMSSq () > m_RMSmax * m_RMSmax) continue ;
107  if (fabs (average - m_bestPedestal) < delta && average>1 )
108  {
109  delta = fabs (average - m_bestPedestal) ;
110  dummyBestDAC = DAC ;
111  }
112  }
113 
114  bestDAC.m_DACvalue[gainId-1][crystal] = dummyBestDAC ;
115 
116  if ((dummyBestDAC == (DACend-1) || dummyBestDAC == -1) && hasDigis)
117  {
118  int gainHuman;
119  if (gainId ==1) gainHuman =12;
120  else if (gainId ==2) gainHuman =6;
121  else if (gainId ==3) gainHuman =1;
122  else gainHuman =-1;
123  edm::LogError("EcalPedOffset")
124  << " TPedValues : cannot find best DAC value for channel: "
125  << endcapCrystalNumbers[crystal]
126  << " gain: " << gainHuman;
127  }
128 
129  } // loop over crystals
130  } // loop over gains
131  return bestDAC ;
132 }
133 
134 
135 int TPedValues::checkEntries (const int & DACstart, const int & DACend) const
136 {
137  assert (DACstart >= 0) ;
138  assert (DACend <= 256) ;
139  int returnCode = 0 ;
141  for (int gainId = 1 ; gainId < 4 ; ++gainId)
142  {
144  for (int crystal = 0 ; crystal < 1700 ; ++crystal)
145  {
147  for (int DAC = DACstart ; DAC < DACend ; ++DAC)
148  {
149  double average = m_entries[gainId-1][crystal][DAC].average () ;
150  if (average == -1)
151  {
152  ++returnCode ;
154 /*
155  std::cerr << "[TPedValues][checkEntries] WARNING!"
156  << "\tgainId " << gainId
157  << "\tcrystal " << crystal+1
158  << "\tDAC " << DAC
159  << " : pedestal measurement missing"
160  << std::endl ;
161 */
162  }
163 /*
164  std::cout << "[pietro][RMS]: " << m_entries[gainId-1][crystal][DAC].RMS () //FIXME
165  << "\t" << m_entries[gainId-1][crystal][DAC].RMSSq () //FIXME
166  << "\t" << DAC //FIXME
167  << "\t" << gainId //FIXME
168  << "\t" << crystal << std::endl ; //FIXME
169 */
170  }
171  } // loop over crystals
172  } // loop over gains
173  return returnCode ;
174 }
175 
176 
178 int TPedValues::makePlots (TFile * rootFile, const std::string & dirName,
179  const double maxSlope, const double minSlope, const double maxChi2OverNDF) const
180 {
181  using namespace std;
182  // prepare the ROOT file
183  if (!rootFile->cd (dirName.c_str ()))
184  {
185  rootFile->mkdir (dirName.c_str ()) ;
186  rootFile->cd (dirName.c_str ()) ;
187  }
188 
189  // loop over the crystals
190  for (int xtl=0 ; xtl<1700 ; ++xtl)
191  {
192  // loop over the gains
193  for (int gain=0 ; gain<3 ; ++gain)
194  {
195  vector<double> asseX;
196  vector<double> sigmaX;
197  vector<double> asseY;
198  vector<double> sigmaY;
199  asseX.reserve(256);
200  sigmaX.reserve(256);
201  asseY.reserve(256);
202  sigmaY.reserve(256);
203  // loop over DAC values
204  for (int dac=0 ; dac<256 ; ++dac)
205  {
206  double average = m_entries[gain][xtl][dac].average();
207  if(average > -1)
208  {
209  double rms = m_entries[gain][xtl][dac].RMS();
210  asseX.push_back(dac);
211  sigmaX.push_back(0);
212  asseY.push_back(average);
213  sigmaY.push_back(rms);
214  }
215  } // loop over DAC values
216  if(asseX.size() > 0)
217  {
218  int lastBin = 0;
219  while(lastBin<(int)asseX.size()-1 && asseY[lastBin+1]>0
220  && (asseY[lastBin+1]-asseY[lastBin+2])!=0)
221  lastBin++;
222 
223  int fitRangeEnd = (int)asseX[lastBin];
224  int kinkPt = 64;
225  if(fitRangeEnd < 66)
226  kinkPt = fitRangeEnd-4;
227  TGraphErrors graph(asseX.size(),&(*asseX.begin()),&(*asseY.begin()),
228  &(*sigmaX.begin()),&(*sigmaY.begin()));
229  char funct[120];
230  sprintf(funct,"(x<%d)*([0]*x+[1])+(x>=%d)*([2]*x+[3])",kinkPt,kinkPt);
231  TF1 fitFunction("fitFunction",funct,asseX[0],fitRangeEnd);
232  fitFunction.SetLineColor(2);
233 
234  char name[120] ;
235  int gainHuman;
236  if (gain ==0) gainHuman =12;
237  else if (gain ==1) gainHuman =6;
238  else if (gain ==2) gainHuman =1;
239  else gainHuman =-1;
240  sprintf (name,"XTL%04d_GAIN%02d",endcapCrystalNumbers[xtl],gainHuman) ;
241  graph.GetXaxis()->SetTitle("DAC value");
242  graph.GetYaxis()->SetTitle("Average pedestal ADC");
243  graph.Fit(&fitFunction,"RWQ");
244  graph.Write (name);
245 
246  double slope1 = fitFunction.GetParameter(0);
247  double slope2 = fitFunction.GetParameter(2);
248 
249  if(fitFunction.GetChisquare()/fitFunction.GetNDF()>maxChi2OverNDF ||
250  fitFunction.GetChisquare()/fitFunction.GetNDF()<0 ||
251  slope1>0 || slope2>0 ||
252  ((slope1<-29 || slope1>-18) && slope1<0) ||
253  ((slope2<-29 || slope2>-18) && slope2<0))
254  {
255  edm::LogError("EcalPedOffset") << "TPedValues : TGraph for channel:" <<
256  endcapCrystalNumbers[xtl] <<
257  " gain:" << gainHuman << " is not linear;" << " slope of line1:" <<
258  fitFunction.GetParameter(0) << " slope of line2:" <<
259  fitFunction.GetParameter(2) << " reduced chi-squared:" <<
260  fitFunction.GetChisquare()/fitFunction.GetNDF();
261  }
262  //LogDebug("EcalPedOffset") << "TPedValues : TGraph for channel:" << xtl+1 << " gain:"
263  // << gainHuman << " has " << asseX.size() << " points...back is:" << asseX.back()
264  // << " and front+1 is:" << asseX.front()+1;
265  if((asseX.back()-asseX.front()+1)!=asseX.size())
266  edm::LogError("EcalPedOffset") << "TPedValues : Pedestal average not found " <<
267  "for all DAC values scanned in channel:" << endcapCrystalNumbers[xtl]
268  << " gain:" << gainHuman;
269  }
270  } // loop over the gains
271  } // (loop over the crystals)
272 
273  return 0 ;
274 }
275 
276 // Look up the crystal number in the EE schema and return it
277 int TPedValues::getCrystalNumber(int xtal) const
278 {
279  return endcapCrystalNumbers[xtal];
280 }
#define LogDebug(id)
dbl * delta
Definition: mlp_gen.cc:36
int i
Definition: DBlmapReader.cc:9
tuple maxChi2OverNDF
Definition: MergeJob_cfg.py:56
TSinglePedEntry m_entries[3][1700][256]
Definition: TPedValues.h:54
int gainId(sample_type sample)
get the gainId (2 bits)
assert(m_qm.get())
int makePlots(TFile *rootFile, const std::string &dirName, const double maxSlopeAllowed, const double minSlopeAllowed, const double maxChi2OverNDF) const
create a plot of the DAC pedestal trend
Definition: TPedValues.cc:178
Transient container Store all the pedestal values depending on the gain and pedestal offset $Date: $R...
int m_DACvalue[3][1700]
Definition: TPedResult.h:28
TPedValues(double RMSmax=2, int bestPedestal=200)
ctor
Definition: TPedValues.cc:17
double RMS() const
get the RMS of the inserted values
int m_bestPedestal
Definition: TPedValues.h:56
double average() const
get the average of the inserted values
TPedResult terminate(const int &DACstart=0, const int &DACend=256) const
calculate the offset values for all the containers
Definition: TPedValues.cc:83
int getCrystalNumber(int xtal) const
return the index from the table
Definition: TPedValues.cc:277
Definition: adjgraph.h:12
int checkEntries(const int &DACstart=0, const int &DACend=256) const
check whether the scan was complete in the range
Definition: TPedValues.cc:135
double m_RMSmax
Definition: TPedValues.h:57
int average
Definition: PDRates.py:137
return(e1-e2)*(e1-e2)+dp *dp
int endcapCrystalNumbers[1700]
Definition: TPedValues.h:59
void insert(const int gainId, const int crystal, const int DAC, const int pedestal, const int endcapIndex)
add a single value
Definition: TPedValues.cc:47
~TPedValues()
dtor
Definition: TPedValues.cc:44
void reset(double vett[256])
Definition: TPedValues.cc:11
void insert(const int &pedestal)
add a single value