CMS 3D CMS Logo

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