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>
20#include <TFile.h>
21#include <TSystem.h>
22#include <iostream>
23#include <thread>
24#include <chrono>
25#include <random>
26#include <stdexcept>
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 throw std::runtime_error ("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 throw std::runtime_error ("failed to open file: " + name);
66 }
67
68
69
70 std::unique_ptr<SampleLocal>
71 mergeFiles (const Sample& sample, const std::string& location,
72 bool overwrite)
73 {
74 auto result = std::make_unique<SampleLocal> (sample.name());
75 result->add (location);
76 *result->meta() = *sample.meta();
77 if (overwrite || gSystem->AccessPathName (location.c_str()) != 0)
78 {
79 std::vector<std::string> files = sample.makeFileList();
80 RCU::hadd (location, files);
81 }
82 return result;
83 }
84
85
86
87 SampleHandler
88 mergeFiles (const SampleHandler& sh, const std::string& location,
89 bool overwrite)
90 {
91 SampleHandler result;
92 for (auto sample : sh)
93 {
94 std::string sublocation = location + sample->name() + ".root";
95 result.add (mergeFiles (*sample, sublocation, overwrite));
96 }
97 return result;
98 }
99}
const std::regex re(r_e)
A class that manages meta-data to be associated with an object.
Definition MetaObject.h:48
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
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:52
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:29
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition DiskList.cxx:21
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