Skip to content

Commit

Permalink
First round
Browse files Browse the repository at this point in the history
  • Loading branch information
mteroerd committed Nov 23, 2020
1 parent 820b4c9 commit 8d42526
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 42 deletions.
24 changes: 15 additions & 9 deletions Alignment/APEEstimation/plugins/ApeEstimator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
/////////
#include "DataFormats/GeometryVector/interface/LocalVector.h"
#include "DataFormats/GeometrySurface/interface/Bounds.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
#include "CondFormats/DataRecord/interface/SiStripLorentzAngleRcd.h"
#include "DataFormats/BeamSpot/interface/BeamSpot.h"
Expand Down Expand Up @@ -181,6 +180,9 @@ class ApeEstimator : public edm::one::EDAnalyzer<> {

// ----------member data ---------------------------
const edm::ParameterSet parameterSet_;
const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> magFieldToken_;
const edm::ESGetToken<SiStripLorentzAngle, SiStripLorentzAngleDepRcd> lorentzAngleToken_;

std::map<unsigned int, TrackerSectorStruct> m_tkSector_;
TrackerDetectorStruct tkDetector_;
SiStripClusterInfo siStripClusterInfo_;
Expand All @@ -204,6 +206,10 @@ class ApeEstimator : public edm::one::EDAnalyzer<> {
const bool calculateApe_;

unsigned int counter1, counter2, counter3, counter4, counter5, counter6;




};

//
Expand All @@ -218,7 +224,10 @@ class ApeEstimator : public edm::one::EDAnalyzer<> {
// constructors and destructor
//
ApeEstimator::ApeEstimator(const edm::ParameterSet& iConfig)
: parameterSet_(iConfig),
:
parameterSet_(iConfig),
magFieldToken_(esConsumes()),
lorentzAngleToken_(esConsumes()),
siStripClusterInfo_(consumesCollector(), std::string("")),
tjTagToken_(
consumes<TrajTrackAssociationCollection>(parameterSet_.getParameter<edm::InputTag>("tjTkAssociationMapTag"))),
Expand All @@ -227,7 +236,8 @@ ApeEstimator::ApeEstimator(const edm::ParameterSet& iConfig)
maxTracksPerEvent_(parameterSet_.getParameter<unsigned int>("maxTracksPerEvent")),
minGoodHitsPerTrack_(parameterSet_.getParameter<unsigned int>("minGoodHitsPerTrack")),
analyzerMode_(parameterSet_.getParameter<bool>("analyzerMode")),
calculateApe_(parameterSet_.getParameter<bool>("calculateApe")) {
calculateApe_(parameterSet_.getParameter<bool>("calculateApe"))
{
counter1 = counter2 = counter3 = counter4 = counter5 = counter6 = 0;
}

Expand Down Expand Up @@ -1658,16 +1668,12 @@ TrackStruct::HitParameterStruct ApeEstimator::fillHitVariables(const TrajectoryM
return hitParams;
}

edm::ESHandle<MagneticField> magFieldHandle;
iSetup.get<IdealMagneticFieldRecord>().get(magFieldHandle);

edm::ESHandle<SiStripLorentzAngle> lorentzAngleHandle;
iSetup.get<SiStripLorentzAngleDepRcd>().get(lorentzAngleHandle); //MODIFIED BY LOIC QUERTENMONT
const MagneticField *magField = &iSetup.getData(magFieldToken_);
const SiStripLorentzAngle *lorentzAngle = &iSetup.getData(lorentzAngleToken_);

const StripGeomDetUnit* stripDet = (const StripGeomDetUnit*)(&detUnit);
const MagneticField* magField(magFieldHandle.product());
LocalVector bField = (stripDet->surface()).toLocal(magField->inTesla(stripDet->surface().position()));
const SiStripLorentzAngle* lorentzAngle(lorentzAngleHandle.product());
float tanLorentzAnglePerTesla = lorentzAngle->getLorentzAngle(stripDet->geographicalId().rawId());

float dirX = -tanLorentzAnglePerTesla * bField.y();
Expand Down
96 changes: 67 additions & 29 deletions Alignment/APEEstimation/plugins/ApeEstimatorSummary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
Expand All @@ -35,6 +34,7 @@

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/isFinite.h"

#include "CLHEP/Matrix/SymMatrix.h"

Expand Down Expand Up @@ -78,6 +78,8 @@ class ApeEstimatorSummary : public edm::one::EDAnalyzer<> {

// ----------member data ---------------------------
const edm::ParameterSet parameterSet_;
const edm::ESGetToken<AlignmentErrorsExtended, TrackerAlignmentErrorExtendedRcd> alignmentErrorToken_;


bool firstEvent = true;

Expand All @@ -99,7 +101,7 @@ class ApeEstimatorSummary : public edm::one::EDAnalyzer<> {
// constructors and destructor
//
ApeEstimatorSummary::ApeEstimatorSummary(const edm::ParameterSet& iConfig)
: parameterSet_(iConfig), inputFile_(nullptr) {}
: parameterSet_(iConfig), alignmentErrorToken_(esConsumes()), inputFile_(nullptr) {}

ApeEstimatorSummary::~ApeEstimatorSummary() {}

Expand Down Expand Up @@ -364,9 +366,8 @@ void ApeEstimatorSummary::calculateApe() {
TTree* sectorNameTree(nullptr);
iterationFile->GetObject("nameTree;1", sectorNameTree);

bool firstIter(false);
if (!iterationTreeX) { // should be always true in setBaseline mode, since file is recreated
firstIter = true;
const bool firstIter(!iterationTreeX);
if (firstIter) { // should be always true in setBaseline mode, since file is recreated
if (!setBaseline) {
iterationTreeX = new TTree("iterTreeX", "Tree for APE x values of all iterations");
iterationTreeY = new TTree("iterTreeY", "Tree for APE y values of all iterations");
Expand Down Expand Up @@ -438,34 +439,45 @@ void ApeEstimatorSummary::calculateApe() {
}

// Check whether sector definitions are identical with the ones of previous iterations and with the ones in baseline file
bool failed(false);
for (auto& i_sector : m_tkSector_) {
const std::string& name(i_sector.second.name);
if (!firstIter) {
if (firstIter) {
a_sectorName[i_sector.first] = new std::string(name);
}else{
const std::string& nameLastIter(*a_sectorName[i_sector.first]);
if (name != nameLastIter) {
edm::LogError("CalculateAPE") << "Inconsistent sector definition in iterationFile for sector " << i_sector.first
<< ",\n"
<< "Recent iteration has name \"" << name << "\", while previous had \""
<< nameLastIter << "\"\n"
<< "...APE calculation stopped. Please check sector definitions in config!\n";
return;
failed = true;
}
} else {
a_sectorName[i_sector.first] = new std::string(name);

}
if (!setBaseline && baselineFile) {
const std::string& nameBaseline(*a_sectorBaselineName[i_sector.first]);
if (name != nameBaseline) {
failed = true;
edm::LogError("CalculateAPE") << "Inconsistent sector definition in baselineFile for sector " << i_sector.first
<< ",\n"
<< "Recent iteration has name \"" << name << "\", while baseline had \""
<< nameBaseline << "\"\n"
<< "...APE calculation stopped. Please check sector definitions in config!\n";
return;
}
}
}


if (failed){
if (firstIter){
for (auto& i_sector : m_tkSector_) {
delete a_sectorName[i_sector.first];
}
}
return;
}

// Set up text file for writing out APE values per module
std::ofstream apeOutputFile;
if (!setBaseline) {
Expand Down Expand Up @@ -634,13 +646,13 @@ void ApeEstimatorSummary::calculateApe() {
double correctionY_1 = correctionY2_1 >= 0. ? std::sqrt(correctionY2_1) : -std::sqrt(-correctionY2_1);
double correctionY_2 = correctionY2_2 >= 0. ? std::sqrt(correctionY2_2) : -std::sqrt(-correctionY2_2);
// Meanwhile, this got very bad default values, or? (negative corrections allowed)
if (isnan(correctionX_1))
if (edm::isNotFinite(correctionX_1))
correctionX_1 = -0.0010;
if (isnan(correctionX_2))
if (edm::isNotFinite(correctionX_2))
correctionX_2 = -0.0010;
if (isnan(correctionY_1))
if (edm::isNotFinite(correctionY_1))
correctionY_1 = -0.0010;
if (isnan(correctionY_2))
if (edm::isNotFinite(correctionY_2))
correctionY_2 = -0.0010;

if (entriesX < minHitsPerInterval) {
Expand Down Expand Up @@ -876,6 +888,10 @@ void ApeEstimatorSummary::calculateApe() {
if (firstIter) {
sectorNameTree->Fill();
sectorNameTree->Write("nameTree");
for (auto& i_sector : m_tkSector_) {
delete a_sectorName[i_sector.first];
}

}
iterationFile->Close();
delete iterationFile;
Expand All @@ -894,9 +910,8 @@ void ApeEstimatorSummary::analyze(const edm::Event& iEvent, const edm::EventSetu
// Set baseline or calculate APE value?
const bool setBaseline(parameterSet_.getParameter<bool>("setBaseline"));

edm::ESHandle<AlignmentErrorsExtended> alignmentErrors;
iSetup.get<TrackerAlignmentErrorExtendedRcd>().get(alignmentErrors);

const AlignmentErrorsExtended *alignmentErrors = &iSetup.getData(alignmentErrorToken_);

// Read in baseline file for calculation of APE value (if not setting baseline)
// Has same format as iterationFile
const std::string baselineFileName(parameterSet_.getParameter<std::string>("BaselineFile"));
Expand Down Expand Up @@ -932,9 +947,8 @@ void ApeEstimatorSummary::analyze(const edm::Event& iEvent, const edm::EventSetu
TTree* sectorNameTree(nullptr);
defaultFile->GetObject("nameTree;1", sectorNameTree);

bool firstIter(false);
if (!defaultTreeX) { // should be always true in setBaseline mode, since file is recreated
firstIter = true;
const bool firstIter(!defaultTreeX);
if (firstIter) { // should be always true in setBaseline mode, since file is recreated
defaultTreeX = new TTree("iterTreeX", "Tree for default APE x values from GT");
defaultTreeY = new TTree("iterTreeY", "Tree for default APE y values from GT");
edm::LogInfo("CalculateAPE") << "First APE iteration (number 0.), create default file with TTree";
Expand Down Expand Up @@ -986,20 +1000,22 @@ void ApeEstimatorSummary::analyze(const edm::Event& iEvent, const edm::EventSetu
}

// Check whether sector definitions are identical with the ones of previous iterations and with the ones in baseline file
bool failed(false);
for (auto& i_sector : m_tkSector_) {
const std::string& name(i_sector.second.name);
if (!firstIter) {
if (firstIter) {
a_sectorName[i_sector.first] = new std::string(name);
} else {
const std::string& nameLastIter(*a_sectorName[i_sector.first]);
if (name != nameLastIter) {
edm::LogError("CalculateAPE") << "Inconsistent sector definition in iterationFile for sector "
<< i_sector.first << ",\n"
<< "Recent iteration has name \"" << name << "\", while previous had \""
<< nameLastIter << "\"\n"
<< "...APE calculation stopped. Please check sector definitions in config!\n";
return;
failed = true;
}
} else
a_sectorName[i_sector.first] = new std::string(name);
}
if (!setBaseline && baselineFile) {
const std::string& nameBaseline(*a_sectorBaselineName[i_sector.first]);
if (name != nameBaseline) {
Expand All @@ -1008,10 +1024,23 @@ void ApeEstimatorSummary::analyze(const edm::Event& iEvent, const edm::EventSetu
<< "Recent iteration has name \"" << name << "\", while baseline had \""
<< nameBaseline << "\"\n"
<< "...APE calculation stopped. Please check sector definitions in config!\n";
return;
failed = true;
}
}
}

if (failed){
if (firstIter){
delete defaultTreeX;
delete defaultTreeY;
delete sectorNameTree;
for (auto& i_sector : m_tkSector_) {
delete a_sectorName[i_sector.first];
}
}
return;
}


// Loop over sectors for calculating getting default APE
for (auto& i_sector : m_tkSector_) {
Expand Down Expand Up @@ -1040,14 +1069,23 @@ void ApeEstimatorSummary::analyze(const edm::Event& iEvent, const edm::EventSetu
defaultTreeX->Write("iterTreeX");
defaultTreeY->Fill();
defaultTreeY->Write("iterTreeY");

defaultFile->Close();
delete defaultFile;

if (baselineFile) {
baselineFile->Close();
delete baselineFile;
}

delete defaultFile;
if (firstIter){
for (auto& i_sector : m_tkSector_) {
delete a_sectorName[i_sector.first];
}
delete sectorNameTree;
delete defaultTreeX;
delete defaultTreeY;
}


firstEvent = false;
}
Expand Down
9 changes: 5 additions & 4 deletions Alignment/APEEstimation/plugins/ApeTreeCreateDefault.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
Expand Down Expand Up @@ -81,6 +80,7 @@ class ApeTreeCreateDefault : public edm::one::EDAnalyzer<> {
bool checkModulePositions(const float, const std::vector<double>&) const;

// ----------member data ---------------------------
const edm::ESGetToken<AlignmentErrorsExtended, TrackerAlignmentErrorExtendedRcd> alignmentErrorToken_;
const std::string resultFile_;
const std::string trackerTreeFile_;
const std::vector<edm::ParameterSet> sectors_;
Expand All @@ -102,7 +102,9 @@ class ApeTreeCreateDefault : public edm::one::EDAnalyzer<> {
// constructors and destructor
//
ApeTreeCreateDefault::ApeTreeCreateDefault(const edm::ParameterSet& iConfig)
: resultFile_(iConfig.getParameter<std::string>("resultFile")),
:
alignmentErrorToken_(esConsumes()),
resultFile_(iConfig.getParameter<std::string>("resultFile")),
trackerTreeFile_(iConfig.getParameter<std::string>("trackerTreeFile")),
sectors_(iConfig.getParameter<std::vector<edm::ParameterSet>>("sectors")) {}

Expand Down Expand Up @@ -399,8 +401,7 @@ void ApeTreeCreateDefault::analyze(const edm::Event& iEvent, const edm::EventSet
// Same procedure as in ApeEstimatorSummary.cc minus reading of baseline tree

// Load APEs from the GT and write them to root files similar to the ones from calculateAPE()
edm::ESHandle<AlignmentErrorsExtended> alignmentErrors;
iSetup.get<TrackerAlignmentErrorExtendedRcd>().get(alignmentErrors);
const AlignmentErrorsExtended *alignmentErrors = &iSetup.getData(alignmentErrorToken_);

// Set up root file for default APE values
const std::string defaultFileName(resultFile_);
Expand Down

0 comments on commit 8d42526

Please sign in to comment.