CMS 3D CMS Logo

CaloMeanResponse.cc
Go to the documentation of this file.
3 
4 #include <CLHEP/Units/SystemOfUnits.h>
5 using CLHEP::GeV;
6 
7 #include <iostream>
8 #include <fstream>
9 #include <iomanip>
10 
11 //#define EDM_ML_DEBUG
12 
14  edm::ParameterSet m_p = p.getParameter<edm::ParameterSet>("CaloResponse");
15  edm::FileInPath fp = m_p.getParameter<edm::FileInPath>("ResponseFile");
16  std::string fName = fp.fullPath();
17  useTable = m_p.getParameter<bool>("UseResponseTable");
18  scale = m_p.getParameter<double>("ResponseScale");
19  edm::LogVerbatim("CaloSim") << "CaloMeanResponse initialized with scale " << scale << " and use Table " << useTable
20  << " from file " << fName;
22 }
23 
25 
26 double CaloMeanResponse::getWeight(int genPID, double genP) {
27  double weight = 1;
28  bool found = false;
29  for (unsigned int i = 0; i < pionTypes.size(); i++) {
30  if (genPID == pionTypes[i]) {
31  found = true;
32  break;
33  }
34  }
35  if (found) {
36  weight = scale;
37  if (useTable) {
38  if (piLast >= 0)
40  for (unsigned int i = 0; i < pionTable.size(); i++) {
41  if (genP < pionMomentum[i]) {
42  if (i == 0)
43  weight = scale * pionTable[i];
44  else
45  weight =
46  scale * ((pionTable[i - 1] * (pionMomentum[i] - genP) + pionTable[i] * (genP - pionMomentum[i - 1])) /
47  (pionMomentum[i] - pionMomentum[i - 1]));
48  break;
49  }
50  }
51  }
52 #ifdef EDM_ML_DEBUG
53  edm::LogVerbatim("CaloSim") << "CaloMeanResponse::getWeight PID " << genPID << " uses pion list and gets weight "
54  << weight << " for momentum " << genP / GeV << " GeV/c";
55 #endif
56  } else {
57  for (unsigned int i = 0; i < protonTypes.size(); i++) {
58  if (genPID == protonTypes[i]) {
59  found = true;
60  break;
61  }
62  }
63  if (found) {
64  weight = scale;
65  if (useTable) {
66  if (pLast >= 0)
68  for (unsigned int i = 0; i < protonTable.size(); i++) {
69  if (genP < protonMomentum[i]) {
70  if (i == 0)
72  else
73  weight =
74  scale *
75  ((protonTable[i - 1] * (protonMomentum[i] - genP) + protonTable[i] * (genP - protonMomentum[i - 1])) /
76  (protonMomentum[i] - protonMomentum[i - 1]));
77  break;
78  }
79  }
80  }
81 #ifdef EDM_ML_DEBUG
82  edm::LogVerbatim("CaloSim") << "CaloMeanResponse::getWeight PID " << genPID << " uses proton list and gets "
83  << "weight " << weight << " for momentum " << genP / GeV << " GeV/c";
84 #endif
85  } else {
86 #ifdef EDM_ML_DEBUG
87  edm::LogVerbatim("CaloSim") << "CaloMeanResponse::getWeight PID " << genPID << " is not in either lists and "
88  << "weight " << weight;
89 #endif
90  }
91  }
92  return weight;
93 }
94 
96  std::ifstream infile;
97  infile.open(fName.c_str(), std::ios::in);
98 
99  if (infile) {
100  int nene, npart, pid;
101  double ene, responseData, responseMC, ratio;
102 
103  // First read the pion data
104  infile >> nene >> npart;
105  for (int i = 0; i < npart; i++) {
106  infile >> pid;
107  pionTypes.push_back(pid);
108  }
109  for (int i = 0; i < nene; i++) {
110  infile >> ene >> responseData >> responseMC;
111  if (responseMC > 0)
112  ratio = responseData / responseMC;
113  else
114  ratio = 1;
115  pionMomentum.push_back(ene * GeV);
116  pionTable.push_back(ratio);
117  }
118 
119  // Then read the proton data
120  infile >> nene >> npart;
121  for (int i = 0; i < npart; i++) {
122  infile >> pid;
123  protonTypes.push_back(pid);
124  }
125  for (int i = 0; i < nene; i++) {
126  infile >> ene >> responseData >> responseMC;
127  if (responseMC > 0)
128  ratio = responseData / responseMC;
129  else
130  ratio = 1;
131  protonMomentum.push_back(ene * GeV);
132  protonTable.push_back(ratio);
133  }
134  infile.close();
135  }
136 
137  piLast = (int)(pionTable.size()) - 1;
138  pLast = (int)(protonTable.size()) - 1;
139 #ifdef EDM_ML_DEBUG
140  edm::LogVerbatim("CaloSim") << "CaloMeanResponse::readResponse finds " << pionTypes.size()
141  << " particles to use pion "
142  << "response map with a table of " << pionTable.size() << " data points " << piLast;
143  for (unsigned int i = 0; i < pionTypes.size(); i++)
144  edm::LogVerbatim("CaloSim") << "Particle ID[" << i << "] = " << pionTypes[i];
145  for (unsigned int i = 0; i < pionTable.size(); i++)
146  edm::LogVerbatim("CaloSim") << "Momentum[" << i << "] (" << pionMomentum[i] / GeV << " GeV/c) --> " << pionTable[i];
147  edm::LogVerbatim("CaloSim") << "CaloMeanResponse::readResponse finds " << protonTypes.size() << " particles to use "
148  << "proton response map with a table of " << protonTable.size() << " data points "
149  << pLast;
150  for (unsigned int i = 0; i < protonTypes.size(); i++)
151  edm::LogVerbatim("CaloSim") << "Particle ID[" << i << "] = " << protonTypes[i];
152  for (unsigned int i = 0; i < protonTable.size(); i++)
153  edm::LogVerbatim("CaloSim") << "Momentum[" << i << "] (" << protonMomentum[i] / GeV << " GeV/c) --> "
154  << protonTable[i];
155 #endif
156 }
Log< level::Info, true > LogVerbatim
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void readResponse(std::string fName)
std::vector< int > protonTypes
Definition: weight.py:1
double npart
Definition: HydjetWrapper.h:48
virtual ~CaloMeanResponse()
double getWeight(int genPID, double genP)
std::vector< double > protonMomentum
std::vector< double > pionTable
std::vector< int > pionTypes
CaloMeanResponse(edm::ParameterSet const &p)
std::vector< double > protonTable
std::vector< double > pionMomentum