ATLAS Offline Software
Loading...
Searching...
No Matches
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
18namespace PMGTools {
19
21PMGCrossSectionTool(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
37bool 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);
54 AllSampleInfo help;
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
96bool PMGCrossSectionTool::readInfosFromDir(const std::string& inputDir)
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(std::move(myfile));
114 }
115 gSystem->FreeDirectory (dirp);
116 }
117 catch (...)
118 {
119 gSystem->FreeDirectory (dirp);
120 //throw;
121 return false;
122 }
123
124 readInfosFromFiles(inFiles);
125 return true;
126}
127
128
129double 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
139std::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
149double 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
171double 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
180double 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
189double 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
195double 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?
206double 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
218std::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
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
double getKfactor(const int dsid, const int etag=-1) const
return the branching ratio for DSID
bool readInfosFromDir(const std::string &inputDir)
read infos from all files in dir
std::string getSampleName(const int dsid, const int etag=-1) const
return the sample name for DSID
double getXsectionUncertaintyUP(const int dsid, const int etag=-1) const
return the cross-section uncertainty for DSID
double getXsectionUncertaintyDOWN(const int dsid, const int etag=-1) const
return the cross-section uncertainty for DSID
double getSampleXsection(const int dsid, const int etag=-1) const
return the sample cross-section for DSID
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
double getXsectionUncertainty(const int dsid, const int etag=-1) const
return the cross-section uncertainty for DSID
double getAMIXsection(const int dsid, const int etag=-1) const
return the AMI cross-section for DSID
PMGCrossSectionTool(const std::string &name="PMGCrossSectionTool")
Standard tool constructor, with name.
StatusCode initialize()
initialize() is required by AsgTool base class
double getFilterEff(const int dsid, const int etag=-1) const
return filter efficiency for DSID
std::vector< int > getLoadedDSIDs() const
get a list of the DSID for the loaded samples
std::map< std::pair< unsigned, int >, PMGTools::AllSampleInfo > m_fStoreSampleInfo
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
Tool providing sample cross-sections and k-factors etc.
DataModel_detail::iterator< DVL > unique(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of unique for DataVector/List.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
TFile * file