Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:33

0001 #ifndef _CSCGAINSDBCONDITIONS_H
0002 #define _CSCGAINSDBCONDITIONS_H
0003 
0004 #include "FWCore/Framework/interface/ESHandle.h"
0005 #include "FWCore/Framework/interface/ESProducer.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0009 #include "FWCore/Framework/interface/Frameworkfwd.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "FWCore/Framework/interface/SourceFactory.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include <cmath>
0014 #include <memory>
0015 
0016 #include "CondFormats/CSCObjects/interface/CSCDBGains.h"
0017 #include "CondFormats/DataRecord/interface/CSCDBGainsRcd.h"
0018 #include <DataFormats/MuonDetId/interface/CSCDetId.h>
0019 
0020 class CSCGainsDBConditions : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0021 public:
0022   CSCGainsDBConditions(const edm::ParameterSet &);
0023   ~CSCGainsDBConditions() override;
0024 
0025   inline static CSCDBGains *prefillDBGains();
0026 
0027   typedef std::unique_ptr<CSCDBGains> ReturnType;
0028 
0029   ReturnType produceDBGains(const CSCDBGainsRcd &);
0030 
0031 private:
0032   // ----------member data ---------------------------
0033   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
0034                       const edm::IOVSyncValue &,
0035                       edm::ValidityInterval &) override;
0036   CSCDBGains *cndbGains;
0037 };
0038 
0039 #include <fstream>
0040 #include <iostream>
0041 #include <vector>
0042 
0043 // to workaround plugin library
0044 inline CSCDBGains *CSCGainsDBConditions::prefillDBGains() {
0045   // const int MAX_SIZE = 273024;//for ME1a unganged
0046   const int MAX_SIZE = 252288;
0047   const int FACTOR = 1000;
0048   const int MAX_SHORT = 32767;
0049   CSCDBGains *cndbgains = new CSCDBGains();
0050 
0051   int db_index;
0052   float db_gainslope;  // db_intercpt, db_chisq;
0053   std::vector<int> db_index_id;
0054   std::vector<float> db_slope;
0055   std::vector<float> db_intercept;
0056   std::vector<float> db_chi2;
0057   int new_index;
0058   float new_gainslope, new_intercpt, new_chisq;
0059   std::vector<int> new_cham_id;
0060   std::vector<int> new_index_id;
0061   std::vector<int> new_strips;
0062   std::vector<float> new_slope;
0063   std::vector<float> new_intercept;
0064   std::vector<float> new_chi2;
0065 
0066   int counter;
0067   int db_nrlines = 0;
0068   int new_nrlines = 0;
0069 
0070   std::ifstream dbdata;
0071   dbdata.open("old_dbgains.dat", std::ios::in);
0072   if (!dbdata) {
0073     std::cerr << "Error: old_dbgains.dat -> no such file!" << std::endl;
0074     exit(1);
0075   }
0076 
0077   while (!dbdata.eof()) {
0078     dbdata >> db_index >> db_gainslope;
0079     db_index_id.push_back(db_index);
0080     db_slope.push_back(db_gainslope);
0081     // db_intercept.push_back(db_intercpt);
0082     // db_chi2.push_back(db_chisq);
0083     db_nrlines++;
0084   }
0085   dbdata.close();
0086 
0087   std::ifstream newdata;
0088   newdata.open("gains.dat", std::ios::in);
0089   if (!newdata) {
0090     std::cerr << "Error: gains.dat -> no such file!" << std::endl;
0091     exit(1);
0092   }
0093 
0094   while (!newdata.eof()) {
0095     newdata >> new_index >> new_gainslope >> new_intercpt >> new_chisq;
0096     new_index_id.push_back(new_index);
0097     new_slope.push_back(new_gainslope);
0098     new_intercept.push_back(new_intercpt);
0099     new_chi2.push_back(new_chisq);
0100     new_nrlines++;
0101   }
0102   newdata.close();
0103 
0104   CSCDBGains::GainContainer &itemvector = cndbgains->gains;
0105   itemvector.resize(MAX_SIZE);
0106   cndbgains->factor_gain = (short int)(FACTOR);
0107   std::cout << " myfactor " << cndbgains->factor_gain << std::endl;
0108 
0109   for (int i = 0; i < MAX_SIZE; ++i) {
0110     itemvector[i].gain_slope = int(db_slope[i] * FACTOR + 0.5);
0111   }
0112 
0113   for (int i = 0; i < MAX_SIZE; ++i) {
0114     counter = db_index_id[i];
0115     itemvector[i] = itemvector[counter];
0116     itemvector[i].gain_slope = int(db_slope[i]);
0117 
0118     for (unsigned int k = 0; k < new_index_id.size() - 1; k++) {
0119       if (counter == new_index_id[k]) {
0120         if ((short int)(fabs(new_slope[k] * FACTOR + 0.5)) < MAX_SHORT)
0121           itemvector[counter].gain_slope = int(new_slope[k] * FACTOR + 0.5);
0122         itemvector[i] = itemvector[counter];
0123       }
0124     }
0125     if (counter > 223968) {
0126       itemvector[counter].gain_slope = int(db_slope[i]);
0127       itemvector[i] = itemvector[counter];
0128     }
0129   }
0130   return cndbgains;
0131 }
0132 
0133 #endif