CMS 3D CMS Logo

StoreESCondition.cc
Go to the documentation of this file.
2 
8 
9 #include <fstream>
10 #include <iostream>
11 #include <string>
12 #include <cstring>
13 #include <time.h>
14 #include <unistd.h>
15 
16 using std::string;
17 
19 
20  prog_name_ = "StoreESCondition";
21 
22  logfile_ = iConfig.getParameter< std::string >("logfile");
23 
24  esgain_ = iConfig.getParameter< unsigned int >("gain");
25 
26  typedef std::vector< edm::ParameterSet > Parameters;
27  Parameters toPut=iConfig.getParameter<Parameters>("toPut");
28  for (Parameters::iterator itToPut = toPut.begin(); itToPut != toPut.end(); ++itToPut) {
29  inpFileName_.push_back(itToPut->getUntrackedParameter<std::string>("inputFile"));
30  objectName_.push_back(itToPut->getUntrackedParameter<std::string>("conditionType"));
31  since_.push_back(itToPut->getUntrackedParameter<unsigned int>("since"));
32  }
33 
34 }
35 
37 
39  if ( !mydbservice.isAvailable() ) {
40  edm::LogError("StoreESCondition")<<"PoolDBOutputService is unavailable"<< "\n";
41  return;
42  }
43 
44  bool toAppend = false;
45  // copy a string to the char *
46  std::string message = "finished OK\n";
47  size_t messageSize = message.size() + 1;
48  char * messChar = new char [messageSize];
49  strncpy(messChar, message.c_str(), messageSize);
50 
51  for (unsigned int i=0;i<objectName_.size();i++) {
52  cond::Time_t newTime;
53 
54  if ( mydbservice->isNewTagRequest( objectName_[i]+std::string("Rcd") ) ) {
55  // This is the first object for this tag.
56  // Append mode should be off.
57  // newTime is the end of this new objects IOV.
58  newTime = mydbservice->beginOfTime();
59  } else {
60  // There should already be an object in the DB for this tag.
61  // Append IOV mode should be on.
62  // newTime is the beginning of this new objects IOV.
63  toAppend=true;
64  newTime = (cond::Time_t)since_[i];
65  }
66  edm::LogInfo("StoreESCondition") << "Reading " << objectName_[i]
67  << " from file and writing to DB with newTime " << newTime << "\n";
68  if (objectName_[i] == "ESChannelStatus") {
69  edm::LogInfo("StoreESCondition") << " ESChannelStatus file " << inpFileName_[i] << "\n";
71  edm::LogInfo("StoreESCondition") << " ESChannelStatus file read " << "\n";
72  if(!toAppend){
73  edm::LogInfo("StoreESCondition") << " before create " << "\n";
74  mydbservice->createNewIOV<ESChannelStatus>(mycali,newTime,mydbservice->endOfTime(),"ESChannelStatusRcd");
75  edm::LogInfo("StoreESCondition") << " after create " << "\n";
76  }else{
77  edm::LogInfo("StoreESCondition") << " before append " << "\n";
78  mydbservice->appendSinceTime<ESChannelStatus>(mycali,newTime,"ESChannelStatusRcd");
79  edm::LogInfo("StoreESCondition") << " after append " << "\n";
80  }
81  } else if (objectName_[i] == "ESIntercalibConstants") {
83  if(!toAppend){
84  mydbservice->createNewIOV<ESIntercalibConstants>(myintercalib, newTime, mydbservice->endOfTime(), "ESIntercalibConstantsRcd");
85  }else{
86  mydbservice->appendSinceTime<ESIntercalibConstants>(myintercalib, newTime, "ESIntercalibConstantsRcd");
87  }
88  } else if (objectName_[i] == "ESTimeSampleWeights") {
90  if(!toAppend){
91  mydbservice->createNewIOV<ESTimeSampleWeights>(myintercalib, newTime, mydbservice->endOfTime(), "ESTimeSampleWeightsRcd");
92  }else{
93  mydbservice->appendSinceTime<ESTimeSampleWeights>(myintercalib, newTime, "ESTimeSampleWeightsRcd");
94  }
95  } else if (objectName_[i] == "ESGain") {
96  ESGain* myintercalib = readESGainFromFile(inpFileName_[i].c_str());
97  if(!toAppend){
98  mydbservice->createNewIOV<ESGain>(myintercalib, newTime, mydbservice->endOfTime(), "ESGainRcd");
99  }else{
100  mydbservice->appendSinceTime<ESGain>(myintercalib, newTime, "ESGainRcd");
101  }
102  } else if (objectName_[i] == "ESMissingEnergyCalibration") {
104  if(!toAppend){
105  mydbservice->createNewIOV<ESMissingEnergyCalibration>(myintercalib, newTime, mydbservice->endOfTime(), "ESMissingEnergyCalibrationRcd");
106  }else{
107  mydbservice->appendSinceTime<ESMissingEnergyCalibration>(myintercalib, newTime, "ESMissingEnergyCalibrationRcd");
108  }
109  } else if (objectName_[i] == "ESRecHitRatioCuts") {
111  if(!toAppend){
112  mydbservice->createNewIOV<ESRecHitRatioCuts>(myintercalib, newTime, mydbservice->endOfTime(), "ESRecHitRatioCutsRcd");
113  }else{
114  mydbservice->appendSinceTime<ESRecHitRatioCuts>(myintercalib, newTime, "ESRecHitRatioCutsRcd");
115  }
116  } else if (objectName_[i] == "ESThresholds") {
117  ESThresholds* myintercalib = readESThresholdsFromFile(inpFileName_[i].c_str());
118  if(!toAppend){
119  mydbservice->createNewIOV<ESThresholds>(myintercalib, newTime, mydbservice->endOfTime(), "ESThresholdsRcd");
120  }else{
121  mydbservice->appendSinceTime<ESThresholds>(myintercalib, newTime, "ESThresholdsRcd");
122  }
123  } else if (objectName_[i] == "ESPedestals") {
124  ESPedestals* myintercalib = readESPedestalsFromFile(inpFileName_[i].c_str());
125  if(!toAppend){
126  mydbservice->createNewIOV<ESPedestals>(myintercalib, newTime, mydbservice->endOfTime(), "ESPedestalsRcd");
127  }else{
128  mydbservice->appendSinceTime<ESPedestals>(myintercalib, newTime, "ESPedestalsRcd");
129  }
130  } else if (objectName_[i] == "ESEEIntercalibConstants") {
132  if(!toAppend){
133  mydbservice->createNewIOV<ESEEIntercalibConstants>(myintercalib, newTime, mydbservice->endOfTime(), "ESEEIntercalibConstantsRcd");
134  }else{
135  mydbservice->appendSinceTime<ESEEIntercalibConstants>(myintercalib, newTime, "ESEEIntercalibConstantsRcd");
136  }
137  } else {
138  edm::LogError("StoreESCondition")<< "Object " << objectName_[i] << " is not supported by this program." << "\n";
139  }
140  // if more records write here else if ....
141 
142  writeToLogFileResults(messChar);
143 
144  edm::LogInfo("StoreESCondition") << "Finished endJob" << "\n";
145  }
146 
147  delete [] messChar;
148 }
149 
151 
152 }
153 
154 void StoreESCondition::analyze( const edm::Event& evt, const edm::EventSetup& evtSetup) {
155 }
156 
157 void StoreESCondition::writeToLogFile(string a, string b, unsigned long long since) {
158 
159  FILE *outFile; // output log file for appending
160  outFile = fopen(logfile_.c_str(),"a");
161  if (!outFile) {
162  edm::LogError("StoreESCondition") <<"*** Can not open file: " << logfile_;
163  return;
164  }
165  char header[256];
166  fillHeader(header);
167  char appendMode[10];
168  if (since != 0)
169  strcpy(appendMode,"append");
170  else
171  strcpy(appendMode,"create");
172 
173  //fprintf(outFile, "%s %s condition from file %s written into DB for SM %d (mapped to SM %d) in %s mode (since run %u)\n",
174  //header, a.c_str(), b.c_str(), appendMode, (unsigned int)since);
175 
176  fclose(outFile); // close out file
177 
178 }
179 
181 
182  FILE *outFile; // output log file for appending
183  outFile = fopen(logfile_.c_str(),"a");
184  if (!outFile) {
185  edm::LogError("StoreESCondition")<<"*** Can not open file: " << logfile_;
186  return;
187  }
188  char header[256];
189  fillHeader(header);
190  fprintf(outFile, "%s %s\n", header,arg);
191  fclose(outFile); // close out file
192 }
193 
195  time_t rawtime;
196  struct tm * timeinfo;
197  time ( &rawtime );
198  timeinfo = localtime ( &rawtime );
199  char user[50];
200  strcpy(user,getlogin());
201  strcpy(header,asctime(timeinfo));
202  strcpy(header,user);
203 }
204 
206 
207  std::ifstream ESThresholdsFile(edm::FileInPath(inputFile).fullPath().c_str());
208  float ts2, zs; //2nd time sample, ZS threshold
209  ESThresholdsFile >> ts2;
210  ESThresholdsFile >> zs;
211  ESThresholds* esThresholds = new ESThresholds(ts2, zs);
212 
213  return esThresholds;
214 }
215 
217 
218  std::ifstream ESEEIntercalibFile(edm::FileInPath(inputFile).fullPath().c_str());
219  float gammaLow0, alphaLow0, gammaHigh0, alphaHigh0, gammaLow1, alphaLow1, gammaHigh1, alphaHigh1,
220  gammaLow2, alphaLow2, gammaHigh2, alphaHigh2, gammaLow3, alphaLow3, gammaHigh3, alphaHigh3;
221  // const float ESEEIntercalibValue[16];
222  // for (int i = 0; i < 16; ++i) {
223  // ESEEIntercalibFile >> ESEEIntercalibValue[i];
224  // }
225  // ESEEIntercalibConstants* eseeIntercalibConstants = new ESEEIntercalibConstants(ESEEIntercalibValue);
226  ESEEIntercalibFile >> gammaLow0;
227  ESEEIntercalibFile >> alphaLow0;
228  ESEEIntercalibFile >> gammaHigh0;
229  ESEEIntercalibFile >> alphaHigh0;
230  ESEEIntercalibFile >> gammaLow1;
231  ESEEIntercalibFile >> alphaLow1;
232  ESEEIntercalibFile >> gammaHigh1;
233  ESEEIntercalibFile >> alphaHigh1;
234  ESEEIntercalibFile >> gammaLow2;
235  ESEEIntercalibFile >> alphaLow2;
236  ESEEIntercalibFile >> gammaHigh2;
237  ESEEIntercalibFile >> alphaHigh2;
238  ESEEIntercalibFile >> gammaLow3;
239  ESEEIntercalibFile >> alphaLow3;
240  ESEEIntercalibFile >> gammaHigh3;
241  ESEEIntercalibFile >> alphaHigh3;
242  ESEEIntercalibConstants* eseeIntercalibConstants = new ESEEIntercalibConstants(gammaLow0, alphaLow0, gammaHigh0, alphaHigh0,
243  gammaLow1, alphaLow1, gammaHigh1, alphaHigh1,
244  gammaLow2, alphaLow2, gammaHigh2, alphaHigh2,
245  gammaLow3, alphaLow3, gammaHigh3, alphaHigh3);
246 
247  return eseeIntercalibConstants;
248 
249 }
250 
252 
253  std::ifstream ESMissingEnergyFile(edm::FileInPath(inputFile).fullPath().c_str());
254  float ConstAEta0, ConstBEta0, ConstAEta1, ConstBEta1, ConstAEta2, ConstBEta2, ConstAEta3, ConstBEta3;
255  ESMissingEnergyFile >> ConstAEta0;
256  ESMissingEnergyFile >> ConstBEta0;
257  ESMissingEnergyFile >> ConstAEta1;
258  ESMissingEnergyFile >> ConstBEta1;
259  ESMissingEnergyFile >> ConstAEta2;
260  ESMissingEnergyFile >> ConstBEta2;
261  ESMissingEnergyFile >> ConstAEta3;
262  ESMissingEnergyFile >> ConstBEta3;
263  ESMissingEnergyCalibration* esMissingEnergy = new ESMissingEnergyCalibration(ConstAEta0, ConstBEta0, ConstAEta1, ConstBEta1,
264  ConstAEta2, ConstBEta2, ConstAEta3, ConstBEta3);
265 
266  return esMissingEnergy;
267 }
268 
270 
271  ESPedestals* esPedestals = new ESPedestals();
272 
273  // int ped[2][2][40][40][32];
274  // for (int i=0; i<2; ++i)
275  // for (int j=0; j<2; ++j)
276  // for (int k=0; k<40; ++k)
277  // for (int m=0; m<40; ++m)
278  // for (int n=0; n<32; ++n)
279  // ped[i][j][k][m][n] = 0;
280 
282 
283  int iz, ip, ix, iy, is, ped_, zside;
284  std::ifstream pedestalFile(edm::FileInPath(inputFile).fullPath().c_str());
285 
286  for (int i=0; i<137216; ++i) {
287  pedestalFile >> iz >> ip >> ix >> iy >> is >> ped_;
288 
289  zside = (iz==-1) ? 1 : 0;
290  ped[zside][ip-1][ix-1][iy-1][is-1] = ped_;
291  }
292 
293  for (int iz=-1; iz<=1; ++iz) {
294 
295  if (iz==0) continue;
296  zside = (iz==-1) ? 1 : 0;
297 
298  for (int iplane=ESDetId::PLANE_MIN; iplane<=ESDetId::PLANE_MAX; ++iplane)
299  for (int ix=ESDetId::IX_MIN; ix<=ESDetId::IX_MAX; ++ix)
300  for (int iy=ESDetId::IY_MIN; iy<=ESDetId::IY_MAX; ++iy)
301  for (int istrip=ESDetId::ISTRIP_MIN; istrip<=ESDetId::ISTRIP_MAX; ++istrip) {
302 
303  ESPedestals::Item ESitem;
304  ESitem.mean = ped[zside][iplane-1][ix-1][iy-1][istrip-1];
305  ESitem.rms = 3; // LG : 3, HG : 6
306 
307  if(ESDetId::validDetId(istrip, ix, iy, iplane, iz)) {
308  ESDetId esId(istrip, ix, iy, iplane, iz);
309  esPedestals->insert(std::make_pair(esId.rawId(), ESitem));
310  }
311  }
312  }
313 
314  return esPedestals;
315 }
316 
318 
319  std::ifstream ESRecHitRatioCutsFile(edm::FileInPath(inputFile).fullPath().c_str());
320 
321  float r12Low, r23Low, r12High, r23High;
322  ESRecHitRatioCutsFile >> r12Low;
323  ESRecHitRatioCutsFile >> r23Low;
324  ESRecHitRatioCutsFile >> r12High;
325  ESRecHitRatioCutsFile >> r23High;
326  ESRecHitRatioCuts* esRecHitRatioCuts= new ESRecHitRatioCuts(r12Low, r23Low, r12High, r23High);
327  // cout<<"We are in RH ratio cut : gain : " << esgain_<<endl;
328 
329  // HG (R12Low, R23Low, R12High, R23High)
330  // ESRecHitRatioCuts* esRecHitRatioCuts;
331  //if (esgain_ == 2) esRecHitRatioCuts = new ESRecHitRatioCuts(-99999., 0.24, 0.61, 2.23);
332  // if (esgain_ == 2) esRecHitRatioCuts = new ESRecHitRatioCuts(-99999., 0.61, 0.24, 2.23); // HG
333  // else esRecHitRatioCuts = new ESRecHitRatioCuts(-99999., 0.2, 0.39, 2.8);
334  // LG (R12Low, R23Low, R12High, R23High)
335  //ESRecHitRatioCuts* esRecHitRatioCuts = new ESRecHitRatioCuts(-99999., 99999., -99999., 99999.); // first running
336  //ESRecHitRatioCuts* esRecHitRatioCuts = new ESRecHitRatioCuts(-99999., 0.2, 0.39, 2.8);
337 
338  return esRecHitRatioCuts;
339 }
340 
342 
343  std::ifstream amplFile(edm::FileInPath(inputFile).fullPath().c_str());
344 
345  int gain;
346  amplFile >> gain;
347  edm::LogInfo("StoreESCondition") << "gain : "<< gain << "\n";
348 
349  ESGain* esGain = new ESGain(gain); // 1: LG, 2: HG
350  return esGain;
351 }
352 
354 
355  std::ifstream amplFile(edm::FileInPath(inputFile).fullPath().c_str());
356 
357  float w[3];
358  for (int k = 0; k < 3; ++k) {
359  float ww;
360  amplFile >> ww;
361  w[k] = ww;
362  edm::LogInfo("StoreESCondition") <<"weight : "<<k<<" "<<w[k]<< "\n";
363  }
364 
365  ESTimeSampleWeights* esWeights = new ESTimeSampleWeights(w[0], w[1], w[2]);
366  return esWeights;
367 
368 }
369 
371 
373 
374  std::ifstream mipFile(edm::FileInPath(inputFile).fullPath().c_str());
375 
376  for (int i=0; i<137216; ++i) {
377  int iz, ip, ix, iy, is;
378  double mip;
379  mipFile >> iz >> ip >> ix >> iy >> is >> mip;
380  //if (mip <20 || mip> 70) cout<<iz<<" "<<ip<<" "<<ix<<" "<<iy<<" "<<is<<" "<<mip<<endl; // HG
381  // LG : HG MIP/6/1.14
382  //mip = mip/6/1.14;
383  // LG : HG MIP/6
384  if (esgain_ == 1) mip = mip/6.; // LG
385  if (mip <20 || mip> 70) edm::LogInfo("StoreESCondition") <<iz<<" "<<ip<<" "<<ix<<" "<<iy<<" "<<is<<" "<<mip<< "\n"; // LG
386 
387  if(ESDetId::validDetId(is, ix, iy, ip, iz)) {
388  ESDetId esId(is, ix, iy, ip, iz);
389  ical->setValue(esId.rawId(), mip);
390  }
391  }
392 
393  return ical;
394 }
395 
397 
398 
399  int z[1000], p[1000], x[1000], y[1000], nsensors;
400  std::ifstream statusFile(edm::FileInPath(inputFile).fullPath().c_str());
401  statusFile >> nsensors;
402  edm::LogInfo("StoreESCondition") << " nsensors " << nsensors << "\n";
403  if(nsensors >= 1000) {
404  edm::LogInfo("StoreESCondition") << " *** value too high, modify the method!***" << "\n";
405  exit(-1);
406  }
407  for (int i = 0; i < nsensors; ++i) {
408  statusFile >> z[i] >> p[i] >> x[i] >> y[i];
409  }
410  ESChannelStatus* ecalStatus = new ESChannelStatus();
411  int Nbstatus = 0, Nbstrip = 0;
412  for (int istrip=ESDetId::ISTRIP_MIN;istrip<=ESDetId::ISTRIP_MAX;istrip++) {
413  for (int ix=ESDetId::IX_MIN;ix<=ESDetId::IX_MAX;ix++) {
414  for (int iy=ESDetId::IY_MIN;iy<=ESDetId::IY_MAX;iy++) {
415  for (int iplane = 1; iplane <= 2; iplane++) {
416  for (int izeta = -1; izeta <= 1; izeta = izeta + 2) {
417  // if (izeta==0) continue;
418  // try {
419 
420  //ESDetId Plane iplane Zside izeta
421  // if(!ESDetId::validDetId(istrip,ix,iy,iplane,izeta)) cout << " Unvalid DetId" << endl;
422  // else {
423  if(ESDetId::validDetId(istrip,ix,iy,iplane,izeta)) {
424  ESDetId anESId(istrip,ix,iy,iplane,izeta);
425  int status = 0;
426  // std::ifstream statusFile(edm::FileInPath(inputFile).fullPath().c_str());
427  Nbstrip++;
428  for (int i=0; i<nsensors; ++i) {
429  if (izeta == z[i] && iplane == p[i] && ix == x[i] && iy == y[i]) status = 1;
430  }
431  if(status == 1) {
432  Nbstatus++;
433  if(istrip == 1) edm::LogInfo("StoreESCondition") << " Bad channel ix " << ix << " iy " << iy << " iplane " << iplane << " iz " << izeta << "\n"; // print only once
434  }
435  ecalStatus->setValue(anESId, status);
436  // statusFile.close();
437  } // valid DetId
438  // catch ( cms::Exception &e ) { }
439  } // loop over z
440  } // loop over plane
441  } // loop over y
442  } // loop over x
443  } // loop over strips
444  edm::LogInfo("StoreESCondition") << " Nb of strips " << Nbstrip << " Number of bad channels " << Nbstatus << "\n";
445  statusFile.close();
446 
447  // overwrite the statuses which are in the file
448  return ecalStatus;
449 
450 }
451 
T getParameter(std::string const &) const
ESPedestals * readESPedestalsFromFile(const char *)
const double w
Definition: UKUtility.cc:23
Definition: ESGain.h:7
static const int IY_MAX
Definition: ESDetId.h:74
std::vector< unsigned long long > since_
static const int ISTRIP_MAX
Definition: ESDetId.h:76
ESGain * readESGainFromFile(const char *)
std::vector< std::string > inpFileName_
A arg
Definition: Factorize.h:36
ESPedestalsMap ESPedestals
Definition: ESPedestals.h:32
static const int PLANE_MIN
Definition: ESDetId.h:77
std::string logfile_
ESChannelStatusMap ESChannelStatus
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
void appendSinceTime(T *payloadObj, cond::Time_t sinceTime, const std::string &recordName, bool withlogging=false)
unsigned long long Time_t
Definition: Time.h:16
vector< ParameterSet > Parameters
static const int PLANE_MAX
Definition: ESDetId.h:78
bool isNewTagRequest(const std::string &recordName)
void insert(std::pair< uint32_t, Item > const &a)
void setValue(const uint32_t id, const Item &item)
ESTimeSampleWeights * readESTimeSampleWeightsFromFile(const char *)
bool isAvailable() const
Definition: Service.h:46
ESRecHitRatioCuts * readESRecHitRatioCutsFromFile(const char *)
float mean
Definition: ESPedestals.h:14
void writeToLogFileResults(char *)
void createNewIOV(T *firstPayloadObj, cond::Time_t firstSinceTime, cond::Time_t firstTillTime, const std::string &recordName, bool withlogging=false)
void writeToLogFile(std::string, std::string, unsigned long long)
ESEEIntercalibConstants * readESEEIntercalibConstantsFromFile(const char *)
int k[5][pyjets_maxn]
static const int IY_MIN
Definition: ESDetId.h:72
static const int IZ_NUM
Definition: ESDetId.h:79
ESIntercalibConstantMap ESIntercalibConstants
static bool validDetId(int istrip, int ixs, int iys, int iplane, int iz)
check if a valid index combination
Definition: ESDetId.cc:22
double b
Definition: hdecay.h:120
unsigned int esgain_
static const int IX_MAX
Definition: ESDetId.h:73
float rms
Definition: ESPedestals.h:15
ESMissingEnergyCalibration * readESMissingEnergyFromFile(const char *)
double a
Definition: hdecay.h:121
std::vector< std::string > objectName_
ESThresholds * readESThresholdsFromFile(const char *)
void fillHeader(char *)
virtual void endJob()
ESIntercalibConstants * readESIntercalibConstantsFromFile(const char *)
std::string prog_name_
virtual void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup)
ESChannelStatus * readESChannelStatusFromFile(const char *)
StoreESCondition(const edm::ParameterSet &iConfig)
static const int ISTRIP_MIN
Definition: ESDetId.h:75
static const int IX_MIN
Definition: ESDetId.h:71