Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef _CSCGASGAINCORRECTIONDBCONDITIONS_H
0002 #define _CSCGASGAINCORRECTIONDBCONDITIONS_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/CSCDBGasGainCorrection.h"
0017 #include "CondFormats/DataRecord/interface/CSCDBGasGainCorrectionRcd.h"
0018 #include "DataFormats/MuonDetId/interface/CSCIndexer.h"
0019 #include <DataFormats/MuonDetId/interface/CSCDetId.h>
0020 
0021 class CSCGasGainCorrectionDBConditions : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0022 public:
0023   CSCGasGainCorrectionDBConditions(const edm::ParameterSet &);
0024   ~CSCGasGainCorrectionDBConditions() override;
0025 
0026   inline static CSCDBGasGainCorrection *prefillDBGasGainCorrection(bool isForMC, std::string dataCorrFileName);
0027 
0028   typedef std::unique_ptr<CSCDBGasGainCorrection> ReturnType;
0029 
0030   ReturnType produceDBGasGainCorrection(const CSCDBGasGainCorrectionRcd &);
0031 
0032 private:
0033   // ----------member data ---------------------------
0034   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
0035                       const edm::IOVSyncValue &,
0036                       edm::ValidityInterval &) override;
0037 
0038   // Flag for determining if this is for setting MC or data corrections
0039   bool isForMC;
0040   // File for reading 55944 gas gain corrections.  MC will be fake;
0041   std::string dataCorrFileName;
0042 };
0043 
0044 #include "CondFormats/CSCObjects/interface/CSCDBGasGainCorrection.h"
0045 #include "CondFormats/DataRecord/interface/CSCDBGasGainCorrectionRcd.h"
0046 #include <DataFormats/MuonDetId/interface/CSCDetId.h>
0047 
0048 #include <fstream>
0049 #include <iostream>
0050 #include <vector>
0051 
0052 // to workaround plugin library
0053 inline CSCDBGasGainCorrection *CSCGasGainCorrectionDBConditions::prefillDBGasGainCorrection(bool isMC,
0054                                                                                             std::string filename) {
0055   if (isMC)
0056     printf("\n Generating fake DB constants for MC\n");
0057   else {
0058     printf("\n Reading gas gain corrections from file %s \n", filename.data());
0059   }
0060 
0061   CSCIndexer indexer;
0062 
0063   const int MAX_SIZE = 55944;
0064 
0065   CSCDBGasGainCorrection *cndbGasGainCorr = new CSCDBGasGainCorrection();
0066 
0067   CSCDBGasGainCorrection::GasGainContainer &itemvector = cndbGasGainCorr->gasGainCorr;
0068   itemvector.resize(MAX_SIZE);
0069 
0070   // Filling corrections for MC is very simple
0071   if (isMC) {
0072     for (int i = 0; i < MAX_SIZE; i++) {
0073       itemvector[i].gainCorr = 1.;
0074     }
0075     return cndbGasGainCorr;
0076   }
0077 
0078   struct gain_info {
0079     int gas_gain_index;
0080     int endcap;
0081     int station;
0082     int ring;
0083     int chamber;
0084     int layer;
0085     int hvsegment;
0086     int cfeb;
0087     int nentries;
0088     float mean;
0089     float truncated_mean;
0090     float gas_gain_correction;
0091   } gains[MAX_SIZE];
0092 
0093   for (int j = 0; j < MAX_SIZE; j++) {
0094     gains[j].gas_gain_index = -999;
0095     gains[j].endcap = -999;
0096     gains[j].station = -999;
0097     gains[j].ring = -999;
0098     gains[j].chamber = -999;
0099     gains[j].layer = -999;
0100     gains[j].hvsegment = -999;
0101     gains[j].cfeb = -999;
0102     gains[j].nentries = -999;
0103     gains[j].mean = -999.;
0104     gains[j].truncated_mean = -999.;
0105     gains[j].gas_gain_correction = -999.;
0106   }
0107 
0108   FILE *fin = fopen(filename.data(), "r");
0109 
0110   int linecounter = 0;  // set the line counter to the first serial number in the file....
0111 
0112   while (!feof(fin)) {
0113     // note space at end of format string to convert last \n
0114     int check = fscanf(fin,
0115                        "%d %d %d %d %d %d %d %d %d %f %f %f \n",
0116                        &gains[linecounter].gas_gain_index,
0117                        &gains[linecounter].endcap,
0118                        &gains[linecounter].station,
0119                        &gains[linecounter].ring,
0120                        &gains[linecounter].chamber,
0121                        &gains[linecounter].layer,
0122                        &gains[linecounter].hvsegment,
0123                        &gains[linecounter].cfeb,
0124                        &gains[linecounter].nentries,
0125                        &gains[linecounter].mean,
0126                        &gains[linecounter].truncated_mean,
0127                        &gains[linecounter].gas_gain_correction);
0128 
0129     if (check != 12) {
0130       printf("The input file format is not as expected\n");
0131       assert(0);
0132     }
0133 
0134     linecounter++;
0135   }
0136 
0137   fclose(fin);
0138 
0139   if (linecounter == MAX_SIZE) {
0140     std::cout << "Total number of gas gains read in = " << linecounter << std::endl;
0141   } else {
0142     std::cout << "ERROR:  Total number of gas-gains read in = " << linecounter
0143               << " while total number expected = " << MAX_SIZE << std::endl;
0144   }
0145 
0146   // Fill the chip corrections with values from the file
0147   for (int i = 0; i < MAX_SIZE; i++) {
0148     itemvector[i].gainCorr = 0.;
0149 
0150     if (gains[i].gas_gain_correction > 0.) {
0151       itemvector[i].gainCorr = gains[i].gas_gain_correction;
0152     } else {
0153       // if there is no value, this should be fixed...
0154       std::cout << "ERROR:  gas_gain_correction < 0 for index " << gains[i].gas_gain_index << std::endl;
0155     }
0156   }
0157 
0158   return cndbGasGainCorr;
0159 }
0160 
0161 #endif