CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
JetCorrectorParameters.cc
Go to the documentation of this file.
1 //
2 // Original Author: Fedor Ratnikov Nov 9, 2007
3 // $Id: JetCorrectorParameters.cc,v 1.20 2012/03/01 18:24:53 srappocc Exp $
4 //
5 // Generic parameters for Jet corrections
6 //
9 #include <iostream>
10 #include <iomanip>
11 #include <fstream>
12 #include <sstream>
13 #include <stdlib.h>
14 #include <algorithm>
15 #include <cmath>
16 #include <iterator>
17 
18 //------------------------------------------------------------------------
19 //--- JetCorrectorParameters::Definitions constructor --------------------
20 //--- takes specific arguments for the member variables ------------------
21 //------------------------------------------------------------------------
22 JetCorrectorParameters::Definitions::Definitions(const std::vector<std::string>& fBinVar, const std::vector<std::string>& fParVar, const std::string& fFormula, bool fIsResponse)
23 {
24  for(unsigned i=0;i<fBinVar.size();i++)
25  mBinVar.push_back(fBinVar[i]);
26  for(unsigned i=0;i<fParVar.size();i++)
27  mParVar.push_back(fParVar[i]);
28  mFormula = fFormula;
29  mIsResponse = fIsResponse;
30  mLevel = "";
31 }
32 //------------------------------------------------------------------------
33 //--- JetCorrectorParameters::Definitions constructor --------------------
34 //--- reads the member variables from a string ---------------------------
35 //------------------------------------------------------------------------
37 {
38  std::vector<std::string> tokens = getTokens(fLine);
39  if (!tokens.empty())
40  {
41  if (tokens.size() < 6)
42  {
43  std::stringstream sserr;
44  sserr<<"(line "<<fLine<<"): less than 6 expected tokens:"<<tokens.size();
45  handleError("JetCorrectorParameters::Definitions",sserr.str());
46  }
47  unsigned nvar = getUnsigned(tokens[0]);
48  unsigned npar = getUnsigned(tokens[nvar+1]);
49  for(unsigned i=0;i<nvar;i++)
50  mBinVar.push_back(tokens[i+1]);
51  for(unsigned i=0;i<npar;i++)
52  mParVar.push_back(tokens[nvar+2+i]);
53  mFormula = tokens[npar+nvar+2];
54  std::string ss = tokens[npar+nvar+3];
55  if (ss == "Response")
56  mIsResponse = true;
57  else if (ss == "Correction")
58  mIsResponse = false;
59  else if (ss == "Resolution")
60  mIsResponse = false;
61  else if (ss.find("PAR")==0)
62  mIsResponse = false;
63  else
64  {
65  std::stringstream sserr;
66  sserr<<"unknown option ("<<ss<<")";
67  handleError("JetCorrectorParameters::Definitions",sserr.str());
68  }
69  mLevel = tokens[npar+nvar+4];
70  }
71 }
72 //------------------------------------------------------------------------
73 //--- JetCorrectorParameters::Record constructor -------------------------
74 //--- reads the member variables from a string ---------------------------
75 //------------------------------------------------------------------------
76 JetCorrectorParameters::Record::Record(const std::string& fLine,unsigned fNvar) : mMin(0),mMax(0)
77 {
78  mNvar = fNvar;
79  // quckly parse the line
80  std::vector<std::string> tokens = getTokens(fLine);
81  if (!tokens.empty())
82  {
83  if (tokens.size() < 3)
84  {
85  std::stringstream sserr;
86  sserr<<"(line "<<fLine<<"): "<<"three tokens expected, "<<tokens.size()<<" provided.";
87  handleError("JetCorrectorParameters::Record",sserr.str());
88  }
89  for(unsigned i=0;i<mNvar;i++)
90  {
91  mMin.push_back(getFloat(tokens[i*mNvar]));
92  mMax.push_back(getFloat(tokens[i*mNvar+1]));
93  }
94  unsigned nParam = getUnsigned(tokens[2*mNvar]);
95  if (nParam != tokens.size()-(2*mNvar+1))
96  {
97  std::stringstream sserr;
98  sserr<<"(line "<<fLine<<"): "<<tokens.size()-(2*mNvar+1)<<" parameters, but nParam="<<nParam<<".";
99  handleError("JetCorrectorParameters::Record",sserr.str());
100  }
101  for (unsigned i = (2*mNvar+1); i < tokens.size(); ++i)
102  mParameters.push_back(getFloat(tokens[i]));
103  }
104 }
105 //------------------------------------------------------------------------
106 //--- JetCorrectorParameters constructor ---------------------------------
107 //--- reads the member variables from a string ---------------------------
108 //------------------------------------------------------------------------
109 JetCorrectorParameters::JetCorrectorParameters(const std::string& fFile, const std::string& fSection)
110 {
111  std::ifstream input(fFile.c_str());
112  std::string currentSection = "";
113  std::string line;
114  std::string currentDefinitions = "";
115  while (std::getline(input,line))
116  {
117  std::string section = getSection(line);
118  std::string tmp = getDefinitions(line);
119  if (!section.empty() && tmp.empty())
120  {
121  currentSection = section;
122  continue;
123  }
124  if (currentSection == fSection)
125  {
126  if (!tmp.empty())
127  {
128  currentDefinitions = tmp;
129  continue;
130  }
131  Definitions definitions(currentDefinitions);
132  if (!(definitions.nBinVar()==0 && definitions.formula()==""))
135  bool check(true);
136  for(unsigned i=0;i<mDefinitions.nBinVar();++i)
137  if (record.xMin(i)==0 && record.xMax(i)==0)
138  check = false;
139  if (record.nParameters() == 0)
140  check = false;
141  if (check)
142  mRecords.push_back(record);
143  }
144  }
145  if (currentDefinitions=="")
146  handleError("JetCorrectorParameters","No definitions found!!!");
147  if (mRecords.empty() && currentSection == "") mRecords.push_back(Record());
148  if (mRecords.empty() && currentSection != "")
149  {
150  std::stringstream sserr;
151  sserr<<"the requested section "<<fSection<<" doesn't exist!";
152  handleError("JetCorrectorParameters",sserr.str());
153  }
154  std::sort(mRecords.begin(), mRecords.end());
155  valid_ = true;
156 }
157 //------------------------------------------------------------------------
158 //--- returns the index of the record defined by fX ----------------------
159 //------------------------------------------------------------------------
160 int JetCorrectorParameters::binIndex(const std::vector<float>& fX) const
161 {
162  int result = -1;
163  unsigned N = mDefinitions.nBinVar();
164  if (N != fX.size())
165  {
166  std::stringstream sserr;
167  sserr<<"# bin variables "<<N<<" doesn't correspont to requested #: "<<fX.size();
168  handleError("JetCorrectorParameters",sserr.str());
169  }
170  unsigned tmp;
171  for (unsigned i = 0; i < size(); ++i)
172  {
173  tmp = 0;
174  for (unsigned j=0;j<N;j++)
175  if (fX[j] >= record(i).xMin(j) && fX[j] < record(i).xMax(j))
176  tmp+=1;
177  if (tmp==N)
178  {
179  result = i;
180  break;
181  }
182  }
183  return result;
184 }
185 //------------------------------------------------------------------------
186 //--- returns the neighbouring bins of fIndex in the direction of fVar ---
187 //------------------------------------------------------------------------
188 int JetCorrectorParameters::neighbourBin(unsigned fIndex, unsigned fVar, bool fNext) const
189 {
190  int result = -1;
191  unsigned N = mDefinitions.nBinVar();
192  if (fVar >= N)
193  {
194  std::stringstream sserr;
195  sserr<<"# of bin variables "<<N<<" doesn't correspond to requested #: "<<fVar;
196  handleError("JetCorrectorParameters",sserr.str());
197  }
198  unsigned tmp;
199  for (unsigned i = 0; i < size(); ++i)
200  {
201  tmp = 0;
202  for (unsigned j=0;j<fVar;j++)
203  if (fabs(record(i).xMin(j)-record(fIndex).xMin(j))<0.0001)
204  tmp+=1;
205  for (unsigned j=fVar+1;j<N;j++)
206  if (fabs(record(i).xMin(j)-record(fIndex).xMin(j))<0.0001)
207  tmp+=1;
208  if (tmp<N-1)
209  continue;
210  if (tmp==N-1)
211  {
212  if (fNext)
213  if (fabs(record(i).xMin(fVar)-record(fIndex).xMax(fVar))<0.0001)
214  tmp+=1;
215  if (!fNext)
216  if (fabs(record(i).xMax(fVar)-record(fIndex).xMin(fVar))<0.0001)
217  tmp+=1;
218  }
219  if (tmp==N)
220  {
221  result = i;
222  break;
223  }
224  }
225  return result;
226 }
227 //------------------------------------------------------------------------
228 //--- returns the number of bins in the direction of fVar ----------------
229 //------------------------------------------------------------------------
230 unsigned JetCorrectorParameters::size(unsigned fVar) const
231 {
232  if (fVar >= mDefinitions.nBinVar())
233  {
234  std::stringstream sserr;
235  sserr<<"requested bin variable index "<<fVar<<" is greater than number of variables "<<mDefinitions.nBinVar();
236  handleError("JetCorrectorParameters",sserr.str());
237  }
238  unsigned result = 0;
239  float tmpMin(-9999),tmpMax(-9999);
240  for (unsigned i = 0; i < size(); ++i)
241  if (record(i).xMin(fVar) > tmpMin && record(i).xMax(fVar) > tmpMax)
242  {
243  result++;
244  tmpMin = record(i).xMin(fVar);
245  tmpMax = record(i).xMax(fVar);
246  }
247  return result;
248 }
249 //------------------------------------------------------------------------
250 //--- returns the vector of bin centers of fVar --------------------------
251 //------------------------------------------------------------------------
252 std::vector<float> JetCorrectorParameters::binCenters(unsigned fVar) const
253 {
254  std::vector<float> result;
255  for (unsigned i = 0; i < size(); ++i)
256  result.push_back(record(i).xMiddle(fVar));
257  return result;
258 }
259 //------------------------------------------------------------------------
260 //--- prints parameters on screen ----------------------------------------
261 //------------------------------------------------------------------------
263 {
264  std::cout<<"--------------------------------------------"<<std::endl;
265  std::cout<<"//////// PARAMETERS: //////////////////////"<<std::endl;
266  std::cout<<"--------------------------------------------"<<std::endl;
267  std::cout<<"Number of binning variables: "<<definitions().nBinVar()<<std::endl;
268  std::cout<<"Names of binning variables: ";
269  for(unsigned i=0;i<definitions().nBinVar();i++)
270  std::cout<<definitions().binVar(i)<<" ";
271  std::cout<<std::endl;
272  std::cout<<"--------------------------------------------"<<std::endl;
273  std::cout<<"Number of parameter variables: "<<definitions().nParVar()<<std::endl;
274  std::cout<<"Names of parameter variables: ";
275  for(unsigned i=0;i<definitions().nParVar();i++)
276  std::cout<<definitions().parVar(i)<<" ";
277  std::cout<<std::endl;
278  std::cout<<"--------------------------------------------"<<std::endl;
279  std::cout<<"Parametrization Formula: "<<definitions().formula()<<std::endl;
280  if (definitions().isResponse())
281  std::cout<<"Type (Response or Correction): "<<"Response"<<std::endl;
282  else
283  std::cout<<"Type (Response or Correction): "<<"Correction"<<std::endl;
284  std::cout<<"Correction Level: "<<definitions().level()<<std::endl;
285  std::cout<<"--------------------------------------------"<<std::endl;
286  std::cout<<"------- Bin contents -----------------------"<<std::endl;
287  for(unsigned i=0;i<size();i++)
288  {
289  for(unsigned j=0;j<definitions().nBinVar();j++)
290  std::cout<<record(i).xMin(j)<<" "<<record(i).xMax(j)<<" ";
291  std::cout<<record(i).nParameters()<<" ";
292  for(unsigned j=0;j<record(i).nParameters();j++)
293  std::cout<<record(i).parameter(j)<<" ";
294  std::cout<<std::endl;
295  }
296 }
297 //------------------------------------------------------------------------
298 //--- prints parameters on file ----------------------------------------
299 //------------------------------------------------------------------------
300 void JetCorrectorParameters::printFile(const std::string& fFileName) const
301 {
302  std::ofstream txtFile;
303  txtFile.open(fFileName.c_str());
304  txtFile.setf(std::ios::right);
305  txtFile<<"{"<<definitions().nBinVar()<<std::setw(15);
306  for(unsigned i=0;i<definitions().nBinVar();i++)
307  txtFile<<definitions().binVar(i)<<std::setw(15);
308  txtFile<<definitions().nParVar()<<std::setw(15);
309  for(unsigned i=0;i<definitions().nParVar();i++)
310  txtFile<<definitions().parVar(i)<<std::setw(15);
311  txtFile<<std::setw(definitions().formula().size()+15)<<definitions().formula()<<std::setw(15);
312  if (definitions().isResponse())
313  txtFile<<"Response"<<std::setw(15);
314  else
315  txtFile<<"Correction"<<std::setw(15);
316  txtFile<<definitions().level()<<"}"<<"\n";
317  for(unsigned i=0;i<size();i++)
318  {
319  for(unsigned j=0;j<definitions().nBinVar();j++)
320  txtFile<<record(i).xMin(j)<<std::setw(15)<<record(i).xMax(j)<<std::setw(15);
321  txtFile<<record(i).nParameters()<<std::setw(15);
322  for(unsigned j=0;j<record(i).nParameters();j++)
323  txtFile<<record(i).parameter(j)<<std::setw(15);
324  txtFile<<"\n";
325  }
326  txtFile.close();
327 }
328 
329 
330 
331 const char *
333  {
334  "L1Offset",
335  "L2Relative",
336  "L3Absolute",
337  "L4EMF",
338  "L5Flavor",
339  "L6UE",
340  "L7Parton",
341  "L1JPTOffset",
342  "L2L3Residual",
343  "Uncertainty",
344  "L1FastJet",
345  "UncertaintyAbsolute",
346  "UncertaintyHighPtExtra",
347  "UncertaintySinglePion",
348  "UncertaintyFlavor",
349  "UncertaintyTime",
350  "UncertaintyRelativeJEREC1",
351  "UncertaintyRelativeJEREC2",
352  "UncertaintyRelativeJERHF",
353  "UncertaintyRelativeStatEC2",
354  "UncertaintyRelativeStatHF",
355  "UncertaintyRelativeFSR",
356  "UncertaintyPileUpDataMC",
357  "UncertaintyPileUpOOT",
358  "UncertaintyPileUpPt",
359  "UncertaintyPileUpBias",
360  "UncertaintyPileUpJetRate"
361  };
362 
363 const char *
365  {
366  "L5Flavor_bJ",
367  "L5Flavor_cJ",
368  "L5Flavor_qJ",
369  "L5Flavor_gJ",
370  "L5Flavor_bT",
371  "L5Flavor_cT",
372  "L5Flavor_qT",
373  "L5Flavor_gT"
374  };
375 
376 const char *
378  {
379  "L7Parton_gJ",
380  "L7Parton_qJ",
381  "L7Parton_cJ",
382  "L7Parton_bJ",
383  "L7Parton_jJ",
384  "L7Parton_qT",
385  "L7Parton_cT",
386  "L7Parton_bT",
387  "L7Parton_tT"
388  };
389 
390 
391 std::vector<std::string>
393  labelsArray_ + sizeof(labelsArray_)/sizeof(*labelsArray_) );
394 
395 std::vector<std::string>
397  l5FlavorArray_ + sizeof(l5FlavorArray_)/sizeof(*l5FlavorArray_) );
398 
399 std::vector<std::string>
401  l7PartonArray_ + sizeof(l7PartonArray_)/sizeof(*l7PartonArray_) );
402 
403 
405  std::vector<std::string> & outputs )
406 {
407  outputs.clear();
408  std::ifstream input( inputFile.c_str() );
409  while( !input.eof() ) {
410  char buff[10000];
411  input.getline(buff,10000);
412  std::string in(buff);
413  if ( in[0] == '[' ) {
414  std::string tok = getSection(in);
415  if ( tok != "" ) {
416  outputs.push_back( tok );
417  }
418  }
419  }
420  std::cout << "Found these sections for file: " << std::endl;
421  copy(outputs.begin(),outputs.end(), std::ostream_iterator<std::string>(std::cout, "\n") );
422 }
423 
424 
425 // Add a JetCorrectorParameter object, possibly with flavor.
427  std::cout << "i = " << i << std::endl;
428  std::cout << "flav = " << flav << std::endl;
429  if ( isL5(i) ) {
430  std::cout << "This is L5, getL5Bin = " << getL5Bin(flav) << std::endl;
431  correctionsL5_.push_back( pair_type(getL5Bin(flav),j) );
432  }
433  else if ( isL7(i) ) {
434  std::cout << "This is L7, getL7Bin = " << getL7Bin(flav) << std::endl;
435  correctionsL7_.push_back( pair_type(getL7Bin(flav),j) );
436  }
437  else if ( flav == "" ) {
438  corrections_.push_back( pair_type(i,j) );
439  } else {
440  std::cout << "***** NOT ADDING " << flav << ", corresponding position in JetCorrectorParameters is not found." << std::endl;
441  }
442 }
443 
444 
445 // Access the JetCorrectorParameter via the key k.
446 // key_type is hashed to deal with the three collections
448  collection_type::const_iterator ibegin, iend, i;
449  if ( isL5(k) ) {
450  ibegin = correctionsL5_.begin();
451  iend = correctionsL5_.end();
452  i = ibegin;
453  } else if ( isL7(k) ) {
454  ibegin = correctionsL7_.begin();
455  iend = correctionsL7_.end();
456  i = ibegin;
457  } else {
458  ibegin = corrections_.begin();
459  iend = corrections_.end();
460  i = ibegin;
461  }
462  for ( ; i != iend; ++i ) {
463  if ( k == i->first ) return i->second;
464  }
465  throw cms::Exception("InvalidInput") << " cannot find key " << static_cast<int>(k)
466  << " in the JEC payload, this usually means you have to change the global tag" << std::endl;
467 }
468 
469 // Get a list of valid keys. These will contain hashed keys
470 // that are aware of all three collections.
471 void JetCorrectorParametersCollection::validKeys(std::vector<key_type> & keys ) const {
472  keys.clear();
473  for ( collection_type::const_iterator ibegin = corrections_.begin(),
474  iend = corrections_.end(), i = ibegin; i != iend; ++i ) {
475  keys.push_back( i->first );
476  }
477  for ( collection_type::const_iterator ibegin = correctionsL5_.begin(),
478  iend = correctionsL5_.end(), i = ibegin; i != iend; ++i ) {
479  keys.push_back( i->first );
480  }
481  for ( collection_type::const_iterator ibegin = correctionsL7_.begin(),
482  iend = correctionsL7_.end(), i = ibegin; i != iend; ++i ) {
483  keys.push_back( i->first );
484  }
485 }
486 
487 
488 // Find the L5 bin for hashing
490 JetCorrectorParametersCollection::getL5Bin( std::string const & flav ){
491  std::vector<std::string>::const_iterator found =
492  find( l5Flavors_.begin(), l5Flavors_.end(), flav );
493  if ( found != l5Flavors_.end() ) {
494  return (found - l5Flavors_.begin() + 1) * 100;
495  }
496  else return L5Flavor;
497 }
498 // Find the L7 bin for hashing
500 JetCorrectorParametersCollection::getL7Bin( std::string const & flav ){
501  std::vector<std::string>::const_iterator found =
502  find( l7Partons_.begin(), l7Partons_.end(), flav );
503  if ( found != l7Partons_.end() ) {
504  return (found - l7Partons_.begin() + 1) * 1000;
505  }
506  else return L7Parton;
507 }
508 
509 // Check if this is an L5 hashed value
511  return k == L5Flavor ||
512  ( k / 100 > 0 && k / 1000 == 0 );
513 }
514 // Check if this is an L7 hashed value
516  return k == L7Parton ||
517  ( k / 1000 > 0 );
518 }
519 
520 
521 // Find the key corresponding to each label
523 JetCorrectorParametersCollection::findKey( std::string const & label ) const {
524 
525  // First check L5 corrections
526  std::vector<std::string>::const_iterator found1 =
527  find( l5Flavors_.begin(), l5Flavors_.end(), label );
528  if ( found1 != l5Flavors_.end() ) {
529  return getL5Bin(label);
530  }
531 
532  // Next check L7 corrections
533  std::vector<std::string>::const_iterator found2 =
534  find( l7Partons_.begin(), l7Partons_.end(), label );
535  if ( found2 != l7Partons_.end() ) {
536  return getL7Bin(label);
537  }
538 
539  // Finally check the default corrections
540  std::vector<std::string>::const_iterator found3 =
541  find( labels_.begin(), labels_.end(), label );
542  if ( found3 != labels_.end() ) {
543  return static_cast<key_type>(found3 - labels_.begin());
544  }
545 
546  // Didn't find default corrections, throw exception
547  throw cms::Exception("InvalidInput") << " Cannot find label " << label << std::endl;
548 
549 }
550 
551 
552 //#include "FWCore/Framework/interface/EventSetup.h"
553 //#include "FWCore/Framework/interface/ESHandle.h"
554 //#include "FWCore/Framework/interface/ModuleFactory.h"
556 
float xMin(unsigned fVar) const
int i
Definition: DBlmapReader.cc:9
JetCorrectorParametersCollection::pair_type pair_type
Definition: classes.h:15
static std::vector< std::string > labels_
void push_back(key_type i, value_type const &j, label_type const &flav="")
const Definitions & definitions() const
static key_type getL5Bin(std::string const &flav)
static const char * l5FlavorArray_[N_L5_SPECIES]
static const char * labelsArray_[N_LEVELS]
std::vector< JetCorrectorParameters::Record > mRecords
const Record & record(unsigned fBin) const
std::vector< std::string > parVar() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
float parameter(unsigned fIndex) const
void printFile(const std::string &fFileName) const
int binIndex(const std::vector< float > &fX) const
std::vector< float > binCenters(unsigned fVar) const
bool check(const DataFrame &df, bool capcheck, bool dvercheck)
tuple result
Definition: query.py:137
int j
Definition: DBlmapReader.cc:9
static const char * l7PartonArray_[N_L7_SPECIES]
static std::vector< std::string > l7Partons_
key_type findKey(std::string const &label) const
float xMax(unsigned fVar) const
int k[5][pyjets_maxn]
#define TYPELOOKUP_DATA_REG(_dataclass_)
Definition: typelookup.h:97
#define N
Definition: blowfish.cc:9
static key_type getL7Bin(std::string const &flav)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
static std::vector< std::string > l5Flavors_
static void getSections(std::string inputFile, std::vector< std::string > &outputs)
tuple cout
Definition: gather_cfg.py:121
void validKeys(std::vector< key_type > &keys) const
std::vector< std::string > binVar() const
JetCorrectorParameters::Definitions mDefinitions
int neighbourBin(unsigned fIndex, unsigned fVar, bool fNext) const
JetCorrectorParameters const & operator[](key_type k) const