SiPixelFakeTemplateDBObjectESSource

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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
// system includes
#include <memory>
#include <fstream>

// framework includes
#include "CondFormats/DataRecord/interface/SiPixelTemplateDBObjectRcd.h"
#include "CondFormats/SiPixelObjects/interface/SiPixelTemplateDBObject.h"
#include "FWCore/Framework/interface/ESProducer.h"
#include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/ModuleFactory.h"
#include "FWCore/Framework/interface/SourceFactory.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"

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

  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

  typedef std::vector<std::string> vstring;

  virtual std::unique_ptr<SiPixelTemplateDBObject> produce(const SiPixelTemplateDBObjectRcd&);

protected:
  void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
                      const edm::IOVSyncValue&,
                      edm::ValidityInterval&) override;

private:
  vstring templateCalibrations_;
  float version_;
};

SiPixelFakeTemplateDBObjectESSource::SiPixelFakeTemplateDBObjectESSource(const edm::ParameterSet& conf_)
    : templateCalibrations_(conf_.getParameter<vstring>("siPixelTemplateCalibrations")),
      version_(conf_.getParameter<double>("Version")) {
  edm::LogInfo("SiPixelFakeTemplateDBObjectESSource::SiPixelFakeTemplateDBObjectESSource");
  //the following line is needed to tell the framework what
  // data is being produced
  setWhatProduced(this);
  findingRecord<SiPixelTemplateDBObjectRcd>();
}

SiPixelFakeTemplateDBObjectESSource::~SiPixelFakeTemplateDBObjectESSource() {}

std::unique_ptr<SiPixelTemplateDBObject> SiPixelFakeTemplateDBObjectESSource::produce(
    const SiPixelTemplateDBObjectRcd&) {
  using namespace edm::es;

  //Mostly copied from CondTools/SiPixel/test/SiPixelTemplateDBObjectUploader.cc
  //--- Make the POOL-ORA object to store the database object
  SiPixelTemplateDBObject* obj = new SiPixelTemplateDBObject;

  // Local variables
  const char* tempfile;
  int m;

  // Set the number of templates to be passed to the dbobject
  obj->setNumOfTempl(templateCalibrations_.size());

  // Set the version of the template dbobject - this is an external parameter
  obj->setVersion(version_);

  //  open the template file(s)
  for (m = 0; m < obj->numOfTempl(); ++m) {
    edm::FileInPath file(templateCalibrations_[m].c_str());
    tempfile = (file.fullPath()).c_str();

    std::ifstream in_file(tempfile, std::ios::in);

    if (in_file.is_open()) {
      edm::LogPrint("SiPixelFakeTemplateDBObjectESSource")
          << "Opened Template File: " << file.fullPath().c_str() << std::endl;

      // Local variables
      char title_char[80], c;
      SiPixelTemplateDBObject::char2float temp;
      float tempstore;
      unsigned int iter;

      // Templates contain a header char - we must be clever about storing this
      for (iter = 0; (c = in_file.get()) != '\n' && iter < 79; ++iter) {
        title_char[iter] = c;
      }
      if (iter == 79) {
        title_char[iter] = '\n';
      } else {
        unsigned int ilast = 3 - (iter % 4);
        for (unsigned int it = 0; it != ilast; it++) {
          title_char[iter] = ' ';
          iter++;
        }
        title_char[iter] = '\n';
      }

      for (unsigned int j = 0; j <= iter; j += 4) {
        temp.c[0] = title_char[j];
        temp.c[1] = title_char[j + 1];
        temp.c[2] = title_char[j + 2];
        temp.c[3] = title_char[j + 3];
        obj->push_back(temp.f);
        obj->setMaxIndex(obj->maxIndex() + 1);
      }

      // Fill the dbobject
      in_file >> tempstore;
      while (!in_file.eof()) {
        obj->setMaxIndex(obj->maxIndex() + 1);
        obj->push_back(tempstore);
        in_file >> tempstore;
      }

      in_file.close();
    } else {
      // If file didn't open, report this
      edm::LogError("SiPixeFakelTemplateDBObjectESSource") << "Error opening File" << tempfile << std::endl;
    }
  }

  //std::cout << *obj << std::endl;
  return std::unique_ptr<SiPixelTemplateDBObject>(obj);
}

void SiPixelFakeTemplateDBObjectESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
                                                         const edm::IOVSyncValue& iosv,
                                                         edm::ValidityInterval& oValidity) {
  edm::ValidityInterval infinity(iosv.beginOfTime(), iosv.endOfTime());
  oValidity = infinity;
}

void SiPixelFakeTemplateDBObjectESSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  edm::ParameterSetDescription desc;
  desc.add<vstring>(
      "siPixelTemplateCalibrations",
      {"CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0310.out",
       "CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0311.out",
       "CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0312.out",
       "CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0313.out",
       "CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0314.out",
       "CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0315.out"});
  desc.add<double>("Version", 1.0);
  descriptions.addWithDefaultLabel(desc);
}

DEFINE_FWK_EVENTSETUP_SOURCE(SiPixelFakeTemplateDBObjectESSource);