ATLAS Offline Software
Loading...
Searching...
No Matches
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
15#include <RootCoreUtils/hadd.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
32namespace 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}
const boost::regex re(r_e)
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:58
A class that manages meta-data to be associated with an object.
Definition MetaObject.h:56
A class that manages a list of Sample objects.
A Sample based on a simple file list.
Definition SampleLocal.h:38
a base class that manages a set of files belonging to a particular data set and the associated meta-d...
Definition Sample.h:54
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:50
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
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition PrunDriver.h:15
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
std::unique_ptr< TFile > openFile(const std::string &name, const MetaObject &options)
open a file with the given options
static const std::string & openRetries()
the number of retries for opening a file
Definition MetaNames.cxx:23
static double openRetriesWait_default()
the default value of openRetriesWait
Definition MetaNames.cxx:49
static unsigned openRetries_default()
the default value of openRetries
Definition MetaNames.cxx:32
static const std::string & openRetriesWait()
the amount of time (in seconds) to wait when doing retries
Definition MetaNames.cxx:40