CSCGainsDBConditions

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#ifndef _CSCGAINSDBCONDITIONS_H
#define _CSCGAINSDBCONDITIONS_H

#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ESProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/SourceFactory.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include <cmath>
#include <memory>

#include "CondFormats/CSCObjects/interface/CSCDBGains.h"
#include "CondFormats/DataRecord/interface/CSCDBGainsRcd.h"
#include <DataFormats/MuonDetId/interface/CSCDetId.h>

class CSCGainsDBConditions : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
public:
  CSCGainsDBConditions(const edm::ParameterSet &);
  ~CSCGainsDBConditions() override;

  inline static CSCDBGains *prefillDBGains();

  typedef std::unique_ptr<CSCDBGains> ReturnType;

  ReturnType produceDBGains(const CSCDBGainsRcd &);

private:
  // ----------member data ---------------------------
  void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
                      const edm::IOVSyncValue &,
                      edm::ValidityInterval &) override;
  CSCDBGains *cndbGains;
};

#include <fstream>
#include <iostream>
#include <vector>

// to workaround plugin library
inline CSCDBGains *CSCGainsDBConditions::prefillDBGains() {
  // const int MAX_SIZE = 273024;//for ME1a unganged
  const int MAX_SIZE = 252288;
  const int FACTOR = 1000;
  const int MAX_SHORT = 32767;
  CSCDBGains *cndbgains = new CSCDBGains();

  int db_index;
  float db_gainslope;  // db_intercpt, db_chisq;
  std::vector<int> db_index_id;
  std::vector<float> db_slope;
  std::vector<float> db_intercept;
  std::vector<float> db_chi2;
  int new_index;
  float new_gainslope, new_intercpt, new_chisq;
  std::vector<int> new_cham_id;
  std::vector<int> new_index_id;
  std::vector<int> new_strips;
  std::vector<float> new_slope;
  std::vector<float> new_intercept;
  std::vector<float> new_chi2;

  int counter;

  std::ifstream dbdata;
  dbdata.open("old_dbgains.dat", std::ios::in);
  if (!dbdata) {
    std::cerr << "Error: old_dbgains.dat -> no such file!" << std::endl;
    exit(1);
  }

  while (!dbdata.eof()) {
    dbdata >> db_index >> db_gainslope;
    db_index_id.push_back(db_index);
    db_slope.push_back(db_gainslope);
    // db_intercept.push_back(db_intercpt);
    // db_chi2.push_back(db_chisq);
  }
  dbdata.close();

  std::ifstream newdata;
  newdata.open("gains.dat", std::ios::in);
  if (!newdata) {
    std::cerr << "Error: gains.dat -> no such file!" << std::endl;
    exit(1);
  }

  while (!newdata.eof()) {
    newdata >> new_index >> new_gainslope >> new_intercpt >> new_chisq;
    new_index_id.push_back(new_index);
    new_slope.push_back(new_gainslope);
    new_intercept.push_back(new_intercpt);
    new_chi2.push_back(new_chisq);
  }
  newdata.close();

  CSCDBGains::GainContainer &itemvector = cndbgains->gains;
  itemvector.resize(MAX_SIZE);
  cndbgains->factor_gain = (short int)(FACTOR);
  std::cout << " myfactor " << cndbgains->factor_gain << std::endl;

  for (int i = 0; i < MAX_SIZE; ++i) {
    itemvector[i].gain_slope = int(db_slope[i] * FACTOR + 0.5);
  }

  for (int i = 0; i < MAX_SIZE; ++i) {
    counter = db_index_id[i];
    itemvector[i] = itemvector[counter];
    itemvector[i].gain_slope = int(db_slope[i]);

    for (unsigned int k = 0; k < new_index_id.size() - 1; k++) {
      if (counter == new_index_id[k]) {
        if ((short int)(fabs(new_slope[k] * FACTOR + 0.5)) < MAX_SHORT)
          itemvector[counter].gain_slope = int(new_slope[k] * FACTOR + 0.5);
        itemvector[i] = itemvector[counter];
      }
    }
    if (counter > 223968) {
      itemvector[counter].gain_slope = int(db_slope[i]);
      itemvector[i] = itemvector[counter];
    }
  }
  return cndbgains;
}

#endif