CMS 3D CMS Logo

SamplingAlgorithm.cc
Go to the documentation of this file.
7 #include "TProfile.h"
8 #include "TF1.h"
9 #include <iostream>
10 #include <sstream>
11 #include <iomanip>
12 #include <cmath>
13 
14 using namespace sistrip;
15 
16 // ----------------------------------------------------------------------------
17 //
18 SamplingAlgorithm::SamplingAlgorithm( const edm::ParameterSet & pset, SamplingAnalysis* const anal, uint32_t latencyCode )
19  : CommissioningAlgorithm(anal),
20  histo_(nullptr,""),
21  deconv_fitter_(nullptr),
22  peak_fitterA_(nullptr),
23  peak_fitterB_(nullptr),
24  latencyCode_(latencyCode),
25  samp_(nullptr)
26 {
27  peak_fitterA_ = new TF1("peak_fitterA",fpeak_convoluted,-4800,0,5);
28  peak_fitterA_->SetNpx(2000);
29  peak_fitterA_->FixParameter(0,0);
30  peak_fitterA_->SetParLimits(1,0,4800);
31  peak_fitterA_->SetParLimits(2,0,20);
32  peak_fitterA_->FixParameter(3,50);
33  peak_fitterA_->SetParLimits(4,0,75);
34  peak_fitterA_->SetParameters(0.,1250,10,50,10);
35 
36  peak_fitterB_ = new TF1("peak_fitterB",fpeak_convoluted,-100,100,5);
37  peak_fitterB_->SetNpx(200);
38  peak_fitterB_->FixParameter(0,0);
39  peak_fitterB_->SetParLimits(1,-100,100);
40  peak_fitterB_->SetParLimits(2,0,20);
41  peak_fitterB_->FixParameter(3,50);
42  peak_fitterB_->SetParLimits(4,0,75);
43  peak_fitterB_->SetParameters(0.,-50,10,50,10);
44 
45  deconv_fitter_ = new TF1("deconv_fitter",fdeconv_convoluted,-50,50,5);
46  deconv_fitter_->SetNpx(1000);
47  deconv_fitter_->FixParameter(0,0);
48  deconv_fitter_->SetParLimits(1,-50,50);
49  deconv_fitter_->SetParLimits(2,0,200);
50  deconv_fitter_->SetParLimits(3,5,100);
51  deconv_fitter_->FixParameter(3,50);
52  deconv_fitter_->SetParLimits(4,0,75);
53  deconv_fitter_->SetParameters(0.,-2.82,0.96,50,20);
54 }
55 
56 // ----------------------------------------------------------------------------
57 //
58 void SamplingAlgorithm::extract( const std::vector<TH1*>& histos) {
59 
60  if ( !anal() ) {
62  << "[SamplingAlgorithm::" << __func__ << "]"
63  << " NULL pointer to Analysis object!";
64  return;
65  }
66 
68  samp_ = dynamic_cast<SamplingAnalysis*>( tmp );
69  if ( !samp_ ) {
71  << "[SamplingAlgorithm::" << __func__ << "]"
72  << " NULL pointer to derived Analysis object!";
73  return;
74  }
75 
76  // Check
77  if ( histos.size() != 1 && histos.size() != 2 ) {
79  }
80 
81  // Extract FED key from histo title
82  if ( !histos.empty() ) { samp_->fedKey( extractFedKey( histos.front() ) ); }
83 
84  // Extract
85  std::vector<TH1*>::const_iterator ihis = histos.begin();
86  for ( ; ihis != histos.end(); ihis++ ) {
87 
88  // Check pointer
89  if ( !(*ihis) ) {
90  edm::LogWarning(mlCommissioning_) << " NULL pointer to histogram!";
91  continue;
92  }
93 
94  // Check name
95  SiStripHistoTitle title( (*ihis)->GetName() );
96  if ( title.runType() != sistrip::APV_LATENCY && title.runType() != sistrip::FINE_DELAY) {
98  continue;
99  }
100  // Set the mode for later fits
101  samp_->runType_ = title.runType();
102 
103  // Set the granularity
104  samp_->granularity_ = title.granularity();
105 
106  // Extract timing histo
107  if ( title.extraInfo().find(sistrip::extrainfo::clusterCharge_) != std::string::npos ) {
108  histo_.first = *ihis;
109  histo_.second = (*ihis)->GetName();
110  }
111 
112  }
113 
114 }
115 
116 // ----------------------------------------------------------------------------
117 //
119 
120  if ( !samp_ ) {
122  << "[SamplingAlgorithm::" << __func__ << "]"
123  << " NULL pointer to derived Analysis object!";
124  return;
125  }
126 
127  TProfile* prof = (TProfile*)(histo_.first);
128  if ( !prof ) {
130  << " NULL pointer to histogram!" ;
131  return;
132  }
133 
134  // set the right error mode: rms
135  prof->SetErrorOption(" ");
136 
137  //that should not be needed, but it seems histos are stored with error option " " and errors "s" in all cases.
138  //it MUST be removed if the DQM (?) bug is solved
139  for(int i=0;i<prof->GetNbinsX();++i) {
140  if(prof->GetBinEntries(i)>0)
141  prof->SetBinError(i,prof->GetBinError(i)/sqrt(prof->GetBinEntries(i)));
142  }
143 
144  // prune the profile
145  pruneProfile(prof);
146 
147  // correct for the binning
148  correctBinning(prof);
149 
150  // correct for clustering effects
152 
153  // fit depending on the mode
155 
156  // initialize the fit (overal latency)
157  float max = prof->GetBinCenter(prof->GetMaximumBin());
158  float ampl = prof->GetMaximum();
159  peak_fitterA_->SetParameters(0.,50-max,ampl/20.,50,10);
160 
161  // fit
162  if(prof->Fit(peak_fitterA_,"Q")==0)
163  prof->Fit(peak_fitterA_,"QEM");
164 
165  // Set monitorables
166  samp_->max_ = peak_fitterA_->GetMaximumX();
167  samp_->error_ = peak_fitterA_->GetParError(1);
168 
169  } else { // sistrip::FINE_DELAY
170 
171  // initialize the fit (overal latency)
172  float max = prof->GetBinCenter(prof->GetMaximumBin());
173  float ampl = prof->GetMaximum();
174  deconv_fitter_->SetParameters(0.,-max,ampl/10.,50,20);
175  peak_fitterB_->SetParameters(0.,50-max,ampl/20.,50,10);
176  if(latencyCode_&0x80) { // deconv mode
177  // fit
178  if(prof->Fit(deconv_fitter_,"Q")==0)
179  prof->Fit(deconv_fitter_,"QEM");
180  // Set monitorables
181  samp_->max_ = deconv_fitter_->GetMaximumX();
182  samp_->error_ = deconv_fitter_->GetParError(1);
183  } else { // peak mode
184  // fit
185  if(prof->Fit(peak_fitterB_,"Q")==0)
186  prof->Fit(peak_fitterB_,"QEM");
187  // Set monitorables
188  samp_->max_ = peak_fitterB_->GetMaximumX();
189  samp_->error_ = peak_fitterB_->GetParError(1);
190  }
191 
192  }
193 
194 }
195 
196 // ----------------------------------------------------------------------------
197 //
199 {
200  // loop over bins to find the max stat
201  uint32_t nbins=profile->GetNbinsX();
202  uint32_t max = 0;
203  for(uint32_t bin=1;bin<=nbins;++bin) max = max < profile->GetBinEntries(bin) ? uint32_t(profile->GetBinEntries(bin)) : max;
204  // loop over bins to clean
205  uint32_t min = max/10;
206  for(uint32_t bin=1;bin<=nbins;++bin)
207  if(profile->GetBinEntries(bin)<min) {
208  profile->SetBinContent(bin,0.);
209  profile->SetBinError(bin,0.);
210  }
211 }
212 
213 // ----------------------------------------------------------------------------
214 //
215 void SamplingAlgorithm::correctBinning(TProfile* prof) const
216 {
217  prof->GetXaxis()->SetLimits(prof->GetXaxis()->GetXmin()-prof->GetBinWidth(1)/2.,
218  prof->GetXaxis()->GetXmax()-prof->GetBinWidth(1)/2.);
219 }
220 
221 // ----------------------------------------------------------------------------
222 //
223 void SamplingAlgorithm::correctProfile(TProfile* profile, float SoNcut) const
224 {
225  if ( !samp_ ) { return; }
226  uint32_t nbins=profile->GetNbinsX();
227  float min = samp_->limit(SoNcut);
228  for(uint32_t bin=1;bin<=nbins;++bin)
229  if(profile->GetBinContent(bin)<min) {
230  profile->SetBinContent(bin,0.);
231  profile->SetBinError(bin,0.);
232  profile->SetBinEntries(bin,0);
233  }
234  else {
235  profile->SetBinContent(bin,
236  profile->GetBinEntries(bin)*
237  samp_->correctMeasurement(profile->GetBinContent(bin),SoNcut));
238  }
239 }
void correctBinning(TProfile *prof) const
static const char unexpectedTask_[]
Analysis for latency run.
double fdeconv_convoluted(double *x, double *par)
const uint32_t & fedKey() const
Utility class that holds histogram title.
sistrip::RunType runType_
static const char numberOfHistos_[]
float limit(float SoNcut) const
#define nullptr
sistrip classes
SamplingAnalysis * samp_
double fpeak_convoluted(double *x, double *par)
static const char mlCommissioning_[]
T sqrt(T t)
Definition: SSEVec.h:18
uint32_t extractFedKey(const TH1 *const )
void correctProfile(TProfile *profile, float SoNcut=3.) const
virtual void addErrorCode(const std::string &error)
T min(T a, T b)
Definition: MathUtil.h:58
bin
set the eta bin as selection string.
static const char clusterCharge_[]
void analyse() override
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
float correctMeasurement(float mean, float SoNcut=3.) const
void pruneProfile(TProfile *profile) const
Abstract base for derived classes that provide analysis of commissioning histograms.
void extract(const std::vector< TH1 * > &) override
sistrip::Granularity granularity_
CommissioningAnalysis *const anal() const