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()) {
172 if (
m_modules.count(mod.moduleId()) == 0) {
173 m_modules.insert(std::pair<FPGATrackSimModuleId, Module>(mod.moduleId(), mod));
182 return StatusCode::SUCCESS;
191StatusCode
FPGATrackSimMapMakerAlg::writePmapAndRmap(std::vector<FPGATrackSimHit>
const & pbHits, std::vector<FPGATrackSimHit>
const & peHits, std::vector<FPGATrackSimHit>
const & sbHits, std::vector<FPGATrackSimHit>
const & seHits,
int reg)
205 m_pmap.open(pmap_path, std::ofstream::out);
210 m_pmap <<
"! silicon endCap physDisk physLayer ['stereo' stripSide <strip only>] 'plane1' logiLayer1 'plane2' logiLayer2\n";
211 m_pmap <<
"\nregion " << reg <<
"\n";
214 for (
int lyr = 0; lyr <=
m_pbmax; lyr++) {
217 m_pmap <<
"pixel 0 -1 " << lyr <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
219 for (
int lyr = 0; lyr <=
m_pemax[0]; lyr++) {
222 m_pmap <<
"pixel 1 " << lyr <<
" " << lyr <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
224 for (
int lyr = 0; lyr <=
m_pemax[1]; lyr++) {
227 m_pmap <<
"pixel 2 " << lyr <<
" " << lyr <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
229 for (
int lyr = 0; lyr <=
m_sbmax; lyr++) {
232 m_pmap <<
"SCT 0 -1 " << lyr <<
" stereo " << lyr % 2 <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
234 for (
int lyr = 0; lyr <=
m_semax[0]; lyr++) {
237 m_pmap <<
"SCT 1 " << lyr/2 <<
" " << lyr <<
" stereo " << lyr % 2 <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
239 for (
int lyr = 0; lyr <=
m_semax[1]; lyr++) {
242 m_pmap <<
"SCT 2 " << lyr/2 <<
" " << lyr <<
" stereo " << lyr % 2 <<
" plane1 " << p1 <<
" plane2 " << p2 <<
"\n";
248 m_rmap.open(rmap_path, std::ofstream::out);
249 m_rmap <<
"towers 1 phi 16\n\n0\n";
263 std::ifstream inputFile(pmap_path);
265 ATH_MSG_ERROR(
"Error: Unable to open file for reading: " << pmap_path);
266 return StatusCode::FAILURE;
269 std::ostringstream buffer;
270 buffer << inputFile.rdbuf();
271 std::string fileContent = buffer.str();
275 std::string newContent;
277 newContent += fileContent;
282 std::ofstream outputFile(pmap_path);
284 ATH_MSG_ERROR(
"Error: Unable to open file for writing: " << pmap_path);
285 return StatusCode::FAILURE;
288 outputFile << newContent;
291 return StatusCode::SUCCESS;
299 std::set<int> key_etamods;
301 float total_hits = 0;
303 total_hits += etamod.second;
305 ATH_MSG_INFO(
"Found " << total_hits <<
" hits in the key layer, applying global trim factor of " <<
m_globalTrim <<
"%");
310 key_etamods.insert(etamod.first);
312 ATH_MSG_INFO(
"Eta module " << etamod.first <<
" only contains " << etamod.second <<
" out of " << total_hits <<
" hits, excluding from slices.");
317 float etasPerSlice = float(key_etamods.size())/
m_nSlices.value();
318 std::vector<std::vector<int>> key_modules_for_slices;
321 std::vector<int> key_etas;
322 std::vector<int> key_etas2;
323 key_etas.insert(key_etas.end(), key_etamods.begin(), key_etamods.end());
325 for (
unsigned i = 0; i < key_etas.size(); i++)
327 if (i >= (key_modules_for_slices.size() * etasPerSlice)) key_modules_for_slices.push_back(std::vector<int>());
328 key_modules_for_slices.back().push_back(key_etas[i]);
331 std::map<int, int> keymod2slice;
332 for (
unsigned s = 0; s < key_modules_for_slices.size(); s++)
333 for (
unsigned e = 0; e < key_modules_for_slices[s].size(); e++)
334 keymod2slice[key_modules_for_slices[s][e]] = s;
337 key.erase(
std::remove(key.begin(), key.end(),
','), key.end());
338 key.erase(
std::remove(key.begin(), key.end(),
' '), key.end());
340 key2.erase(
std::remove(key2.begin(), key2.end(),
','), key2.end());
341 key2.erase(
std::remove(key2.begin(), key2.end(),
' '), key2.end());
347 std::stringstream eta_slices;
348 for (
unsigned s = 0; s < key_modules_for_slices.size(); s++){
349 for (
unsigned e = 0; e < key_modules_for_slices[s].size(); e++){
350 eta_slices << key_modules_for_slices[s][e] <<
" ";
361 std::vector<std::vector<int>> key_modules_for_slices2;
364 for (
unsigned i = 0; i < key_etas2.size(); i++)
366 if (i >= (key_modules_for_slices2.size() * etasPerSlice2)) key_modules_for_slices2.push_back(std::vector<int>());
367 key_modules_for_slices2.back().push_back(key_etas2[i]);
370 std::map<int, int> keymod2slice2;
371 for (
unsigned s = 0; s < key_modules_for_slices2.size(); s++)
372 for (
unsigned e = 0; e < key_modules_for_slices2[s].size(); e++)
373 keymod2slice2[key_modules_for_slices2[s][e]] = s;
377 for (
int s1 = 0; s1 <
m_nSlices.value(); s1++){
378 for (
int s2 = 0; s2 <
m_nSlices.value(); s2++){
385 for (
auto& m:
pair.second)
388 if (keymod2slice[m->eta] == s1) key1 =
true;
391 if (keymod2slice2[m->eta] == s2) key2 =
true;
396 int newSlice =
m_nSlices.value()*s1 + s2;
399 if (newSlice + 1 > new_nSlice) new_nSlice = newSlice + 1;
405 ATH_MSG_INFO(
"These slices were further divided based on the key layer 2: '" << key2 <<
"'. Now nSlices = " <<
m_nSlices.value());
416 if (
isOnKeyLayer(1,hit.getDetType(),hit.getDetectorZone(), hit.getPhysLayer()))
418 if (keymod2slice.count(hit.getEtaModule()) > 0) {
419 int s = keymod2slice[hit.getEtaModule()];
428 std::stringstream
trim;
429 std::stringstream gtrim;
430 trim << std::fixed << std::setprecision(3) <<
m_trim;
431 gtrim << std::fixed << std::setprecision(3) <<
m_globalTrim;
432 std::string str_trim =
trim.str();
433 std::string str_gtrim = gtrim.str();
434 int dot = str_trim.find_last_of(
".");
435 str_trim.replace(
dot,1,
"p");
436 str_gtrim.replace(str_gtrim.find_last_of(
"."), 1,
"p");
441 m_subrmap.open(subrmap_path, std::ofstream::out);
448 if (m->numTracks.empty()) {
449 m->numTracks.resize(
m_nSlices.value(), 0);
457 std::vector<std::vector<int>> slicedTracks (
m_nSlices.value());
460 slicedTracks[s].push_back(trk);
472 for (
int s = 0; s <
m_nSlices.value(); s++) {
474 for (
auto trk : slicedTracks[s]) {
478 return 100 * ( float(m->numTracks[s]) / float(slicedTracks[s].size()) ) <
m_trim;
494 ATH_MSG_INFO(
"Trimmed off " << trimmed <<
" modules that were hit by less than " <<
m_trim <<
"% of tracks");
498 for (
int s = 0; s <
m_nSlices.value(); s++)
512 return StatusCode::SUCCESS;
517 std::stringstream
trim;
518 std::stringstream gtrim;
519 trim << std::fixed << std::setprecision(3) <<
m_trim;
520 gtrim << std::fixed << std::setprecision(3) <<
m_globalTrim;
521 std::string str_trim =
trim.str();
522 std::string str_gtrim = gtrim.str();
523 int dot = str_trim.find_last_of(
".");
524 str_trim.replace(
dot,1,
"p");
525 str_gtrim.replace(str_gtrim.find_last_of(
"."), 1,
"p");
527 std::string slicingType =
"";
528 if (
m_key2) slicingType =
"2D";
532 ATH_MSG_INFO(
"Creating eta patterns file: " << etapat_path);
533 m_etapat.open(etapat_path, std::ofstream::out);
537 for (
auto& m:
pair.second)
551 std::stringstream track_etapatts;
552 unsigned planesDone = 0;
553 for (
unsigned p = 0; p < (
m_planes)->size(); p++)
556 if (m->plane ==
static_cast<int>(p))
558 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";
564 if (planesDone == (
m_planes)->size())
565 m_etapat << track_etapatts.str() <<
"\n";
569 return StatusCode::SUCCESS;
576 for (
const auto& hit: allHits)
580 int lyr = hit.getPhysLayer();
591 m_radii[slice][plane].push_back(hit.getR());
599 m_radfile.open(radii_path, std::ofstream::out);
600 for (
int s = 0; s <
m_nSlices.value(); s++){
602 for (
unsigned p = 0; p < (
m_planes2)->size(); p++){
603 if (
m_radii[s][p].size() != 0){
607 float avg = std::accumulate(
m_radii[s][p].begin(),
m_radii[s][p].end(), 0.0f) / float(
m_radii[s][p].size());
608 m_radfile << std::setprecision(3) << std::fixed << avg <<
" ";
619 for (
unsigned p = 0; p < (
m_planes2)->size(); p++) {
622 for (
int s = 0; s <
m_nSlices.value(); s++) {
623 if (
m_radii[s][p].size() != 0) {
624 avg = std::accumulate(
m_radii[s][p].begin(),
m_radii[s][p].end(), avg);
630 m_radfile << std::setprecision(3) << std::fixed << avg <<
" ";
639 return StatusCode::SUCCESS;
645 m_z.resize(
m_nSlices.value(), std::vector<std::vector<float>>((
m_planes2)->size(),std::vector<float>(0)));
646 for (
const auto& hit: allHits)
650 int lyr = hit.getPhysLayer();
661 m_z[slice][plane].push_back(hit.getZ());
669 m_zedfile.open(zed_path, std::ofstream::out);
670 for (
int s = 0; s <
m_nSlices.value(); s++){
672 for (
unsigned p = 0; p < (
m_planes2)->size(); p++){
673 if (
m_z[s][p].size() != 0){
674 float minZ = *std::min_element(
m_z[s][p].begin(),
m_z[s][p].end());
675 float maxZ = *std::max_element(
m_z[s][p].begin(),
m_z[s][p].end());
676 float median = (minZ + maxZ)/2;
677 m_zedfile << std::setprecision(3) << std::fixed << median <<
" ";
688 for (
unsigned p = 0; p < (
m_planes2)->size(); p++) {
691 bool doneInitial =
false;
692 for (
int s = 0; s <
m_nSlices.value(); s++) {
693 if (
m_z[s][p].size() != 0) {
694 float newMinZ = *std::min_element(
m_z[s][p].begin(),
m_z[s][p].end());
695 float newMaxZ = *std::max_element(
m_z[s][p].begin(),
m_z[s][p].end());
698 minZ = std::min(minZ, newMinZ);
699 maxZ = std::max(maxZ, newMaxZ);
708 float median = (minZ + maxZ)/2;
709 m_zedfile << std::setprecision(3) << std::fixed << median <<
" ";
719 return StatusCode::SUCCESS;
734 return StatusCode::SUCCESS;
745 std::vector<TH2F*> h_slicemap;
746 char *hname =
new char[20];
748 for (
unsigned i = 0; i < (unsigned)
m_nSlices.value(); i++)
750 sprintf(hname,
"rz_slice%u",i);
753 TH2F *
h =
new TH2F(hname,hname,7000,-3500,3500,1200,0,1200);
754 h_slicemap.push_back(
h);
757 for (
const auto& hit: allHits)
761 h_slicemap[s]->Fill(hit.getZ(),hit.getR());
764 for (
int i = 0; i <
m_nSlices.value(); i++)
765 h_slicemap[i]->Write();
772 int det =
static_cast<int>(t_det);
773 int bec =
static_cast<int>(t_bec);
788 for (
auto& plane : *planes) {
789 for (
auto& layer : plane) {
790 if (test == layer)
return pcounter;
799 std::stringstream rmap_line;
800 std::set<int> etas, phis;
802 for(
int lyr = 0; lyr <=
max; lyr++)
806 for (
const auto& hit: hits)
808 if(
static_cast<int>(hit.getPhysLayer()) == lyr && hit.getDetectorZone() == bec)
810 etas.insert(hit.getEtaModule());
811 phis.insert(hit.getPhiModule());
814 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";
815 else rmap_line << static_cast<int>(det) <<
" " <<
static_cast<int>(bec) <<
" " << lyr <<
" 0 0 0 0 0 0\n";
819 return rmap_line.str();
825 std::string delimiter =
",";
827 std::string det, bec, lyr;
828 std::map <std::string, std::vector<std::string>> abrevs = { {
"pb",{
"pixel",
"barrel"}}, {
"pe",{
"pixel",
"endcap"}}, {
"sb",{
"strip",
"barrel"}}, {
"se",{
"strip",
"endcap"}} };
829 if( s.find(delimiter) != std::string::npos)
832 std::string det = s.substr(0, s.find(delimiter));
833 s.erase(0, s.find(delimiter) + delimiter.length());
834 std::string bec = s.substr(0, s.find(delimiter));
835 s.erase(0, s.find(delimiter) + delimiter.length());
836 std::string lyr = s.substr(0, s.find(delimiter));
837 s.erase(0, s.find(delimiter) + delimiter.length());
843 ATH_MSG_ERROR(
"Invalid KeyString: '" <<
m_keystring.value() <<
"'." <<
"Accepted formats are 'strip,posEndcap,2', 'pixel,barrel,3', or 'plane 0'");
848 std::string delimiter =
" ";
850 s.erase(0, s.find(delimiter) + delimiter.length());
851 std::string plane = s.substr(0, s.find(delimiter));
852 std::vector<std::string> s = (
m_planes)->at(std::stoi(plane));
853 for (
unsigned i = 0; i < s.size(); i++){
854 std::string reg = s[i].substr(0, 2);
855 std::vector<std::string> zone = abrevs[reg];
856 if (s[i].back() ==
'+') zone[1] =
"posEndcap";
857 if (s[i].back() ==
'-') zone[1] =
"negEndcap";
858 s[i].erase(
std::remove(s[i].begin(), s[i].end(),
'+'), s[i].end());
859 s[i].erase(
std::remove(s[i].begin(), s[i].end(),
'-'), s[i].end());
860 std::string lyr = s[i].substr(2);
867 ATH_MSG_ERROR(
"Invalid KeyString: '" <<
m_keystring.value() <<
"'." <<
"Accepted formats are 'strip,posEndcap,2', 'pixel,barrel,3', or 'plane 0'");
876 if( s.find(delimiter) != std::string::npos)
879 std::string det = s.substr(0, s.find(delimiter));
880 s.erase(0, s.find(delimiter) + delimiter.length());
881 std::string bec = s.substr(0, s.find(delimiter));
882 s.erase(0, s.find(delimiter) + delimiter.length());
883 std::string lyr = s.substr(0, s.find(delimiter));
884 s.erase(0, s.find(delimiter) + delimiter.length());
890 ATH_MSG_ERROR(
"Invalid KeyString2: '" <<
m_keystring2.value() <<
"'." <<
"Accepted formats are 'strip,posEndcap,2', 'pixel,barrel,3', or 'plane 0'");
895 std::string delimiter =
" ";
897 s.erase(0, s.find(delimiter) + delimiter.length());
898 std::string plane = s.substr(0, s.find(delimiter));
899 std::vector<std::string> s = (
m_planes)->at(std::stoi(plane));
900 for (
unsigned i = 0; i < s.size(); i++){
901 std::string reg = s[i].substr(0, 2);
902 std::vector<std::string> zone = abrevs[reg];
903 if (s[i].back() ==
'+') zone[1] =
"posEndcap";
904 if (s[i].back() ==
'-') zone[1] =
"negEndcap";
905 s[i].erase(
std::remove(s[i].begin(), s[i].end(),
'+'), s[i].end());
906 s[i].erase(
std::remove(s[i].begin(), s[i].end(),
'-'), s[i].end());
907 std::string lyr = s[i].substr(2);
914 ATH_MSG_ERROR(
"Invalid KeyString2: '" <<
m_keystring2.value() <<
"'." <<
"Accepted formats are 'strip,posEndcap,2', 'pixel,barrel,3', or 'plane 0'");
922 std::stringstream subrmap_line;
923 std::set<int> etas, phis;
925 std::vector<Module*> mods;
926 for (
auto* mod: allmods)
927 if (mod->det == det && mod->bec == bec) mods.push_back(mod);
929 for(
int lyr = 0; lyr <=
max; lyr++)
937 etas.insert(mod->eta);
938 phis.insert(mod->phi);
941 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";
942 else subrmap_line << static_cast<int>(det) <<
" " <<
static_cast<int>(bec) <<
" " << lyr <<
" 0 0 0 0 0 0\n";
946 return subrmap_line.str();
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define ATH_CHECK
Evaluate an expression and check for errors.
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
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)
StatusCode execute() override
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
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.