11#include "GaudiKernel/IEventProcessor.h"
31 while (std::getline(
ss, line,
'\n'))
48 ATH_MSG_INFO(
"Using Python configuration for regions. All pixel layers -> first stage, all strip layers -> second stage");
62 return StatusCode::SUCCESS;
76 SmartIF<IEventProcessor> appMgr{service(
"ApplicationMgr")};
78 ATH_MSG_ERROR(
"Failed to retrieve ApplicationMgr as IEventProcessor");
79 return StatusCode::FAILURE;
81 return appMgr->stopRun();
87 return StatusCode::SUCCESS;
107 ATH_MSG_INFO(
"Cannot read more events from file, returning");
108 return StatusCode::SUCCESS;
116 return StatusCode::SUCCESS;
125 if (hit.getBarcodePt() == 0)
continue;
129 int lyr = hit.getPhysLayer();
130 int eta = hit.getEtaModule();
131 int phi = hit.getPhiModule();
133 if (hit.isPixel() && hit.isBarrel()) {
137 else if (hit.isPixel() && !hit.isBarrel()) {
140 eta = hit.getEtaModule();
141 lyr = hit.getPhysLayer();
147 else if (!hit.isPixel() && hit.isBarrel()) {
151 else if (!hit.isPixel() && !hit.isBarrel()) {
168 auto [it, inserted] =
m_modules.try_emplace(mod.moduleId(), mod);
169 Module* modref = &(it->second);
176 return StatusCode::SUCCESS;
185StatusCode
FPGATrackSimMapMakerAlg::writePmapAndRmap(std::vector<FPGATrackSimHit>
const & pbHits, std::vector<FPGATrackSimHit>
const & peHits, std::vector<FPGATrackSimHit>
const & sbHits, std::vector<FPGATrackSimHit>
const & seHits,
int reg)
199 std::ostringstream buffer;
202 buffer <<
m_pbmax+1 <<
" pixel barrel \n" <<
m_pemax[0]+1 <<
" pixel endcap+ \n" <<
m_pemax[1]+1 <<
" pixel endcap- \n";
203 buffer <<
m_sbmax+1 <<
" SCT barrel \n" <<
m_semax[0]+1 <<
" SCT endcap+\n" <<
m_semax[1]+1 <<
" SCT endcap-\n";
204 buffer <<
"! silicon endCap physDisk physLayer ['stereo' stripSide <strip only>] 'plane1' logiLayer1 'plane2' logiLayer2\n";
205 buffer <<
"\nregion " << reg <<
"\n";
208 for (
int lyr = 0; lyr <=
m_pbmax; lyr++) {
211 buffer <<
"pixel 0 -1 " << lyr <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
213 for (
int lyr = 0; lyr <=
m_pemax[0]; lyr++) {
216 buffer <<
"pixel 1 " << lyr <<
" " << lyr <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
218 for (
int lyr = 0; lyr <=
m_pemax[1]; lyr++) {
221 buffer <<
"pixel 2 " << lyr <<
" " << lyr <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
223 for (
int lyr = 0; lyr <=
m_sbmax; lyr++) {
226 buffer <<
"SCT 0 -1 " << lyr <<
" stereo " << lyr % 2 <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
228 for (
int lyr = 0; lyr <=
m_semax[0]; lyr++) {
231 buffer <<
"SCT 1 " << lyr/2 <<
" " << lyr <<
" stereo " << lyr % 2 <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
233 for (
int lyr = 0; lyr <=
m_semax[1]; lyr++) {
236 buffer <<
"SCT 2 " << lyr/2 <<
" " << lyr <<
" stereo " << lyr % 2 <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
242 std::ofstream rmap(rmap_path, std::ofstream::out);
243 rmap <<
"towers 1 phi 16\n\n0\n";
257 std::ofstream outputFile(pmap_path);
259 ATH_MSG_ERROR(
"Error: Unable to open file for writing: " << pmap_path);
260 return StatusCode::FAILURE;
263 std::string fileContent = buffer.str();
265 outputFile << fileContent <<
'\n';
268 return StatusCode::SUCCESS;
276 std::set<int> key_etamods;
278 float total_hits = 0;
280 total_hits += etamod.second;
282 ATH_MSG_INFO(
"Found " << total_hits <<
" hits in the key layer, applying global trim factor of " <<
m_globalTrim <<
"%");
284 ATH_MSG_ERROR(
"FPGATrackSimMapMakerAlg::writeSubrmap: Failure due to zero total hits.");
285 return StatusCode::FAILURE;
290 key_etamods.insert(etamod.first);
292 ATH_MSG_INFO(
"Eta module " << etamod.first <<
" only contains " << etamod.second <<
" out of " << total_hits <<
" hits, excluding from slices.");
297 float etasPerSlice = float(key_etamods.size())/
m_nSlices.value();
298 std::vector<std::vector<int>> key_modules_for_slices;
301 std::vector<int> key_etas;
302 std::vector<int> key_etas2;
303 key_etas.insert(key_etas.end(), key_etamods.begin(), key_etamods.end());
305 for (
unsigned i = 0; i < key_etas.size(); i++)
307 if (i >= (key_modules_for_slices.size() * etasPerSlice)) key_modules_for_slices.push_back(std::vector<int>());
308 key_modules_for_slices.back().push_back(key_etas[i]);
311 std::map<int, int> keymod2slice;
312 for (
unsigned s = 0; s < key_modules_for_slices.size(); s++)
313 for (
unsigned e = 0; e < key_modules_for_slices[s].size(); e++)
314 keymod2slice[key_modules_for_slices[s][e]] = s;
317 key.erase(
std::remove(key.begin(), key.end(),
','), key.end());
318 key.erase(
std::remove(key.begin(), key.end(),
' '), key.end());
320 key2.erase(
std::remove(key2.begin(), key2.end(),
','), key2.end());
321 key2.erase(
std::remove(key2.begin(), key2.end(),
' '), key2.end());
327 std::stringstream eta_slices;
328 for (
unsigned s = 0; s < key_modules_for_slices.size(); s++){
329 for (
unsigned e = 0; e < key_modules_for_slices[s].size(); e++){
330 eta_slices << key_modules_for_slices[s][e] <<
" ";
341 std::vector<std::vector<int>> key_modules_for_slices2;
344 for (
unsigned i = 0; i < key_etas2.size(); i++)
346 if (i >= (key_modules_for_slices2.size() * etasPerSlice2)) key_modules_for_slices2.push_back(std::vector<int>());
347 key_modules_for_slices2.back().push_back(key_etas2[i]);
350 std::map<int, int> keymod2slice2;
351 for (
unsigned s = 0; s < key_modules_for_slices2.size(); s++)
352 for (
unsigned e = 0; e < key_modules_for_slices2[s].size(); e++)
353 keymod2slice2[key_modules_for_slices2[s][e]] = s;
357 for (
int s1 = 0; s1 <
m_nSlices.value(); s1++){
358 for (
int s2 = 0; s2 <
m_nSlices.value(); s2++){
365 for (
auto& m:
pair.second)
368 if (keymod2slice[m->eta] == s1) key1 =
true;
371 if (keymod2slice2[m->eta] == s2) key2 =
true;
376 int newSlice =
m_nSlices.value()*s1 + s2;
379 if (newSlice + 1 > new_nSlice) new_nSlice = newSlice + 1;
385 ATH_MSG_INFO(
"These slices were further divided based on the key layer 2: '" << key2 <<
"'. Now nSlices = " <<
m_nSlices.value());
396 if (
isOnKeyLayer(1,hit.getDetType(),hit.getDetectorZone(), hit.getPhysLayer()))
398 if (keymod2slice.count(hit.getEtaModule()) > 0) {
399 int s = keymod2slice[hit.getEtaModule()];
411 std::ofstream subrmap(subrmap_path, std::ofstream::out);
412 subrmap <<
"towers " <<
m_nSlices.value() <<
" phi 16\n\n";
418 if (m->numTracks.empty()) {
419 m->numTracks.resize(
m_nSlices.value(), 0);
427 std::vector<std::vector<int>> slicedTracks (
m_nSlices.value());
430 slicedTracks[s].push_back(trk);
442 for (
int s = 0; s <
m_nSlices.value(); s++) {
444 for (
auto trk : slicedTracks[s]) {
448 return 100 * ( float(m->numTracks[s]) / float(slicedTracks[s].
size()) ) <
m_trim;
464 ATH_MSG_INFO(
"Trimmed off " << trimmed <<
" modules that were hit by less than " <<
m_trim <<
"% of tracks");
468 for (
int s = 0; s <
m_nSlices.value(); s++)
470 subrmap << s <<
"\n";
482 return StatusCode::SUCCESS;
487 std::string slicingType =
"";
488 if (
m_key2) slicingType =
"2D";
492 ATH_MSG_INFO(
"Creating eta patterns file: " << etapat_path);
493 std::ofstream etapat(etapat_path, std::ofstream::out);
497 for (
auto& m:
pair.second)
511 std::stringstream track_etapatts;
512 unsigned planesDone = 0;
513 for (
unsigned p = 0; p < (
m_planes)->size(); p++)
516 if (m->plane ==
static_cast<int>(p))
518 track_etapatts << std::to_string(static_cast<int>(m->det)) <<
"\t" << std::to_string(
static_cast<int>(m->bec)) <<
"\t" << std::to_string(m->eta) <<
"\t\t";
525 etapat << track_etapatts.str() <<
"\n";
529 return StatusCode::SUCCESS;
536 for (
const auto& hit: allHits)
540 int lyr = hit.getPhysLayer();
551 m_radii[slice][plane].push_back(hit.getR());
559 std::ofstream radfile(radii_path, std::ofstream::out);
560 for (
int s = 0; s <
m_nSlices.value(); s++){
561 radfile << std::to_string(s) <<
" ";
562 for (
unsigned p = 0; p < (
m_planes2)->size(); p++){
568 radfile << std::setprecision(3) << std::fixed << avg <<
" ";
571 radfile << avg <<
" ";
578 radfile << -1 <<
" ";
579 for (
unsigned p = 0; p < (
m_planes2)->size(); p++) {
582 for (
int s = 0; s <
m_nSlices.value(); s++) {
584 avg = std::accumulate(
m_radii[s][p].begin(),
m_radii[s][p].end(), avg);
590 radfile << std::setprecision(3) << std::fixed << avg <<
" ";
592 radfile << -1 <<
" ";
599 return StatusCode::SUCCESS;
605 m_z.resize(
m_nSlices.value(), std::vector<std::vector<float>>((
m_planes2)->size(),std::vector<float>(0)));
606 for (
const auto& hit: allHits)
610 int lyr = hit.getPhysLayer();
621 m_z[slice][plane].push_back(hit.getZ());
629 std::ofstream zedfile(zed_path, std::ofstream::out);
630 for (
int s = 0; s <
m_nSlices.value(); s++){
631 zedfile << std::to_string(s) <<
" ";
632 for (
unsigned p = 0; p < (
m_planes2)->size(); p++){
634 float minZ = *std::min_element(
m_z[s][p].begin(),
m_z[s][p].end());
635 float maxZ = *std::max_element(
m_z[s][p].begin(),
m_z[s][p].end());
636 float median = (minZ + maxZ)/2;
637 zedfile << std::setprecision(3) << std::fixed << median <<
" ";
640 zedfile << median <<
" ";
647 zedfile << -1 <<
" ";
648 for (
unsigned p = 0; p < (
m_planes2)->size(); p++) {
651 bool doneInitial =
false;
652 for (
int s = 0; s <
m_nSlices.value(); s++) {
654 float newMinZ = *std::min_element(
m_z[s][p].begin(),
m_z[s][p].end());
655 float newMaxZ = *std::max_element(
m_z[s][p].begin(),
m_z[s][p].end());
658 minZ = std::min(minZ, newMinZ);
659 maxZ = std::max(maxZ, newMaxZ);
668 float median = (minZ + maxZ)/2;
669 zedfile << std::setprecision(3) << std::fixed << median <<
" ";
672 zedfile << median <<
" ";
679 return StatusCode::SUCCESS;
694 return StatusCode::SUCCESS;
705 std::vector<TH2F*> h_slicemap;
708 for (
unsigned i = 0; i < (unsigned)
m_nSlices.value(); i++)
710 sprintf(hname,
"rz_slice%u",i);
713 TH2F *
h =
new TH2F(hname,hname,7000,-3500,3500,1200,0,1200);
714 h_slicemap.push_back(
h);
717 for (
const auto& hit: allHits)
721 h_slicemap[s]->Fill(hit.getZ(),hit.getR());
724 for (
int i = 0; i <
m_nSlices.value(); i++)
725 h_slicemap[i]->Write();
731 int det =
static_cast<int>(t_det);
732 int bec =
static_cast<int>(t_bec);
747 for (
auto& plane : *planes) {
748 for (
auto& layer : plane) {
749 if (test == layer)
return pcounter;
758 std::stringstream rmap_line;
759 std::set<int> etas, phis;
761 for(
int lyr = 0; lyr <=
max; lyr++)
765 for (
const auto& hit: hits)
767 if(
static_cast<int>(hit.getPhysLayer()) == lyr && hit.getDetectorZone() == bec)
769 etas.insert(hit.getEtaModule());
770 phis.insert(hit.getPhiModule());
773 if (etas.size() != 0) rmap_line << static_cast<int>(det) <<
" " <<
static_cast<int>(bec) <<
" " << lyr <<
" " << *phis.begin() <<
" " << *phis.rbegin() <<
" " << phis.size() <<
" " << *etas.begin() <<
" " << *etas.rbegin() <<
" " << etas.size() <<
"\n";
774 else rmap_line << static_cast<int>(det) <<
" " <<
static_cast<int>(bec) <<
" " << lyr <<
" 0 0 0 0 0 0\n";
778 return rmap_line.str();
786 std::string det, bec, lyr;
787 std::map <std::string, std::vector<std::string>> abrevs = { {
"pb",{
"pixel",
"barrel"}}, {
"pe",{
"pixel",
"endcap"}}, {
"sb",{
"strip",
"barrel"}}, {
"se",{
"strip",
"endcap"}} };
788 if( s.find(
delimiter) != std::string::npos)
791 std::string det = s.substr(0, s.find(
delimiter));
793 std::string bec = s.substr(0, s.find(
delimiter));
795 std::string lyr = s.substr(0, s.find(
delimiter));
802 ATH_MSG_ERROR(
"Invalid KeyString: '" <<
m_keystring.value() <<
"'." <<
"Accepted formats are 'strip,posEndcap,2', 'pixel,barrel,3', or 'plane 0'");
810 std::string plane = s.substr(0, s.find(
delimiter));
811 std::vector<std::string> s = (
m_planes)->at(std::stoi(plane));
812 for (
unsigned i = 0; i < s.size(); i++){
813 std::string reg = s[i].substr(0, 2);
814 std::vector<std::string> zone = abrevs[reg];
815 if (s[i].back() ==
'+') zone[1] =
"posEndcap";
816 if (s[i].back() ==
'-') zone[1] =
"negEndcap";
817 s[i].erase(
std::remove(s[i].begin(), s[i].end(),
'+'), s[i].end());
818 s[i].erase(
std::remove(s[i].begin(), s[i].end(),
'-'), s[i].end());
819 std::string lyr = s[i].substr(2);
826 ATH_MSG_ERROR(
"Invalid KeyString: '" <<
m_keystring.value() <<
"'." <<
"Accepted formats are 'strip,posEndcap,2', 'pixel,barrel,3', or 'plane 0'");
835 if( s.find(
delimiter) != std::string::npos)
838 std::string det = s.substr(0, s.find(
delimiter));
840 std::string bec = s.substr(0, s.find(
delimiter));
842 std::string lyr = s.substr(0, s.find(
delimiter));
849 ATH_MSG_ERROR(
"Invalid KeyString2: '" <<
m_keystring2.value() <<
"'." <<
"Accepted formats are 'strip,posEndcap,2', 'pixel,barrel,3', or 'plane 0'");
857 std::string plane = s.substr(0, s.find(
delimiter));
858 std::vector<std::string> s = (
m_planes)->at(std::stoi(plane));
859 for (
unsigned i = 0; i < s.size(); i++){
860 std::string reg = s[i].substr(0, 2);
861 std::vector<std::string> zone = abrevs[reg];
862 if (s[i].back() ==
'+') zone[1] =
"posEndcap";
863 if (s[i].back() ==
'-') zone[1] =
"negEndcap";
864 s[i].erase(
std::remove(s[i].begin(), s[i].end(),
'+'), s[i].end());
865 s[i].erase(
std::remove(s[i].begin(), s[i].end(),
'-'), s[i].end());
866 std::string lyr = s[i].substr(2);
873 ATH_MSG_ERROR(
"Invalid KeyString2: '" <<
m_keystring2.value() <<
"'." <<
"Accepted formats are 'strip,posEndcap,2', 'pixel,barrel,3', or 'plane 0'");
881 std::stringstream subrmap_line;
882 std::set<int> etas, phis;
884 std::vector<Module*> mods;
885 for (
auto* mod: allmods)
886 if (mod->det == det && mod->bec == bec) mods.push_back(mod);
888 for(
int lyr = 0; lyr <=
max; lyr++)
896 etas.insert(mod->eta);
897 phis.insert(mod->phi);
900 if (etas.size() != 0) subrmap_line << static_cast<int>(det) <<
" " <<
static_cast<int>(bec) <<
" " << lyr <<
" " << *phis.begin() <<
" " << *phis.rbegin() <<
" " << phis.size() <<
" " << *etas.begin() <<
" " << *etas.rbegin() <<
" " << etas.size() <<
"\n";
901 else subrmap_line << static_cast<int>(det) <<
" " <<
static_cast<int>(bec) <<
" " << lyr <<
" 0 0 0 0 0 0\n";
905 return subrmap_line.str();
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define ATH_CHECK
Evaluate an expression and check for errors.
size_t size() const
Number of registered mappings.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Header file for AthHistogramAlgorithm.
std::unique_ptr< TFile > m_monitorFile
StatusCode writePmapAndRmap(std::vector< FPGATrackSimHit > const &pbHits, std::vector< FPGATrackSimHit > const &peHits, std::vector< FPGATrackSimHit > const &sbHits, std::vector< FPGATrackSimHit > const &seHits, int region)
Gaudi::Property< float > m_trim
std::string makeSubrmapLines(std::vector< Module * > const &allmods, SiliconTech det, DetectorZone bec, int max)
StatusCode writeRadiiFile(std::vector< FPGATrackSimHit > const &allHits)
ToolHandle< IFPGATrackSimInputTool > m_hitSGInputTool
StatusCode finalize() override
std::vector< std::vector< std::vector< float > > > m_radii
FPGATrackSimMapMakerAlg(const std::string &name, ISvcLocator *pSvcLocator)
std::string makeRmapLines(std::vector< FPGATrackSimHit > const &hits, SiliconTech det, DetectorZone bec, int max)
bool isOnKeyLayer(int keynum, SiliconTech det, DetectorZone bec, int lyr)
std::map< std::string, std::set< int > > m_keylayer2
std::map< std::string, std::set< int > > m_keylayer
StatusCode execute(const EventContext &ctx) override
Execute method.
Gaudi::Property< std::string > m_description
std::vector< int > m_pemax
std::vector< FPGATrackSimHit > m_sbHits
Gaudi::Property< float > m_globalTrim
std::map< FPGATrackSimModuleId, Module > m_modules
FPGATrackSimEventInputHeader m_eventHeader
std::map< std::string, SiliconTech > m_det2tech
std::vector< int > m_semax
std::vector< FPGATrackSimHit > m_seHits
std::vector< FPGATrackSimHit > m_pbHits
Gaudi::Property< std::string > m_keystring2
Gaudi::Property< std::string > m_outFileName
std::set< int > m_key_etamods2
const std::vector< std::vector< std::string > > * m_planes
StatusCode writeEtaPatterns()
std::map< int, int > m_track2slice
ToolHandle< IFPGATrackSimEventInputHeaderTool > m_hitInputTool
int findPlane(const std::vector< std::vector< std::string > > *planes, const std::string &test)
StatusCode initialize() override
Gaudi::Property< std::string > m_keystring
std::map< int, std::vector< Module * > > m_track2modules
Gaudi::Property< std::vector< std::vector< std::string > > > m_overridePlanes
StatusCode writeMedianZFile(std::vector< FPGATrackSimHit > const &allHits)
std::set< int > m_usedTracks
Gaudi::Property< bool > m_remapModules
const std::vector< std::vector< std::string > > * m_planes2
Gaudi::Property< bool > m_drawSlices
FPGATrackSimModuleRelabel * m_moduleRelabel
Gaudi::Property< int > m_region
std::vector< std::vector< std::vector< float > > > m_z
std::map< int, int > m_key_etamods
Gaudi::Property< std::vector< std::vector< std::string > > > m_overridePlanes2
std::vector< FPGATrackSimHit > m_peHits
Gaudi::Property< std::string > m_geoTag
std::map< int, std::vector< Module * > > m_slice2modules
std::map< std::string, DetectorZone > m_bec2zone
std::vector< FPGATrackSimHit > m_allHits
StatusCode readInputs(bool &done)
StatusCode writeSubrmap(std::vector< FPGATrackSimHit > const &allHits)
Gaudi::Property< int > m_maxEvents
ServiceHandle< IFPGATrackSimEventSelectionSvc > m_evtSel
void drawSlices(std::vector< FPGATrackSimHit > const &allHits)
Gaudi::Property< int > m_nSlices
std::string find(const std::string &s)
return a remapped string
int count(std::string s, const std::string ®x)
count how many occurances of a regx are in a string
static const std::string delimiter("/")
DataModel_detail::iterator< DVL > remove(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end, const T &value)
Specialization of remove for DataVector/List.
DataModel_detail::iterator< DVL > remove_if(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end, Predicate pred)
Specialization of remove_if for DataVector/List.