CMS 3D CMS Logo

METCorrectorParameters.cc
Go to the documentation of this file.
1 // Generic parameters for MET corrections
2 //
5 #include <iostream>
6 #include <iomanip>
7 #include <fstream>
8 #include <sstream>
9 #include <cstdlib>
10 #include <algorithm>
11 #include <cmath>
12 #include <iterator>
13 //#include <string>
14 
15 //------------------------------------------------------------------------
16 //--- METCorrectorParameters::Definitions constructor --------------------
17 //--- takes specific arguments for the member variables ------------------
18 //------------------------------------------------------------------------
19 METCorrectorParameters::Definitions::Definitions(const std::vector<std::string>& fBinVar, const std::vector<std::string>& fParVar, const std::string& fFormula )
20 {
21  for(unsigned i=0;i<fBinVar.size();i++)
22  mBinVar.push_back(fBinVar[i]);
23  for(unsigned i=0;i<fParVar.size();i++)
24  mParVar.push_back(fParVar[i]);
25  mFormula = fFormula;
26 }
27 //------------------------------------------------------------------------
28 //--- METCorrectorParameters::Definitions constructor --------------------
29 //--- reads the member variables from a string ---------------------------
30 //------------------------------------------------------------------------
32 {
33  std::vector<std::string> tokens = getTokens(fLine);
34  // corrType N_bin binVa.. N_var var... formula
35  if (!tokens.empty())
36  {
37  if (tokens.size() < 6)
38  {
39  std::stringstream sserr;
40  sserr<<"(line "<<fLine<<"): less than 6 expected tokens:"<<tokens.size();
41  handleError("METCorrectorParameters::Definitions",sserr.str());
42  }
43  // No. of Bin Variable
44  std::cout<<"Definitions==========="<<std::endl;
45  ptclType = getSigned(tokens[0]);
46  std::cout<<tokens[0]<<"\t";
47  unsigned nBinVar = getUnsigned(tokens[1]);
48  std::cout<<tokens[1]<<"\t";
49  // No of Parameterization Variable
50  unsigned nParVar = getUnsigned(tokens[nBinVar+2]);
51  std::cout<<tokens[2]<<"\t";
52  for(unsigned i=0;i<nBinVar;i++)
53  {
54  mBinVar.push_back(tokens[i+2]);
55  std::cout<<tokens[i+2]<<"\t";
56  }
57  for(unsigned i=0;i<nParVar;i++)
58  {
59  mParVar.push_back(tokens[nBinVar+3+i]);
60  std::cout<<tokens[nBinVar+3+i]<<"\t";
61  }
62  mFormula = tokens[nParVar+nBinVar+3];
63  std::cout<<tokens[nParVar+nBinVar+3]<<std::endl;
64  }
65 }
66 //------------------------------------------------------------------------
67 //--- METCorrectorParameters::Record constructor -------------------------
68 //--- reads the member variables from a string ---------------------------
69 //------------------------------------------------------------------------
70 METCorrectorParameters::Record::Record(const std::string& fLine,unsigned fNvar) : mMin(0), mMax(0), mParameters(0)
71 {
72  mNvar = fNvar;
73  // quckly parse the line
74  std::vector<std::string> tokens = getTokens(fLine);
75  if (!tokens.empty())
76  {
77  if (tokens.size() < 6)
78  {
79  std::stringstream sserr;
80  sserr<<"(line "<<fLine<<"): "<<"three tokens expected, "<<tokens.size()<<" provided.";
81  handleError("METCorrectorParameters::Record",sserr.str());
82  }
83  std::cout<<"Record ==============="<<std::endl;
84  for(unsigned i=0;i<mNvar;i++)
85  {
86  mMin.push_back(getFloat(tokens[i*2]));
87  mMax.push_back(getFloat(tokens[i*2+1]));
88  std::cout<<tokens[i*2]<<"\t";
89  std::cout<<tokens[i*2+1]<<"\t";
90  }
91  unsigned nParam = getUnsigned(tokens[2*mNvar]);
92  std::cout<<tokens[2*mNvar]<<"\t";
93  if (nParam != tokens.size()-(2*mNvar+1))
94  {
95  std::stringstream sserr;
96  sserr<<"(line "<<fLine<<"): "<<tokens.size()-(2*mNvar+1)<<" parameters, but nParam="<<nParam<<".";
97  handleError("METCorrectorParameters::Record",sserr.str());
98  }
99  for (unsigned i = (2*mNvar+1); i < tokens.size(); ++i)
100  {
101  mParameters.push_back(getFloat(tokens[i]));
102  std::cout<<tokens[i]<<"\t";
103  }
104  std::cout<<std::endl;
105  }
106 }
107 //------------------------------------------------------------------------
108 //--- JetCorrectorParameters constructor ---------------------------------
109 //--- reads the member variables from a string ---------------------------
110 //------------------------------------------------------------------------
112 {
113  std::ifstream input(fFile.c_str());
114  std::string currentSection = "";
116  std::string currentDefinitions = "";
117  while (std::getline(input,line))
118  {
119  //std::cout << " Line of parameters " << line << std::endl;
121  std::string tmp = getDefinitions(line);
122  if (!section.empty() && tmp.empty())
123  {
124  currentSection = section;
125  continue;
126  }
127  if (currentSection == fSection)
128  {
129  if (!tmp.empty())
130  {
131  currentDefinitions = tmp;
132  continue;
133  }
134  Definitions definitions(currentDefinitions);
135  if (!(definitions.nBinVar()==0 && definitions.formula()==""))
138  bool check(true);
139  for(unsigned i=0;i<mDefinitions.nBinVar();++i)
140  if (record.xMin(i)==0 && record.xMax(i)==0)
141  check = false;
142  if (record.nParameters() == 0)
143  check = false;
144  if (check)
145  mRecords.push_back(record);
146  }
147  }
148  if (currentDefinitions=="")
149  handleError("METCorrectorParameters","No definitions found!!!");
150  if (mRecords.empty() && currentSection == "") mRecords.push_back(Record());
151  if (mRecords.empty() && currentSection != "")
152  {
153  std::stringstream sserr;
154  sserr<<"the requested section "<<fSection<<" doesn't exist!";
155  handleError("METCorrectorParameters",sserr.str());
156  }
157  std::sort(mRecords.begin(), mRecords.end());
158  valid_ = true;
159 }
160 //------------------------------------------------------------------------
161 //--- returns the index of the record defined by fX ----------------------
162 //------------------------------------------------------------------------
163 /*
164 int METCorrectorParameters::binIndex(const std::vector<float>& fX) const
165 {
166  int result = -1;
167  unsigned N = mDefinitions.nVar();
168  if (N != fX.size())
169  {
170  std::stringstream sserr;
171  sserr<<"# bin variables "<<N<<" doesn't correspont to requested #: "<<fX.size();
172  handleError("METCorrectorParameters",sserr.str());
173  }
174  unsigned tmp;
175  for (unsigned i = 0; i < size(); ++i)
176  {
177  tmp = 0;
178  for (unsigned j=0;j<N;j++)
179  if (fX[j] >= record(i).xMin(j) && fX[j] < record(i).xMax(j))
180  tmp+=1;
181  if (tmp==N)
182  {
183  result = i;
184  break;
185  }
186  }
187  return result;
188 }
189 */
190 //------------------------------------------------------------------------
191 //--- returns the neighbouring bins of fIndex in the direction of fVar ---
192 //------------------------------------------------------------------------
193 /*
194 int METCorrectorParameters::neighbourBin(unsigned fIndex, unsigned fVar, bool fNext) const
195 {
196  int result = -1;
197  unsigned N = mDefinitions.nVar();
198  if (fVar >= N)
199  {
200  std::stringstream sserr;
201  sserr<<"# of bin variables "<<N<<" doesn't correspond to requested #: "<<fVar;
202  handleError("METCorrectorParameters",sserr.str());
203  }
204  unsigned tmp;
205  for (unsigned i = 0; i < size(); ++i)
206  {
207  tmp = 0;
208  for (unsigned j=0;j<fVar;j++)
209  if (fabs(record(i).xMin(j)-record(fIndex).xMin(j))<0.0001)
210  tmp+=1;
211  for (unsigned j=fVar+1;j<N;j++)
212  if (fabs(record(i).xMin(j)-record(fIndex).xMin(j))<0.0001)
213  tmp+=1;
214  if (tmp<N-1)
215  continue;
216  if (tmp==N-1)
217  {
218  if (fNext)
219  if (fabs(record(i).xMin(fVar)-record(fIndex).xMax(fVar))<0.0001)
220  tmp+=1;
221  if (!fNext)
222  if (fabs(record(i).xMax(fVar)-record(fIndex).xMin(fVar))<0.0001)
223  tmp+=1;
224  }
225  if (tmp==N)
226  {
227  result = i;
228  break;
229  }
230  }
231  return result;
232 }
233 */
234 //------------------------------------------------------------------------
235 //--- returns the number of bins in the direction of fVar ----------------
236 //------------------------------------------------------------------------
237 /*
238 unsigned METCorrectorParameters::size(unsigned fVar) const
239 {
240  if (fVar >= mDefinitions.nVar())
241  {
242  std::stringstream sserr;
243  sserr<<"requested bin variable index "<<fVar<<" is greater than number of variables "<<mDefinitions.nVar();
244  handleError("METCorrectorParameters",sserr.str());
245  }
246  unsigned result = 0;
247  float tmpMin(-9999),tmpMax(-9999);
248  for (unsigned i = 0; i < size(); ++i)
249  if (record(i).xMin(fVar) > tmpMin && record(i).xMax(fVar) > tmpMax)
250  {
251  result++;
252  tmpMin = record(i).xMin(fVar);
253  tmpMax = record(i).xMax(fVar);
254  }
255  return result;
256 }
257 */
258 //------------------------------------------------------------------------
259 //--- returns the vector of bin centers of fVar --------------------------
260 //------------------------------------------------------------------------
261 /*
262 std::vector<float> METCorrectorParameters::binCenters(unsigned fVar) const
263 {
264  std::vector<float> result;
265  for (unsigned i = 0; i < size(); ++i)
266  result.push_back(record(i).xMiddle(fVar));
267  return result;
268 }
269 */
270 //------------------------------------------------------------------------
271 //--- prints parameters on screen ----------------------------------------
272 //------------------------------------------------------------------------
274 {
275  std::cout<<"--------------------------------------------"<<std::endl;
276  std::cout<<"//////// PARAMETERS: //////////////////////"<<std::endl;
277  std::cout<<"--------------------------------------------"<<std::endl;
278  std::cout<<"Number of binning variables: "<<definitions().nBinVar()<<std::endl;
279  std::cout<<"Names of binning variables: ";
280  for(unsigned i=0;i<definitions().nBinVar();i++)
281  std::cout<<definitions().binVar(i)<<" ";
282  std::cout<<std::endl;
283  std::cout<<"--------------------------------------------"<<std::endl;
284  std::cout<<"Number of parameter variables: "<<definitions().nParVar()<<std::endl;
285  std::cout<<"Names of parameter variables: ";
286  for(unsigned i=0;i<definitions().nParVar();i++)
287  std::cout<<definitions().parVar(i)<<" ";
288  std::cout<<std::endl;
289  std::cout<<"--------------------------------------------"<<std::endl;
290  std::cout<<"Parametrization Formula: "<<definitions().formula()<<std::endl;
291  std::cout<<"--------------------------------------------"<<std::endl;
292  std::cout<<"------- Bin contents -----------------------"<<std::endl;
293  for(unsigned i=0;i<size();i++)
294  {
295  for(unsigned j=0;j<definitions().nBinVar();j++)
296  std::cout<<record(i).xMin(j)<<" "<<record(i).xMax(j)<<" ";
297  std::cout<<record(i).nParameters()<<" ";
298  for(unsigned j=0;j<record(i).nParameters();j++)
299  std::cout<<record(i).parameter(j)<<" ";
300  std::cout<<std::endl;
301  }
302 }
303 //------------------------------------------------------------------------
304 //--- prints parameters on file ----------------------------------------
305 //------------------------------------------------------------------------
306 void METCorrectorParameters::printFile(const std::string& fFileName) const
307 {
308  std::ofstream txtFile;
309  txtFile.open(fFileName.c_str());
310  txtFile.setf(std::ios::right);
311  txtFile<<"{"<<definitions().nBinVar()<<std::setw(15);
312  for(unsigned i=0;i<definitions().nBinVar();i++)
313  txtFile<<definitions().binVar(i)<<std::setw(15);
314  txtFile<<definitions().nParVar()<<std::setw(15);
315  for(unsigned i=0;i<definitions().nParVar();i++)
316  txtFile<<definitions().parVar(i)<<std::setw(15);
317  txtFile<<std::setw(definitions().formula().size()+15)<<definitions().formula()<<std::setw(15);
318  txtFile<<"}"<<"\n";
319  for(unsigned i=0;i<size();i++)
320  {
321  for(unsigned j=0;j<definitions().nBinVar();j++)
322  txtFile<<record(i).xMin(j)<<std::setw(15)<<record(i).xMax(j)<<std::setw(15);
323  txtFile<<record(i).nParameters()<<std::setw(15);
324  for(unsigned j=0;j<record(i).nParameters();j++)
325  txtFile<<record(i).parameter(j)<<std::setw(15);
326  txtFile<<"\n";
327  }
328  txtFile.close();
329 }
330 
331 namespace {
332 const std::vector<std::string> labels_ = {
333  "MiniAod"
334 };
335 const std::vector<std::string> MiniAodSource_ = {
336  "MiniAod_ShiftX",
337  "MiniAod_ShiftY"
338 };
339 
340 }//namespace
341 
344  std::cout<<"findLabel with key_type: "<<k<<std::endl;
345  if( isMiniAod(k) )
346  {
347  std::cout<<"is MiniAod"<<std::endl;
348  return findMiniAodSource(k);
349  }
350  return labels_[k];
351 }
354 {
355  if( k == MiniAod) return labels_[MiniAod];
356  else
357  return MiniAodSource_[k - MiniAod*100 -1];
358 }
360  std::vector<std::string> & outputs )
361 {
362  outputs.clear();
363  std::ifstream input( inputFile.c_str() );
364  while( !input.eof() ) {
365  char buff[10000];
366  input.getline(buff,10000);
367  std::string in(buff);
368  if ( in[0] == '[' ) {
369  std::string tok = getSection(in);
370  if ( tok != "" ) {
371  outputs.push_back( tok );
372  }
373  }
374  }
375  std::cout << "Found these sections for file: " << std::endl;
376  copy(outputs.begin(),outputs.end(), std::ostream_iterator<std::string>(std::cout, "\n") );
377 }
378 
379 // Add a METCorrectorParameter object.
381 {
382  std::cout << "i = " << i << std::endl;
383  std::cout << "source = " << source << std::endl;
384  if( isMiniAod(i))
385  {
386  std::cout << "This is MiniAod, getMiniAodBin = " << getMiniAodBin(source) << std::endl;
387  correctionsMiniAod_.push_back( pair_type(getMiniAodBin(source),j) );
388  }else{
389  std::cout << "***** NOT ADDING " << source << ", corresponding position in METCorrectorParameters is not found." << std::endl;
390  }
391 }
392 
393 // Access the METCorrectorParameter via the key k.
394 // key_type is hashed to deal with the three collections
396  collection_type::const_iterator ibegin, iend, i;
397  if ( isMiniAod(k) ) {
398  ibegin = correctionsMiniAod_.begin();
399  iend = correctionsMiniAod_.end();
400  i = ibegin;
401  }
402  for ( ; i != iend; ++i ) {
403  if ( k == i->first ) return i->second;
404  }
405  throw cms::Exception("InvalidInput") << " cannot find key " << static_cast<int>(k)
406  << " in the METC payload, this usually means you have to change the global tag" << std::endl;
407 }
408 
409 // Get a list of valid keys. These will contain hashed keys
410 // that are aware of all three collections.
411 void METCorrectorParametersCollection::validKeys(std::vector<key_type> & keys ) const {
412  keys.clear();
413  for ( collection_type::const_iterator ibegin = correctionsMiniAod_.begin(),
414  iend = correctionsMiniAod_.end(), i = ibegin; i != iend; ++i ) {
415  keys.push_back( i->first );
416  }
417 }
418 
419 
422  std::vector<std::string>::const_iterator found =
423  find( MiniAodSource_.begin(), MiniAodSource_.end(), source );
424  if ( found != MiniAodSource_.end() ) {
425  return (found - MiniAodSource_.begin() + 1)+ MiniAod * 100;
426  }
427  else return MiniAod;
428 }
429 
431  return k == MiniAod ||
432  (k > MiniAod*100 && k < MiniAod*100 + 100);
433 }
434 
436 
JetCorrectorParametersCollection::pair_type pair_type
Definition: classes.h:11
std::vector< std::string > parVar() const
def copy(args, dbName)
const Record & record(unsigned fBin) const
static std::string findMiniAodSource(key_type k)
static void getSections(std::string inputFile, std::vector< std::string > &outputs)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
std::vector< METCorrectorParameters::Record > mRecords
static std::string const input
Definition: EdmProvDump.cc:48
const Definitions & definitions() const
static key_type getMiniAodBin(std::string const &source)
float xMax(unsigned fVar) const
static std::string findLabel(key_type k)
void push_back(key_type i, value_type const &j, label_type const &source="")
METCorrectorParameters const & operator[](key_type k) const
std::vector< std::string > binVar() const
void validKeys(std::vector< key_type > &keys) const
void printFile(const std::string &fFileName) const
METCorrectorParameters::Definitions mDefinitions
int k[5][pyjets_maxn]
#define TYPELOOKUP_DATA_REG(_dataclass_)
Definition: typelookup.h:102
def getSection(rootNode, name)
float xMin(unsigned fVar) const
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
float parameter(unsigned fIndex) const
def check(config)
Definition: trackerTree.py:14
static std::string const source
Definition: EdmProvDump.cc:47