CMS 3D CMS Logo

MEtXYcorrectParameters.cc
Go to the documentation of this file.
1 // Generic parameters for MET corrections
2 //
6 #include <iostream>
7 #include <iomanip>
8 #include <fstream>
9 #include <sstream>
10 #include <cstdlib>
11 #include <algorithm>
12 #include <cmath>
13 #include <iterator>
14 //#include <string>
15 
16 //------------------------------------------------------------------------
17 //--- MEtXYcorrectParameters::Definitions constructor --------------------
18 //--- takes specific arguments for the member variables ------------------
19 //------------------------------------------------------------------------
20 MEtXYcorrectParameters::Definitions::Definitions(const std::vector<std::string>& fBinVar, const std::vector<std::string>& fParVar, const std::string& fFormula )
21 {
22  mBinVar.reserve(fBinVar.size());
23  for(unsigned i=0;i<fBinVar.size();i++)
24  mBinVar.push_back(fBinVar[i]);
25 
26  mParVar.reserve(fParVar.size());
27  for(unsigned i=0;i<fParVar.size();i++)
28  mParVar.push_back(getUnsigned(fParVar[i]));
29 
30  mFormula = fFormula;
31 }
32 //------------------------------------------------------------------------
33 //--- MEtXYcorrectParameters::Definitions constructor --------------------
34 //--- reads the member variables from a string ---------------------------
35 //------------------------------------------------------------------------
37 {
38  std::vector<std::string> tokens = getTokens(fLine);
39  // corrType N_bin binVa.. N_var var... formula
40  if (!tokens.empty())
41  {
42  if (tokens.size() < 6)
43  {
44  std::stringstream sserr;
45  sserr<<"(line "<<fLine<<"): Great than or equal to 6 expected but the number of tokens:"<<tokens.size();
46  handleError("MEtXYcorrectParameters::Definitions",sserr.str());
47  }
48  // No. of Bin Variable
49  //edm::LogInfo ("default")<<"Definitions===========";
50  ptclType = getSigned(tokens[0]);
51  unsigned nBinVar = getUnsigned(tokens[1]);
52  unsigned nParVar = getUnsigned(tokens[nBinVar+2]);
53  mBinVar.reserve(nBinVar);
54  mParVar.reserve(nParVar);
55 
56  for(unsigned i=0;i<nBinVar;i++)
57  {
58  mBinVar.push_back(tokens[i+2]);
59  }
60  for(unsigned i=0;i<nParVar;i++)
61  {
62  mParVar.push_back(getUnsigned(tokens[nBinVar+3+i]));
63  }
64  mFormula = tokens[nParVar+nBinVar+3];
65  if (tokens.size() != nParVar+nBinVar+4 )
66  {
67  std::stringstream sserr;
68  sserr<<"(line "<<fLine<<"): token size should be:"<<nParVar+nBinVar+4<<" but it is "<<tokens.size();
69  handleError("MEtXYcorrectParameters::Definitions",sserr.str());
70  }
71 
72  }
73 }
74 //------------------------------------------------------------------------
75 //--- MEtXYcorrectParameters::Record constructor -------------------------
76 //--- reads the member variables from a string ---------------------------
77 //------------------------------------------------------------------------
78 MEtXYcorrectParameters::Record::Record(const std::string& fLine,unsigned fNvar) : mMin(0), mMax(0), mParameters(0)
79 {
80  mNvar = fNvar;
81  // quckly parse the line
82  std::vector<std::string> tokens = getTokens(fLine);
83  if (!tokens.empty())
84  {
85  if (tokens.size() < 5)
86  {
87  std::stringstream sserr;
88  sserr<<"(line "<<fLine<<"): "<<"five tokens expected, "<<tokens.size()<<" provided.";
89  handleError("MEtXYcorrectParameters::Record",sserr.str());
90  }
91  mMetAxis = tokens[0];
92  for(unsigned i=0;i<mNvar;i++)
93  {
94  mMin.push_back(getFloat(tokens[i*2+1]));
95  mMax.push_back(getFloat(tokens[i*2+2]));
96  }
97  unsigned nParam = getUnsigned(tokens[2*mNvar+1]);
98  if (nParam != tokens.size()-(2*mNvar+2))
99  {
100  std::stringstream sserr;
101  sserr<<"(line "<<fLine<<"): "<<tokens.size()-(2*mNvar+2)<<" parameters, but nParam="<<nParam<<".";
102  handleError("MEtXYcorrectParameters::Record",sserr.str());
103  }
104  for (unsigned i = (2*mNvar+2); i < tokens.size(); ++i)
105  {
106  mParameters.push_back(getFloat(tokens[i]));
107  }
108  }
109 }
110 //------------------------------------------------------------------------
111 //--- MEtXYcorrectParameters constructor ---------------------------------
112 //--- reads the member variables from a string ---------------------------
113 //------------------------------------------------------------------------
115 {
116  std::ifstream input(fFile.c_str());
117  std::string currentSection = "";
119  std::string currentDefinitions = "";
120  while (std::getline(input,line))
121  {
123  std::string tmp = getDefinitions(line);
124  if (!section.empty() && tmp.empty())
125  {
126  currentSection = section;
127  continue;
128  }
129  if (currentSection == fSection)
130  {
131  if (!tmp.empty())
132  {
133  currentDefinitions = tmp;
134  continue;
135  }
136  Definitions definitions(currentDefinitions);
137  if (!(definitions.nBinVar()==0 && definitions.formula()==""))
140  bool check(true);
141  for(unsigned i=0;i<mDefinitions.nBinVar();++i)
142  if (record.xMin(i)==0 && record.xMax(i)==0)
143  check = false;
144  if (record.nParameters() == 0)
145  check = false;
146  if (check)
147  mRecords.push_back(record);
148  }
149  }
150  if (currentDefinitions=="")
151  handleError("MEtXYcorrectParameters","No definitions found!!!");
152  if (mRecords.empty() && currentSection == "") mRecords.push_back(Record());
153  if (mRecords.empty() && currentSection != "")
154  {
155  std::stringstream sserr;
156  sserr<<"the requested section "<<fSection<<" doesn't exist!";
157  handleError("MEtXYcorrectParameters",sserr.str());
158  }
159  std::sort(mRecords.begin(), mRecords.end());
160  valid_ = true;
161 }
162 //------------------------------------------------------------------------
163 //--- prints parameters on screen ----------------------------------------
164 //------------------------------------------------------------------------
166 {
167  std::cout<<"--------------------------------------------"<<std::endl;
168  std::cout<<"//////// PARAMETERS: //////////////////////"<<std::endl;
169  std::cout<<"--------------------------------------------"<<std::endl;
170  std::cout<<"Number of binning variables: "<<definitions().nBinVar()<<std::endl;
171  std::cout<<"Names of binning variables: ";
172  for(unsigned i=0;i<definitions().nBinVar();i++)
173  std::cout<<definitions().binVar(i)<<" ";
174  std::cout<<std::endl;
175  std::cout<<"--------------------------------------------"<<std::endl;
176  std::cout<<"Number of parameter variables: "<<definitions().nParVar()<<std::endl;
177  std::cout<<"Names of parameter variables: ";
178  for(unsigned i=0;i<definitions().nParVar();i++)
179  std::cout<<definitions().parVar(i)<<" ";
180  std::cout<<std::endl;
181  std::cout<<"--------------------------------------------"<<std::endl;
182  std::cout<<"Parametrization Formula: "<<definitions().formula()<<std::endl;
183  std::cout<<"--------------------------------------------"<<std::endl;
184  std::cout<<"------- Bin contents -----------------------"<<std::endl;
185  for(unsigned i=0;i<size();i++)
186  {
187  for(unsigned j=0;j<definitions().nBinVar();j++)
188  std::cout<<record(i).xMin(j)<<" "<<record(i).xMax(j)<<" ";
189  std::cout<<record(i).nParameters()<<" ";
190  for(unsigned j=0;j<record(i).nParameters();j++)
191  std::cout<<record(i).parameter(j)<<" ";
192  std::cout<<std::endl;
193  }
194 }
196 {
197  std::cout<<"--------------------------------------------"<<std::endl;
198  std::cout<<"//////// PARAMETERS: //////////////////////"<<std::endl;
199  std::cout<<"--------------------------------------------"<<std::endl;
200  std::cout<<"["<<Section<<"]"<<"\n";
201  std::cout<<"Number of binning variables: "<<definitions().nBinVar()<<std::endl;
202  std::cout<<"Names of binning variables: ";
203  for(unsigned i=0;i<definitions().nBinVar();i++)
204  std::cout<<definitions().binVar(i)<<" ";
205  std::cout<<std::endl;
206  std::cout<<"--------------------------------------------"<<std::endl;
207  std::cout<<"Number of parameter variables: "<<definitions().nParVar()<<std::endl;
208  std::cout<<"Names of parameter variables: ";
209  for(unsigned i=0;i<definitions().nParVar();i++)
210  std::cout<<definitions().parVar(i)<<" ";
211  std::cout<<std::endl;
212  std::cout<<"--------------------------------------------"<<std::endl;
213  std::cout<<"Parametrization Formula: "<<definitions().formula()<<std::endl;
214  std::cout<<"--------------------------------------------"<<std::endl;
215  std::cout<<"------- Bin contents -----------------------"<<std::endl;
216  for(unsigned i=0;i<size();i++) //mRecords size
217  {
218  std::cout<<record(i).MetAxis()<<" ";
219  std::cout<<"nBinVar ("<<definitions().nBinVar()<<") ";
220  for(unsigned j=0;j<definitions().nBinVar();j++)
221  std::cout<<record(i).xMin(j)<<" "<<record(i).xMax(j)<<" ";
222  std::cout<<"nParameters ("<<record(i).nParameters()<<") ";
223  for(unsigned j=0;j<record(i).nParameters();j++)
224  std::cout<<record(i).parameter(j)<<" ";
225  std::cout<<std::endl;
226  }
227 }
228 //------------------------------------------------------------------------
229 //--- prints parameters on file ----------------------------------------
230 //------------------------------------------------------------------------
231 void MEtXYcorrectParameters::printFile(const std::string& fFileName) const
232 {
233  std::ofstream txtFile;
234  txtFile.open(fFileName.c_str());
235  txtFile.setf(std::ios::right);
236  txtFile<<"{"<<definitions().nBinVar()<<std::setw(15);
237  for(unsigned i=0;i<definitions().nBinVar();i++)
238  txtFile<<definitions().binVar(i)<<std::setw(15);
239  txtFile<<definitions().nParVar()<<std::setw(15);
240  for(unsigned i=0;i<definitions().nParVar();i++)
241  txtFile<<definitions().parVar(i)<<std::setw(15);
242  txtFile<<std::setw(definitions().formula().size()+15)<<definitions().formula()<<std::setw(15);
243  txtFile<<"}"<<"\n";
244  for(unsigned i=0;i<size();i++)
245  {
246  for(unsigned j=0;j<definitions().nBinVar();j++)
247  txtFile<<record(i).xMin(j)<<std::setw(15)<<record(i).xMax(j)<<std::setw(15);
248  txtFile<<record(i).nParameters()<<std::setw(15);
249  for(unsigned j=0;j<record(i).nParameters();j++)
250  txtFile<<record(i).parameter(j)<<std::setw(15);
251  txtFile<<"\n";
252  }
253  txtFile.close();
254 }
255 void MEtXYcorrectParameters::printFile(const std::string& fFileName,const std::string &Section) const
256 {
257  std::ofstream txtFile;
258  txtFile.open(fFileName.c_str(),std::ofstream::app);
259  txtFile.setf(std::ios::right);
260  txtFile<<"["<<Section<<"]"<<"\n";
261  txtFile<<"{"<<" "<<definitions().PtclType()<<" "<<definitions().nBinVar();
262  for(unsigned i=0;i<definitions().nBinVar();i++)
263  txtFile<<" "<<definitions().binVar(i);
264  txtFile<<" "<<definitions().nParVar();
265  for(unsigned i=0;i<definitions().nParVar();i++)
266  txtFile<<" "<<definitions().parVar(i);
267  txtFile<<" "<<definitions().formula();
268  txtFile<<"}"<<"\n";
269  for(unsigned i=0;i<size();i++) //mRecords size
270  {
271  txtFile<<record(i).MetAxis();
272  for(unsigned j=0;j<definitions().nBinVar();j++)
273  txtFile<<" "<<record(i).xMin(j)<<" "<<record(i).xMax(j);
274  txtFile<<" "<<record(i).nParameters();
275  for(unsigned j=0;j<record(i).nParameters();j++)
276  txtFile<<" "<<record(i).parameter(j);
277  txtFile<<"\n";
278  }
279  txtFile.close();
280 }
281 
282 namespace {
283 const std::vector<std::string> labels_ = {
284  "shiftMC",
285  "shiftDY",
286  "shiftTTJets",
287  "shiftWJets",
288  "shiftData"
289 };
290 const std::vector<std::string> shiftFlavors_ = {
291  "hEtaPlus",
292  "hEtaMinus",
293  "h0Barrel",
294  "h0EndcapPlus",
295  "h0EndcapMinus",
296  "gammaBarrel",
297  "gammaEndcapPlus",
298  "gammaEndcapMinus",
299  "hHFPlus",
300  "hHFMinus",
301  "egammaHFPlus",
302  "egammaHFMinus"
303 };
304 
305 }//namespace
306 
309  if( isShiftMC(k) ){
310  return findShiftMCflavor(k);
312  return findShiftDYflavor(k);
314  return findShiftTTJetsFlavor(k);
316  return findShiftWJetsFlavor(k);
318  return findShiftDataFlavor(k);
319  }
320  return labels_[k];
321 }
322 
325  if( isShiftMC(k) ){
326  return labels_[shiftMC];
327  }else if( isShiftDY(k) ){
328  return labels_[shiftDY];
329  }else if( isShiftTTJets(k) ){
330  return labels_[shiftTTJets];
331  }else if( isShiftWJets(k) ){
332  return labels_[shiftWJets];
333  }else if( isShiftData(k) ){
334  return labels_[shiftData];
335  }else{ return "Can't find the level name !!!!";}
336 
337 }
338 
341 {
342  if( k == shiftMC) return labels_[shiftMC];
343  else
344  return shiftFlavors_[k - (shiftMC+1)*100 -1];
345 }
348 {
349  if( k == shiftDY) return labels_[shiftDY];
350  else
351  return shiftFlavors_[k - (shiftDY+1)*100 -1];
352 }
355 {
356  if( k == shiftTTJets) return labels_[shiftTTJets];
357  else
358  return shiftFlavors_[k - (shiftTTJets+1)*100 -1];
359 }
362 {
363  if( k == shiftWJets) return labels_[shiftWJets];
364  else
365  return shiftFlavors_[k - (shiftWJets+1)*100 -1];
366 }
369 {
370  if( k == shiftData) return labels_[shiftData];
371  else
372  return shiftFlavors_[k - (shiftData+1)*100 -1];
373 }
374 
376  std::vector<std::string> & outputs )
377 {
378  outputs.clear();
379  std::ifstream input( inputFile.c_str() );
380  while( !input.eof() ) {
381  char buff[10000];
382  input.getline(buff,10000);
383  std::string in(buff);
384  if ( in[0] == '[' ) {
385  std::string tok = getSection(in);
386  if ( tok != "" ) {
387  outputs.push_back( tok );
388  }
389  }
390  }
391  //copy(outputs.begin(),outputs.end(), std::ostream_iterator<std::string>(std::cout, "\n") );
392 
393  std::string sectionNames;
394  for(std::vector<std::string>::const_iterator it=outputs.begin(); it!=outputs.end();it++){
395  sectionNames+=*it;
396  sectionNames+="\n";
397  }
398  edm::LogInfo ("getSections")<<"Sections read from file: "<<"\n"<<sectionNames;
399 }
400 
401 // Add a METCorrectorParameter object.
403 {
404  if( isShiftMC(i))
405  {
406  correctionsShift_.push_back( pair_type(getShiftMcFlavBin(flav),j) );
407  }else if( isShiftDY(i))
408  {
409  correctionsShift_.push_back( pair_type(getShiftDyFlavBin(flav),j) );
410  }else if( isShiftTTJets(i))
411  {
412  correctionsShift_.push_back( pair_type(getShiftTTJetsFlavBin(flav),j) );
413  }else if( isShiftWJets(i))
414  {
415  correctionsShift_.push_back( pair_type(getShiftWJetsFlavBin(flav),j) );
416  }else if( isShiftData(i))
417  {
418  correctionsShift_.push_back( pair_type(getShiftDataFlavBin(flav),j) );
419  }else{
420  std::stringstream sserr;
421  sserr<<"The level type: "<<i<<" is not in the level list";
422  handleError("MEtXYcorrectParameters::Definitions",sserr.str());
423  }
424 
425 }
426 
427 // Access the METCorrectorParameter via the key k.
428 // key_type is hashed to deal with the three collections
430  collection_type::const_iterator ibegin, iend, i;
431  if ( isShiftMC(k) || isShiftDY(k) || isShiftTTJets(k) || isShiftWJets(k) || isShiftData(k) ) {
432  ibegin = correctionsShift_.begin();
433  iend = correctionsShift_.end();
434  i = ibegin;
435  }
436  for ( ; i != iend; ++i ) {
437  if ( k == i->first ) return i->second;
438  }
439  throw cms::Exception("InvalidInput") << " cannot find key " << static_cast<int>(k)
440  << " in the METC payload, this usually means you have to change the global tag" << std::endl;
441 }
442 
443 // Get a list of valid keys. These will contain hashed keys
444 // that are aware of all three collections.
445 void MEtXYcorrectParametersCollection::validKeys(std::vector<key_type> & keys ) const {
446  keys.clear();
447  for ( collection_type::const_iterator ibegin = correctionsShift_.begin(),
448  iend = correctionsShift_.end(), i = ibegin; i != iend; ++i ) {
449  keys.push_back( i->first );
450  }
451 }
452 
453 
454 
457  std::vector<std::string>::const_iterator found =
458  find( shiftFlavors_.begin(), shiftFlavors_.end(), flav );
459  if ( found != shiftFlavors_.end() ) {
460  return (found - shiftFlavors_.begin() + 1)+ (shiftMC+1) * 100;
461  }
462  else{
463  throw cms::Exception("InvalidInput") <<
464  "************** Can't find ShiftSection: "<<flav<<std::endl;
465  }
466  return 0;
467 }
468 
471  std::vector<std::string>::const_iterator found =
472  find( shiftFlavors_.begin(), shiftFlavors_.end(), flav );
473  if ( found != shiftFlavors_.end() ) {
474  return (found - shiftFlavors_.begin() + 1)+ (shiftDY+1) * 100;
475  }
476  else{
477  throw cms::Exception("InvalidInput") <<
478  "************** Can't find ShiftSection: "<<flav<<std::endl;
479  }
480  return 0;
481 }
484  std::vector<std::string>::const_iterator found =
485  find( shiftFlavors_.begin(), shiftFlavors_.end(), flav );
486  if ( found != shiftFlavors_.end() ) {
487  return (found - shiftFlavors_.begin() + 1)+ (shiftTTJets+1) * 100;
488  }
489  else{
490  throw cms::Exception("InvalidInput") <<
491  "************** Can't find shiftSection: "<<flav<<std::endl;
492  }
493  return 0;
494 }
497  std::vector<std::string>::const_iterator found =
498  find( shiftFlavors_.begin(), shiftFlavors_.end(), flav );
499  if ( found != shiftFlavors_.end() ) {
500  return (found - shiftFlavors_.begin() + 1)+ (shiftWJets+1) * 100;
501  }
502  else{
503  throw cms::Exception("InvalidInput") <<
504  "************** Can't find shiftSection: "<<flav<<std::endl;
505  }
506  return 0;
507 }
510  std::vector<std::string>::const_iterator found =
511  find( shiftFlavors_.begin(), shiftFlavors_.end(), flav );
512  if ( found != shiftFlavors_.end() ) {
513  return (found - shiftFlavors_.begin() + 1)+ (shiftData+1) * 100;
514  }
515  else{
516  throw cms::Exception("InvalidInput") <<
517  "************** Can't find shiftSection: "<<flav<<std::endl;
518  }
519  return 0;
520 }
521 
523  return k == shiftMC ||
524  (k > (shiftMC+1)*100 && k < (shiftMC + 2)*100 );
525 }
527  return k == shiftDY ||
528  (k > (shiftDY+1)*100 && k < (shiftDY + 2)*100 );
529 }
531  return k == shiftTTJets ||
532  (k > (shiftTTJets+1)*100 && k < (shiftTTJets + 2)*100 );
533 }
535  return k == shiftWJets ||
536  (k > (shiftWJets+1)*100 && k < (shiftWJets + 2)*100 );
537 }
539  return k == shiftData ||
540  (k > (shiftData+1)*100 && k < (shiftData + 2)*100 );
541 }
542 
543 
545 
float parameter(unsigned fIndex) const
JetCorrectorParametersCollection::pair_type pair_type
Definition: classes.h:11
void validKeys(std::vector< key_type > &keys) const
std::vector< MEtXYcorrectParameters::Record > mRecords
key_type getShiftDataFlavBin(std::string const &Flav)
const Definitions & definitions() const
key_type getShiftTTJetsFlavBin(std::string const &Flav)
static std::string findShiftWJetsFlavor(key_type k)
float xMax(unsigned fVar) const
static std::string findShiftDataFlavor(key_type k)
void getSections(std::string inputFile, std::vector< std::string > &outputs)
static std::string findShiftMCflavor(key_type k)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
void printFile(const std::string &fFileName) const
static std::string const input
Definition: EdmProvDump.cc:48
const Record & record(unsigned fBin) const
MEtXYcorrectParameters const & operator[](key_type k) const
key_type getShiftMcFlavBin(std::string const &Flav)
static std::string findShiftDYflavor(key_type k)
int k[5][pyjets_maxn]
#define TYPELOOKUP_DATA_REG(_dataclass_)
Definition: typelookup.h:102
void push_back(key_type i, value_type const &j, label_type const &flav="")
def getSection(rootNode, name)
MEtXYcorrectParameters::Definitions mDefinitions
std::vector< std::string > binVar() const
static std::string findLabel(key_type k)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
float xMin(unsigned fVar) const
key_type getShiftDyFlavBin(std::string const &Flav)
key_type getShiftWJetsFlavBin(std::string const &Flav)
def check(config)
Definition: trackerTree.py:14
static std::string findShiftTTJetsFlavor(key_type k)
std::vector< unsigned > parVar() const
static std::string levelName(key_type k)