ATLAS Offline Software
PMGCrossSectionTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id: PMGCrossSectionTool.cxx 764400 2016-07-26 17:47:39Z tripiana $
6 
7 #include <fstream>
8 #include <sstream>
9 #include <string>
10 #include <TSystem.h>
11 #include <TString.h>
12 #include <stdlib.h>
13 #include <limits>
14 
15 // Local include(s):
17 
18 namespace PMGTools {
19 
21 PMGCrossSectionTool(const std::string& name) : asg::AsgTool(name) { }
22 
23 
25 
26  // Tell the user what's happening:
27  ATH_MSG_INFO( "Initializing " << name() << "..." );
28  //ATH_MSG_INFO( "Read in all Xsec Info ..." );
29 
30  //readInfosFromDir(inputDir.c_str());
31 
32  // Return gracefully:
33  return StatusCode::SUCCESS;
34 }
35 
36 
37 bool PMGCrossSectionTool::readInfosFromFiles(const std::vector<std::string> & InputFiles)
38 {
39 
40  for (const auto& currentFileName : InputFiles) {
41 
42  std::ifstream currentFile(currentFileName);
43  if (not currentFile.is_open()) {
44  ATH_MSG_WARNING("cannot open file " << currentFileName);
45  continue;
46  }
47 
48  // skip first line
49  currentFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
50 
51  int nfound = 0; // for this file
52  for (std::string line; std::getline(currentFile, line); ) {
53  std::stringstream input_line(line);
55 
56  input_line >> help.dsid;
57  input_line >> help.containerName;
58  input_line >> help.amiXSec;
59  input_line >> help.filterEff;
60  input_line >> help.kFactor;
61  input_line >> help.XSecUncUP;
62  input_line >> help.XSecUncDOWN;
63  // we don't care about the generator name here
64  // let's skip it
65  std::string dummy;
66  input_line >> dummy;
67 
68  std::string etag;
69  input_line >> etag;
70  etag = etag.substr(1);
71  help.etag = std::stoi(etag);
72  // :: below is for future use?
73  //input_line >> help.br;
74  //input_line >> help.higherOrderXsecTotal;
75  //input_line >> help.higherOrderXsecSample; // this is hoXsec * filter eff
76 
77  if (input_line.fail()) { ATH_MSG_ERROR("cannot parse line '" << line << "' from file " << currentFileName); continue; }
78 
79  m_fStoreSampleInfo[{help.dsid, -1}] = help;
80  m_fStoreSampleInfo[{help.dsid, help.etag}] = help;
81  ++nfound;
82  }
83 
84  if (nfound == 0) { ATH_MSG_WARNING("no sample read from file " << currentFileName); }
85  }
86 
87  if (m_fStoreSampleInfo.empty()) {
88  ATH_MSG_ERROR("list of sample is empty");
89  return false;
90  }
91 
92  return true;
93 }
94 
95 
97 {
98 
99  TString mydir = inputDir;
100  gSystem->ExpandPathName (mydir);
101  void *dirp = 0;
102 
103  std::vector<std::string> inFiles = {};
104 
105  try
106  {
107  dirp = gSystem->OpenDirectory (mydir.Data());
108  const char *file = 0;
109  while ((file = gSystem->GetDirEntry (dirp)))
110  {
111  std::string myfile = inputDir + "/" + file;
112  if (myfile.size() > 4 && myfile.substr (myfile.size() - 4) == ".txt")
113  inFiles.push_back(myfile);
114  }
115  gSystem->FreeDirectory (dirp);
116  }
117  catch (...)
118  {
119  gSystem->FreeDirectory (dirp);
120  //throw;
121  return false;
122  }
123 
125  return true;
126 }
127 
128 
129 double PMGCrossSectionTool::getFilterEff(const int dsid, const int etag) const
130 {
131  const auto it = m_fStoreSampleInfo.find({dsid, etag});
132  if (it != m_fStoreSampleInfo.end()) { return it->second.filterEff; }
133 
134  ATH_MSG_ERROR("Sample with DSID " << dsid << " and e-tag " << etag << " has no info stored!!!");
135  return -1;
136 }
137 
138 
139 std::string PMGCrossSectionTool::getSampleName(const int dsid, const int etag) const
140 {
141  const auto it = m_fStoreSampleInfo.find({dsid, etag});
142  if (it != m_fStoreSampleInfo.end()) { return it->second.containerName; }
143 
144  ATH_MSG_ERROR("Sample with DSID " << dsid << " and e-tag " << etag << " has no info stored!!!");
145  return std::string("");
146 }
147 
148 
149 double PMGCrossSectionTool::getAMIXsection(const int dsid, const int etag) const
150 {
151  const auto it = m_fStoreSampleInfo.find({dsid, etag});
152  if (it != m_fStoreSampleInfo.end()) { return it->second.amiXSec; }
153 
154  ATH_MSG_ERROR("Sample with DSID " << dsid << " and e-tag " << etag << " has no info stored!!!");
155  return -1;
156 }
157 
158 
159 // :: below is for future use?
160 /*double PMGCrossSectionTool::getBR(const int dsid, const int etag) const
161 {
162  const auto it = m_fStoreSampleInfo.find({dsid, etag});
163  if (it != m_fStoreSampleInfo.end()) { return it->second.br; }
164 
165  ATH_MSG_ERROR("Sample with DSID " << dsid << " and e-tag " << etag << " has no info stored!!!");
166  return -1;
167 
168 }*/
169 
170 
171 double PMGCrossSectionTool::getXsectionUncertaintyUP(const int dsid, const int etag) const
172 {
173  const auto it = m_fStoreSampleInfo.find({dsid, etag});
174  if (it != m_fStoreSampleInfo.end()) { return it->second.XSecUncUP; }
175 
176  ATH_MSG_ERROR("Sample with DSID " << dsid << " and e-tag " << etag << " has no info stored!!!");
177  return -1;
178 }
179 
180 double PMGCrossSectionTool::getXsectionUncertaintyDOWN(const int dsid, const int etag) const
181 {
182  const auto it = m_fStoreSampleInfo.find({dsid, etag});
183  if (it != m_fStoreSampleInfo.end()) { return it->second.XSecUncDOWN; }
184 
185  ATH_MSG_ERROR("Sample with DSID " << dsid << " and e-tag " << etag << " has no info stored!!!");
186  return -1;
187 }
188 
189 double PMGCrossSectionTool::getXsectionUncertainty(const int dsid, const int etag) const
190 {
191  //symmetrize the up and down variations
192  return 0.5*( fabs(getXsectionUncertaintyDOWN(dsid, etag)) + fabs(getXsectionUncertaintyUP(dsid, etag)) );
193 }
194 
195 double PMGCrossSectionTool::getKfactor(const int dsid, const int etag) const
196 {
197  const auto it = m_fStoreSampleInfo.find({dsid, etag});
198  if (it != m_fStoreSampleInfo.end()) { return it->second.kFactor; }
199 
200  ATH_MSG_ERROR("Sample with DSID " << dsid << " and e-tag " << etag << " has no info stored!!!");
201  return -1;
202 }
203 
204 
205 // :: below is for future use?
206 double PMGCrossSectionTool::getSampleXsection(const int dsid, const int etag) const
207 {
208  const auto it = m_fStoreSampleInfo.find({dsid, etag});
209  if (it != m_fStoreSampleInfo.end()) { return it->second.amiXSec * it->second.kFactor * it->second.filterEff; }
210  // :: below is for future use?
211  //return info.higherOrderXsecSample;
212 
213  ATH_MSG_ERROR("Sample with DSID " << dsid << " and e-tag " << etag << " has no info stored!!!");
214  return -1;
215 }
216 
217 
218 std::vector<int> PMGCrossSectionTool::getLoadedDSIDs() const
219 {
220  std::vector<int> dsids;
221  dsids.reserve(m_fStoreSampleInfo.size());
222  for (const auto& key_info : m_fStoreSampleInfo) {
223  dsids.push_back(key_info.second.dsid);
224  }
225  // DSIDs can appear multiple times with different e-tags
226  // remove duplicates here
227  std::sort( dsids.begin(), dsids.end() );
228  dsids.erase( std::unique( dsids.begin(), dsids.end() ), dsids.end() );
229 
230  return dsids;
231 }
232 
233 } // end namespace
PMGTools::PMGCrossSectionTool::PMGCrossSectionTool
PMGCrossSectionTool(const std::string &name="PMGCrossSectionTool")
Standard tool constructor, with name.
Definition: PMGCrossSectionTool.cxx:21
PMGTools::PMGCrossSectionTool::readInfosFromFiles
bool readInfosFromFiles(const std::vector< std::string > &)
read infos from file, store them in the structure and make a vector that keeps all of them
Definition: PMGCrossSectionTool.cxx:37
PMGCrossSectionTool.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
PMGTools::PMGCrossSectionTool::getXsectionUncertaintyDOWN
double getXsectionUncertaintyDOWN(const int dsid, const int etag=-1) const
return the cross-section uncertainty for DSID
Definition: PMGCrossSectionTool.cxx:180
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
Make4DCorrelationMatrix.inFiles
list inFiles
Definition: Make4DCorrelationMatrix.py:59
skel.it
it
Definition: skel.GENtoEVGEN.py:407
asg
Definition: DataHandleTestTool.h:28
PMGTools::PMGCrossSectionTool::getKfactor
double getKfactor(const int dsid, const int etag=-1) const
return the branching ratio for DSID
Definition: PMGCrossSectionTool.cxx:195
grl_maker.mydir
mydir
Definition: grl_maker.py:4
PMGTools::PMGCrossSectionTool::getXsectionUncertaintyUP
double getXsectionUncertaintyUP(const int dsid, const int etag=-1) const
return the cross-section uncertainty for DSID
Definition: PMGCrossSectionTool.cxx:171
dq_defect_bulk_create_defects.line
line
Definition: dq_defect_bulk_create_defects.py:27
PMGTools::PMGCrossSectionTool::getSampleXsection
double getSampleXsection(const int dsid, const int etag=-1) const
return the sample cross-section for DSID
Definition: PMGCrossSectionTool.cxx:206
PMGTools::PMGCrossSectionTool::getLoadedDSIDs
std::vector< int > getLoadedDSIDs() const
get a list of the DSID for the loaded samples
Definition: PMGCrossSectionTool.cxx:218
PMGTools::PMGCrossSectionTool::getFilterEff
double getFilterEff(const int dsid, const int etag=-1) const
return filter efficiency for DSID
Definition: PMGCrossSectionTool.cxx:129
PMGTools::PMGCrossSectionTool::getAMIXsection
double getAMIXsection(const int dsid, const int etag=-1) const
return the AMI cross-section for DSID
Definition: PMGCrossSectionTool.cxx:149
PMGTools::PMGCrossSectionTool::initialize
StatusCode initialize()
initialize() is required by AsgTool base class
Definition: PMGCrossSectionTool.cxx:24
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
PMGTools::PMGCrossSectionTool::getXsectionUncertainty
double getXsectionUncertainty(const int dsid, const int etag=-1) const
return the cross-section uncertainty for DSID
Definition: PMGCrossSectionTool.cxx:189
PMGTools::PMGCrossSectionTool::m_fStoreSampleInfo
std::map< std::pair< unsigned, int >, PMGTools::AllSampleInfo > m_fStoreSampleInfo
Definition: PMGCrossSectionTool.h:82
Generate_dsid_ranseed.dsid
dsid
Definition: Generate_dsid_ranseed.py:6
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
std::unique
std::reverse_iterator< DataModel_detail::iterator< DVL > > unique(typename std::reverse_iterator< DataModel_detail::iterator< DVL > > beg, typename std::reverse_iterator< DataModel_detail::iterator< DVL > > end, BinaryPredicate pred)
Specialization of unique for DataVector/List.
Definition: DVL_algorithms.h:199
PMGTools::AllSampleInfo
Definition: IPMGCrossSectionTool.h:18
PMGTools
Tool providing sample cross-sections and k-factors etc.
Definition: AnalysisCommon/PMGTools/PMGTools/IPMGSherpaVjetsSysTool.h:16
CalibDbCompareRT.dummy
dummy
Definition: CalibDbCompareRT.py:59
file
TFile * file
Definition: tile_monitor.h:29
TestSUSYToolsAlg.inputDir
string inputDir
Definition: TestSUSYToolsAlg.py:74
python.CaloAddPedShiftConfig.help
help
Definition: CaloAddPedShiftConfig.py:42
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
mergeIDTPM.InputFiles
list InputFiles
Definition: mergeIDTPM.py:18
std::sort
void sort(typename std::reverse_iterator< DataModel_detail::iterator< DVL > > beg, typename std::reverse_iterator< DataModel_detail::iterator< DVL > > end, const Compare &comp)
Specialization of sort for DataVector/List.
Definition: DVL_algorithms.h:623
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
PMGTools::PMGCrossSectionTool::getSampleName
std::string getSampleName(const int dsid, const int etag=-1) const
return the sample name for DSID
Definition: PMGCrossSectionTool.cxx:139
PMGTools::PMGCrossSectionTool::readInfosFromDir
bool readInfosFromDir(const std::string &inputDir)
read infos from all files in dir
Definition: PMGCrossSectionTool.cxx:96