ATLAS Offline Software
Loading...
Searching...
No Matches
EmptyFileHandler.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8//
9// includes
10//
11
13
17#include <RootCoreUtils/hadd.h>
19#include <EventLoop/Job.h>
25#include <TList.h>
26#include <TChain.h>
27#include <TFile.h>
28#include <TObjString.h>
29#include <memory>
30
31#include <iostream>
32
33//
34// method implementations
35//
36
37namespace EL
38{
39 namespace
40 {
41 std::vector<std::string>
42 readVectorFromTree (SH::Sample *sample, const std::string& treeName,
43 const std::string& branchName)
44 {
45 std::vector<std::string> result;
46 auto fileNames = sample->makeFileList();
47 TString *var = nullptr;
48 for (auto& fileName : fileNames)
49 {
50 std::unique_ptr<TFile> file (TFile::Open (fileName.c_str(), "READ"));
51 if (file == nullptr)
52 RCU_THROW_MSG ("failed to open file: " + fileName);
53 TTree *tree = dynamic_cast<TTree*>(file->Get (treeName.c_str()));
54 Long64_t nentries = 0;
55 if (tree != nullptr && (nentries = tree->GetEntries()) > 0)
56 {
57 TBranch *branch = nullptr;
58 tree->SetBranchAddress (branchName.c_str(), &var, &branch);
59 for (Long64_t entry = 0; entry < nentries; ++ entry)
60 {
61 if (branch->GetEntry(entry) <= 0)
62 RCU_THROW_MSG ("failed to read entry from branch");
63 result.push_back (var->Data());
64 }
65 }
66 }
67 return result;
68 }
69
70
71
72 SH::SampleHandler makeReprocessSamples (const std::string& submitdir)
73 {
75 SH::SampleHandler shInput;
76 shInput.load (submitdir + "/input");
77
79 SH::SampleHandler shHist;
80 shHist.load (submitdir + "/hist");
81 shHist.setMetaString (SH::MetaFields::treeName, "EventLoop_FileExecuted");
82
84 SH::SampleHandler shReprocess;
85
86 for (SH::Sample *sampleInput : shInput)
87 {
88 SH::Sample *sampleHist = shHist.get (sampleInput->name());
89 RCU_ASSERT (sampleHist != nullptr);
90 std::vector<std::string> filesUsedVector
91 = readVectorFromTree (sampleHist, "EventLoop_FileExecuted", "file");
92 std::set<std::string> filesUsed
93 (filesUsedVector.begin(), filesUsedVector.end());
94 RCU_ASSERT (filesUsed.size() == filesUsedVector.size());
95
96 std::unique_ptr<SH::SampleLocal> sampleReprocess;
97 std::vector<std::string> filesInput (sampleInput->makeFileList ());
98
99 for (auto fileInput : filesInput)
100 {
101 std::string fileName = fileInput;
102 auto split = fileName.rfind ('/');
103 if (split != std::string::npos)
104 fileName = fileName.substr (split + 1);
105
106 if (filesUsed.find (fileName) == filesUsed.end())
107 {
108 if (sampleReprocess == nullptr)
109 {
110 sampleReprocess.reset (new SH::SampleLocal (sampleInput->name()));
111 *sampleReprocess->meta() = *sampleInput->meta();
112 }
113 sampleReprocess->add (fileInput);
114 }
115 }
116 if (filesInput.empty())
117 {
118 sampleReprocess.reset (new SH::SampleLocal (sampleInput->name()));
119 *sampleReprocess->meta() = *sampleInput->meta();
120 }
121 if (sampleReprocess != nullptr)
122 {
123 RCU_ASSERT (sampleReprocess->makeFileList().size() + filesUsed.size() == filesInput.size());
124 shReprocess.add (sampleReprocess.release());
125 }
126 }
127 return shReprocess;
128 }
129
130
131
132 void mergeHists (const std::string& mainDir,
133 const std::string& extraDir,
134 const SH::SampleHandler& samples)
135 {
136 for (auto& sample : samples)
137 {
138 std::string tmp = extraDir + "/hist2-" + sample->name() + ".root";
139 std::string target = mainDir + "/hist-" + sample->name() + ".root";
140
141 RCU::hadd (tmp, {target, extraDir + "/hist-" + sample->name() + ".root"});
142 RCU::Shell::exec ("mv -f " + RCU::Shell::quote (tmp) + " " + RCU::Shell::quote (target));
143 }
144 }
145
146
147
148 void mergeNtuple (const std::string& mainDir,
149 const std::string& extraDir)
150 {
151 SH::SampleHandler mainSH;
152 mainSH.load (mainDir);
153 SH::SampleHandler extraSH;
154 extraSH.load (extraDir);
155 SH::SampleHandler newSH;
156
157 for (SH::Sample *extraSample : extraSH)
158 {
159 SH::Sample *mainSample = mainSH.get (extraSample->name());
160 RCU_ASSERT (mainSample != nullptr);
161 std::unique_ptr<SH::SampleLocal> newSample
162 (new SH::SampleLocal (extraSample->name()));
163 *newSample->meta() = *mainSample->meta();
164
165 for (auto& file : mainSample->makeFileList())
166 newSample->add (file);
167 for (auto& file : extraSample->makeFileList())
168 newSample->add (file);
169 newSH.add (newSample.release());
170 }
171
172 for (SH::Sample *mainSample : mainSH)
173 {
174 if (newSH.get (mainSample->name()) == nullptr)
175 newSH.add (mainSample);
176 }
177
178 mainSH.save (mainDir);
179 }
180 }
181
182 void processEmptyFiles (const std::string& submitdir,
183 const Job& job)
184 {
185 SH::SampleHandler shReprocess = makeReprocessSamples (submitdir);
186
187 if (shReprocess.size() > 0)
188 {
189 std::string mysubmitdir = submitdir + "/emptyFiles";
190 Job myjob = job;
191 myjob.sampleHandler (shReprocess);
192
193 DirectDriver driver;
194 driver.submit (myjob, mysubmitdir);
195
196 mergeHists (submitdir, mysubmitdir, shReprocess);
197 for (auto output = job.outputBegin(),
198 end = job.outputEnd(); output != end; ++ output)
199 {
200 mergeNtuple (submitdir + "/output-" + output->label(),
201 mysubmitdir + "/output-" + output->label());
202 }
203 }
204 }
205}
#define RCU_ASSERT(x)
Definition Assert.h:222
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:58
a Driver that runs directly inside the submission job itself
Definition Job.h:51
const SH::SampleHandler & sampleHandler() const
description: the sample handler used guarantee: no-fail / strong failures: out of memory II
Definition Job.cxx:195
A class that manages a list of Sample objects.
void setMetaString(const std::string &name, const std::string &value)
set the meta-data string with the given name for all samples.
std::size_t size() const
the number of samples contained
Sample * get(const std::string &name)
get the sample with the given name
void add(Sample *sample)
add a sample to the handler
void load(const std::string &directory)
load all the samples from the given directory
MetaObject * meta()
the meta-information for this sample
std::vector< std::string > makeFileList() const
make a list of all files, prestaging them if necessary
const std::string & name() const
the name of the sample we are using
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
This module defines the arguments passed from the BATCH driver to the BATCH worker.
void processEmptyFiles(const std::string &submitdir, const Job &job)
check the output of the given (completed) job, rerun all (empty) files that did not get a fileExecute...
void exec(const std::string &cmd)
effects: execute the given command guarantee: strong failures: out of memory II failures: system fail...
Definition ShellExec.cxx:29
std::string quote(const std::string &name)
effects: quote the given name to protect it from the shell returns: the quoted name guarantee: strong...
Definition ShellExec.cxx:75
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
static const std::string treeName
the name of the tree in the sample
Definition MetaFields.h:52
TChain * tree
TFile * file