Skip to content

Commit

Permalink
Merge pull request #35421 from tvami/ModernizeCPEObjCode
Browse files Browse the repository at this point in the history
Modernize SiPixel CPE object code in `CondTools/SiPixel`
  • Loading branch information
cmsbuild committed Sep 29, 2021
2 parents fbd47f7 + 4b20456 commit ccafe03
Show file tree
Hide file tree
Showing 29 changed files with 674 additions and 1,010 deletions.
6 changes: 6 additions & 0 deletions CondTools/SiPixel/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<use name="Geometry/Records"/>
<use name="Geometry/TrackerGeometryBuilder"/>
<use name="CondCore/DBOutputService"/>
<use name="CondTools/SiPixel"/>
<use name="CalibTracker/Records"/>
<use name="MagneticField/Records"/>
<use name="RecoLocalTracker/SiPixelRecHits"/>
<library name="CondToolsSiPixelPlugins" file="*.cc">
<flags EDM_PLUGIN="1"/>
</library>
Original file line number Diff line number Diff line change
@@ -1,94 +1,137 @@
#include "CondTools/SiPixel/test/SiPixel2DTemplateDBObjectReader.h"
#include <iomanip>
#include <fstream>
#include <iostream>
#include <cmath>
#include <memory>
#include "FWCore/ParameterSet/interface/FileInPath.h"

#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/ESWatcher.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "FWCore/Utilities/interface/ESGetToken.h"

#include "MagneticField/Engine/interface/MagneticField.h"
#include "CondFormats/SiPixelObjects/interface/SiPixel2DTemplateDBObject.h"
#include "CondFormats/DataRecord/interface/SiPixel2DTemplateDBObjectRcd.h"
#include "CalibTracker/Records/interface/SiPixel2DTemplateDBObjectESProducerRcd.h"
#include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"

class SiPixel2DTemplateDBObjectReader : public edm::one::EDAnalyzer<> {
public:
explicit SiPixel2DTemplateDBObjectReader(const edm::ParameterSet&);
~SiPixel2DTemplateDBObjectReader() override;

private:
void beginJob() override;
void analyze(const edm::Event&, const edm::EventSetup&) override;
void endJob() override;

edm::ESWatcher<SiPixel2DTemplateDBObjectESProducerRcd> SiPix2DTemplDBObjectWatcher_;
edm::ESWatcher<SiPixel2DTemplateDBObjectRcd> SiPix2DTemplDBObjWatcher_;

std::string the2DTemplateCalibrationLocation;
bool theDetailed2DTemplateDBErrorOutput;
bool theFull2DTemplateDBOutput;
bool testGlobalTag;
bool hasTriggeredWatcher;
edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> magneticFieldToken_;
edm::ESGetToken<SiPixel2DTemplateDBObject, SiPixel2DTemplateDBObjectESProducerRcd> the2DTemplateESProdToken_;
edm::ESGetToken<SiPixel2DTemplateDBObject, SiPixel2DTemplateDBObjectRcd> the2DTemplateToken_;
};

SiPixel2DTemplateDBObjectReader::SiPixel2DTemplateDBObjectReader(const edm::ParameterSet& iConfig)
: the2DTemplateCalibrationLocation(iConfig.getParameter<std::string>("siPixel2DTemplateCalibrationLocation")),
theDetailed2DTemplateDBErrorOutput(iConfig.getParameter<bool>("wantDetailed2DTemplateDBErrorOutput")),
theFull2DTemplateDBOutput(iConfig.getParameter<bool>("wantFull2DTemplateDBOutput")),
testGlobalTag(iConfig.getParameter<bool>("TestGlobalTag")),
hasTriggeredWatcher(false) {}
hasTriggeredWatcher(false),
magneticFieldToken_(esConsumes()),
the2DTemplateESProdToken_(esConsumes()),
the2DTemplateToken_(esConsumes()) {}

SiPixel2DTemplateDBObjectReader::~SiPixel2DTemplateDBObjectReader() {}
SiPixel2DTemplateDBObjectReader::~SiPixel2DTemplateDBObjectReader() = default;

void SiPixel2DTemplateDBObjectReader::beginJob() {}

