Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticl em vs had bdt #3

Open
wants to merge 3 commits into
base: TICL_inReco
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DataFormats/HGCalReco/interface/Trackster.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ namespace ticl {
std::array<Vector, 3> eigenvectors;
std::array<float, 3> sigmas;

//BDT for EM vs. HAD
float bdt_em_vs_had;

// types considered by the particle identification
enum class ParticleType {
photon = 0,
Expand Down
2 changes: 1 addition & 1 deletion DataFormats/HGCalReco/src/classes_def.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<lcgdict>
<class name="ticl::Trackster" ClassVersion="6">
<version ClassVersion="6" checksum="4096263277"/>
<version ClassVersion="6" checksum="2751422430"/>
<version ClassVersion="5" checksum="1679804166"/>
<version ClassVersion="4" checksum="2963413313"/>
<version ClassVersion="3" checksum="3878166595"/>
Expand Down
1 change: 1 addition & 0 deletions RecoHGCal/TICL/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<use name="DataFormats/HGCalReco"/>
<use name="DataFormats/VertexReco"/>
<use name="FWCore/PluginManager"/>
<use name="CommonTools/MVAUtils"/>
<export>
<lib name="1"/>
</export>
50 changes: 50 additions & 0 deletions RecoHGCal/TICL/plugins/PatternRecognitionbyCA.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/ParameterSet/interface/FileInPath.h"
#include "PatternRecognitionbyCA.h"
#include "HGCGraph.h"

#include "TrackstersPCA.h"

using namespace ticl;


PatternRecognitionbyCA::PatternRecognitionbyCA(const edm::ParameterSet &conf, const CacheBase *cache)
: PatternRecognitionAlgoBase(conf, cache),
theGraph_(std::make_unique<HGCGraph>()),
Expand All @@ -29,6 +31,7 @@ PatternRecognitionbyCA::PatternRecognitionbyCA(const edm::ParameterSet &conf, co
eidMinClusterEnergy_(conf.getParameter<double>("eid_min_cluster_energy")),
eidNLayers_(conf.getParameter<int>("eid_n_layers")),
eidNClusters_(conf.getParameter<int>("eid_n_clusters")),
bdtweights_(conf.getParameter<edm::FileInPath>("bdt_weights")),
eidSession_(nullptr) {
// mount the tensorflow graph onto the session when set
const TrackstersCache *trackstersCache = dynamic_cast<const TrackstersCache *>(cache);
Expand All @@ -37,6 +40,25 @@ PatternRecognitionbyCA::PatternRecognitionbyCA(const edm::ParameterSet &conf, co
<< "PatternRecognitionbyCA received an empty graph definition from the global cache";
}
eidSession_ = tensorflow::createSession(trackstersCache->eidGraphDef);

// BDT releted stuff
// LG: this can be improved but for now suggest to leave it like this
reader_ = new TMVA::Reader();
reader_->AddVariable("ts_energy", &ts_energy_);
reader_->AddVariable("ts_x", &ts_x_);
reader_->AddVariable("ts_y", &ts_y_);
reader_->AddVariable("ts_z", &ts_z_);
reader_->AddVariable("ts_pcaeigval0", &ts_pcaeigval0_);
reader_->AddVariable("ts_pcasig0", &ts_pcasig0_);
reader_->AddVariable("ts_pcaeigval1", &ts_pcaeigval1_);
reader_->AddVariable("ts_pcasig1", &ts_pcasig1_);
reader_->AddVariable("ts_pcaeigval2", &ts_pcaeigval2_);
reader_->AddVariable("ts_pcasig2", &ts_pcasig2_);

std::string weightfilename_ = bdtweights_.fullPath();
std::string tmvaMethod_ = "BDTG method";
reco::details::loadTMVAWeights(reader_, tmvaMethod_ , weightfilename_);

}

PatternRecognitionbyCA::~PatternRecognitionbyCA(){};
Expand Down Expand Up @@ -118,6 +140,10 @@ void PatternRecognitionbyCA::makeTracksters(const PatternRecognitionAlgoBase::In
ticl::assignPCAtoTracksters(result, input.layerClusters,
rhtools_.getPositionLayer(rhtools_.lastLayerEE()).z());

// add bdt em-vs-had
TracksterEmVsHadMVAReader(result, reader_);


// run energy regression and ID
energyRegressionAndID(input.layerClusters, result);
if (0) {
Expand All @@ -132,6 +158,30 @@ void PatternRecognitionbyCA::makeTracksters(const PatternRecognitionAlgoBase::In
}
}


void PatternRecognitionbyCA::TracksterEmVsHadMVAReader(std::vector<Trackster> & tracksters, TMVA::Reader* reader_) {

float mva_ = 1.;
for (unsigned int itrkster = 0; itrkster<tracksters.size(); ++itrkster) {
ts_energy_ = tracksters.at(itrkster).raw_energy;
ts_x_ = tracksters.at(itrkster).barycenter.x();
ts_y_ = tracksters.at(itrkster).barycenter.y();
ts_z_ = abs(tracksters.at(itrkster).barycenter.z());
ts_pcaeigval0_ = tracksters.at(itrkster).eigenvalues.at(0);
ts_pcasig0_ = tracksters.at(itrkster).sigmas.at(0);
ts_pcaeigval1_ = tracksters.at(itrkster).eigenvalues.at(1);
ts_pcasig1_ = tracksters.at(itrkster).sigmas.at(1);
ts_pcaeigval2_ = tracksters.at(itrkster).eigenvalues.at(2);
ts_pcasig2_ = tracksters.at(itrkster).sigmas.at(2);

mva_ = reader_->EvaluateMVA("BDTG method");
tracksters.at(itrkster).bdt_em_vs_had = mva_;
}

}



void PatternRecognitionbyCA::energyRegressionAndID(const std::vector<reco::CaloCluster> &layerClusters,
std::vector<Trackster> &tracksters) {
// Energy regression and particle identification strategy:
Expand Down
10 changes: 9 additions & 1 deletion RecoHGCal/TICL/plugins/PatternRecognitionbyCA.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include "RecoHGCal/TICL/plugins/PatternRecognitionAlgoBase.h"
#include "RecoLocalCalo/HGCalRecAlgos/interface/RecHitTools.h"
#include "PhysicsTools/TensorFlow/interface/TensorFlow.h"
#include "TMVA/Factory.h"
#include "TMVA/Reader.h"
#include "CommonTools/MVAUtils/interface/TMVAZipReader.h"


class HGCGraph;

Expand All @@ -17,7 +21,7 @@ namespace ticl {
~PatternRecognitionbyCA() override;

void makeTracksters(const PatternRecognitionAlgoBase::Inputs& input, std::vector<Trackster>& result) override;

void TracksterEmVsHadMVAReader(std::vector<Trackster> & tracksters, TMVA::Reader* reader_);
void energyRegressionAndID(const std::vector<reco::CaloCluster>& layerClusters, std::vector<Trackster>& result);

private:
Expand All @@ -35,6 +39,10 @@ namespace ticl {
const float eidMinClusterEnergy_;
const int eidNLayers_;
const int eidNClusters_;
const edm::FileInPath bdtweights_;

TMVA::Reader* reader_;
float ts_energy_, ts_x_, ts_y_, ts_z_, ts_pcaeigval0_, ts_pcasig0_, ts_pcaeigval1_, ts_pcasig1_, ts_pcaeigval2_, ts_pcasig2_;

hgcal::RecHitTools rhtools_;
tensorflow::Session* eidSession_;
Expand Down
1 change: 1 addition & 0 deletions RecoHGCal/TICL/plugins/TrackstersProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void TrackstersProducer::fillDescriptions(edm::ConfigurationDescriptions& descri
desc.add<double>("eid_min_cluster_energy", 1.);
desc.add<int>("eid_n_layers", 50);
desc.add<int>("eid_n_clusters", 10);
desc.add<edm::FileInPath>("bdt_weights" , edm::FileInPath("RecoHGCal/data/em_vs_had_xgboost.xml"));
descriptions.add("trackstersProducer", desc);
}

Expand Down
1 change: 1 addition & 0 deletions RecoHGCal/data/em_vs_had_xgboost.xml

Large diffs are not rendered by default.