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...");
35 "List of files (wildcards not allowed - use python glob.glob() to expand to list of files)", {
"lowTestRedesign_454970355.root"});
40 "If true, the full trained graphs will be printed to screen. WARNING: produces a lot of output for full detector-cases!",
44 "Relative threshold (in %) used to prune the sector maps. Will remove X % of the least used subgraphs.", {0});
53 B2INFO(
"RawSecMapMerger::getRootFiles(): loading mapName: " << mapName);
55 std::vector<std::string> files4ThisMap;
57 if (fileName.find(mapName) == string::npos) {
58 B2DEBUG(20,
"getRootFiles: fileName " << fileName <<
" was _not_ accepted for map " << mapName);
61 B2DEBUG(20,
"getRootFiles: fileName " << fileName <<
" accepted for map " << mapName);
62 files4ThisMap.push_back(fileName);
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 std::vector<std::string> fileList =
getRootFiles(configuration.secMapName);
77 for (std::string& file : fileList) { treeChain->Add(file.c_str()); }
87 std::unique_ptr<TChain>& chain,
88 const std::vector<std::string>& branchNames)
90 std::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");
110 std::vector<std::string>& secBranchNames,
111 std::vector<std::string>& filterBranchNames)
114 secBranchNames = {
"outerSecID",
"innerSecID"};
117 for (
const auto& filterNameToFunction : twoHitsFilterNameToFunction) {
118 std::string filterName = filterNameToFunction.first ;
119 filterBranchNames.push_back(filterName);
125 secBranchNames = {
"outerSecID",
"centerSecID",
"innerSecID"};
128 for (
const auto& filterNameToFunction : threeHitsFilterNameToFunction) {
129 std::string filterName = filterNameToFunction.first ;
130 filterBranchNames.push_back(filterName);
135 B2ERROR(
"prepareNHitSpecificStuff: wrong chainLength!");
141 std::unique_ptr<TChain>& chain,
145 auto nEntries = chain->GetEntries();
146 B2DEBUG(29,
"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; }
167 for (
auto&
filter : filterBranches) {
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 std::vector<std::string> filterNames;
189 for (
auto& entry : filterBranches) {
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(29,
"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();
223 for (
auto&
filter : filterBranches) {
225 pos->second.checkAndReplaceIfMinMax(FilterType(
filter.name),
filter.value);
229 B2INFO(
"RawSecMapMerger::buildGraph(): mainGraph finished - has now size: " << mainGraph.
size());
230 B2DEBUG(20,
"fullGraph-Print: " << mainGraph.
print());
238 switch (ids.size()) {
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() <<
278 "/" << filterBranches.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 <<
": " << progressCounter <<
" \% of data printed.");
286 auto thisEntry = chain->LoadTree(i);
289 for (
unsigned k = 0 ; k < sectorBranches.size(); k++) {
290 sectorBranches[k].branch->GetEntry(thisEntry);
295 for (
unsigned k = 0 ; k < filterBranches.size(); k++) {
296 filterBranches[k].branch->GetEntry(thisEntry);
297 out += filterBranches[k].name +
": " + to_string(filterBranches[k].value) +
". ";
308 std::string configName,
unsigned int nHitCombinations,
bool print2File)
310 SecMapHelper::printStaticSectorRelations<SpacePoint>(filters, configName, nHitCombinations, print2File);
324 for (
VxdID sensor : vxdIDs) {
329 std::sort(allTrainedSecIDsOfSensor.begin(), allTrainedSecIDsOfSensor.end());
330 allTrainedSecIDsOfSensor.erase(
332 allTrainedSecIDsOfSensor.begin(),
333 allTrainedSecIDsOfSensor.end()),
334 allTrainedSecIDsOfSensor.end());
336 for (
FullSecID sector : allTrainedSecIDsOfSensor) {
338 bool success = segFilters.
setSubLayerIDs(sector, sector.getSubLayerID());
340 if (!success) B2FATAL(
"There is a mismatch between the FullSecIDs in the Trainings Graph and the SectorMap!");
343 B2DEBUG(20,
"Sensor: " << sensor <<
" had " << allTrainedSecIDsOfSensor.size() <<
" trained IDs and ");
346 return vxdIDs.size() + 1;
377 B2DEBUG(20,
"RawSecMapMerger::getSegmentFilters: secMap " << config.secMapName <<
" got the following sectors:\n" <<
381 for (
auto& subGraph : mainGraph) {
383 if (nSecChainLength == 2) {
385 }
else if (nSecChainLength == 3) {
387 }
else if (nSecChainLength == 4) {
389 }
else { B2FATAL(
"nSecChainLength " << nSecChainLength <<
" is not within allowed range [2;4]!"); }
408 std::string filterVals;
409 for (
const auto& filterNameToFunction : filterNameToFunctions) {
410 string filterName = filterNameToFunction.first ;
411 filterVals += filterName +
": "
412 + std::to_string(filterCutsMap.at(filterName).getMin())
414 + std::to_string(filterCutsMap.at(filterName).getMax()) +
", ";
416 B2DEBUG(20,
"SubGraph " << subGraph.
getID().
print() <<
" - filter:min/max: " << filterVals);
421 (filterCutsMap.at(
"DistanceInTimeUside").getMin() <= DistanceInTimeUside<SpacePoint>() <=
422 filterCutsMap.at(
"DistanceInTimeUside").getMax()) &&
423 (filterCutsMap.at(
"DistanceInTimeVside").getMin() <= DistanceInTimeVside<SpacePoint>() <=
424 filterCutsMap.at(
"DistanceInTimeVside").getMax()) &&
425 (filterCutsMap.at(
"Distance3DSquared").getMin() <= Distance3DSquared<SpacePoint>() <=
426 filterCutsMap.at(
"Distance3DSquared").getMax()) &&
427 (filterCutsMap.at(
"Distance2DXYSquared").getMin() <= Distance2DXYSquared<SpacePoint>() <=
428 filterCutsMap.at(
"Distance2DXYSquared").getMax()) &&
429 (filterCutsMap.at(
"Distance1DZ").getMin() <= Distance1DZ<SpacePoint>() <= filterCutsMap.at(
"Distance1DZ").getMax()) &&
430 (filterCutsMap.at(
"SlopeRZ").getMin() <= SlopeRZ<SpacePoint>() <= filterCutsMap.at(
"SlopeRZ").getMax()) &&
431 (filterCutsMap.at(
"Distance3DNormed").getMin() <= Distance3DNormed<SpacePoint>() <=
432 filterCutsMap.at(
"Distance3DNormed").getMax())
439 B2DEBUG(20,
"RawSecMapMerger::add2HitFilters: now adding FriendSectorFilter for secIDs (outer/inner): " << secIDs.at(
440 0) <<
"/" << secIDs.at(1));
442 friendSectorsSegmentFilter) == 0)
443 B2WARNING(
"secMap: " << config.secMapName <<
"Problem adding the friendship relation from the inner sector:" <<
444 secIDs.at(1) <<
" -> " << secIDs.at(0) <<
" outer sector");
457 std::string filterVals;
459 for (
auto& filterNameToFunction : filterNameToFunctions) {
460 string filterName = filterNameToFunction.first ;
461 filterVals += filterName +
": "
462 + std::to_string(filterCutsMap.at(filterName).getMin())
464 + std::to_string(filterCutsMap.at(filterName).getMax()) +
", ";
466 B2DEBUG(20,
"SubGraph " << subGraph.
getID().
print() <<
" - filter:min/max: " << filterVals);
470 ((filterCutsMap.at(
"DistanceInTime").getMin() <= DistanceInTime<SpacePoint>() <= filterCutsMap.at(
"DistanceInTime").getMax()) &&
471 (filterCutsMap.at(
"Angle3DSimple").getMin() <= Angle3DSimple<SpacePoint>() <= filterCutsMap.at(
"Angle3DSimple").getMax()) &&
472 (filterCutsMap.at(
"CosAngleXY").getMin() <= CosAngleXY<SpacePoint>() <= filterCutsMap.at(
"CosAngleXY").getMax()) &&
473 (filterCutsMap.at(
"AngleRZSimple").getMin() <= AngleRZSimple<SpacePoint>() <= filterCutsMap.at(
"AngleRZSimple").getMax()) &&
474 (CircleDist2IP<SpacePoint>() <= filterCutsMap.at(
"CircleDist2IP").getMax()) &&
475 (filterCutsMap.at(
"DeltaSlopeRZ").getMin() <= DeltaSlopeRZ<SpacePoint>()) <= filterCutsMap.at(
"DeltaSlopeRZ").getMax() &&
476 (filterCutsMap.at(
"DeltaSlopeZoverS").getMin() <= DeltaSlopeZoverS<SpacePoint>() <=
477 filterCutsMap.at(
"DeltaSlopeZoverS").getMax()) &&
478 (filterCutsMap.at(
"DeltaSoverZ").getMin() <= DeltaSoverZ<SpacePoint>() <= filterCutsMap.at(
"DeltaSoverZ").getMax()) &&
479 (filterCutsMap.at(
"HelixParameterFit").getMin() <= HelixParameterFit<SpacePoint>() <=
480 filterCutsMap.at(
"HelixParameterFit").getMax()) &&
481 (filterCutsMap.at(
"Pt").getMin() <= Pt<SpacePoint>() <= filterCutsMap.at(
"Pt").getMax()) &&
482 (filterCutsMap.at(
"CircleRadius").getMin() <= CircleRadius<SpacePoint>() <= filterCutsMap.at(
"CircleRadius").getMax())
490 B2DEBUG(20,
"RawSecMapMerger::add3HitFilters: now adding FriendSectorFilter for secIDs (outer/center/inner): "
491 << secIDs.at(0) <<
"/"
492 << secIDs.at(1) <<
"/"
495 threeHitFilter) == 0)
496 B2WARNING(
"secMap: " << config.secMapName <<
"Problem adding the friendship relation for the secIDs (outer/center/inner): "
497 << secIDs.at(0) <<
"/"
498 << secIDs.at(1) <<
"/"
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
void setDescription(const std::string &description)
Sets the description of the module.
unsigned updateFilterSubLayerIDs(SectorGraph< FilterType > &mainGraph, VXDTFFilters< SpacePoint > &segFilters)
updates the sublayer ID of the FullSecIDs used in the VXDTFFilters with the one used during the train...
void add3HitFilters(VXDTFFilters< SpacePoint > &filterContainer, SubGraph< FilterType > &subGraph, const SectorMapConfig &config)
WARNING TODO clean up and documentation!
SectorGraph< FilterType > buildGraph(std::unique_ptr< TChain > &chain, std::vector< BranchInterface< unsigned > > §orBranches, std::vector< BranchInterface< double > > &filterBranches)
build graph with secChains found in TChain.
bool m_PARAMprintFullGraphs
If true, the full trained graphs will be printed to screen.
std::vector< BranchInterface< ValueType > > getBranches(std::unique_ptr< TChain > &chain, const std::vector< std::string > &branchNames)
for given chain and names of branches: this function returns their pointers to the branch and the con...
void printVXDTFFilters(const VXDTFFilters< SpacePoint > &filters, std::string configName, unsigned int nHitCombinations, bool print2File)
for debugging purposes: print VXDTFFilters into a file of name of the sectorMapConfig.
std::unique_ptr< TChain > createTreeChain(const SectorMapConfig &configuration, const std::string &nHitString)
bundle all relevant files to a TChain
std::vector< std::string > m_PARAMrootFileNames
///////////////////////////////////////////////////////////////////////////////// member variables of...
std::vector< std::string > m_PARAMmapNames
contains names of sectorMaps to be loaded.
void printData(std::unique_ptr< TChain > &chain, std::vector< BranchInterface< unsigned > > §orBranches, std::vector< BranchInterface< double > > &filterBranches)
for debugging: print data for crosschecks.
std::vector< std::string > getRootFiles(std::string mapName)
returns all names of root-files fitting given parameter mapName
int m_RelThreshold
Relative threshold for pruning the sector maps (in %).
std::vector< unsigned > getSecIDs(std::vector< BranchInterface< unsigned > > &secBranches, Long64_t entry)
returns secIDs of current entry in the secBranches.
void add4HitFilters(VXDTFFilters< SpacePoint > &filterContainer, SubGraph< FilterType > &subGraph, const SectorMapConfig &config)
WARNING TODO clean up and documentation!
RawSecMapMergerModule()
Constructor of the module.
void add2HitFilters(VXDTFFilters< SpacePoint > &filterContainer, SubGraph< FilterType > &subGraph, const SectorMapConfig &config)
WARNING TODO clean up and documentation!
void getSegmentFilters(const SectorMapConfig &config, SectorGraph< FilterType > &mainGraph, VXDTFFilters< SpacePoint > *xHitFilters, int nSecChainLength)
returns all VxdIDs (sensors) compatible with given configData.
void trainGraph(SectorGraph< FilterType > &mainGraph, std::unique_ptr< TChain > &chain, std::vector< BranchInterface< unsigned > > §orBranches, std::vector< BranchInterface< double > > &filterBranches)
fill the graphs with raw data fitting to their filters respectively.
std::string prepareNHitSpecificStuff(unsigned nHits, const SectorMapConfig &config, std::vector< std::string > &secBranchNames, std::vector< std::string > &filterBranchNames)
sets everything which is hit-dependent.
bool good(const std::vector< unsigned > &ids)
check that the vector of FullSecIDs
unsigned size() const
returns number of collected subgraphs so far.
std::vector< FullSecID > getAllFullSecIDsOfSensor(VxdID sensor)
returns a Vector containing all FullSecIDs found for given sensor.
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.
stores the ID of a subgraph, which is basically a chain of FullSecID coded as unsigned ints.
std::vector< FullSecID > getFullSecIDs() const
returns SecIDs coded as FullSecIDs:
std::string print() const
returns string of entries.
contains all relevant stuff needed for dealing with a subGraph.
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.
const SubGraphID & getID() const
returns iD of this graph
Class that contains all the static sectors to which the filters are attached.
decltype((0.<=DistanceInTimeUside< point_t >()<=0. &&0.<=DistanceInTimeVside< point_t >()<=0. &&0.<=Distance3DSquared< point_t >()<=0.&&0.<=Distance2DXYSquared< point_t >()<=0.&&0.<=Distance1DZ< point_t >()<=0.&&0.<=SlopeRZ< point_t >()<=0.&&0.<=Distance3DNormed< point_t >()<=0.)) twoHitFilter_t
minimal working 2-hits-example used for redesign of VXDTF.
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
decltype((0.<=DistanceInTime< point_t >()<=0. &&0.<=Angle3DSimple< point_t >()<=0.&&0.<=CosAngleXY< point_t >()<=0.&&0.<=AngleRZSimple< point_t >()<=0.&&CircleDist2IP< point_t >()<=0.&&0.<=DeltaSlopeRZ< point_t >()<=0.&&0.<=DeltaSlopeZoverS< point_t >()<=0.&&0.<=DeltaSoverZ< point_t >()<=0.&&0.<=HelixParameterFit< point_t >()<=0.&&0.<=Pt< point_t >()<=0.&&0.<=CircleRadius< point_t >()<=0.)) threeHitFilter_t
minimal working example for 3-hits:
const std::vector< VxdID > getListOfSensors() const
Get list of all sensors.
static GeoCache & getInstance()
Return a reference to the singleton instance.
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.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#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...
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.