ATLAS Offline Software
UserLimitsSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // class header
6 #include "UserLimitsSvc.h"
7 
8 // Geant4 includes used in functions
9 #include "G4LogicalVolume.hh"
10 #include "G4LogicalVolumeStore.hh"
11 #include "G4UserLimits.hh"
12 
13 // STL library
14 #include <boost/tokenizer.hpp>
15 #include <limits>
16 UserLimitsSvc::UserLimitsSvc( const std::string& name, ISvcLocator* pSvcLocator )
17  : base_class(name,pSvcLocator)
18 {
19 }
20 
21 // Athena method, called at initialization time
23 {
24  ATH_MSG_INFO(" initializing UserLimitsSvc "<<name() );
25  G4LogicalVolumeStore& lvs=*(G4LogicalVolumeStore::GetInstance());
26 
27  ATH_MSG_INFO("G4LogicalVolumeStore size: " << lvs.size());
28  std::set<G4String> volumes;
29  for (unsigned int i = 0; i < lvs.size(); i++) {
30  volumes.insert(lvs[i]->GetName());
31  }
32  ATH_MSG_INFO("G4LogicalVolumeStore unique size: " << volumes.size());
33 
34  // Define with a configurable string which string comparison fucntion to use
35  using function_t = bool (UserLimitsSvc::*) (const std::string& pattern, const std::string& logicalVolume) const;
36  std::map<std::string, function_t> funcMap;
37  funcMap.emplace("isMatch", &UserLimitsSvc::isMatch);
38  funcMap.emplace("contains", &UserLimitsSvc::contains);
39 
40  // Call Limit setting methods here:
41  std::vector<std::string>::const_iterator volumeItr(m_logicalVolumes.value().begin());
42  const std::vector<std::string>::const_iterator endOfVolumesItr(m_logicalVolumes.value().end());
43  while(volumeItr!=endOfVolumesItr)
44  {
45  const std::string& volName(*volumeItr);
46  unsigned int ndone(0);
47  for (unsigned int i=0;i<lvs.size();i++)
48  {
49  G4LogicalVolume *lv=lvs[i];
50  // Compare two strings with a configurable method...
51  if ( (this->*(funcMap[m_matchType.value()]))(volName, lv->GetName()) ){
52  G4UserLimits *ul=lv->GetUserLimits();
53  if (!ul) ul=new G4UserLimits;
54  if (-0.5 < m_MaxStep) { ul->SetMaxAllowedStep(m_MaxStep); }
55  if (-0.5 < m_MinEkine) { ul->SetUserMinEkine(m_MinEkine); }
56  if (-0.5 < m_MaxTrackLength) { ul->SetUserMaxTrackLength(m_MaxTrackLength); }
57  if (-0.5 < m_MaxTime) { ul->SetUserMaxTime(m_MaxTime); }
58  if (-0.5 < m_MinRange) { ul->SetUserMinRange(m_MinRange); }
59  lv->SetUserLimits(ul);
60  ++ndone;
61  }
62  }
63  if (-0.5 < m_MaxStep) { ATH_MSG_DEBUG ( "MaxStep set to "<<m_MaxStep<<" to "<<ndone<<" copies of "<<volName ); }
64  if (-0.5 < m_MinEkine){ ATH_MSG_DEBUG ( "MinEkine set to "<<m_MinEkine<<" to "<<ndone<<" copies of "<<volName );}
65  if (-0.5 < m_MaxTrackLength) { ATH_MSG_DEBUG ( "MaxTrackLength set to "<<m_MaxTrackLength<<" to "<<ndone<<" copies of "<<volName ); }
66  if (-0.5 < m_MaxTime) { ATH_MSG_DEBUG ( "MaxTime set to "<<m_MaxTime<<" to "<<ndone<<" copies of "<<volName ); }
67  if (-0.5 < m_MinRange) { ATH_MSG_DEBUG ( "MinRange set to "<<m_MinRange<<" to "<<ndone<<" copies of "<<volName ); }
68 
69  ++volumeItr;
70  }
71  // TODO would probably be more CPU efficient to loop over the geometry and compare with each limit volume.
72  return StatusCode::SUCCESS;
73 }
74 
75 bool UserLimitsSvc::contains(const std::string& pattern, const std::string& logicalVolume) const
76 {
77  return (logicalVolume.find(pattern) != std::string::npos);
78 }
79 
80 bool UserLimitsSvc::isMatch(const std::string& a,const std::string& b) const
81 {
82  // straightforward cases
83  if (a=="*") return true;
84  if (a==b) return true;
85  // wildcards
86  boost::char_separator<char> sep{"*"};
87  typedef boost::tokenizer< boost::char_separator<char> > tokenizer;
88  tokenizer tok{a, sep};
89  bool returnValue=true;
90  std::string temp=b;
91  //FIXME This next bit is a bit hacky
92  std::vector<std::string> tokens;
93  for (const auto& token : tok)
94  {
95  tokens.push_back(token);
96  }
97  for (unsigned int i=0;i<tokens.size();i++)
98  {
99  if (tokens[i].empty()) continue;
100  std::string::size_type npos=temp.find(tokens[i]);
101  if (i==0 && npos)
102  return false;
103  if (npos!=std::string::npos)
104  {
105  temp=temp.substr(npos, temp.size()-npos);
106  }
107  else
108  {
109  returnValue=false;
110  break;
111  }
112  }
113  if (returnValue && !tokens[tokens.size()-1].empty())
114  {
115  std::string temp=tokens[tokens.size()-1];
116  std::string temp2=b.substr(b.size()-temp.size(),temp.size());
117  if (temp!=temp2)
118  return false;
119  }
120  return returnValue;
121 }
UserLimitsSvc::contains
bool contains(const std::string &pattern, const std::string &logicalVolume) const
Definition: UserLimitsSvc.cxx:75
mergePhysValFiles.pattern
pattern
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:26
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
UserLimitsSvc::m_logicalVolumes
Gaudi::Property< std::vector< std::string > > m_logicalVolumes
List of Logical volume to which these limits should be applied.
Definition: UserLimitsSvc.h:46
UserLimitsSvc::m_MaxStep
Gaudi::Property< double > m_MaxStep
Maximum step length.
Definition: UserLimitsSvc.h:34
UserLimitsSvc::m_MinEkine
Gaudi::Property< double > m_MinEkine
Minimum remaining kinetic energy for a track.
Definition: UserLimitsSvc.h:36
beamspotman.tokens
tokens
Definition: beamspotman.py:1284
empty
bool empty(TH1 *h)
Definition: computils.cxx:294
UserLimitsSvc::m_MinRange
Gaudi::Property< double > m_MinRange
Minimum remaining range for a track.
Definition: UserLimitsSvc.h:42
UserLimitsSvc::isMatch
bool isMatch(const std::string &pattern, const std::string &logicalVolume) const
Functions for string comparison.
Definition: UserLimitsSvc.cxx:80
UserLimitsSvc::initialize
virtual StatusCode initialize() override final
Definition: UserLimitsSvc.cxx:22
UserLimitsSvc::m_MaxTrackLength
Gaudi::Property< double > m_MaxTrackLength
Maximum total track length.
Definition: UserLimitsSvc.h:38
lumiFormat.i
int i
Definition: lumiFormat.py:92
UserLimitsSvc::m_MaxTime
Gaudi::Property< double > m_MaxTime
Maximum global time for a track.
Definition: UserLimitsSvc.h:40
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
grepfile.sep
sep
Definition: grepfile.py:38
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
UserLimitsSvc::m_matchType
Gaudi::Property< std::string > m_matchType
Use 'contains' or 'isMatch' function for string comparison.
Definition: UserLimitsSvc.h:44
UserLimitsSvc::UserLimitsSvc
UserLimitsSvc(const std::string &name, ISvcLocator *pSvcLocator)
Definition: UserLimitsSvc.cxx:16
UserLimitsSvc.h
a
TList * a
Definition: liststreamerinfos.cxx:10
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
UserLimitsSvc
Definition: UserLimitsSvc.h:21