ATLAS Offline Software
ToolsOther.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 //
9 // includes
10 //
11 
13 
14 #include <RootCoreUtils/Assert.h>
15 #include <RootCoreUtils/hadd.h>
16 #include <RootCoreUtils/ThrowMsg.h>
21 #include <TFile.h>
22 #include <TSystem.h>
23 #include <iostream>
24 #include <thread>
25 #include <chrono>
26 #include <random>
27 
28 //
29 // method implementations
30 //
31 
32 namespace SH
33 {
34  std::unique_ptr<TFile>
35  openFile (const std::string& name, const MetaObject& options)
36  {
37  const unsigned retries
38  = options.castInteger (MetaNames::openRetries(),
40  const double wait
41  = options.castDouble (MetaNames::openRetriesWait(),
43  if (wait < 0)
44  RCU_THROW_MSG ("negative values not allowed for: " + MetaNames::openRetriesWait());
45 
46  std::unique_ptr<TFile> result;
47 
48  static std::atomic<unsigned> seed = 0;
49  std::default_random_engine re (++seed);
50  std::uniform_real_distribution wait_dist (wait/2, wait);
51 
52  for (unsigned tries = 0; tries <= retries; ++ tries)
53  {
54  if (tries > 0)
55  {
56  unsigned mywait = 1000 * wait_dist (re);
57  std::cout << "open failed, waiting " << (mywait/1000.) << " seconds: "
58  << name << std::endl;
59  std::this_thread::sleep_for (std::chrono::milliseconds (mywait));
60  }
61  result.reset (TFile::Open (name.c_str(), "READ"));
62  if (result != nullptr)
63  return result;
64  }
65  RCU_THROW_MSG ("failed to open file: " + name);
66  //cppcheck-suppress rethrowNoCurrentException
67  throw; //compiler dummy
68  }
69 
70 
71 
72  std::unique_ptr<SampleLocal>
73  mergeFiles (const Sample& sample, const std::string& location,
74  bool overwrite)
75  {
76  std::unique_ptr<SampleLocal> result (new SampleLocal (sample.name()));
77  result->add (location);
78  *result->meta() = *sample.meta();
79  if (overwrite || gSystem->AccessPathName (location.c_str()) != 0)
80  {
81  std::vector<std::string> files = sample.makeFileList();
82  RCU::hadd (location, files);
83  }
84  return result;
85  }
86 
87 
88 
89  SampleHandler
90  mergeFiles (const SampleHandler& sh, const std::string& location,
91  bool overwrite)
92  {
94  for (auto& sample : sh)
95  {
96  std::string sublocation = location + sample->name() + ".root";
97  result.add (mergeFiles (*sample, sublocation, overwrite));
98  }
99  return result;
100  }
101 }
get_generator_info.result
result
Definition: get_generator_info.py:21
SH::MetaNames::openRetries_default
static unsigned openRetries_default()
the default value of openRetries
Definition: MetaNames.cxx:32
SH::MetaObject
A class that manages meta-data to be associated with an object.
Definition: MetaObject.h:56
SH::MetaNames::openRetriesWait_default
static double openRetriesWait_default()
the default value of openRetriesWait
Definition: MetaNames.cxx:49
SampleHandler.h
hadd.h
Assert.h
MetaNames.h
RCU::hadd
void hadd(const std::string &output_file, const std::vector< std::string > &input_files, unsigned max_files)
effects: perform the hadd functionality guarantee: basic failures: out of memory III failures: i/o er...
Definition: hadd.cxx:28
SampleLocal.h
MetaObject.h
FullCPAlgorithmsTest_eljob.sample
sample
Definition: FullCPAlgorithmsTest_eljob.py:116
ToolsOther.h
RCU::Shell
Definition: ShellExec.cxx:28
generateReferenceFile.files
files
Definition: generateReferenceFile.py:12
SH::MetaNames::openRetriesWait
static const std::string & openRetriesWait()
the amount of time (in seconds) to wait when doing retries
Definition: MetaNames.cxx:40
SH::openFile
std::unique_ptr< TFile > openFile(const std::string &name, const MetaObject &options)
open a file with the given options
Definition: ToolsOther.cxx:35
python.AtlRunQueryLib.options
options
Definition: AtlRunQueryLib.py:379
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
SH::mergeFiles
std::unique_ptr< SampleLocal > mergeFiles(const Sample &sample, const std::string &location, bool overwrite)
merge all the files in the sample into a single file in the given location
Definition: ToolsOther.cxx:73
SH::MetaNames::openRetries
static const std::string & openRetries()
the number of retries for opening a file
Definition: MetaNames.cxx:23
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
ThrowMsg.h
re
const boost::regex re(r_e)
SH::SampleLocal
A Sample based on a simple file list.
Definition: SampleLocal.h:38
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_THROW_MSG
#define RCU_THROW_MSG(message)
Definition: PrintMsg.h:58