CMS 3D CMS Logo

JetCorrFactors.cc
Go to the documentation of this file.
1 #include <iomanip>
2 #include <sstream>
3 #include <iostream>
4 #include <algorithm>
5 
9 
10 
11 using namespace pat;
12 
13 
14 JetCorrFactors::JetCorrFactors(const std::string& label, const std::vector<CorrectionFactor>& jec): label_(label), jec_(jec)
15 {
16  for(std::vector<CorrectionFactor>::const_iterator corrFactor=jec.begin(); corrFactor!=jec.end(); ++corrFactor){
17  if (!isValid(*corrFactor))
18  invalidFactor();
19  }
20 }
21 
22 void JetCorrFactors::insertFactor(const unsigned int& position, const CorrectionFactor& corrFactor) {
23  if (!isValid(corrFactor))
24  invalidFactor();
25  jec_.insert(jec_.begin() + position, corrFactor);
26 }
27 
29 JetCorrFactors::jecFlavor(const Flavor& flavor) const
30 {
31  std::map<Flavor, std::string> flavors;
32  flavors[UDS]="uds"; flavors[CHARM]="charm"; flavors[BOTTOM]="bottom"; flavors[GLUON]="gluon"; flavors[NONE]="none";
33  return flavors.find(flavor)->second;
34 }
35 
38 {
39  std::map<std::string, Flavor> flavors;
40  std::transform(flavor.begin(), flavor.end(), flavor.begin(), [&](int c){ return std::tolower(c);} );
41  flavors["uds"]=UDS; flavors["charm"]=CHARM; flavors["bottom"]=BOTTOM; flavors["gluon"]=GLUON; flavors["none"]=NONE;
42  if(flavors.find(flavor)==flavors.end()){
43  throw cms::Exception("InvalidRequest") << "You ask for a flavor, which does not exist. Available flavors are: \n"
44  << "'uds', 'charm', 'bottom', 'gluon', 'none', (not case sensitive). \n";
45  }
46  return flavors.find(flavor)->second;
47 }
48 
49 int
51 {
52  for(std::vector<CorrectionFactor>::const_iterator corrFactor=jec_.begin(); corrFactor!=jec_.end(); ++corrFactor){
53  if(corrFactor->first==level) return (corrFactor-jec_.begin());
54  }
55  return -1;
56 }
57 
58 float
59 JetCorrFactors::correction(unsigned int level, Flavor flavor) const
60 {
61  if(!(level<jec_.size())){
62  throw cms::Exception("InvalidRequest") << "You try to call a jet energy correction level wich does not exist. \n"
63  << "Available jet energy correction levels are: \n"
65  }
66  if(flavorDependent(jec_.at(level)) && flavor==NONE){
67  throw cms::Exception("InvalidRequest") << "You try to call a flavor dependent jet energy correction level: \n"
68  << "level : " << level << " label: " << jec_.at(level).first << " \n"
69  << "You need to specify one of the following flavors: GLUON, UDS, \n"
70  << "CHARM, BOTTOM. \n";
71  }
72  return flavorDependent(jec_.at(level)) ? jec_.at(level).second.at(flavor) : jec_.at(level).second.at(0);
73 }
74 
77 {
79  for(std::vector<CorrectionFactor>::const_iterator corrFactor=jec_.begin(); corrFactor!=jec_.end(); ++corrFactor){
80  std::stringstream idx; idx << (corrFactor-jec_.begin());
81  labels.append(idx.str()).append(" ").append(corrFactor->first).append("\n");
82  }
83  return labels;
84 }
85 
86 std::vector<std::string>
88 {
89  std::vector<std::string> labels;
90  for(std::vector<CorrectionFactor>::const_iterator corrFactor=jec_.begin(); corrFactor!=jec_.end(); ++corrFactor){
91  labels.push_back(corrFactor->first);
92  }
93  return labels;
94 }
95 
96 void
98 {
99  edm::LogInfo message( "JetCorrFactors" );
100  for(std::vector<CorrectionFactor>::const_iterator corrFactor=jec_.begin(); corrFactor!=jec_.end(); ++corrFactor){
101  unsigned int corrFactorIdx=corrFactor-jec_.begin();
102  message << std::setw(3) << corrFactorIdx << " " << corrFactor->first;
103  if( flavorDependent(*corrFactor) ){
104  for(std::vector<float>::const_iterator flavor=corrFactor->second.begin(); flavor!=corrFactor->second.end(); ++flavor){
105  unsigned int flavorIdx=flavor-corrFactor->second.begin();
106  message << std::setw(10) << correction(corrFactorIdx, (Flavor)flavorIdx);
107  }
108  }
109  else{
110  message << std::setw(10) << correction (corrFactor-jec_.begin(), NONE);
111  }
112  message << "\n";
113  }
114 }
115 
117  throw cms::Exception("InvalidRequest")
118  << "You try to create a CorrectionFactor which is neither flavor dependent nor \n"
119  << "flavor independent. The CorrectionFactor should obey the following rules: \n"
120  << "\n"
121  << " * CorrectionFactor is a std::pair<std::string, std::vector<float> >. \n"
122  << " * The std::string holds the label of the correction level (following the \n"
123  << " conventions of JetMET. \n"
124  << " * The std::vector<float> holds the correction factors, these factors are \n"
125  << " up to the given level. They include all previous correction steps. \n"
126  << " * The vector has the length *1* for flavor independent correction factors \n"
127  << " or *5* for flavor dependent correction factors. \n"
128  << " * The expected order of flavor dependent correction factors is: NONE, \n"
129  << " GLUON, UDS, CHARM, BOTTOM. If follows the JetMET conventions and is \n"
130  << " in the Flavor enumerator of the JetCorrFactos class. \n"
131  << " * For flavor depdendent correction factors the first entry in the vector \n"
132  << " (corresponding to NONE) is invalid and should be set to -1. It will not \n"
133  << " be considered by the class structure though. \n"
134  << "\n"
135  << "Make sure that all elements of the argument vector to this contructor are \n"
136  << "in accordance with these rules.\n";
137 }
float correction(unsigned int level, Flavor flavor=NONE) const
std::vector< std::string > correctionLabels() const
std::string jecLevel(const unsigned int &level) const
std::vector< CorrectionFactor > jec_
void insertFactor(const unsigned int &position, const CorrectionFactor &factor)
Definition: HeavyIon.h:7
char const * label
void print() const
std::string correctionLabelString() const
bool isValid(const CorrectionFactor &jec) const
std::pair< std::string, std::vector< float > > CorrectionFactor
std::string jecFlavor(const Flavor &flavor) const
void invalidFactor() const
static int position[264][3]
Definition: ReadPGInfo.cc:509
bool flavorDependent(unsigned int level) const