diff --git a/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuMcReader.cc b/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuMcReader.cc index 2bca1cd..1ceba69 100644 --- a/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuMcReader.cc +++ b/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuMcReader.cc @@ -8,6 +8,7 @@ #include "BDtaunuReader.h" #include "BDtaunuMcReader.h" #include "McGraphManager.h" +#include "TruthMatcher.h" using namespace boost; using namespace bdtaunu; @@ -24,6 +25,7 @@ BDtaunuMcReader::BDtaunuMcReader( AllocateBuffer(); ClearBuffer(); mc_graph_manager = McGraphManager(this); + truth_matcher = TruthMatcher(this); } @@ -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); @@ -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); } @@ -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 @@ -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(); } diff --git a/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuMcReader.h b/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuMcReader.h index 9f8387b..647fe43 100644 --- a/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuMcReader.h +++ b/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuMcReader.h @@ -10,6 +10,8 @@ #include "BDtaunuReader.h" #include "McGraphManager.h" +#include "TruthMatcher.h" + /** * @brief * Much like its parent class BDtaunuReader, this class reads @@ -32,6 +34,9 @@ class BDtaunuMcReader : public BDtaunuReader { friend class McGraphManager; + friend class TruthMatcher; + friend class TruthMatchDfsVisitor; + public: // Constructors @@ -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; @@ -93,6 +101,8 @@ class BDtaunuMcReader : public BDtaunuReader { // MC graph helpers McGraphManager mc_graph_manager; + TruthMatcher truth_matcher; + }; #endif diff --git a/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuReader.h b/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuReader.h index 0c82d49..cd47572 100644 --- a/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuReader.h +++ b/create_database/tuple_reader/bdtaunu_tuple_analyzer/BDtaunuReader.h @@ -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 @@ -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; @@ -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 upsilon_candidates; @@ -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; diff --git a/create_database/tuple_reader/bdtaunu_tuple_analyzer/GraphDef.cc b/create_database/tuple_reader/bdtaunu_tuple_analyzer/GraphDef.cc index 4b9de4d..3167152 100644 --- a/create_database/tuple_reader/bdtaunu_tuple_analyzer/GraphDef.cc +++ b/create_database/tuple_reader/bdtaunu_tuple_analyzer/GraphDef.cc @@ -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; diff --git a/create_database/tuple_reader/bdtaunu_tuple_analyzer/GraphDef.h b/create_database/tuple_reader/bdtaunu_tuple_analyzer/GraphDef.h index 39650d2..21ba7cd 100644 --- a/create_database/tuple_reader/bdtaunu_tuple_analyzer/GraphDef.h +++ b/create_database/tuple_reader/bdtaunu_tuple_analyzer/GraphDef.h @@ -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; } diff --git a/create_database/tuple_reader/bdtaunu_tuple_analyzer/Makefile b/create_database/tuple_reader/bdtaunu_tuple_analyzer/Makefile index fa00a04..7f17f4f 100644 --- a/create_database/tuple_reader/bdtaunu_tuple_analyzer/Makefile +++ b/create_database/tuple_reader/bdtaunu_tuple_analyzer/Makefile @@ -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 # ------------ diff --git a/create_database/tuple_reader/bdtaunu_tuple_analyzer/RecoGraphManager.h b/create_database/tuple_reader/bdtaunu_tuple_analyzer/RecoGraphManager.h index 9f80062..5126d27 100644 --- a/create_database/tuple_reader/bdtaunu_tuple_analyzer/RecoGraphManager.h +++ b/create_database/tuple_reader/bdtaunu_tuple_analyzer/RecoGraphManager.h @@ -9,6 +9,8 @@ #include "GraphManager.h" #include "RecoGraphVisitors.h" +#include "TruthMatcher.h" + class BDtaunuReader; /** @brief This class builds and analyzes the reconstructed particle @@ -106,6 +108,8 @@ class BDtaunuReader; class RecoGraphManager : public GraphManager { friend class RecoGraphDfsVisitor; + friend class TruthMatcher; + friend class TruthMatchDfsVisitor; public: diff --git a/create_database/tuple_reader/bdtaunu_tuple_analyzer/TruthMatcher.cc b/create_database/tuple_reader/bdtaunu_tuple_analyzer/TruthMatcher.cc new file mode 100644 index 0000000..ebb2da8 --- /dev/null +++ b/create_database/tuple_reader/bdtaunu_tuple_analyzer/TruthMatcher.cc @@ -0,0 +1,65 @@ +#include +#include +#include + +#include + +#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::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)); + } +} diff --git a/create_database/tuple_reader/bdtaunu_tuple_analyzer/TruthMatcher.h b/create_database/tuple_reader/bdtaunu_tuple_analyzer/TruthMatcher.h new file mode 100644 index 0000000..ef661db --- /dev/null +++ b/create_database/tuple_reader/bdtaunu_tuple_analyzer/TruthMatcher.h @@ -0,0 +1,48 @@ +#ifndef _TRUTHMATCHER_H_ +#define _TRUTHMATCHER_H_ + +#include +#include + +#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 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