Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-17 04:57:59

0001 // system includes
0002 #include <memory>
0003 #include <fstream>
0004 
0005 // framework includes
0006 #include "CondFormats/DataRecord/interface/SiPixelTemplateDBObjectRcd.h"
0007 #include "CondFormats/SiPixelObjects/interface/SiPixelTemplateDBObject.h"
0008 #include "FWCore/Framework/interface/ESProducer.h"
0009 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "FWCore/Framework/interface/ModuleFactory.h"
0012 #include "FWCore/Framework/interface/SourceFactory.h"
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0017 
0018 class SiPixelFakeTemplateDBObjectESSource : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0019 public:
0020   SiPixelFakeTemplateDBObjectESSource(const edm::ParameterSet&);
0021   ~SiPixelFakeTemplateDBObjectESSource() override;
0022 
0023   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0024 
0025   typedef std::vector<std::string> vstring;
0026 
0027   virtual std::unique_ptr<SiPixelTemplateDBObject> produce(const SiPixelTemplateDBObjectRcd&);
0028 
0029 protected:
0030   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
0031                       const edm::IOVSyncValue&,
0032                       edm::ValidityInterval&) override;
0033 
0034 private:
0035   vstring templateCalibrations_;
0036   float version_;
0037 };
0038 
0039 SiPixelFakeTemplateDBObjectESSource::SiPixelFakeTemplateDBObjectESSource(const edm::ParameterSet& conf_)
0040     : templateCalibrations_(conf_.getParameter<vstring>("siPixelTemplateCalibrations")),
0041       version_(conf_.getParameter<double>("Version")) {
0042   edm::LogInfo("SiPixelFakeTemplateDBObjectESSource::SiPixelFakeTemplateDBObjectESSource");
0043   //the following line is needed to tell the framework what
0044   // data is being produced
0045   setWhatProduced(this);
0046   findingRecord<SiPixelTemplateDBObjectRcd>();
0047 }
0048 
0049 SiPixelFakeTemplateDBObjectESSource::~SiPixelFakeTemplateDBObjectESSource() {}
0050 
0051 std::unique_ptr<SiPixelTemplateDBObject> SiPixelFakeTemplateDBObjectESSource::produce(
0052     const SiPixelTemplateDBObjectRcd&) {
0053   using namespace edm::es;
0054 
0055   //Mostly copied from CondTools/SiPixel/test/SiPixelTemplateDBObjectUploader.cc
0056   //--- Make the POOL-ORA object to store the database object
0057   SiPixelTemplateDBObject* obj = new SiPixelTemplateDBObject;
0058 
0059   // Local variables
0060   const char* tempfile;
0061   int m;
0062 
0063   // Set the number of templates to be passed to the dbobject
0064   obj->setNumOfTempl(templateCalibrations_.size());
0065 
0066   // Set the version of the template dbobject - this is an external parameter
0067   obj->setVersion(version_);
0068 
0069   //  open the template file(s)
0070   for (m = 0; m < obj->numOfTempl(); ++m) {
0071     edm::FileInPath file(templateCalibrations_[m].c_str());
0072     tempfile = (file.fullPath()).c_str();
0073 
0074     std::ifstream in_file(tempfile, std::ios::in);
0075 
0076     if (in_file.is_open()) {
0077       edm::LogPrint("SiPixelFakeTemplateDBObjectESSource")
0078           << "Opened Template File: " << file.fullPath().c_str() << std::endl;
0079 
0080       // Local variables
0081       char title_char[80], c;
0082       SiPixelTemplateDBObject::char2float temp;
0083       float tempstore;
0084       unsigned int iter;
0085 
0086       // Templates contain a header char - we must be clever about storing this
0087       for (iter = 0; (c = in_file.get()) != '\n' && iter < 79; ++iter) {
0088         title_char[iter] = c;
0089       }
0090       if (iter == 79) {
0091         title_char[iter] = '\n';
0092       } else {
0093         unsigned int ilast = 3 - (iter % 4);
0094         for (unsigned int it = 0; it != ilast; it++) {
0095           title_char[iter] = ' ';
0096           iter++;
0097         }
0098         title_char[iter] = '\n';
0099       }
0100 
0101       for (unsigned int j = 0; j <= iter; j += 4) {
0102         temp.c[0] = title_char[j];
0103         temp.c[1] = title_char[j + 1];
0104         temp.c[2] = title_char[j + 2];
0105         temp.c[3] = title_char[j + 3];
0106         obj->push_back(temp.f);
0107         obj->setMaxIndex(obj->maxIndex() + 1);
0108       }
0109 
0110       // Fill the dbobject
0111       in_file >> tempstore;
0112       while (!in_file.eof()) {
0113         obj->setMaxIndex(obj->maxIndex() + 1);
0114         obj->push_back(tempstore);
0115         in_file >> tempstore;
0116       }
0117 
0118       in_file.close();
0119     } else {
0120       // If file didn't open, report this
0121       edm::LogError("SiPixeFakelTemplateDBObjectESSource") << "Error opening File" << tempfile << std::endl;
0122     }
0123   }
0124 
0125   //std::cout << *obj << std::endl;
0126   return std::unique_ptr<SiPixelTemplateDBObject>(obj);
0127 }
0128 
0129 void SiPixelFakeTemplateDBObjectESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
0130                                                          const edm::IOVSyncValue& iosv,
0131                                                          edm::ValidityInterval& oValidity) {
0132   edm::ValidityInterval infinity(iosv.beginOfTime(), iosv.endOfTime());
0133   oValidity = infinity;
0134 }
0135 
0136 void SiPixelFakeTemplateDBObjectESSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0137   edm::ParameterSetDescription desc;
0138   desc.add<vstring>(
0139       "siPixelTemplateCalibrations",
0140       {"CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0310.out",
0141        "CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0311.out",
0142        "CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0312.out",
0143        "CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0313.out",
0144        "CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0314.out",
0145        "CalibTracker/SiPixelESProducers/data/SiPixelTemplateDBObject_0T_phase1_BoR3_v1/template_summary_zp0315.out"});
0146   desc.add<double>("Version", 1.0);
0147   descriptions.addWithDefaultLabel(desc);
0148 }
0149 
0150 DEFINE_FWK_EVENTSETUP_SOURCE(SiPixelFakeTemplateDBObjectESSource);