Skip to content

Commit

Permalink
Added a truth matcher class
Browse files Browse the repository at this point in the history
  • Loading branch information
dchao committed Sep 24, 2014
1 parent 4b457e8 commit fd08fbb
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "BDtaunuReader.h"
#include "BDtaunuMcReader.h"
#include "McGraphManager.h"
#include "TruthMatcher.h"

using namespace boost;
using namespace bdtaunu;
Expand All @@ -24,6 +25,7 @@ BDtaunuMcReader::BDtaunuMcReader(
AllocateBuffer();
ClearBuffer();
mc_graph_manager = McGraphManager(this);
truth_matcher = TruthMatcher(this);

}

Expand All @@ -41,6 +43,9 @@ void BDtaunuMcReader::AllocateBuffer() {
dauIdx = new int[max_mc_length];
dauLen = new int[max_mc_length];
mcenergy = new float[max_mc_length];
hMCIdx = new int[maximum_h_candidates];
lMCIdx = new int[maximum_l_candidates];
gammaMCIdx = new int[maximum_gamma_candidates];

// Specify the variables where each ntuple branch should be read into.
tr->SetBranchAddress("mcLen", &mcLen);
Expand All @@ -49,6 +54,9 @@ void BDtaunuMcReader::AllocateBuffer() {
tr->SetBranchAddress("dauIdx", dauIdx);
tr->SetBranchAddress("dauLen", dauLen);
tr->SetBranchAddress("mcenergy", mcenergy);
tr->SetBranchAddress("hMCIdx", hMCIdx);
tr->SetBranchAddress("lMCIdx", lMCIdx);
tr->SetBranchAddress("gammaMCIdx", gammaMCIdx);

}

Expand All @@ -69,6 +77,9 @@ void BDtaunuMcReader::DeleteBuffer() {
delete[] dauIdx;
delete[] dauLen;
delete[] mcenergy;
delete[] hMCIdx;
delete[] lMCIdx;
delete[] gammaMCIdx;
}

// Read in the next event in the ntuple and update the buffer
Expand All @@ -95,6 +106,9 @@ RootReader::Status BDtaunuMcReader::next_record() {
mc_graph_manager.construct_graph();
mc_graph_manager.analyze_graph();

// Truth Match
truth_matcher.analyze();

// Make derived information ready for access
FillMcInfo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "BDtaunuReader.h"
#include "McGraphManager.h"

#include "TruthMatcher.h"

/**
* @brief
* Much like its parent class BDtaunuReader, this class reads
Expand All @@ -32,6 +34,9 @@ class BDtaunuMcReader : public BDtaunuReader {

friend class McGraphManager;

friend class TruthMatcher;
friend class TruthMatchDfsVisitor;

public:

// Constructors
Expand Down Expand Up @@ -74,6 +79,9 @@ class BDtaunuMcReader : public BDtaunuReader {
int *dauIdx;
int *dauLen;
float *mcenergy;
int *hMCIdx;
int *lMCIdx;
int *gammaMCIdx;

bool continuum;
bdtaunu::McBTypeCatalogue::BMcType b1_mctype, b2_mctype;
Expand All @@ -93,6 +101,8 @@ class BDtaunuMcReader : public BDtaunuReader {
// MC graph helpers
McGraphManager mc_graph_manager;

TruthMatcher truth_matcher;

};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class BDtaunuReader : public RootReader {
// Manages reco particle graph and all information computed from it.
friend class RecoGraphManager;

friend class TruthMatcher;
friend class TruthMatchDfsVisitor;

public:

// Constructors
Expand Down Expand Up @@ -92,6 +95,8 @@ class BDtaunuReader : public RootReader {
static const int maximum_B_candidates;
static const int maximum_D_candidates;
static const int maximum_C_candidates;

protected:
static const int maximum_h_candidates;
static const int maximum_l_candidates;
static const int maximum_gamma_candidates;
Expand Down Expand Up @@ -133,6 +138,7 @@ class BDtaunuReader : public RootReader {
int *hd1Lund, *hd2Lund;
int *ld1Lund, *ld2Lund, *ld3Lund;

protected:
// Data derived from information present in the buffer elements
std::vector<UpsilonCandidate> upsilon_candidates;

Expand All @@ -149,6 +155,7 @@ class BDtaunuReader : public RootReader {
// Mutator helpers
void FillRecoInfo();

protected:
// Reco graph manager.
// Responsible for all reco graph related computations.
RecoGraphManager reco_graph_manager;
Expand Down
18 changes: 18 additions & 0 deletions create_database/tuple_reader/bdtaunu_tuple_analyzer/GraphDef.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ int RecoGraph::RecoIndexer::operator()(int lund, int idx) const {
return -1;
}

bool RecoGraph::RecoIndexer::is_h_candidate(int reco_index) const {
int h_offset = nY + nB + nD + nC;
return ((reco_index >= h_offset) &&
(reco_index < h_offset + nh)) ? true : false;
}

bool RecoGraph::RecoIndexer::is_l_candidate(int reco_index) const {
int l_offset = nY + nB + nD + nC + nh;
return ((reco_index >= l_offset) &&
(reco_index < l_offset + nl)) ? true : false;
}

bool RecoGraph::RecoIndexer::is_gamma_candidate(int reco_index) const {
int gamma_offset = nY + nB + nD + nC + nh + nl;
return ((reco_index >= gamma_offset) &&
(reco_index < gamma_offset + ngamma)) ? true : false;
}

void RecoGraph::RecoIndexer::clear() {
nY = 0;
nB = 0;
Expand Down
11 changes: 10 additions & 1 deletion create_database/tuple_reader/bdtaunu_tuple_analyzer/GraphDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,16 @@ class RecoIndexer {
~RecoIndexer() {};

//! Given the lundId and block index, return the unique reco index.
int operator()(int lund, int idx) const;
int operator()(int lund, int block_idx) const;

//! Given the reco index, decide if it is a h candidate.
bool is_h_candidate(int reco_index) const;

//! Given the reco index, decide if it is a l candidate.
bool is_l_candidate(int reco_index) const;

//! Given the reco index, decide if it is a gamm candidate.
bool is_gamma_candidate(int reco_index) const;

//! Return total number of reco particles in this event.
int total() const { return nY + nB + nD + nC + nh + nl + ngamma; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ SOURCES = BDtaunuDef.cc GraphDef.cc \
BDtaunuUtils.cc UpsilonCandidate.cc \
RootReader.cc BDtaunuReader.cc BDtaunuMcReader.cc \
RecoGraphVisitors.cc RecoGraphManager.cc \
McGraphManager.cc McGraphVisitors.cc
McGraphManager.cc McGraphVisitors.cc TruthMatcher.cc

# Dependencies
# ------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "GraphManager.h"
#include "RecoGraphVisitors.h"

#include "TruthMatcher.h"

class BDtaunuReader;

/** @brief This class builds and analyzes the reconstructed particle
Expand Down Expand Up @@ -106,6 +108,8 @@ class BDtaunuReader;
class RecoGraphManager : public GraphManager {

friend class RecoGraphDfsVisitor;
friend class TruthMatcher;
friend class TruthMatchDfsVisitor;

public:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <map>
#include <utility>
#include <cassert>

#include <boost/graph/depth_first_search.hpp>

#include "TruthMatcher.h"
#include "BDtaunuMcReader.h"
#include "GraphDef.h"

using namespace boost;

TruthMatcher::TruthMatcher() : reader(nullptr) {
}

TruthMatcher::TruthMatcher(BDtaunuMcReader *_reader) : reader(_reader) {
}

bool TruthMatcher::is_truth_matched(int reco_idx) const {
std::map<int, bool>::const_iterator it = truth_match.find(reco_idx);
assert(it != truth_match.end());
return it->second;
}

void TruthMatcher::analyze() {
truth_match.clear();
depth_first_search((reader->reco_graph_manager).g, visitor(TruthMatchDfsVisitor(this)));
}

TruthMatchDfsVisitor::TruthMatchDfsVisitor() : tm(nullptr) {
}

TruthMatchDfsVisitor::TruthMatchDfsVisitor(TruthMatcher *_tm) : tm(_tm) {
lund_pm = get(vertex_lund_id, (tm->reader->reco_graph_manager).g);
reco_idx_pm = get(vertex_reco_index, (tm->reader->reco_graph_manager).g);
block_idx_pm = get(vertex_block_index, (tm->reader->reco_graph_manager).g);
}

void TruthMatchDfsVisitor::finish_vertex(RecoGraph::Vertex u, const RecoGraph::Graph &g) {

int reco_idx = get(reco_idx_pm, u);
if ((tm->reader->reco_graph_manager).reco_indexer.is_h_candidate(reco_idx)) {
bool is_tm = false;
if (tm->reader->hMCIdx[get(block_idx_pm, u)] >= 0) is_tm = true;
(tm->truth_match).insert(std::make_pair(reco_idx, is_tm));
} else if ((tm->reader->reco_graph_manager).reco_indexer.is_l_candidate(reco_idx)) {
bool is_tm = false;
if (tm->reader->lMCIdx[get(block_idx_pm, u)] >= 0) is_tm = true;
(tm->truth_match).insert(std::make_pair(reco_idx, is_tm));
} else if ((tm->reader->reco_graph_manager).reco_indexer.is_gamma_candidate(reco_idx)) {
bool is_tm = false;
if (tm->reader->gammaMCIdx[get(block_idx_pm, u)] >= 0) is_tm = true;
(tm->truth_match).insert(std::make_pair(reco_idx, is_tm));
} else {
bool is_tm = true;
RecoGraph::AdjacencyIterator ai, ai_end;
for (tie(ai, ai_end) = adjacent_vertices(u, g); ai != ai_end; ++ai) {
if (!(tm->truth_match).find(get(reco_idx_pm, *ai))->second) {
is_tm = false;
break;
}
}
(tm->truth_match).insert(std::make_pair(reco_idx, is_tm));
}
}
48 changes: 48 additions & 0 deletions create_database/tuple_reader/bdtaunu_tuple_analyzer/TruthMatcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef _TRUTHMATCHER_H_
#define _TRUTHMATCHER_H_

#include <map>
#include <boost/graph/depth_first_search.hpp>

#include "GraphDef.h"

class BDtaunuMcReader;

class TruthMatcher {

friend class TruthMatchDfsVisitor;

public:
TruthMatcher();
TruthMatcher(BDtaunuMcReader *reader);
TruthMatcher(const TruthMatcher&) = default;
TruthMatcher &operator=(const TruthMatcher&) = default;

bool is_truth_matched(int reco_idx) const;

void analyze();

private:

std::map<int, bool> truth_match;

BDtaunuMcReader *reader;
};

class TruthMatchDfsVisitor : public boost::default_dfs_visitor {

public:
TruthMatchDfsVisitor();
TruthMatchDfsVisitor(TruthMatcher*);
~TruthMatchDfsVisitor() {};

void finish_vertex(RecoGraph::Vertex u, const RecoGraph::Graph &g);

private:
TruthMatcher *tm;
RecoGraph::LundIdPropertyMap lund_pm;
RecoGraph::RecoIndexPropertyMap reco_idx_pm;
RecoGraph::BlockIndexPropertyMap block_idx_pm;
};

#endif

0 comments on commit fd08fbb

Please sign in to comment.