ATLAS Offline Software
FileMetaDataTool.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 // Local include(s):
7 
8 // standard library includes
9 #include <memory>
10 #include <utility>
11 
12 // EDM include(s):
15 
16 
17 namespace xAODMaker {
18 
20  : asg::AsgMetadataTool(name) {
21  declareProperty( "Keys", m_keys = {},
22  "(optional) List of keys to copy. Copy all keys if empty "
23  "(default: empty)");
24 #ifndef XAOD_STANDALONE
25  declareInterface< ::IMetaDataTool >(this);
26 #endif // XAOD_STANDALONE
27  }
28 
31 #ifndef XAOD_STANDALONE
32  ASG_CHECK(m_metaDataSvc.retrieve());
33 #endif // XAOD_STANDALONE
34 
35  // Return gracefully:
36  return StatusCode::SUCCESS;
37  }
38 
41  // Previous input file has been processed
42  std::lock_guard lock(m_toolMutex);
43 
44  // get the keys for all metadata in input
45  std::vector<std::string> keys = m_keys;
46  if (keys.empty()) {
48  } else {
49  // remove keys not in the InputMetaDataStore
50  keys.erase(
51  std::remove_if(
52  keys.begin(), keys.end(),
53  [this](std::string& key) {
54  return !inputMetaStore()->contains<xAOD::FileMetaData>(key);
55  }),
56  keys.end());
57  }
58 
59  // If the input file doesn't have any event format metadata,
60  // then finish right away:
61  if (keys.empty()) return StatusCode::SUCCESS;
62 
63  // Now copy all object to MetaDataStore
64  for(const std::string& key : keys) {
65 #ifdef XAOD_STANDALONE
66  ASG_CHECK(copy(key));
67 #else
68  for(const std::string& stream_key : m_metaDataSvc->getPerStreamKeysFor(key) ) {
69  ASG_CHECK( copy(stream_key) );
70  }
71 #endif // XAOD_STANDALONE
72  }
73  return StatusCode::SUCCESS;
74  }
75 
77  FileMetaDataTool::copy(const std::string& key) {
78  ATH_MSG_DEBUG("Copying \"" << key << "\" from InputMetaDataStore");
79  // Quit gracefully if there is nothing to do
80  if (!inputMetaStore()->contains< xAOD::FileMetaData >(key)) {
81  ATH_MSG_INFO("No \"" << key << "\" in the input file");
82  return StatusCode::SUCCESS;
83  }
84 
85  // Get the FileMetaData object from the input file
86  const xAOD::FileMetaData * input = nullptr;
88 
89  // Emit a warning if the FileMetaData from previous files does not
90  // match that of the new input file
91 #ifdef XAOD_STANDALONE
92  if (outputMetaStore()->contains< xAOD::FileMetaData >(key)) {
93  xAOD::FileMetaData * output = nullptr;
95 #else
96  if (m_metaDataSvc->contains< xAOD::FileMetaData >(key)) {
97  auto *output = m_metaDataSvc->tryRetrieve< xAOD::FileMetaData >(key);
98  if (!output) return StatusCode::FAILURE;
99 #endif // XAOD_STANDALONE
100 
101  copyValues(input, output, "runNumbers");
102  copyValues(input, output, "lumiBlocks");
103 
104  const std::set<std::string> ignore { "runNumbers", "lumiBlocks" };
105  if( !input->compareWith(*output, ignore) )
106  ATH_MSG_WARNING("Inconsistent input file MetaData");
107 
108  return StatusCode::SUCCESS;
109  }
110 
111  ATH_MSG_DEBUG("Creating output objects");
112  auto output = std::make_unique< xAOD::FileMetaData >();
113  auto outputAux = std::make_unique< xAOD::FileMetaDataAuxInfo >();
114  output->setStore(outputAux.get());
115 
116  // Copy input object
117  *output = *input;
118 
119 
120 #ifdef XAOD_STANDALONE
121  ASG_CHECK(
122  outputMetaStore()->record< xAOD::FileMetaData >(
123  std::move(output), key));
124 
125  ASG_CHECK(
126  outputMetaStore()->record< xAOD::FileMetaDataAuxInfo >(
127  std::move(outputAux), key + "Aux."));
128 #else
129  ASG_CHECK(
131  std::move(output), key));
132 
133  ASG_CHECK(
135  std::move(outputAux), key + "Aux."));
136 #endif // XAOD_STANDALONE
137 
138  ATH_MSG_INFO("Copied \"" << key << "\" to MetaDataStore");
139 
140  // Return gracefully:
141  return StatusCode::SUCCESS;
142  }
143 
144 
146  const std::string& var)
147 {
148  std::vector<uint32_t> src_vec, dst_vec;
149  src->value(var, src_vec);
150  dst->value(var, dst_vec);
151  bool updated = false;
152 
153  for( auto val : src_vec ) {
154  // we want a sorted list of unique values (without using std::set)
155  auto it = std::lower_bound( dst_vec.begin(), dst_vec.end(), val );
156  if( it == dst_vec.end() || (*it) != val ) {
157  dst_vec.insert(it, val);
158  updated = true;
159  ATH_MSG_DEBUG("added " << val << " to list of " << var);
160  }
161  }
162  if( updated ) {
163  if( !dst->setValue(var, dst_vec) ) {
164  ATH_MSG_WARNING("error updating values for " + var);
165  }
166  }
167 }
168 
169 } // namespace xAODMaker
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
ASG_CHECK
#define ASG_CHECK(...)
Helper macro for checking the status code returned by a function call.
Definition: Check.h:39
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAOD::FileMetaDataAuxInfo_v1
Auxiliary store for xAOD::FileMetaData_v1.
Definition: FileMetaDataAuxInfo_v1.h:31
xAODMaker::FileMetaDataTool::m_toolMutex
std::mutex m_toolMutex
Definition: FileMetaDataTool.h:83
WriteCellNoiseToCool.src
src
Definition: WriteCellNoiseToCool.py:513
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
skel.it
it
Definition: skel.GENtoEVGEN.py:396
asg
Definition: DataHandleTestTool.h:28
xAODMaker::FileMetaDataTool::copy
StatusCode copy(const std::string &)
Definition: FileMetaDataTool.cxx:77
asg::AsgMetadataTool::inputMetaStore
MetaStorePtr_t inputMetaStore() const
Accessor for the input metadata store.
Definition: AsgMetadataTool.cxx:88
FileMetaDataAuxInfo.h
xAODMaker
Definition: StoreGateSvc.h:72
xAOD::FileMetaData_v1::value
bool value(MetaDataType type, std::string &val) const
Get a pre-defined string value out of the object.
Definition: FileMetaData_v1.cxx:195
DiTauMassTools::ignore
void ignore(T &&)
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:58
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
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
xAOD::FileMetaData_v1
Class holding file-level metadata about an xAOD file.
Definition: FileMetaData_v1.h:34
xAOD::FileMetaData_v1::setValue
bool setValue(MetaDataType type, const std::string &val)
Set a pre-defined string value on the object.
Definition: FileMetaData_v1.cxx:233
xAODMaker::FileMetaDataTool::copyValues
void copyValues(const xAOD::FileMetaData *src, xAOD::FileMetaData *dst, const std::string &var)
Definition: FileMetaDataTool.cxx:145
merge.output
output
Definition: merge.py:17
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
xAODMaker::FileMetaDataTool::m_metaDataSvc
ServiceHandle< IAthMetaDataSvc > m_metaDataSvc
Get a handle on the metadata store for the job.
Definition: FileMetaDataTool.h:79
FileMetaDataTool.h
xAODMaker::FileMetaDataTool::beginInputFile
StatusCode beginInputFile() override
Collecting file metadata from input and write to output.
Definition: FileMetaDataTool.cxx:40
FileMetaData.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
asg::AsgMetadataTool::outputMetaStore
MetaStorePtr_t outputMetaStore() const
Accessor for the output metadata store.
Definition: AsgMetadataTool.cxx:97
xAODMaker::FileMetaDataTool::m_keys
std::vector< std::string > m_keys
(optional) list of keys to propagate from input to output.
Definition: FileMetaDataTool.h:68
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
xAODMaker::FileMetaDataTool::initialize
StatusCode initialize() override
Function initialising the tool.
Definition: FileMetaDataTool.cxx:30
xAODMaker::FileMetaDataTool::FileMetaDataTool
FileMetaDataTool(const std::string &name="FileMetaDataTool")
Regular AsgTool constructor.
Definition: FileMetaDataTool.cxx:19
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37