ATLAS Offline Software
Loading...
Searching...
No Matches
UnitTestFixture.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
15#include <EventLoop/Driver.h>
26#include <TChain.h>
27#include <TFile.h>
28#include <TH1.h>
29#include <TTree.h>
30#include <TSystem.h>
31
32//
33// method implementations
34//
35
36namespace EL
37{
38 namespace
39 {
40 std::vector<std::string>
41 readVectorFromTree (SH::Sample *sample, const std::string& treeName,
42 const std::string& branchName)
43 {
44 std::vector<std::string> result;
45 auto fileNames = sample->makeFileList();
46 TString *var = nullptr;
47 for (auto& fileName : fileNames)
48 {
49 std::unique_ptr<TFile> file (TFile::Open (fileName.c_str(), "READ"));
50 if (file == nullptr)
51 RCU_THROW_MSG ("failed to open file: " + fileName);
52 TTree *tree = dynamic_cast<TTree*>(file->Get (treeName.c_str()));
53 Long64_t nentries = 0;
54 if (tree != nullptr && (nentries = tree->GetEntries()) > 0)
55 {
56 TBranch *branch = nullptr;
57 tree->SetBranchAddress (branchName.c_str(), &var, &branch);
58 for (Long64_t entry = 0; entry < nentries; ++ entry)
59 {
60 if (branch->GetEntry(entry) <= 0)
61 RCU_THROW_MSG ("failed to read entry from branch");
62 result.push_back (var->Data());
63 }
64 }
65 }
66 return result;
67 }
68 }
69
70
71
72
73 std::map<std::shared_ptr<Driver>,std::string> UnitTestFixture::m_jobs;
74
75 SH::SamplePtr UnitTestFixture ::
76 getSample (const std::string& sampleName)
77 {
78 if (sampleName == "empty")
79 {
80 static SH::SamplePtr result;
81 if (result.empty())
82 {
83 std::unique_ptr<SH::SampleLocal> myresult (new SH::SampleLocal ("empty"));
84 myresult->add (makeFile ({}));
85 result = myresult.release();
86 }
87 return result;
88 }
89 if (sampleName == "single")
90 {
91 static SH::SamplePtr result;
92 if (result.empty())
93 {
94 std::unique_ptr<SH::SampleLocal> myresult (new SH::SampleLocal ("single"));
95 std::vector<unsigned> entries;
96 for (unsigned iter = 0; iter != 10000; ++ iter)
97 entries.push_back (iter % 10);
98 myresult->add (makeFile (entries));
99 result = myresult.release();
100 }
101 return result;
102 }
103 if (sampleName == "multi")
104 {
105 static SH::SamplePtr result;
106 if (result.empty())
107 {
108 std::unique_ptr<SH::SampleLocal> myresult (new SH::SampleLocal ("multi"));
109 for (unsigned jter = 0; jter != 10; ++ jter)
110 {
111 std::vector<unsigned> entries;
112 for (unsigned iter = 0; iter != 10000; ++ iter)
113 entries.push_back (iter % 10);
114 myresult->add (makeFile (entries));
115 }
116 result = myresult.release();
117 }
118 return result;
119 }
120 RCU_THROW_MSG ("unknown sample: " + sampleName);
121 }
122
123
124
125 SH::SampleHandler UnitTestFixture ::
126 getSH ()
127 {
129 sh.add (getSample ("empty"));
130 sh.add (getSample ("single"));
131 sh.add (getSample ("multi"));
132 sh.setMetaString (SH::MetaFields::treeName, "physics");
133 return sh;
134 }
135
136
137
138 std::string UnitTestFixture ::
139 getJob ()
140 {
141 using namespace asg::msgUserCode;
142 ANA_MSG_INFO ("in EventLoopTest");
143
144 std::shared_ptr<Driver> driver = GetParam().m_driver;
145 auto iter = m_jobs.find (driver);
146 if (iter != m_jobs.end())
147 return iter->second;
148
149 Job job;
150 job.sampleHandler (getSH());
151 GetParam().setupJob (job);
152 {
154 config.setType ("EL::UnitTestAlg2");
155 config.setName ("newAlg");
156 config.setUseXAODs (false);
157 ANA_CHECK_THROW (config.setProperty ("property", 42));
158 job.algsAdd (config);
159 }
160 {
161 std::unique_ptr<UnitTestAlg1> alg (new UnitTestAlg1);
162 job.algsAdd (alg.release());
163 }
164
165 std::ostringstream submit;
166 submit << "submit-" << driver.get();
167 driver->submit (job, submit.str());
168 driver->wait (submit.str());
169 m_jobs[driver] = submit.str();
170 return submit.str();
171 }
172
173
174
175 TObject *UnitTestFixture ::
176 getTObject (const std::string& sampleName,
177 const std::string& objectName,
178 bool isMandatory)
179 {
181 sh.load (getJob() + "/hist");
182 SH::Sample *sample = sh.get (sampleName);
183 if (sample == nullptr)
184 RCU_THROW_MSG ("couldn't find sample: " + sampleName);
185 TObject *object = sample->readHist (objectName);
186 if (isMandatory && object == nullptr)
187 RCU_THROW_MSG ("couldn't find object: " + objectName);
188 return object;
189 }
190
191
192
193 unsigned UnitTestFixture ::
194 eventCount (const std::string& sampleName)
195 {
196 TH1 *hist = getHist<TH1> (sampleName, "EventLoop_EventCount", true);
197 return hist->GetBinContent (1);
198 }
199
200
201
202 TH1 *UnitTestFixture ::
203 getCallbacks (const std::string& sampleName)
204 {
205 return getHist<TH1> (sampleName, "callbacks", true);
206 }
207
208
209
210 void UnitTestFixture ::
211 checkFileExecuted (const std::string& sampleName)
212 {
213 std::set<std::string> filesOut;
214 {
216 sh.load (getJob() + "/hist");
217 SH::Sample *sample = sh.get (sampleName);
218 if (sample == nullptr)
219 RCU_THROW_MSG ("couldn't find sample: " + sampleName);
220 auto vec = readVectorFromTree (sample, "EventLoop_FileExecuted", "file");
221 filesOut.insert (vec.begin(), vec.end());
222 }
223 std::set<std::string> filesIn;
224 for (auto& file : getSample (sampleName)->makeFileList())
225 {
226 auto split = file.rfind ('/');
227 if (split == std::string::npos)
228 split = 0;
229 else
230 ++ split;
231 std::string fileName = file.substr (split);
232 ASSERT_TRUE (filesIn.find (fileName) == filesIn.end());
233 filesIn.insert (fileName);
234 }
235 ASSERT_EQ (filesIn, filesOut);
236 }
237
238
239
240 std::string UnitTestFixture ::
241 makeFile (const std::vector<unsigned>& entries)
242 {
243 static unsigned index = 0;
244 std::ostringstream fileName;
245 fileName << "file-" << ++ index << ".root";
246
247 std::unique_ptr<SH::DiskWriter> file
248 = GetParam().make_file_writer (fileName.str());
249 {
250 if (!entries.empty())
251 {
252 TTree *tree = new TTree ("physics", "physics");
253 Int_t el_n = 0;
254 tree->Branch ("el_n", &el_n, "el_n/I");
255 for (auto entry : entries)
256 {
257 el_n = entry;
258 tree->Fill ();
259 }
260 }
261 file->file()->Write ();
262 file->close ();
263 }
264 return file->path();
265 }
266
267
268
269 TEST_P (UnitTestFixture, empty_eventCount)
270 {
271 EXPECT_EQ (eventCount ("empty"), 0u);
272 }
273
274
275
276 TEST_P (UnitTestFixture, empty_callbacks)
277 {
278 TH1 *callbacks = getCallbacks ("empty");
279 EXPECT_EQ (0, callbacks->GetBinContent (1 + UnitTestAlg1::CB_CHANGE_INPUT_FIRST));
280 EXPECT_EQ (0, callbacks->GetBinContent (1 + UnitTestAlg1::CB_CHANGE_INPUT_OTHER));
281 EXPECT_EQ (0, callbacks->GetBinContent (1 + UnitTestAlg1::CB_INITIALIZE));
282 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_HIST_INITIALIZE));
283 EXPECT_EQ (0, callbacks->GetBinContent (1 + UnitTestAlg1::CB_EXECUTE));
284 EXPECT_EQ (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_FILE_EXECUTE));
285 EXPECT_EQ (0, callbacks->GetBinContent (1 + UnitTestAlg1::CB_FINALIZE));
286 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_HIST_FINALIZE));
287 }
288
289
290
291 TEST_P (UnitTestFixture, empty_fileExecuted)
292 {
293 checkFileExecuted ("empty");
294 }
295
296
297
298 TEST_P (UnitTestFixture, single_eventCount)
299 {
300 EXPECT_EQ (eventCount ("single"), 10000u);
301 }
302
303
304
305 TEST_P (UnitTestFixture, single_callbacks)
306 {
307 TH1 *callbacks = getCallbacks ("single");
308 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_CHANGE_INPUT_FIRST));
309 EXPECT_LE (0, callbacks->GetBinContent (1 + UnitTestAlg1::CB_CHANGE_INPUT_OTHER));
310 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_INITIALIZE));
311 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_HIST_INITIALIZE));
312 EXPECT_EQ (10000, callbacks->GetBinContent (1 + UnitTestAlg1::CB_EXECUTE));
313 EXPECT_EQ (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_FILE_EXECUTE));
314 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_FINALIZE));
315 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_HIST_FINALIZE));
316 }
317
318
319
320 TEST_P (UnitTestFixture, single_fileExecuted)
321 {
322 checkFileExecuted ("single");
323 }
324
325
326
327 TEST_P (UnitTestFixture, multi_eventCount)
328 {
329 EXPECT_EQ (eventCount ("multi"), 100000u);
330 }
331
332
333
334 TEST_P (UnitTestFixture, multi_callbacks)
335 {
336 TH1 *callbacks = getCallbacks ("multi");
337 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_CHANGE_INPUT_FIRST));
338 EXPECT_LE (0, callbacks->GetBinContent (1 + UnitTestAlg1::CB_CHANGE_INPUT_OTHER));
339 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_INITIALIZE));
340 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_HIST_INITIALIZE));
341 EXPECT_EQ (100000, callbacks->GetBinContent (1 + UnitTestAlg1::CB_EXECUTE));
342 EXPECT_EQ (10, callbacks->GetBinContent (1 + UnitTestAlg1::CB_FILE_EXECUTE));
343 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_FINALIZE));
344 EXPECT_LE (1, callbacks->GetBinContent (1 + UnitTestAlg1::CB_HIST_FINALIZE));
345 }
346
347
348
349 TEST_P (UnitTestFixture, multi_fileExecuted)
350 {
351 checkFileExecuted ("multi");
352 }
353
354
355
356 TEST_P (UnitTestFixture, multi_out_empty)
357 {
359 sh.load (getJob() + "/output-out_empty");
360 SH::Sample *sample = sh.get ("multi");
361 ASSERT_TRUE (sample != nullptr);
362
363 for (auto fileName : sample->makeFileList())
364 {
365 std::unique_ptr<TFile> file (TFile::Open (fileName.c_str(), "READ"));
366 }
367 }
368}
std::vector< size_t > vec
macros for messaging and checking status codes
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
#define ANA_CHECK_THROW(EXP)
check whether the given expression was successful, throwing an exception on failure
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:58
an object that can create a AnaAlgorithm
Definition Job.h:51
static std::map< std::shared_ptr< Driver >, std::string > m_jobs
SH::SamplePtr getSample(const std::string &sameName)
SH::SampleHandler getSH()
T * getHist(const std::string &sampleName, const std::string &objectName, bool isMandatory)
std::string makeFile(const std::vector< unsigned > &entries)
A class that manages a list of Sample objects.
A Sample based on a simple file list.
Definition SampleLocal.h:38
A smart pointer class that holds a single Sample object.
Definition SamplePtr.h:35
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 > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
double entries
Definition listroot.cxx:49
This module defines the arguments passed from the BATCH driver to the BATCH worker.
TEST_P(UnitTestFixture, empty_eventCount)
Definition index.py:1
static const std::string treeName
the name of the tree in the sample
Definition MetaFields.h:52
TChain * tree
TFile * file