ATLAS Offline Software
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 
9 #include <SampleHandler/fetch.h>
10 
11 #include <sstream>
12 #include <mutex>
13 #include <TPython.h>
14 #include <TString.h>
15 #include <TSystem.h>
19 #include <RootCoreUtils/Assert.h>
20 #include <RootCoreUtils/PrintMsg.h>
22 #include <SampleHandler/Sample.h>
24 
25 //
26 // method implementations
27 //
28 
29 namespace SH
30 {
32  {
33  static std::once_flag loaded;
34  auto do_load = []() {
35  TString path = "$ROOTCOREBIN/python/SampleHandler/SampleHandler_QueryAMI.py";
36  gSystem->ExpandPathName (path);
37  TPython::LoadMacro (path.Data());
38  };
39  std::call_once (loaded, do_load);
40 
41  std::ostringstream command;
42 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,33,01)
43  command << "_anyresult = ";
44 #endif
45  command << "SampleHandler_QueryAmi([";
46  for (std::size_t iter = 0, end = query.samples.size(); iter != end; ++ iter)
47  {
48  if (iter != 0)
49  command << ", ";
50  command << "'" << query.samples[iter].name << "'";
51  }
52  command << "])";
53 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,33,01)
54  std::any result;
55  TPython::Exec (command.str().c_str(), &result);
56  query = std::any_cast<MetaDataQuery>(result);
57 #else
58  MetaDataQuery* myquery = static_cast<MetaDataQuery*>
59  ((void*) TPython::Eval (command.str().c_str()));
60  query = *myquery;
61 #endif
62  }
63 
64 
65  void fetchMetaData (SH::SampleHandler& sh, bool override)
66  {
67  std::vector<SH::Sample*> samples;
68  // typedef std::vector<SH::Sample*> SamplesIter;
70  for (SH::SampleHandler::iterator sample = sh.begin(),
71  end = sh.end(); sample != end; ++ sample)
72  {
73  std::string name = (*sample)->meta()->castString (SH::MetaFields::gridName,(*sample)->name());
74  query.samples.push_back (MetaDataSample (name));
75  samples.push_back (&**sample);
76  }
78 
79  if (!query.messages.empty())
80  RCU_PRINT_MSG (query.messages);
81  for (std::size_t iter = 0, end = query.samples.size(); iter != end; ++ iter)
82  {
83  if (query.samples[iter].unknown)
84  {
85  RCU_WARN_MSG ("failed to find sample " + query.samples[iter].name);
86  } else
87  {
88  RCU_ASSERT (iter != samples.size());
89  SH::Sample *sample = samples[iter];
90 
91  if (!override)
92  {
93  query.samples[iter].isData = sample->meta()->castDouble (SH::MetaFields::isData, query.samples[iter].isData);
94  query.samples[iter].luminosity = sample->meta()->castDouble (SH::MetaFields::lumi, query.samples[iter].luminosity);
95  query.samples[iter].crossSection = sample->meta()->castDouble (SH::MetaFields::crossSection, query.samples[iter].crossSection);
96  query.samples[iter].nevents = sample->meta()->castDouble (SH::MetaFields::numEvents, query.samples[iter].nevents);
97  query.samples[iter].kfactor = sample->meta()->castDouble (SH::MetaFields::kfactor, query.samples[iter].kfactor);
98  query.samples[iter].filterEfficiency = sample->meta()->castDouble (SH::MetaFields::filterEfficiency, query.samples[iter].filterEfficiency);
99  }
100  if (query.samples[iter].isData != -1)
101  sample->meta()->setDouble (SH::MetaFields::isData, query.samples[iter].isData);
102  if (query.samples[iter].luminosity != -1)
103  sample->meta()->setDouble (SH::MetaFields::lumi, query.samples[iter].luminosity);
104  if (query.samples[iter].crossSection != -1)
105  sample->meta()->setDouble (SH::MetaFields::crossSection, query.samples[iter].crossSection);
106  if (query.samples[iter].nevents != -1)
107  sample->meta()->setDouble (SH::MetaFields::numEvents, query.samples[iter].nevents);
108  if (query.samples[iter].kfactor != -1)
109  sample->meta()->setDouble (SH::MetaFields::kfactor, query.samples[iter].kfactor);
110  if (query.samples[iter].filterEfficiency != -1)
111  sample->meta()->setDouble (SH::MetaFields::filterEfficiency, query.samples[iter].filterEfficiency);
112  }
113  }
114  }
115 }
SH::SampleHandler::iterator
std::vector< Sample * >::const_iterator iterator
the iterator to use
Definition: SampleHandler.h:475
SH::MetaFields::numEvents
static const std::string numEvents
the number of events
Definition: MetaFields.h:64
get_generator_info.result
result
Definition: get_generator_info.py:21
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
SH::MetaFields::filterEfficiency
static const std::string filterEfficiency
the filter efficiency of the sample
Definition: MetaFields.h:85
SampleHandler.h
Assert.h
SH::MetaFields::isData
static const std::string isData
whether the sample is data
Definition: MetaFields.h:79
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
MetaDataSample.h
query
Definition: query.py:1
MetaObject.h
RCU_WARN_MSG
#define RCU_WARN_MSG(message)
Definition: PrintMsg.h:52
FullCPAlgorithmsTest_eljob.sample
sample
Definition: FullCPAlgorithmsTest_eljob.py:113
SH::MetaFields::crossSection
static const std::string crossSection
the cross section field
Definition: MetaFields.h:58
RCU::Shell
Definition: ShellExec.cxx:28
fetch.h
SH::MetaFields::kfactor
static const std::string kfactor
the k-factor of the sample
Definition: MetaFields.h:82
query_example.query
query
Definition: query_example.py:15
SH::Sample
a base class that manages a set of files belonging to a particular data set and the associated meta-d...
Definition: Sample.h:54
LoadMacro
gROOT LoadMacro("../ISF_FastCaloSimParametrization/MeanAndRMS.h+")
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
MetaFields.h
RCU_PRINT_MSG
#define RCU_PRINT_MSG(message)
Definition: PrintMsg.h:49
SH::MetaFields::gridName
static const std::string gridName
the field containing the name of the dataset on the grid
Definition: MetaFields.h:34
SH::fetchMetaData
void fetchMetaData(MetaDataQuery &query)
effects: fetch information on all the samples/datasets specified guarantee: basic
Definition: fetch.cxx:31
SH::MetaFields::lumi
static const std::string lumi
the luminosity of the sample
Definition: MetaFields.h:76
SH::MetaDataSample
all the meta-data fields that we may try to read from AMI.
Definition: MetaDataSample.h:15
MetaDataQuery.h
SH::SampleHandler
A class that manages a list of Sample objects.
Definition: SampleHandler.h:60
SH
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition: PrunDriver.h:15
RCU_ASSERT
#define RCU_ASSERT(x)
Definition: Assert.h:222
get_generator_info.command
string command
Definition: get_generator_info.py:38
Sample.h
SH::MetaDataQuery
an object containing the result of an AMI meta-data query
Definition: MetaDataQuery.h:17
PrintMsg.h