FairRoot/PandaRoot
PndPatternMatcher.cxx
Go to the documentation of this file.
1 /*
2  * PatternMatcher.cxx
3  *
4  * Created on: Jun 16, 2017
5  * Author: Michael Papenbrock
6  */
7 
8 #include "PndPatternMatcher.h"
9 
10 #include <FairRunAna.h>
11 #include <FairRuntimeDb.h>
12 #include <PndSttHit.h>
13 
14 #include "PndPattern.h"
15 #include <TTreeReaderValue.h>
16 #include <algorithm>
17 
18 
20 
21 PndPatternMatcher::PndPatternMatcher() : fPartialMatchCand(0) {
22  dbFile = NULL;
23  patternTree = NULL;
24 // treeReader = NULL;
25  fSttParameters = NULL;
26  fEventHeader = NULL;
27  fSTTHitArray = NULL;
28 
29 
30  fTrackCandName = "PartialMatchCand";
31  fPersistence = kTRUE;
32 
33  verboseMatches = false;
34  fMatchRatio = 0.5;
35 }
36 
38 }
39 
41  FairRuntimeDb *rtdb = FairRunAna::Instance()->GetRuntimeDb();
42  fSttParameters = (PndGeoSttPar*) rtdb->getContainer("PndGeoSttPar");
43 }
45  LoadPatternDB("patternDB.root");
46 
47 // Get the running instance of the FairRootManager to access tree branches
48  FairRootManager *ioman = FairRootManager::Instance();
49  if(!ioman) {
50  std::cout << "-E- PatternDBGenerator::Init: FairRootManager not instantiated!" << std::endl;
51  return kFATAL;
52  }
53 
54  fEventHeader = (TClonesArray*) ioman->GetObject("EventHeader.");
55  if (!fEventHeader) {
56  std::cout << "-E- PatternDBGenerator:Init: EventHeader not instantiated!" << std::endl;
57  }
58 
59 // Access the STTHit branch
60  fSTTHitArray = (TClonesArray*) ioman->GetObject("STTHit");
61 
62 // Set the output branch
63  fPartialMatchCand = ioman->Register(fTrackCandName,"PndTrackCand","STT",fPersistence);
64 
65  return kSUCCESS;
66 }
67 void PndPatternMatcher::Exec(Option_t* ) {
68  fPartialCand.clear();
69  fPartialMatchCand->Delete();
70  FindMatch();
71 
72 // Write trackCands into the data stream
73  for (auto const& trackCand : fPartialCand) {
74  new ((*fPartialMatchCand)[fPartialMatchCand->GetEntriesFast()]) PndTrackCand(trackCand);
75  }
76 }
78 
79 }
80 
82 // Get a list of all tubeIDs in the event
83  int nSttHits = fSTTHitArray->GetEntriesFast();
84  std::set<int> tubeIDs;
85  for (int iHit = 0; iHit < nSttHits; ++iHit) {
86  PndSttHit *sttHit = (PndSttHit*) fSTTHitArray->At(iHit);
87  int tubeID = sttHit->GetTubeID();
88  tubeIDs.insert(tubeID);
89  }
90 
91 // std::cout << dbTubeIDs.at(1).size() << std::endl;
92 
93 
94  int matches = 0;
95 // Compare each pattern in the data base with the current set of tubeIDs
96  for (auto const& dbPattern : dbTubeIDs) {
97  std::set<int> intersection;
98  intersection.clear();
99 // Determine the intersection between the current database pattern and the event's set of tubeIDs
100  std::set_intersection(tubeIDs.begin(), tubeIDs.end(), dbPattern.begin(), dbPattern.end(), std::inserter(intersection,intersection.begin()));
101 // Determine the ratio to which there is an overlap with the database pattern
102  float matchRatio = (float)intersection.size() / (float)dbPattern.size();
103 // If the matching ratio is large enough, proceed to create trackCand from intersection
104  if (matchRatio > fMatchRatio) {
105 // std::cout<< "match ratio: " << matchRatio << std::endl;
106  matches++;
107 
108 // Detailed output in case it is needed (set verboseMatches to true)
109  if (verboseMatches) {
110  if (intersection.size() > 0) {
111  std::cout << "matching tubes: ";
112  for (auto const& tube : intersection) {
113  std::cout << tube << " ";
114  }
115  std::cout << std::endl;
116  }
117  }
118 
119  CreateTrackCandFromMatch(intersection);
120 
121  }
122  }
123  if (verboseMatches) std::cout << "partial matches found: " << matches << std::endl;
124 }
125 void PndPatternMatcher::CreateTrackCandFromMatch(std::set<int> partialMatch) {
126 // Create a PndTrackCand from the partial match found in FindMatch()
127  PndTrackCand trackCand;
128  int nSttHits = fSTTHitArray->GetEntriesFast();
129 // Loop over all STT hits
130  for (int iHit = 0; iHit < nSttHits; ++iHit) {
131  PndSttHit *sttHit = (PndSttHit*) fSTTHitArray->At(iHit);
132  int tubeID = sttHit->GetTubeID();
133 // verify if this hit is part of the partial match
134  auto search = partialMatch.find(tubeID);
135 // if hit is found in partial match, add it to track cand
136  if (search != partialMatch.end()) {
137  trackCand.AddHit(sttHit->GetEntryNr(),iHit);
138  }
139  }
140 // Add new trackCand to vector of all trackCands for this event
141  fPartialCand.push_back(trackCand);
142 }
144  dbFile = new TFile(filename,"READ");
145  patternTree = (TTree*) dbFile->Get("trackPatterns");
146 
147  dbTubeIDs.clear();
148 
149  PndPattern *pattern;
150  patternTree->SetBranchAddress("pattern", &pattern);
151 
152  Long64_t nPatterns = patternTree->GetEntriesFast();
153  for (Long64_t iEntry = 0; iEntry < nPatterns; ++iEntry) {
154  patternTree->GetEntry(iEntry);
155  std::set<int> tubeIDs = pattern->GetTubeIDs();
156  dbTubeIDs.push_back(tubeIDs);
157  }
158  std::cout << "# DB patterns: " << dbTubeIDs.size() << std::endl;
159 
160 }
virtual void Exec(Option_t *opt)
virtual void SetParContainers()
std::vector< PndTrackCand > fPartialCand
virtual void FinishTask()
TClonesArray * fEventHeader
PndGeoSttPar * fSttParameters
void LoadPatternDB(TString filename)
void AddHit(UInt_t detId, UInt_t hitId, Double_t rho)
virtual InitStatus Init()
std::set< int > GetTubeIDs() const
Definition: PndPattern.h:32
TClonesArray * fSTTHitArray
FairRuntimeDb * rtdb
Definition: hit_dirc.C:66
Int_t GetTubeID() const
Definition: PndSttHit.h:75
TClonesArray * fPartialMatchCand
std::vector< std::set< int > > dbTubeIDs
ClassImp(PndAnaContFact)
void CreateTrackCandFromMatch(std::set< int > partialMatch)
const string filename