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