CMS 3D CMS Logo

UETableProducer.cc
Go to the documentation of this file.
1 // system include files
2 #include <iostream>
3 #include <fstream>
4 #include <vector>
5 #include <string>
6 #include <algorithm>
7 
8 // user include files
11 
17 
19 
23 
24 // For DB entry using JetCorrector to store the vector of float
26 
28 
29 using namespace std;
30 //
31 // class decleration
32 //
33 
34 namespace {
35  class UETableProducer : public edm::one::EDAnalyzer<> {
36  public:
37  explicit UETableProducer(const edm::ParameterSet&);
38  ~UETableProducer() override;
39 
40  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
41 
42  private:
43  void beginJob() override {}
44  void analyze(const edm::Event&, const edm::EventSetup&) override;
45  void endJob() override;
46 
47  // ----------member data ---------------------------
48 
49  bool debug_;
50  bool jetCorrectorFormat_;
51 
52  string calibrationFile_;
53  unsigned int runnum_;
54 
55  unsigned int index = 0, np[5], ni0[2], ni1[2], ni2[2];
56 
57  // ue_interpolation_pf0[15][344],
58  // ue_interpolation_pf1[15][344],
59  // ue_interpolation_pf2[15][82];
60  };
61 } // namespace
62 //
63 // constants, enums and typedefs
64 //
65 
66 //
67 // static data member definitions
68 //
69 
70 //
71 // constructors and destructor
72 //
73 UETableProducer::UETableProducer(const edm::ParameterSet& iConfig) : runnum_(0) {
74  //now do what ever initialization is needed
75  calibrationFile_ = iConfig.getParameter<std::string>("txtFile");
76  //calibrationFile_ = "RecoHI/HiJetAlgos/data/ue_calibrations_pf_data.txt";
77 
78  debug_ = iConfig.getUntrackedParameter<bool>("debug", false);
79  jetCorrectorFormat_ = iConfig.getUntrackedParameter<bool>("jetCorrectorFormat", false);
80 
81  np[0] = 3; // Number of reduced PF ID (track, ECAL, HCAL)
82  np[1] = 15; // Number of pseudorapidity block
83  np[2] = 5; // Fourier series order
84  np[3] = 2; // Re or Im
85  np[4] = 82; // Number of feature parameter
86 
87  ni0[0] = np[1];
88  ni0[1] = 344; // Number of track binning (kept = ECAL)
89 
90  ni1[0] = np[1];
91  ni1[1] = 344; // Number of ECAL binning
92 
93  ni2[0] = np[1];
94  ni2[1] = 82; // Number of HCAL binning
95 }
96 
97 UETableProducer::~UETableProducer() {
98  // do anything here that needs to be done at desctruction time
99  // (e.g. close files, deallocate resources etc.)
100 }
101 
102 //
103 // member functions
104 //
105 
106 // ------------ method called to for each event ------------
107 void UETableProducer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
108  // nothing
109 }
110 
111 // ------------ method called once each job just after ending the event loop ------------
112 void UETableProducer::endJob() {
113  std::string qpDataName = calibrationFile_;
114  std::ifstream textTable_(qpDataName.c_str());
115 
116  std::vector<float> ue_vec;
117  std::unique_ptr<UETable> ue_predictor_pf;
118 
119  if (!jetCorrectorFormat_) {
120  ue_predictor_pf = std::make_unique<UETable>();
121  }
122  // unsigned int Nnp_full = np[0] * np[1] * np[2] * np[3] * np[4];
123  unsigned int Nnp = np[0] * np[1] * (1 + (np[2] - 1) * np[3]) * np[4];
124  unsigned int Nni0 = ni0[0] * ni0[1];
125  unsigned int Nni1 = ni1[0] * ni1[1];
126  unsigned int Nni2 = ni2[0] * ni2[1];
127 
128  if (!jetCorrectorFormat_) {
129  ue_predictor_pf->np.resize(5);
130  ue_predictor_pf->ni0.resize(2);
131  ue_predictor_pf->ni1.resize(2);
132  ue_predictor_pf->ni2.resize(2);
133 
134  std::copy(np, np + 5, ue_predictor_pf->np.begin());
135  std::copy(ni0, ni0 + 2, ue_predictor_pf->ni0.begin());
136  std::copy(ni1, ni1 + 2, ue_predictor_pf->ni1.begin());
137  std::copy(ni2, ni2 + 2, ue_predictor_pf->ni2.begin());
138 
139  static const float edge_pseudorapidity[16] = {-5.191,
140  -2.650,
141  -2.043,
142  -1.740,
143  -1.479,
144  -1.131,
145  -0.783,
146  -0.522,
147  0.522,
148  0.783,
149  1.131,
150  1.479,
151  1.740,
152  2.043,
153  2.650,
154  5.191};
155 
156  ue_predictor_pf->edgeEta.resize(16);
157 
158  std::copy(edge_pseudorapidity, edge_pseudorapidity + 16, ue_predictor_pf->edgeEta.begin());
159  }
160 
162 
163  while (std::getline(textTable_, line)) {
164  if (line.empty() || line[0] == '#') {
165  std::cout << " continue " << std::endl;
166  continue;
167  }
168  std::istringstream linestream(line);
169  float val;
170  int bin0, bin1, bin2, bin3, bin4;
171  if (index < Nnp) {
172  linestream >> bin0 >> bin1 >> bin2 >> bin3 >> bin4 >> val;
173  ue_vec.push_back(val);
174  } else if (index < Nnp + Nni0) {
175  linestream >> bin0 >> bin1 >> val;
176  ue_vec.push_back(val);
177  } else if (index < Nnp + Nni0 + Nni1) {
178  linestream >> bin0 >> bin1 >> val;
179  ue_vec.push_back(val);
180  } else if (index < Nnp + Nni0 + Nni1 + Nni2) {
181  linestream >> bin0 >> bin1 >> val;
182  ue_vec.push_back(val);
183  }
184  ++index;
185  }
186 
188 
189  if (pool.isAvailable()) {
190  if (jetCorrectorFormat_) {
191  // A minimal dummy line that satisfies the JME # token >= 6 requirement, and has the correct key type
192  JetCorrectorParameters::Definitions definition("1 0 0 0 Correction L1Offset");
193  std::vector<JetCorrectorParameters::Record> record(
194  1,
196  ue_vec.size(), std::vector<float>(ue_vec.size(), 0), std::vector<float>(ue_vec.size(), 0), ue_vec));
198 
199  std::unique_ptr<JetCorrectorParametersCollection> jme_payload =
200  std::make_unique<JetCorrectorParametersCollection>();
201 
202  jme_payload->push_back(JetCorrectorParametersCollection::L1Offset, parameter);
203 
204  if (pool->isNewTagRequest("JetCorrectionsRecord")) {
205  pool->createOneIOV<JetCorrectorParametersCollection>(*jme_payload, pool->beginOfTime(), "JetCorrectionsRecord");
206  } else {
207  pool->appendOneIOV<JetCorrectorParametersCollection>(*jme_payload, pool->currentTime(), "JetCorrectionsRecord");
208  }
209  } else {
210  ue_predictor_pf->values = ue_vec;
211 
212  if (pool->isNewTagRequest("HeavyIonUERcd")) {
213  pool->createOneIOV<UETable>(*ue_predictor_pf, pool->beginOfTime(), "HeavyIonUERcd");
214  } else {
215  pool->appendOneIOV<UETable>(*ue_predictor_pf, pool->currentTime(), "HeavyIonUERcd");
216  }
217  }
218  }
219 }
220 
223  desc.add<std::string>("txtFile", "example");
224  desc.addUntracked<bool>("debug", false);
225  desc.addUntracked<bool>("jetCorrectorFormat", false);
226  descriptions.add("produceUETable", desc);
227 }
228 
229 //define this as a plug-in
230 DEFINE_FWK_MODULE(UETableProducer);
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
void beginJob()
Definition: Breakpoints.cc:14
T getUntrackedParameter(std::string const &, T const &) const
int iEvent
Definition: GenABIO.cc:224
int np
Definition: AMPTWrapper.h:43
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: UETable.h:7