ATLAS Offline Software
Loading...
Searching...
No Matches
PhysicsAnalysis/D3PDTools/EventLoopTest/Root/UnitTest.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
14#include <EventLoop/Driver.h>
15#include <EventLoop/Job.h>
26#include <TFile.h>
27#include <TH1.h>
28#include <TSystem.h>
29#include <cstdlib>
30#include <iostream>
31#include <memory>
32#include <sstream>
33
34using namespace EL::msgEventLoop;
35
36//
37// method implementations
38//
39
40namespace EL
41{
42 UnitTest ::
43 UnitTest (const std::string& val_name, std::string val_base_path)
44 : name (val_name), base_path (val_base_path), cleanup (true), testOutput (true), outputDisk (0),
45 testFileExecute (true)
46 {
47 if (base_path.empty())
48 base_path = "$ROOTCOREBIN/data/EventLoop/";
49 }
50
51
52
53 int UnitTest ::
54 run (const Driver& driver) const
55 {
56 std::vector<std::string> files;
57 std::vector<TH1*> histos;
58
59 for (unsigned iter = 0, end = 3; iter != end; ++ iter)
60 {
61 std::ostringstream path;
62 path << base_path << "test_ntuple" << iter << ".root";
63 TString input = path.str();
64 gSystem->ExpandPathName (input);
65 files.push_back (input.Data());
66 std::unique_ptr<TFile> file (TFile::Open (input.Data(), "READ"));
67 RCU_ASSERT_SOFT (file.get() != 0);
68 TH1 *hist = dynamic_cast<TH1*>(file->Get ("hist_n"));
69 RCU_ASSERT_SOFT (hist != 0);
70 histos.push_back(hist);
71 hist->SetDirectory (0);
72 };
73 std::string tree ("physics");
74
75
76 SH::SampleHandler samples;
77
78 std::vector<std::unique_ptr<SH::Sample> > mysamples;
79 if (gridInput)
80 {
81 std::unique_ptr<SH::SampleGrid> mysample;
82 mysample.reset (new SH::SampleGrid ("user.krumnack:user.krumnack.EventLoopTest.2019-03-25.dataset0"));
83 mysample->meta()->setString (SH::MetaFields::gridName, "user.krumnack:user.krumnack.EventLoopTest.2019-03-25.dataset0");
85 mysamples.push_back (std::move (mysample));
86 mysample.reset (new SH::SampleGrid ("user.krumnack:user.krumnack.EventLoopTest.2019-03-25.dataset1"));
87 mysample->meta()->setString (SH::MetaFields::gridName, "user.krumnack:user.krumnack.EventLoopTest.2019-03-25.dataset1");
89 mysamples.push_back (std::move (mysample));
90 } else
91 {
92 std::unique_ptr<SH::SampleLocal> mysample;
93 mysample.reset (new SH::SampleLocal ("dataset0"));
94 mysample->add (files[0]);
95 mysamples.push_back (std::move (mysample));
96 mysample.reset (new SH::SampleLocal ("dataset1"));
97 mysample->add (files[1]);
98 mysample->add (files[2]);
99 mysamples.push_back (std::move (mysample));
100 }
101 {
102 TH1 *hist = 0;
103 hist = dynamic_cast<TH1*>(histos[0]->Clone ("hist"));
104 mysamples[0]->meta()->addReplace (hist);
105
106 hist = dynamic_cast<TH1*>(histos[1]->Clone ("hist"));
107 hist->Add (histos[2]);
108 mysamples[1]->meta()->addReplace (hist);
109 }
110 for (auto& mysample : mysamples)
111 samples.add (std::move (mysample));
112
114
115 samples.print ();
116
117 for (unsigned iter = 0, end = histos.size(); iter != end; ++ iter)
118 delete histos[iter];
119
120 RCU_ASSERT (samples.size() > 0);
121
122 if (scanNEvents)
123 SH::scanNEvents (samples);
124
125 TString submitDir;
126 if (location.empty())
127 {
128 submitDir = "EventLoopTest-" + name;
129 } else submitDir = location;
130 gSystem->ExpandPathName (submitDir);
131
132 try
133 {
134 UnitTestAlg alg;
135 alg.makeOutput = testOutput;
136
137 EL::Job job;
138 // job.options()->setDouble (Job::optD3PDPerfStats, 1);
139 job.options()->setDouble ("jobOpt", 42);
140 // job.options()->setDouble (Job::optPerfTree, 1);
141 // job.options()->setDouble (EL::Job::optCacheSize, 10*1024*1024);
142 // job.options()->setDouble (Job::optPrintPerFileStats, 1);
143 {
144 SH::SampleHandler sh = samples;
145 sh.setMetaString ("mymeta", "test");
146 job.sampleHandler (sh);
147 }
148 job.algsAdd (new UnitTestAlg (alg));
149 bool outputDone = outputDisk == 0;
150 for (Job::outputMIter outputStream = job.outputBegin(),
151 end = job.outputEnd(); outputStream != end; ++ outputStream)
152 {
153 if (!outputDone)
154 {
155 outputStream->output (outputDisk);
156 outputDone = true;
157 }
158 }
159
160 if (!cleanup)
161 ANA_MSG_INFO ("placing temporary files in: " << submitDir);
162 job.options()->setString (Job::optSubmitDirMode, "unique");
163 std::string output = driver.submit (job, submitDir.Data());
164
165 for (std::size_t iter = 0, end = samples.size(); iter != end; ++ iter)
166 {
167 ANA_MSG_INFO ("looking at sample " << samples[iter]->name());
168 TH1 *ref_hist = dynamic_cast<TH1*>(samples[iter]->meta()->get ("hist"));
169 if (ref_hist != 0)
170 {
172 sh.load ((output + "/hist").c_str());
173 SH::Sample *sample = sh.get (samples[iter]->name());
174 if (sample == nullptr)
175 RCU_THROW_MSG ("could not find histogram sample " + samples[iter]->name());
176
177 if (testFileExecute)
178 {
179 TH1 *file_executes = dynamic_cast<TH1*>(sample->readHist ("file_executes"));
180 RCU_ASSERT_SOFT (file_executes != 0);
181 ANA_MSG_INFO ("file executes: " << file_executes->GetEntries() << " " << samples[iter]->makeFileList().size());
182 RCU_ASSERT_SOFT (file_executes->GetEntries() == samples[iter]->makeFileList().size());
183 }
184
185 TH1 *hist = dynamic_cast<TH1*>(sample->readHist ("el_n"));
186 if (samples[iter]->getNumEntries() == 0)
187 {
188 RCU_ASSERT_SOFT (hist == 0);
189 } else
190 {
191 RCU_ASSERT_SOFT (dynamic_cast<TH1*>(sample->readHist ("el_n2")));
192 RCU_ASSERT_SOFT (dynamic_cast<TList*>(sample->readHist ("alpha")));
193 RCU_ASSERT_SOFT (sample->readHist ("beta/dir/hist"));
194 if (hist == 0)
195 RCU_THROW_MSG ("didn't find histogram el_n in sample " + sample->name());
196 if (hist->GetNbinsX() != ref_hist->GetNbinsX())
197 RCU_THROW_MSG ("bin missmatch between histograms");
198 for (int bin = 0, end = hist->GetNbinsX()+2; bin != end; ++ bin)
199 {
200 if (hist->GetBinContent (bin) != ref_hist->GetBinContent (bin))
201 {
202 std::ostringstream str;
203 str << "bin content missmatch in bin " << bin
204 << " found " << hist->GetBinContent (bin)
205 << " expected " << ref_hist->GetBinContent (bin);
206 RCU_THROW_MSG (str.str());
207 }
208 }
209 }
210 TH1 *count = dynamic_cast<TH1*>(sample->readHist ("EventLoop_EventCount"));
211 if (samples[iter]->getNumEntries() > 0 && count == 0)
212 RCU_THROW_MSG ("didn't find histogram EventLoop_EventCount");
213 }
214 if (testOutput && samples[iter]->getNumEntries() > 0)
215 {
217 sh.load ((output + "/output-out").c_str());
218 SH::Sample *const sample = sh.get (samples[iter]->name());
219 if (!sample)
220 RCU_THROW_MSG ("output dataset not found for " + samples[iter]->name());
221 sample->meta()->setString (SH::MetaFields::treeName, "tree");
222 if (ref_hist && samples[iter]->getNumEntries() != ref_hist->GetEntries())
223 {
224 std::ostringstream str;
225 str << "tree entries missmatch found " << samples[iter]->getNumEntries()
226 << " expected " << ref_hist->GetEntries();
227 RCU_THROW_MSG (str.str());
228 }
229 }
230 }
231 if (cleanup)
232 gSystem->Exec (("rm -rf " + output).c_str());
233 return EXIT_SUCCESS;
234 } catch (std::exception& e)
235 {
236 std::cout << "exception caught in unit test: " << e.what() << std::endl;
237 return EXIT_FAILURE;
238 } catch (std::string& e)
239 {
240 std::cout << "exception caught in unit test: " << e << std::endl;
241 return EXIT_FAILURE;
242 } catch (...)
243 {
244 std::cout << "unknown exception caught in unit test" << std::endl;
245 return EXIT_FAILURE;
246 }
247 }
248}
#define RCU_ASSERT(x)
Definition Assert.h:217
#define RCU_ASSERT_SOFT(x)
Definition Assert.h:162
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:53
the base class for the various EventLoop drivers that allow to run jobs on different backends
Definition Driver.h:28
Definition Job.h:42
static const std::string optSubmitDirMode
the submit-dir mode (allowed values: "no-clobber", "overwrite", "unique", "unique-link")
Definition Job.h:191
OutputStream * outputMIter
description: the list of output datasets used guarantee: no-fail / strong failures(3): out of memory ...
Definition Job.h:138
This class implements a Sample located on the grid.
Definition SampleGrid.h:36
A class that manages a list of Sample objects.
void add(const Sample &sample)
add a copy of the sample to the handler
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
void print() const
print the debugging output to the screen
A Sample based on a simple file list.
Definition SampleLocal.h:30
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
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:132
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:148
This module defines the arguments passed from the BATCH driver to the BATCH worker.
void scanNEvents(SampleHandler &sh)
effects: scan each sample in the sample handler and store the number of entries per file in the meta-...
std::string base_path
description: the base path for the data files
bool gridInput
whether to use the inputs from the grid dataset
bool testOutput
description: whether to test creating output n-tuples
bool testFileExecute
whether to test the fileExecute method
SH::DiskOutput * outputDisk
description: the output disk
bool cleanup
description: whether we clean up the submit directory afterwards
std::string name
description: the name of the unit test
std::string location
description: the location for the unit test
bool scanNEvents
whether to scan for number of events
static const std::string gridFilter
the field containing the file filter for the dataset on the grid
Definition MetaFields.h:30
static const std::string gridName
the field containing the name of the dataset on the grid
Definition MetaFields.h:26
static const std::string treeName
the name of the tree in the sample
Definition MetaFields.h:44
static const std::string gridFilter_default
the default value for gridFilter
Definition MetaFields.h:33
TChain * tree
TFile * file