ATLAS Offline Software
Loading...
Searching...
No Matches
fetch.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5//
6// includes
7//
8
10
11#include <sstream>
12#include <mutex>
13#include <stdexcept>
14#include <RVersion.h>
15#include <TPython.h>
16#include <TString.h>
17#include <TSystem.h>
26
27//
28// method implementations
29//
30
31namespace
32{
35 std::string pyQuote (const std::string& name)
36 {
37 std::string result = "'";
38 for (char c : name)
39 {
40 if (c == '\\' || c == '\'')
41 result += '\\';
42 result += c;
43 }
44 result += "'";
45 return result;
46 }
47}
48
49namespace SH
50{
52 {
53 static std::once_flag loaded;
54 auto do_load = []() {
55 // rationale: the helper module is installed via
56 // atlas_install_python_modules and is importable through
57 // PYTHONPATH, so import it by name rather than loading it from
58 // the dead RootCore $ROOTCOREBIN path.
59 if (!TPython::Exec ("import SampleHandler.SampleHandler_QueryAMI"))
60 throw std::runtime_error ("failed to import python module SampleHandler.SampleHandler_QueryAMI");
61 };
62 std::call_once (loaded, do_load);
63
64 std::ostringstream command;
65#if ROOT_VERSION_CODE >= ROOT_VERSION(6,33,01)
66 command << "_anyresult = ";
67#endif
68 command << "SampleHandler.SampleHandler_QueryAMI.SampleHandler_QueryAmi([";
69 for (std::size_t iter = 0, end = query.samples.size(); iter != end; ++ iter)
70 {
71 if (iter != 0)
72 command << ", ";
73 command << pyQuote (query.samples[iter].name);
74 }
75 command << "])";
76#if ROOT_VERSION_CODE >= ROOT_VERSION(6,33,01)
77 std::any result;
78 if (!TPython::Exec (command.str().c_str(), &result))
79 throw std::runtime_error ("failed to execute AMI metadata query");
80 query = std::any_cast<MetaDataQuery>(result);
81#else
82 MetaDataQuery* myquery = static_cast<MetaDataQuery*>
83 ((void*) TPython::Eval (command.str().c_str()));
84 if (myquery == nullptr)
85 throw std::runtime_error ("failed to execute AMI metadata query");
86 query = *myquery;
87#endif
88 }
89
90
91 void fetchMetaData (SH::SampleHandler& sh, bool override)
92 {
93 using namespace msgFetch;
94
95 std::vector<SH::Sample*> samples;
96 // typedef std::vector<SH::Sample*> SamplesIter;
98 for (auto *sample : sh)
99 {
100 std::string name = sample->meta()->castString (SH::MetaFields::gridName, sample->name());
101 query.samples.push_back (MetaDataSample (name));
102 samples.push_back (sample);
103 }
105
106 if (!query.messages.empty())
107 ANA_MSG_INFO (query.messages);
108 for (std::size_t iter = 0, end = query.samples.size(); iter != end; ++ iter)
109 {
110 if (query.samples[iter].unknown)
111 {
112 ANA_MSG_WARNING ("failed to find sample " << query.samples[iter].name);
113 } else
114 {
115 if (iter >= samples.size())
116 throw std::runtime_error ("AMI query returned more samples than were requested");
117 SH::Sample *sample = samples[iter];
118
119 if (!override)
120 {
121 query.samples[iter].isData = sample->meta()->castDouble (SH::MetaFields::isData, query.samples[iter].isData);
122 query.samples[iter].luminosity = sample->meta()->castDouble (SH::MetaFields::lumi, query.samples[iter].luminosity);
123 query.samples[iter].crossSection = sample->meta()->castDouble (SH::MetaFields::crossSection, query.samples[iter].crossSection);
124 query.samples[iter].nevents = sample->meta()->castDouble (SH::MetaFields::numEvents, query.samples[iter].nevents);
125 query.samples[iter].kfactor = sample->meta()->castDouble (SH::MetaFields::kfactor, query.samples[iter].kfactor);
126 query.samples[iter].filterEfficiency = sample->meta()->castDouble (SH::MetaFields::filterEfficiency, query.samples[iter].filterEfficiency);
127 }
128 if (query.samples[iter].isData != -1)
129 sample->meta()->setDouble (SH::MetaFields::isData, query.samples[iter].isData);
130 if (query.samples[iter].luminosity != -1)
131 sample->meta()->setDouble (SH::MetaFields::lumi, query.samples[iter].luminosity);
132 if (query.samples[iter].crossSection != -1)
133 sample->meta()->setDouble (SH::MetaFields::crossSection, query.samples[iter].crossSection);
134 if (query.samples[iter].nevents != -1)
135 sample->meta()->setDouble (SH::MetaFields::numEvents, query.samples[iter].nevents);
136 if (query.samples[iter].kfactor != -1)
137 sample->meta()->setDouble (SH::MetaFields::kfactor, query.samples[iter].kfactor);
138 if (query.samples[iter].filterEfficiency != -1)
139 sample->meta()->setDouble (SH::MetaFields::filterEfficiency, query.samples[iter].filterEfficiency);
140 }
141 }
142 }
143}
#define ANA_MSG_INFO(xmsg,...)
Macro printing info messages.
#define ANA_MSG_WARNING(xmsg,...)
Macro printing warning messages.
A class that manages a list of Sample objects.
a base class that manages a set of files belonging to a particular data set and the associated meta-d...
Definition Sample.h:49
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition DiskList.cxx:21
void fetchMetaData(MetaDataQuery &query)
effects: fetch information on all the samples/datasets specified guarantee: basic
Definition fetch.cxx:51
Definition query.py:1
an object containing the result of an AMI meta-data query
all the meta-data fields that we may try to read from AMI.
static const std::string kfactor
the k-factor of the sample
Definition MetaFields.h:74
static const std::string numEvents
the number of events
Definition MetaFields.h:56
static const std::string gridName
the field containing the name of the dataset on the grid
Definition MetaFields.h:26
static const std::string isData
whether the sample is data
Definition MetaFields.h:71
static const std::string lumi
the luminosity of the sample
Definition MetaFields.h:68
static const std::string crossSection
the cross section field
Definition MetaFields.h:50
static const std::string filterEfficiency
the filter efficiency of the sample
Definition MetaFields.h:77