void SiPixel2DTemplateDBObjectReader::analyze(const edm::Event& iEvent, const edm::EventSetup& setup) {
void SiPixel2DTemplateDBObjectReader::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
//To test with the ESProducer
SiPixel2DTemplateDBObject dbobject;
if (testGlobalTag) {
edm::ESHandle<MagneticField> magfield;
setup.get<IdealMagneticFieldRecord>().get(magfield);
// Get magnetic field
GlobalPoint center(0.0, 0.0, 0.0);
edm::ESHandle<MagneticField> magfield = iSetup.getHandle(magneticFieldToken_);
float theMagField = magfield.product()->inTesla(center).mag();

std::cout << "\nTesting global tag at magfield = " << theMagField << std::endl;
if (SiPix2DTemplDBObjWatcher_.check(setup)) {
edm::ESHandle<SiPixel2DTemplateDBObject> generrorH;
setup.get<SiPixel2DTemplateDBObjectESProducerRcd>().get(generrorH);
dbobject = *generrorH.product();
edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\nTesting global tag at magnetic field = " << theMagField;
if (SiPix2DTemplDBObjWatcher_.check(iSetup)) {
edm::LogPrint("SiPixel2DTemplateDBObjectESProducerRcd") << "With record SiPixel2DTemplateDBObjectESProducerRcd";
dbobject = *&iSetup.getData(the2DTemplateESProdToken_);
hasTriggeredWatcher = true;
}
} else {
std::cout << "\nLoading from file " << std::endl;
if (SiPix2DTemplDBObjWatcher_.check(setup)) {
edm::ESHandle<SiPixel2DTemplateDBObject> generrorH;
setup.get<SiPixel2DTemplateDBObjectRcd>().get(generrorH);
dbobject = *generrorH.product();
edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\nLoading from file " << std::endl;
if (SiPix2DTemplDBObjWatcher_.check(iSetup)) {
edm::LogPrint("SiPixelTemplateDBObjectReader") << "With record SiPixel2DTemplateDBObjectRcd";
dbobject = *&iSetup.getData(the2DTemplateToken_);
hasTriggeredWatcher = true;
}
}

if (hasTriggeredWatcher) {
std::vector<short> tempMapId;

/*hp
if(theFull2DTemplateDBOutput) std::cout << "Map info" << std::endl;
std::map<unsigned int,short> templMap=dbobject.get2DTemplateIDs();
for(std::map<unsigned int,short>::const_iterator it=templMap.begin(); it!=templMap.end();++it) {
if(tempMapId.size()==0) tempMapId.push_back(it->second);
for(unsigned int i=0; i<tempMapId.size();++i) {
if(tempMapId[i]==it->second) continue;
else if(i==tempMapId.size()-1) {
tempMapId.push_back(it->second);
break;
}
}
if(theFull2DTemplateDBOutput)
std::cout<< "DetId: "<< it->first<<" 2DTemplateID: "<< it->second<<"\n";
}
hp*/

std::cout << "\nMap stores 2DTemplate Id(s): ";
if (theFull2DTemplateDBOutput) {
edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "Map info" << std::endl;
std::map<unsigned int, short> templMap = dbobject.getTemplateIDs();
for (std::map<unsigned int, short>::const_iterator it = templMap.begin(); it != templMap.end(); ++it) {
if (tempMapId.empty())
tempMapId.push_back(it->second);
for (unsigned int i = 0; i < tempMapId.size(); ++i) {
if (tempMapId[i] == it->second)
continue;
else if (i == tempMapId.size() - 1) {
tempMapId.push_back(it->second);
break;
}
}
edm::LogPrint("SiPixel2DTemplateDBObjectReader")
<< "DetId: " << it->first << " 2DTemplateID: " << it->second << "\n";
}
}

edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\nMap stores 2DTemplate Id(s): ";
for (unsigned int vindex = 0; vindex < tempMapId.size(); ++vindex)
std::cout << tempMapId[vindex] << " ";
std::cout << std::endl;
edm::LogPrint("SiPixel2DTemplateDBObjectReader") << tempMapId[vindex] << " ";
edm::LogPrint("SiPixel2DTemplateDBObjectReader") << std::endl;

//local variables
const char* tempfile;
char c;
int numOfTempl = dbobject.numOfTempl();
int index = 0;
float tempnum = 0, diff = 0;
float tol = 1.0E-23;
bool error = false, givenErrorMsg = false;
;

std::cout << "\nChecking 2DTemplate DB object version " << dbobject.version() << " containing " << numOfTempl
<< " calibration(s) at " << dbobject.sVector()[index + 22] << "T\n";
edm::LogPrint("SiPixel2DTemplateDBObjectReader")
<< "\nChecking 2DTemplate DB object version " << dbobject.version() << " containing " << numOfTempl
<< " calibration(s) at " << dbobject.sVector()[index + 22] << "T\n";
for (int i = 0; i < numOfTempl; ++i) {
//Removes header in db object from diff
index += 20;

//Tell the person viewing the output what the 2DTemplate ID and version are -- note that version is only valid for >=13
std::cout << "Calibration " << i + 1 << " of " << numOfTempl << ", with 2DTemplate ID "
<< dbobject.sVector()[index] << "\tand Version " << dbobject.sVector()[index + 1] << "\t-------- ";
edm::LogPrint("SiPixel2DTemplateDBObjectReader")
<< "Calibration " << i + 1 << " of " << numOfTempl << ", with 2DTemplate ID " << dbobject.sVector()[index]
<< "\tand Version " << dbobject.sVector()[index + 1] << "\t-------- ";

//Opening the text-based 2DTemplate calibration
std::ostringstream tout;
Expand All @@ -102,9 +145,6 @@ hp*/

if (in_file.is_open()) {
//Removes header in textfile from diff
for (int header = 0; (c = in_file.get()) != '\n'; ++header) {
}

//First read in from the text file -- this will be compared with index = 20
in_file >> tempnum;

Expand All @@ -117,14 +157,15 @@ hp*/
if (diff > tol) {
//We have the if statement to output the message only once
if (!givenErrorMsg)
std::cout << "does NOT match\n";
edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "does NOT match\n";
//If there is an error we want to display a message upon completion
error = true;
givenErrorMsg = true;
//Do we want more detailed output?
if (theDetailed2DTemplateDBErrorOutput) {
std::cout << "from file = " << tempnum << "\t from dbobject = " << dbobject.sVector()[index]
<< "\tdiff = " << diff << "\t db index = " << index << std::endl;
edm::LogPrint("SiPixel2DTemplateDBObjectReader")
<< "from file = " << tempnum << "\t from dbobject = " << dbobject.sVector()[index]
<< "\tdiff = " << diff << "\t db index = " << index << std::endl;
}
}
//Go to the next entries
Expand All @@ -133,22 +174,23 @@ hp*/
}
//There were no errors, the two files match.
if (!givenErrorMsg)
std::cout << "MATCHES\n";
edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "MATCHES\n";
} //end current file
in_file.close();
givenErrorMsg = false;
} //end loop over all files

if (error && !theDetailed2DTemplateDBErrorOutput)
std::cout << "\nThe were differences found between the files and the database.\n"
<< "If you would like more detailed information please set\n"
<< "wantDetailedOutput = True in the cfg file. If you would like a\n"
<< "full output of the contents of the database file please set\n"
<< "wantFullOutput = True. Make sure that you pipe the output to a\n"
<< "log file. This could take a few minutes.\n\n";
edm::LogPrint("SiPixel2DTemplateDBObjectReader")
<< "\nThe were differences found between the files and the database.\n"
<< "If you would like more detailed information please set\n"
<< "wantDetailedOutput = True in the cfg file. If you would like a\n"
<< "full output of the contents of the database file please set\n"
<< "wantFullOutput = True. Make sure that you pipe the output to a\n"
<< "log file. This could take a few minutes.\n\n";

if (theFull2DTemplateDBOutput)
std::cout << dbobject << std::endl;
edm::LogPrint("SiPixel2DTemplateDBObjectReader") << dbobject << std::endl;
}
}

Expand All @@ -167,25 +209,30 @@ std::ostream& operator<<(std::ostream& s, const SiPixel2DTemplateDBObject& dbobj
//!-changes the size of the 2DTemplates based on the version
int sizeSetter = 0, generrorVersion = 0;

std::cout << "\n\nDBobject version: " << dbobject.version() << std::endl;
edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\n\nDBobject version: " << dbobject.version() << std::endl;

for (m = 0; m < dbobject.numOfTempl(); ++m) {
//To change the size of the output based on which 2DTemplate version we are using"
generrorVersion = (int)dbobject.sVector_[index + 21];
if (generrorVersion <= 10) {
std::cout << "*****WARNING***** This code will not format this 2DTemplate version properly *****WARNING*****\n";
edm::LogPrint("SiPixel2DTemplateDBObjectReader")
<< "*****WARNING***** This code will not format this 2DTemplate version properly *****WARNING*****\n";
sizeSetter = 0;
} else if (generrorVersion <= 16)
sizeSetter = 1;
else
std::cout << "*****WARNING***** This code has not been tested at formatting this version *****WARNING*****\n";
edm::LogPrint("SiPixel2DTemplateDBObjectReader")
<< "*****WARNING***** This code has not been tested at formatting this version *****WARNING*****\n";

std::cout << "\n\n*********************************************************************************************"
<< std::endl;
std::cout << "*************** Reading 2DTemplate ID " << dbobject.sVector_[index + 20] << "\t("
<< m + 1 << "/" << dbobject.numOfTempl_ << ") ***************" << std::endl;
std::cout << "*********************************************************************************************\n\n"
<< std::endl;
edm::LogPrint("SiPixel2DTemplateDBObjectReader")
<< "\n\n*********************************************************************************************"
<< std::endl;
edm::LogPrint("SiPixel2DTemplateDBObjectReader")
<< "*************** Reading 2DTemplate ID " << dbobject.sVector_[index + 20] << "\t(" << m + 1
<< "/" << dbobject.numOfTempl_ << ") ***************" << std::endl;
edm::LogPrint("SiPixel2DTemplateDBObjectReader")
<< "*********************************************************************************************\n\n"
<< std::endl;

//Header Title
SiPixel2DTemplateDBObject::char2float temp;
Expand Down Expand Up @@ -341,3 +388,5 @@ std::ostream& operator<<(std::ostream& s, const SiPixel2DTemplateDBObject& dbobj
}
return s;
}

DEFINE_FWK_MODULE(SiPixel2DTemplateDBObjectReader);
Loading

0 comments on commit ccafe03

Please sign in to comment.