9 #include <tracking/modules/VXDTFHelperTools/RawSecMapMergerModule.h>
10 #include <tracking/spacePointCreation/SpacePoint.h>
11 #include <tracking/trackFindingVXD/environment/VXDTFFiltersHelperFunctions.h>
12 #include <tracking/trackFindingVXD/filterMap/filterFramework/SelectionVariableNamesToFunctions.h>
13 #include <vxd/geometry/GeoCache.h>
30 setDescription(
"this module takes a root file containing a raw sectorMap created by the SecMapTrainerBaseModule and converts it to a sectormap which can be read by the VXDTF. Please check the parameters to be set...");
34 addParam(
"rootFileNames", m_PARAMrootFileNames,
35 "List of files (wildcards not allowed - use python glob.glob() to expand to list of files)", {
"lowTestRedesign_454970355.root"});
37 addParam(
"mapNames", m_PARAMmapNames,
"names of sectorMaps to be loaded.", {
""});
39 addParam(
"printFullGraphs", m_PARAMprintFullGraphs,
40 "If true, the full trained graphs will be printed to screen. WARNING: produces a lot of output for full detector-cases!",
43 addParam(
"threshold", m_RelThreshold,
44 "Relative threshold (in %) used to prune the sector maps. Will remove X % of the least used subgraphs.", {0});
51 std::vector<std::string> RawSecMapMergerModule::getRootFiles(std::string mapName)
53 B2INFO(
"RawSecMapMerger::getRootFiles(): loading mapName: " << mapName);
55 vector<string> files4ThisMap;
56 for (
string& fileName : m_PARAMrootFileNames) {
57 if (fileName.find(mapName) == string::npos) {
58 B2DEBUG(1,
"getRootFiles: fileName " << fileName <<
" was _not_ accepted for map " << mapName);
61 B2DEBUG(1,
"getRootFiles: fileName " << fileName <<
" accepted for map " << mapName);
62 files4ThisMap.push_back(fileName);
71 std::unique_ptr<TChain> RawSecMapMergerModule::createTreeChain(
const SectorMapConfig& configuration,
const std::string& nHitString)
73 B2INFO(
"RawSecMapMerger::createTreeChain(): loading mapName: " << configuration.secMapName <<
" with extension " << nHitString);
74 unique_ptr<TChain> treeChain = unique_ptr<TChain>(
new TChain((configuration.secMapName + nHitString).c_str()));
76 vector<string> fileList = getRootFiles(configuration.secMapName);
77 for (
string& file : fileList) { treeChain->Add(file.c_str()); }
86 template<
class ValueType> std::vector<BranchInterface<ValueType>> RawSecMapMergerModule::getBranches(
87 std::unique_ptr<TChain>& chain,
88 const std::vector<std::string>& branchNames)
90 vector<BranchInterface<ValueType>> branches;
91 B2INFO(
"RawSecMapMerger::getBranches(): loading branches: " << branchNames.size());
92 unsigned nBranches = branchNames.size();
95 for (
unsigned fPos = 0; fPos < nBranches; fPos++) {
96 branches[fPos].name = branchNames[fPos];
97 chain->SetBranchAddress(
98 branches[fPos].name.c_str(),
99 &(branches[fPos].value),
100 &(branches[fPos].branch));
102 B2INFO(
"RawSecMapMerger::getBranches(): done");
107 RawSecMapMergerModule::prepareNHitSpecificStuff(
110 std::vector<std::string>& secBranchNames,
111 std::vector<std::string>& filterBranchNames)
114 secBranchNames = {
"outerSecID",
"innerSecID"};
117 for (
const auto& filterNameToFunction : twoHitsFilterNameToFunction) {
118 string filterName = filterNameToFunction.first ;
119 filterBranchNames.push_back(filterName);
125 secBranchNames = {
"outerSecID",
"centerSecID",
"innerSecID"};
128 for (
const auto& filterNameToFunction : threeHitsFilterNameToFunction) {
129 string filterName = filterNameToFunction.first ;
130 filterBranchNames.push_back(filterName);
135 B2ERROR(
"prepareNHitSpecificStuff: wrong chainLength!");
139 template <
class FilterType>
void RawSecMapMergerModule::trainGraph(
141 std::unique_ptr<TChain>& chain,
145 auto nEntries = chain->GetEntries();
146 B2DEBUG(10,
"RawSecMapMerger::trainGraph(): start of " << nEntries <<
" entries in tree and " << sectorBranches.size() <<
148 if (nEntries == 0) { B2WARNING(
"trainGraph: valid file but no data stored!");
return; }
150 auto percentMark = nEntries / 10;
auto progressCounter = 0;
152 for (
auto i = 0 ; i <= nEntries; i++) {
153 if (percentMark < 1 or (i % percentMark) == 0) {
154 B2INFO(
"RawSecMapMerger::trainGraph(): with mark: " << percentMark <<
" and i=" << i <<
", " << progressCounter <<
155 "% related, mainGraph has got " << mainGraph.
size() <<
" sectors...");
156 progressCounter += 10;
158 auto thisEntry = chain->LoadTree(i);
160 auto ids = getSecIDs(sectorBranches, thisEntry);
163 auto pos = mainGraph.
find(currentID);
165 if (pos == mainGraph.
end()) { B2WARNING(
"trainGraph: could not find subgraph " << currentID.print() <<
" - skipping entry...");
continue; }
169 pos->second.addValue(FilterType(
filter.name),
filter.value);
179 std::unique_ptr<TChain>& chain,
183 auto nEntries = chain->GetEntries();
184 B2INFO(
"RawSecMapMerger::buildGraph(): start of " << nEntries <<
" entries in tree and " << sectorBranches.size() <<
188 vector<string> filterNames;
190 filterNames.push_back(entry.name);
194 if (nEntries == 0) { B2WARNING(
"buildGraph: valid file but no data stored!");
return mainGraph; }
195 auto percentMark = nEntries / 10;
196 auto progressCounter = 0;
199 for (
auto i = 0 ; i <= nEntries; i++) {
200 if (percentMark < 1 or (i % percentMark) == 0) {
201 B2INFO(
"RawSecMapMerger::buildGraph(): with mark: " << percentMark <<
" and i=" << i <<
", " << progressCounter <<
202 "% related, mainGraph has got " << mainGraph.
size() <<
" sectors...");
203 progressCounter += 10;
205 auto thisEntry = chain->LoadTree(i);
207 std::vector<unsigned> ids = getSecIDs(sectorBranches, thisEntry);
213 B2DEBUG(10,
"buildGraph-SubGraphID-print: id: " << currentID.print());
215 auto pos = mainGraph.
find(currentID);
217 if (pos == mainGraph.
end()) { pos = mainGraph.
add(currentID); }
219 if (pos == mainGraph.
end()) { B2WARNING(
"could not find nor add subgraph - skipping entry...");
continue; }
221 pos->second.wasFound();
225 pos->second.checkAndReplaceIfMinMax(FilterType(
filter.name),
filter.value);
229 B2INFO(
"RawSecMapMerger::buildGraph(): mainGraph finished - has now size: " << mainGraph.
size());
230 B2DEBUG(1,
"fullGraph-Print: " << mainGraph.
print());
236 bool RawSecMapMergerModule::good(
const std::vector<unsigned>& ids)
238 switch (ids.size()) {
265 void RawSecMapMergerModule::printData(
266 std::unique_ptr<TChain>& chain,
271 unsigned nEntries = chain->GetEntries();
272 unsigned percentMark = 1;
273 if (nEntries > 100) { percentMark = nEntries / 50; }
274 unsigned progressCounter = 0;
276 B2INFO(
"RawSecMapMerger::printData(): start of " << nEntries <<
277 " entries in tree and " << sectorBranches.size() <<
279 " sector-/filter-branches");
281 for (
unsigned i = 0 ; i < nEntries; i++) {
282 if (percentMark > 1 and (i % percentMark) != 0) {
continue; }
283 progressCounter += 2;
284 B2INFO(
"RawSecMapMerger::printData(): entry " << i <<
" of " << nEntries <<
":");
286 auto thisEntry = chain->LoadTree(i);
289 for (
unsigned k = 0 ; k < sectorBranches.size(); k++) {
290 sectorBranches[k].branch->GetEntry(thisEntry);
308 std::string configName,
unsigned int nHitCombinations,
bool print2File)
310 SecMapHelper::printStaticSectorRelations<SpacePoint>(filters , configName , nHitCombinations, print2File);
347 std::vector<VxdID> vxdIDs = VXD::GeoCache::getInstance().getListOfSensors();
352 for (
VxdID sensor : vxdIDs) {
357 std::sort(allTrainedSecIDsOfSensor.begin(), allTrainedSecIDsOfSensor.end());
358 allTrainedSecIDsOfSensor.erase(
360 allTrainedSecIDsOfSensor.begin(),
361 allTrainedSecIDsOfSensor.end()),
362 allTrainedSecIDsOfSensor.end());
364 for (
FullSecID sector : allTrainedSecIDsOfSensor) {
366 bool success = segFilters.
setSubLayerIDs(sector, sector.getSubLayerID());
368 if (!success) B2FATAL(
"There is a mismatch between the FullSecIDs in the Trainings Graph and the SectorMap!");
371 B2DEBUG(1,
"Sensor: " << sensor <<
" had " << allTrainedSecIDsOfSensor.size() <<
" trained IDs and ");
374 return vxdIDs.size() + 1;
382 template <
class FilterType>
void RawSecMapMergerModule::getSegmentFilters(
403 if (nSecChainLength == 2) updateFilterSubLayerIDs(mainGraph, *xHitFilters);
405 B2DEBUG(1,
"RawSecMapMerger::getSegmentFilters: secMap " << config.secMapName <<
" got the following sectors:\n" <<
409 for (
auto& subGraph : mainGraph) {
411 if (nSecChainLength == 2) {
412 add2HitFilters(*xHitFilters, subGraph.second, config);
413 }
else if (nSecChainLength == 3) {
414 add3HitFilters(*xHitFilters, subGraph.second, config);
415 }
else if (nSecChainLength == 4) {
416 add4HitFilters(*xHitFilters, subGraph.second, config);
417 }
else { B2FATAL(
"nSecChainLength " << nSecChainLength <<
" is not within allowed range [2;4]!"); }
436 std::string filterVals;
437 for (
const auto& filterNameToFunction : filterNameToFunctions) {
438 string filterName = filterNameToFunction.first ;
439 filterVals += filterName +
": "
440 + std::to_string(filterCutsMap.at(filterName).getMin())
442 + std::to_string(filterCutsMap.at(filterName).getMax()) +
", ";
444 B2DEBUG(1,
"SubGraph " << subGraph.
getID().
print() <<
" - filter:min/max: " << filterVals);
449 (filterCutsMap.at(
"DistanceInTimeUside").getMin() <= DistanceInTimeUside<SpacePoint>() <=
450 filterCutsMap.at(
"DistanceInTimeUside").getMax()) &&
451 (filterCutsMap.at(
"DistanceInTimeVside").getMin() <= DistanceInTimeVside<SpacePoint>() <=
452 filterCutsMap.at(
"DistanceInTimeVside").getMax()) &&
453 (filterCutsMap.at(
"Distance3DSquared").getMin() <= Distance3DSquared<SpacePoint>() <=
454 filterCutsMap.at(
"Distance3DSquared").getMax()) &&
455 (filterCutsMap.at(
"Distance2DXYSquared").getMin() <= Distance2DXYSquared<SpacePoint>() <=
456 filterCutsMap.at(
"Distance2DXYSquared").getMax()) &&
457 (filterCutsMap.at(
"Distance1DZ").getMin() <= Distance1DZ<SpacePoint>() <= filterCutsMap.at(
"Distance1DZ").getMax()) &&
458 (filterCutsMap.at(
"SlopeRZ").getMin() <= SlopeRZ<SpacePoint>() <= filterCutsMap.at(
"SlopeRZ").getMax()) &&
459 (filterCutsMap.at(
"Distance3DNormed").getMin() <= Distance3DNormed<SpacePoint>() <=
460 filterCutsMap.at(
"Distance3DNormed").getMax())
467 B2DEBUG(1,
"RawSecMapMerger::add2HitFilters: now adding FriendSectorFilter for secIDs (outer/inner): " << secIDs.at(
468 0) <<
"/" << secIDs.at(1));
470 friendSectorsSegmentFilter) == 0)
471 B2WARNING(
"secMap: " << config.secMapName <<
"Problem adding the friendship relation from the inner sector:" <<
472 secIDs.at(1) <<
" -> " << secIDs.at(0) <<
" outer sector");
485 std::string filterVals;
487 for (
auto& filterNameToFunction : filterNameToFunctions) {
488 string filterName = filterNameToFunction.first ;
489 filterVals += filterName +
": "
490 + std::to_string(filterCutsMap.at(filterName).getMin())
492 + std::to_string(filterCutsMap.at(filterName).getMax()) +
", ";
494 B2DEBUG(1,
"SubGraph " << subGraph.
getID().
print() <<
" - filter:min/max: " << filterVals);
498 ((filterCutsMap.at(
"DistanceInTime").getMin() <= DistanceInTime<SpacePoint>() <= filterCutsMap.at(
"DistanceInTime").getMax()) &&
499 (filterCutsMap.at(
"Angle3DSimple").getMin() <= Angle3DSimple<SpacePoint>() <= filterCutsMap.at(
"Angle3DSimple").getMax()) &&
500 (filterCutsMap.at(
"CosAngleXY").getMin() <= CosAngleXY<SpacePoint>() <= filterCutsMap.at(
"CosAngleXY").getMax()) &&
501 (filterCutsMap.at(
"AngleRZSimple").getMin() <= AngleRZSimple<SpacePoint>() <= filterCutsMap.at(
"AngleRZSimple").getMax()) &&
502 (CircleDist2IP<SpacePoint>() <= filterCutsMap.at(
"CircleDist2IP").getMax()) &&
503 (filterCutsMap.at(
"DeltaSlopeRZ").getMin() <= DeltaSlopeRZ<SpacePoint>()) <= filterCutsMap.at(
"DeltaSlopeRZ").getMax() &&
504 (filterCutsMap.at(
"DeltaSlopeZoverS").getMin() <= DeltaSlopeZoverS<SpacePoint>() <=
505 filterCutsMap.at(
"DeltaSlopeZoverS").getMax()) &&
506 (filterCutsMap.at(
"DeltaSoverZ").getMin() <= DeltaSoverZ<SpacePoint>() <= filterCutsMap.at(
"DeltaSoverZ").getMax()) &&
507 (filterCutsMap.at(
"HelixParameterFit").getMin() <= HelixParameterFit<SpacePoint>() <=
508 filterCutsMap.at(
"HelixParameterFit").getMax()) &&
509 (filterCutsMap.at(
"Pt").getMin() <= Pt<SpacePoint>() <= filterCutsMap.at(
"Pt").getMax()) &&
510 (filterCutsMap.at(
"CircleRadius").getMin() <= CircleRadius<SpacePoint>() <= filterCutsMap.at(
"CircleRadius").getMax())
518 B2DEBUG(1,
"RawSecMapMerger::add3HitFilters: now adding FriendSectorFilter for secIDs (outer/center/inner): "
519 << secIDs.at(0) <<
"/"
520 << secIDs.at(1) <<
"/"
523 threeHitFilter) == 0)
524 B2WARNING(
"secMap: " << config.secMapName <<
"Problem adding the friendship relation for the secIDs (outer/center/inner): "
525 << secIDs.at(0) <<
"/"
526 << secIDs.at(1) <<
"/"
532 template <
class FilterType>
void RawSecMapMergerModule::add4HitFilters(
Class to identify a sector inside of the VXD.
std::string getFullSecString() const
returns the FullSecID coded as string compatible to secIDs stored in the xml-sectormaps
The RawSecMapMergerModule.
unsigned size() const
returns number of collected subgraphs so far.
Iterator find(SubGraphID idChain)
find entry.
Iterator add(SubGraphID &newID)
add new subgraph if not added already.
std::string print(bool fullPrint=true) const
returns a string giving an overview of the graph.
Iterator end()
returns end of subGraphs.
std::vector< FullSecID > getAllFullSecIDsOfSensor(VxdID sensor)
returns a Vector containing all FullSecIDs found for given sensor.
stores the ID of a subgraph, which is basically a chain of FullSecID coded as unsigned ints.
std::string print() const
returns string of entries.
std::vector< FullSecID > getFullSecIDs() const
returns SecIDs coded as FullSecIDs:
contains all relevant stuff needed for dealing with a subGraph.
const SubGraphID & getID() const
returns iD of this graph
const std::unordered_map< FilterType, MinMax > & getFinalQuantileValues()
this deletes the old min and max values stored and replaces them with the quantiles to be found.
Class that contains all the static sectors to which the filters are attached.
bool setSubLayerIDs(FullSecID sector, int sublayer)
during the trainings phase the sublayer ids have to be updated
int addThreeHitFilter(FullSecID outer, FullSecID center, FullSecID inner, const threeHitFilter_t &filter)
adds a three hit filter
int addTwoHitFilter(FullSecID outer, FullSecID inner, const twoHitFilter_t &filter)
adds a two hit filter
The most CPU efficient Observer for the VXDTF filter tools (even if useless).
Class to uniquely identify a any structure of the PXD and SVD.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
std::map< ExpRun, std::pair< double, double > > filter(const std::map< ExpRun, std::pair< double, double >> &runs, double cut, std::map< ExpRun, std::pair< double, double >> &runsRemoved)
filter events to remove runs shorter than cut, it stores removed runs in runsRemoved
std::unordered_map< std::string, typename Variable::functionType > SelectionVariableNamesToFunctions(Belle2::Filter< Variable, Range, Options... >)
Return a map from the SelectionVariable name to the SelectionVariable function of the Variable used i...
std::set< std::string > filterBranches(const std::set< std::string > &branchesToFilter, const std::vector< std::string > &branches, const std::vector< std::string > &excludeBranches, int durability, bool quiet=false)
Given a list of input branches and lists of branches to include/exclude, returns a list of branches t...
Abstract base class for different kinds of events.
simple struct for interfacing the Branch.
simple struct containing all the configuration data needed for the SecMapTrainer.