11 #include <tracking/modules/VXDTFHelperTools/RawSecMapMergerModule.h>
12 #include <tracking/spacePointCreation/SpacePoint.h>
13 #include <tracking/trackFindingVXD/environment/VXDTFFiltersHelperFunctions.h>
14 #include <tracking/trackFindingVXD/filterMap/filterFramework/SelectionVariableNamesToFunctions.h>
15 #include <vxd/geometry/GeoCache.h>
32 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...");
36 addParam(
"rootFileNames", m_PARAMrootFileNames,
37 "List of files (wildcards not allowed - use python glob.glob() to expand to list of files)", {
"lowTestRedesign_454970355.root"});
39 addParam(
"mapNames", m_PARAMmapNames,
"names of sectorMaps to be loaded.", {
""});
41 addParam(
"printFullGraphs", m_PARAMprintFullGraphs,
42 "If true, the full trained graphs will be printed to screen. WARNING: produces a lot of output for full detector-cases!",
50 std::vector<std::string> RawSecMapMergerModule::getRootFiles(std::string mapName)
52 B2INFO(
"RawSecMapMerger::getRootFiles(): loading mapName: " << mapName);
54 vector<string> files4ThisMap;
55 for (
string& fileName : m_PARAMrootFileNames) {
56 if (fileName.find(mapName) == string::npos) {
57 B2DEBUG(1,
"getRootFiles: fileName " << fileName <<
" was _not_ accepted for map " << mapName);
60 B2DEBUG(1,
"getRootFiles: fileName " << fileName <<
" accepted for map " << mapName);
61 files4ThisMap.push_back(fileName);
70 std::unique_ptr<TChain> RawSecMapMergerModule::createTreeChain(
const SectorMapConfig& configuration,
const std::string& nHitString)
72 B2INFO(
"RawSecMapMerger::createTreeChain(): loading mapName: " << configuration.secMapName <<
" with extension " << nHitString);
73 unique_ptr<TChain> treeChain = unique_ptr<TChain>(
new TChain((configuration.secMapName + nHitString).c_str()));
75 vector<string> fileList = getRootFiles(configuration.secMapName);
76 for (
string& file : fileList) { treeChain->Add(file.c_str()); }
85 template<
class ValueType> std::vector<BranchInterface<ValueType>> RawSecMapMergerModule::getBranches(
86 std::unique_ptr<TChain>& chain,
87 const std::vector<std::string>& branchNames)
89 vector<BranchInterface<ValueType>> branches;
90 B2INFO(
"RawSecMapMerger::getBranches(): loading branches: " << branchNames.size());
91 unsigned nBranches = branchNames.size();
94 for (
unsigned fPos = 0; fPos < nBranches; fPos++) {
95 branches[fPos].name = branchNames[fPos];
96 chain->SetBranchAddress(
97 branches[fPos].name.c_str(),
98 &(branches[fPos].value),
99 &(branches[fPos].branch));
101 B2INFO(
"RawSecMapMerger::getBranches(): done");
106 RawSecMapMergerModule::prepareNHitSpecificStuff(
109 std::vector<std::string>& secBranchNames,
110 std::vector<std::string>& filterBranchNames)
113 secBranchNames = {
"outerSecID",
"innerSecID"};
116 for (
const auto& filterNameToFunction : twoHitsFilterNameToFunction) {
117 string filterName = filterNameToFunction.first ;
118 filterBranchNames.push_back(filterName);
124 secBranchNames = {
"outerSecID",
"centerSecID",
"innerSecID"};
127 for (
const auto& filterNameToFunction : threeHitsFilterNameToFunction) {
128 string filterName = filterNameToFunction.first ;
129 filterBranchNames.push_back(filterName);
134 B2ERROR(
"prepareNHitSpecificStuff: wrong chainLength!");
138 template <
class FilterType>
void RawSecMapMergerModule::trainGraph(
140 std::unique_ptr<TChain>& chain,
144 auto nEntries = chain->GetEntries();
145 B2DEBUG(10,
"RawSecMapMerger::trainGraph(): start of " << nEntries <<
" entries in tree and " << sectorBranches.size() <<
147 if (nEntries == 0) { B2WARNING(
"trainGraph: valid file but no data stored!");
return; }
149 auto percentMark = nEntries / 10;
auto progressCounter = 0;
151 for (
auto i = 0 ; i <= nEntries; i++) {
152 if (percentMark < 1 or (i % percentMark) == 0) {
153 B2INFO(
"RawSecMapMerger::trainGraph(): with mark: " << percentMark <<
" and i=" << i <<
", " << progressCounter <<
154 "% related, mainGraph has got " << mainGraph.
size() <<
" sectors...");
155 progressCounter += 10;
157 auto thisEntry = chain->LoadTree(i);
159 auto ids = getSecIDs(sectorBranches, thisEntry);
162 auto pos = mainGraph.
find(currentID);
164 if (pos == mainGraph.
end()) { B2WARNING(
"trainGraph: could not find subgraph " << currentID.print() <<
" - skipping entry...");
continue; }
168 pos->second.addValue(FilterType(
filter.name),
filter.value);
178 std::unique_ptr<TChain>& chain,
182 auto nEntries = chain->GetEntries();
183 B2INFO(
"RawSecMapMerger::buildGraph(): start of " << nEntries <<
" entries in tree and " << sectorBranches.size() <<
187 vector<string> filterNames;
189 for (
auto& entry :
filterBranches) { filterNames.push_back(entry.name); }
192 if (nEntries == 0) { B2WARNING(
"buildGraph: valid file but no data stored!");
return mainGraph; }
193 auto percentMark = nEntries / 10;
194 auto progressCounter = 0;
197 for (
auto i = 0 ; i <= nEntries; i++) {
198 if (percentMark < 1 or (i % percentMark) == 0) {
199 B2INFO(
"RawSecMapMerger::buildGraph(): with mark: " << percentMark <<
" and i=" << i <<
", " << progressCounter <<
200 "% related, mainGraph has got " << mainGraph.
size() <<
" sectors...");
201 progressCounter += 10;
203 auto thisEntry = chain->LoadTree(i);
205 std::vector<unsigned> ids = getSecIDs(sectorBranches, thisEntry);
211 B2DEBUG(10,
"buildGraph-SubGraphID-print: id: " << currentID.print());
213 auto pos = mainGraph.
find(currentID);
215 if (pos == mainGraph.
end()) { pos = mainGraph.
add(currentID); }
217 if (pos == mainGraph.
end()) { B2WARNING(
"could not find nor add subgraph - skipping entry...");
continue; }
219 pos->second.wasFound();
223 pos->second.checkAndReplaceIfMinMax(FilterType(
filter.name),
filter.value);
227 B2INFO(
"RawSecMapMerger::buildGraph(): mainGraph finished - has now size: " << mainGraph.
size());
228 B2DEBUG(1,
"fullGraph-Print: " << mainGraph.
print());
234 bool RawSecMapMergerModule::good(
const std::vector<unsigned>& ids)
236 switch (ids.size()) {
263 void RawSecMapMergerModule::printData(
264 std::unique_ptr<TChain>& chain,
269 unsigned nEntries = chain->GetEntries();
270 unsigned percentMark = 1;
271 if (nEntries > 100) { percentMark = nEntries / 50; }
272 unsigned progressCounter = 0;
274 B2INFO(
"RawSecMapMerger::printData(): start of " << nEntries <<
275 " entries in tree and " << sectorBranches.size() <<
277 " sector-/filter-branches");
279 for (
unsigned i = 0 ; i < nEntries; i++) {
280 if (percentMark > 1 and (i % percentMark) != 0) {
continue; }
281 progressCounter += 2;
282 B2INFO(
"RawSecMapMerger::printData(): entry " << i <<
" of " << nEntries <<
":");
284 auto thisEntry = chain->LoadTree(i);
287 for (
unsigned k = 0 ; k < sectorBranches.size(); k++) {
288 sectorBranches[k].branch->GetEntry(thisEntry);
306 std::string configName,
unsigned int nHitCombinations,
bool print2File)
308 SecMapHelper::printStaticSectorRelations<SpacePoint>(filters , configName , nHitCombinations, print2File);
345 std::vector<VxdID> vxdIDs = VXD::GeoCache::getInstance().getListOfSensors();
350 for (
VxdID sensor : vxdIDs) {
355 std::sort(allTrainedSecIDsOfSensor.begin(), allTrainedSecIDsOfSensor.end());
356 allTrainedSecIDsOfSensor.erase(
358 allTrainedSecIDsOfSensor.begin(),
359 allTrainedSecIDsOfSensor.end()),
360 allTrainedSecIDsOfSensor.end());
362 for (
FullSecID sector : allTrainedSecIDsOfSensor) {
364 bool success = segFilters.
setSubLayerIDs(sector, sector.getSubLayerID());
366 if (!success) B2FATAL(
"There is a mismatch between the FullSecIDs in the Trainings Graph and the SectorMap!");
369 B2DEBUG(1,
"Sensor: " << sensor <<
" had " << allTrainedSecIDsOfSensor.size() <<
" trained IDs and ");
372 return vxdIDs.size() + 1;
380 template <
class FilterType>
void RawSecMapMergerModule::getSegmentFilters(
401 if (nSecChainLength == 2) updateFilterSubLayerIDs(mainGraph, *xHitFilters);
403 B2DEBUG(1,
"RawSecMapMerger::getSegmentFilters: secMap " << config.secMapName <<
" got the following sectors:\n" <<
407 for (
auto& subGraph : mainGraph) {
409 if (nSecChainLength == 2) {
410 add2HitFilters(*xHitFilters, subGraph.second, config);
411 }
else if (nSecChainLength == 3) {
412 add3HitFilters(*xHitFilters, subGraph.second, config);
413 }
else if (nSecChainLength == 4) {
414 add4HitFilters(*xHitFilters, subGraph.second, config);
415 }
else { B2FATAL(
"nSecChainLength " << nSecChainLength <<
" is not within allowed range [2;4]!"); }
434 std::string filterVals;
435 for (
const auto& filterNameToFunction : filterNameToFunctions) {
436 string filterName = filterNameToFunction.first ;
437 filterVals += filterName +
": "
438 + std::to_string(filterCutsMap.at(filterName).getMin())
440 + std::to_string(filterCutsMap.at(filterName).getMax()) +
", ";
442 B2DEBUG(1,
"SubGraph " << subGraph.
getID().
print() <<
" - filter:min/max: " << filterVals);
447 (filterCutsMap.at(
"DistanceInTimeUside").getMin() <= DistanceInTimeUside<SpacePoint>() <=
448 filterCutsMap.at(
"DistanceInTimeUside").getMax()) &&
449 (filterCutsMap.at(
"DistanceInTimeVside").getMin() <= DistanceInTimeVside<SpacePoint>() <=
450 filterCutsMap.at(
"DistanceInTimeVside").getMax()) &&
451 (filterCutsMap.at(
"Distance3DSquared").getMin() <= Distance3DSquared<SpacePoint>() <=
452 filterCutsMap.at(
"Distance3DSquared").getMax()) &&
453 (filterCutsMap.at(
"Distance2DXYSquared").getMin() <= Distance2DXYSquared<SpacePoint>() <=
454 filterCutsMap.at(
"Distance2DXYSquared").getMax()) &&
455 (filterCutsMap.at(
"Distance1DZ").getMin() <= Distance1DZ<SpacePoint>() <= filterCutsMap.at(
"Distance1DZ").getMax()) &&
456 (filterCutsMap.at(
"SlopeRZ").getMin() <= SlopeRZ<SpacePoint>() <= filterCutsMap.at(
"SlopeRZ").getMax()) &&
457 (filterCutsMap.at(
"Distance3DNormed").getMin() <= Distance3DNormed<SpacePoint>() <=
458 filterCutsMap.at(
"Distance3DNormed").getMax())
465 B2DEBUG(1,
"RawSecMapMerger::add2HitFilters: now adding FriendSectorFilter for secIDs (outer/inner): " << secIDs.at(
466 0) <<
"/" << secIDs.at(1));
468 friendSectorsSegmentFilter) == 0)
469 B2WARNING(
"secMap: " << config.secMapName <<
"Problem adding the friendship relation from the inner sector:" <<
470 secIDs.at(1) <<
" -> " << secIDs.at(0) <<
" outer sector");
483 std::string filterVals;
485 for (
auto& filterNameToFunction : filterNameToFunctions) {
486 string filterName = filterNameToFunction.first ;
487 filterVals += filterName +
": "
488 + std::to_string(filterCutsMap.at(filterName).getMin())
490 + std::to_string(filterCutsMap.at(filterName).getMax()) +
", ";
492 B2DEBUG(1,
"SubGraph " << subGraph.
getID().
print() <<
" - filter:min/max: " << filterVals);
496 ((filterCutsMap.at(
"DistanceInTime").getMin() <= DistanceInTime<SpacePoint>() <= filterCutsMap.at(
"DistanceInTime").getMax()) &&
497 (filterCutsMap.at(
"Angle3DSimple").getMin() <= Angle3DSimple<SpacePoint>() <= filterCutsMap.at(
"Angle3DSimple").getMax()) &&
498 (filterCutsMap.at(
"CosAngleXY").getMin() <= CosAngleXY<SpacePoint>() <= filterCutsMap.at(
"CosAngleXY").getMax()) &&
499 (filterCutsMap.at(
"AngleRZSimple").getMin() <= AngleRZSimple<SpacePoint>() <= filterCutsMap.at(
"AngleRZSimple").getMax()) &&
500 (CircleDist2IP<SpacePoint>() <= filterCutsMap.at(
"CircleDist2IP").getMax()) &&
501 (filterCutsMap.at(
"DeltaSlopeRZ").getMin() <= DeltaSlopeRZ<SpacePoint>()) <= filterCutsMap.at(
"DeltaSlopeRZ").getMax() &&
502 (filterCutsMap.at(
"DeltaSlopeZoverS").getMin() <= DeltaSlopeZoverS<SpacePoint>() <=
503 filterCutsMap.at(
"DeltaSlopeZoverS").getMax()) &&
504 (filterCutsMap.at(
"DeltaSoverZ").getMin() <= DeltaSoverZ<SpacePoint>() <= filterCutsMap.at(
"DeltaSoverZ").getMax()) &&
505 (filterCutsMap.at(
"HelixParameterFit").getMin() <= HelixParameterFit<SpacePoint>() <=
506 filterCutsMap.at(
"HelixParameterFit").getMax()) &&
507 (filterCutsMap.at(
"Pt").getMin() <= Pt<SpacePoint>() <= filterCutsMap.at(
"Pt").getMax()) &&
508 (filterCutsMap.at(
"CircleRadius").getMin() <= CircleRadius<SpacePoint>() <= filterCutsMap.at(
"CircleRadius").getMax())
516 B2DEBUG(1,
"RawSecMapMerger::add3HitFilters: now adding FriendSectorFilter for secIDs (outer/center/inner): "
517 << secIDs.at(0) <<
"/"
518 << secIDs.at(1) <<
"/"
521 threeHitFilter) == 0)
522 B2WARNING(
"secMap: " << config.secMapName <<
"Problem adding the friendship relation for the secIDs (outer/center/inner): "
523 << secIDs.at(0) <<
"/"
524 << secIDs.at(1) <<
"/"
530 template <
class FilterType>
void RawSecMapMergerModule::add4HitFilters(