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>
27 RawSecMapMergerModule::RawSecMapMergerModule() :
Module()
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);
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(20,
"Sensor: " << sensor <<
" had " << allTrainedSecIDsOfSensor.size() <<
" trained IDs and ");
374 return vxdIDs.size() + 1;
405 B2DEBUG(20,
"RawSecMapMerger::getSegmentFilters: secMap " << config.secMapName <<
" got the following sectors:\n" <<
409 for (
auto& subGraph : mainGraph) {
411 if (nSecChainLength == 2) {
413 }
else if (nSecChainLength == 3) {
415 }
else if (nSecChainLength == 4) {
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(20,
"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(20,
"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(20,
"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(20,
"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) <<
"/"
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!
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 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.
void printData(std::unique_ptr< TChain > &chain, std::vector< BranchInterface< unsigned >> §orBranches, std::vector< BranchInterface< double >> &filterBranches)
for debugging: print data for crosschecks.
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.
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 %).
void add4HitFilters(VXDTFFilters< SpacePoint > &filterContainer, SubGraph< FilterType > &subGraph, const SectorMapConfig &config)
WARNING TODO clean up and documentation!
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.
std::vector< unsigned > getSecIDs(std::vector< BranchInterface< unsigned >> &secBranches, Long64_t entry)
returns secIDs of current entry in the secBranches.
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
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.
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
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.