Skip to content

Commit

Permalink
Looping over scs, but weird segfaults
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrothman committed Oct 21, 2021
1 parent c6976f6 commit 047e688
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 57 deletions.
66 changes: 35 additions & 31 deletions Progression/EGM_DRN/plugins/DRNCorrector.cc
Expand Up @@ -124,38 +124,42 @@ void DRNCorrector::setEvent(const edm::Event& event) {
}
}

std::pair<double, double> DRNCorrector::getCorrections(const reco::SuperCluster& sc) const {
//TODO: implement this part

std::pair<double, double> corrEnergyAndRes = {-1, -1};

return corrEnergyAndRes;
}

void DRNCorrector::make_input(const edm::Event& iEvent, TritonInputMap& iInput) const {
void DRNCorrector::make_input(const edm::Event& iEvent, TritonInputMap& iInput, const reco::SuperClusterCollection& inputSCs ) const {
//get event-based seed for RNG
unsigned int runNum_uint = static_cast<unsigned int>(iEvent.id().run());
unsigned int lumiNum_uint = static_cast<unsigned int>(iEvent.id().luminosityBlock());
unsigned int evNum_uint = static_cast<unsigned int>(iEvent.id().event());
std::uint32_t seed = (lumiNum_uint << 10) + (runNum_uint << 20) + evNum_uint;
std::mt19937 rng(seed);

std::uniform_int_distribution<int> randint1(1, 200);

std::vector<unsigned> nHits = {};
unsigned totalHits = 0;
unsigned n;
for(const auto& inputSC : inputSCs){
n = randint1(rng);
totalHits+=n;
nHits.push_back(n);
}

if(nHits.size() ==0)
return;

int nnodes = randint1(rng);

//set shapes
auto& input1 = iInput.at("x__0");
input1.setShape(0, nnodes);
input1.setShape(0, totalHits);
auto data1 = input1.allocate<float>();
auto& vdata1 = (*data1)[0];

auto& input2 = iInput.at("batch__1");
input2.setShape(0, nnodes);
input2.setShape(0, totalHits);
auto data2 = input2.allocate<int64_t>();
auto& vdata2 = (*data2)[0];

auto& input3 = iInput.at("graphx__2");
input3.setShape(0, 2);
input3.setShape(0, 2*nHits.size());
auto data3 = input3.allocate<float>();
auto& vdata3 = (*data3)[0];

Expand All @@ -165,8 +169,16 @@ void DRNCorrector::make_input(const edm::Event& iEvent, TritonInputMap& iInput)
vdata1.push_back(randx(rng));
}

for (unsigned i = 0; i < input2.sizeShape(); ++i) {
vdata2.push_back(0);
//for (unsigned i = 0; i < input2.sizeShape(); ++i) {
// vdata2.push_back(0);
//}
unsigned k=0;
for(unsigned sc=0; sc<nHits.size(); ++sc){
for(unsigned i=0; i< nHits[sc]; ++i){
vdata2.push_back(sc);
++k;
//edm::LogPrint("test") << "hit " << k << " sc " << sc << std::endl;
}
}

for (unsigned i = 0; i < input3.sizeShape(); ++i) {
Expand All @@ -178,28 +190,20 @@ void DRNCorrector::make_input(const edm::Event& iEvent, TritonInputMap& iInput)
input2.toServer(data2);
input3.toServer(data3);

std::cout << "input X shape: " << input1.shape()[0] << ", " << input1.shape()[1];
std::cout << "input batch shape: " << input2.shape()[0];
std::cout << "input graphx shape: " << input3.shape()[0];
edm::LogPrint("DEBUG") << "input X shape: " << input1.shape()[0] << ", " << input1.shape()[1] << std::endl;
edm::LogPrint("test") << "input batch shape: " << input2.shape()[0] << std::endl;
edm::LogPrint("test") << "input graphx shape: " << input3.shape()[0] << std::endl;

}

void DRNCorrector::get_output(const TritonOutputMap& iOutput) const{
TritonOutput<float> DRNCorrector::get_output(const TritonOutputMap& iOutput) {
//check the results
const auto& output1 = iOutput.begin()->second;
// convert from server format
const auto& tmp = output1.fromServer<float>();
const auto& serverout = output1.fromServer<float>();
//if (brief_)
std::cout << "output shape: " << output1.shape()[0] << "," << output1.shape()[1];
}
edm::LogPrint("test") << "output shape: " << output1.shape()[0] << "," << output1.shape()[1] << std::endl;

void DRNCorrector::modifyObject(reco::SuperCluster& sc) const {
std::pair<double, double> cor = getCorrections(sc);
if (cor.first < 0)
return;
sc.setEnergy(cor.first);
sc.setCorrectedEnergy(cor.first);
if (cor.second >= 0) {
sc.setCorrectedEnergyUncertainty(cor.second);
}
return serverout;
}

13 changes: 8 additions & 5 deletions Progression/EGM_DRN/plugins/DRNCorrector.h
Expand Up @@ -16,7 +16,6 @@

#include "HeterogeneousCore/SonicTriton/interface/TritonEDProducer.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
Expand All @@ -32,6 +31,7 @@
#include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
#include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
#include "DataFormats/ParticleFlowReco/interface/PFRecHit.h"
#include "DataFormats/EgammaReco/interface/SuperClusterFwd.h"
#include "DataFormats/VertexReco/interface/VertexFwd.h"

#include "CondFormats/GBRForest/interface/GBRForestD.h"
Expand Down Expand Up @@ -61,12 +61,12 @@ class DRNCorrector {
void setEventSetup(const edm::EventSetup& es);
void setEvent(const edm::Event& e);

std::pair<double, double> getCorrections(const reco::SuperCluster& sc) const;
//std::pair<double, double> getCorrections(const reco::SuperCluster& sc) const;

void make_input(const edm::Event& iEvent, TritonInputMap& iInput) const;
void get_output(const TritonOutputMap& iOutput) const;
void make_input(const edm::Event& iEvent, TritonInputMap& iInput, const reco::SuperClusterCollection& inputSCs) const;
TritonOutput<float> get_output(const TritonOutputMap& iOutput);

void modifyObject(reco::SuperCluster& sc) const;
//void modifyObject(reco::SuperCluster& sc) const;

std::vector<float> getRegData(const reco::SuperCluster& sc) const;

Expand Down Expand Up @@ -95,6 +95,9 @@ class DRNCorrector {
float hitsEnergyThreshold_;
float hgcalCylinderR_;
HGCalShowerShapeHelper hgcalShowerShapes_;

std::vector<float> means;
std::vector<float> sigmas;
};

template <edm::Transition esTransition>
Expand Down
45 changes: 33 additions & 12 deletions Progression/EGM_DRN/plugins/DRNProducer.cc
@@ -1,4 +1,5 @@
#include "HeterogeneousCore/SonicTriton/interface/TritonEDProducer.h"
#include "HeterogeneousCore/SonicTriton/interface/TritonData.h"

#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
Expand Down Expand Up @@ -56,35 +57,55 @@ void DRNProducer::beginLuminosityBlock(const edm::LuminosityBlock& iLumi, const
}

void DRNProducer::acquire(edm::Event const& iEvent, edm::EventSetup const& iSetup, Input& iInput){
edm::LogPrint(debugName_) << std::endl;
edm::LogPrint(debugName_) << "acquiring..." << std::endl;
auto inputSCs = iEvent.get(inputSCToken_);
if(inputSCs.size() ==0){
edm::LogPrint(debugName_) << "skipped acquire for empty event" << std::endl;
return;
}
energyCorrector_.setEvent(iEvent);
energyCorrector_.make_input(iEvent, iInput);
energyCorrector_.make_input(iEvent, iInput, inputSCs);
edm::LogPrint(debugName_) << "acquired" << std::endl;
}

void DRNProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup, Output const& iOutput){
energyCorrector_.get_output(iOutput);

edm::LogPrint(debugName_) << "producing..." << std::endl;
auto inputSCs = iEvent.get(inputSCToken_);
if(inputSCs.size() ==0){
edm::LogPrint(debugName_) << "skipped produce for empty event" << std::endl;
return;
}

const auto& serverout = energyCorrector_.get_output(iOutput);
edm::LogPrint(debugName_) << "got from server" << std::endl;

//assert(inputSCs.size() == serverout.shape()[0]);

//auto inputSCs = iEvent.get(inputSCToken_);
auto corrSCs = std::make_unique<reco::SuperClusterCollection>();
std::vector<std::vector<float>> scFeatures;
unsigned i=0;
for (const auto& inputSC : inputSCs) {
edm::LogPrint(debugName_) << "doing a supercluster" << std::endl;
corrSCs->push_back(inputSC);
energyCorrector_.modifyObject(corrSCs->back());
corrSCs->back().setEnergy(serverout[i][0]);
corrSCs->back().setCorrectedEnergy(serverout[i][0]);
corrSCs->back().setCorrectedEnergyUncertainty(serverout[i][1]);
//energyCorrector_.modifyObject(corrSCs->back());
++i;
}

edm::LogPrint(debugName_) << "putting into event" << std::endl;

auto scHandle = iEvent.put(std::move(corrSCs));

if (writeFeatures_) {
auto valMap = std::make_unique<edm::ValueMap<std::vector<float>>>();
edm::ValueMap<std::vector<float>>::Filler filler(*valMap);
filler.insert(scHandle, scFeatures.begin(), scFeatures.end());
filler.fill();
iEvent.put(std::move(valMap), "features");
}
edm::LogPrint(debugName_) << "produced" << std::endl;
}

void DRNProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<edm::ParameterSetDescription>("correctorCfg", DRNCorrector::makePSetDescription());
TritonClient::fillPSetDescription(desc);
desc.add<bool>("writeFeatures", false);
desc.add<edm::InputTag>("inputSCs", edm::InputTag("particleFlowSuperClusterECAL"));
descriptions.add("scEnergyCorrectorProducer", desc);
Expand Down

0 comments on commit 047e688

Please sign in to comment.