ATLAS Offline Software
UnitTestAlg2.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Copyright Nils Krumnack 2011.
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 // Please feel free to contact me (krumnack@iastate.edu) for bug
11 // reports, feature suggestions, praise and complaints.
12 
13 
14 //
15 // includes
16 //
17 
19 
20 #include <EventLoop/Job.h>
21 #include <EventLoop/OutputStream.h>
22 #include <EventLoop/Worker.h>
23 #include <RootCoreUtils/Assert.h>
24 #include <RootCoreUtils/ThrowMsg.h>
25 #include <TEfficiency.h>
26 #include <TFile.h>
27 #include <TH1.h>
28 #include <TTree.h>
29 #include <TObjString.h>
30 
31 #include <AsgMessaging/MsgStream.h>
33 
34 //
35 // method implementations
36 //
37 
39 
40 namespace EL
41 {
42  void UnitTestAlg2 ::
43  testInvariant () const
44  {
45  RCU_INVARIANT (this != 0);
46  }
47 
48 
49 
51  UnitTestAlg2 (const std::string& name,
52  ISvcLocator* pSvcLocator)
53  : AnaAlgorithm (name, pSvcLocator),
54  makeOutput (true),
55  m_name ("el_n"),
56  m_value (0),// m_hist (0),
57  m_tree (0),
58  m_hasInitialize (false)
59  {
60  declareProperty ("property", m_property, "test property");
61  declareProperty ("string_property", m_string_property, "test string property");
62  declareProperty ("toolHandle", m_toolHandle, "ToolHandle property");
63 
64  RCU_NEW_INVARIANT (this);
65  }
66 
67 
68 
70  initialize ()
71  {
72  RCU_CHANGE_INVARIANT (this);
73 
75 
77 
78  // test that we can create and then retrieve a TEfficiency
79  // histogram
80  ANA_CHECK (book (TEfficiency ("efficiency", "dummy efficiency hist", 50, 0, 50)));
81  (void) hist<TEfficiency> ("efficiency");
82  (void) histeff ("efficiency");
83 
84  ANA_CHECK (book (TH1F ((m_name + "2_2").c_str(), m_name.c_str(), 50, 0, 50)));
85  ANA_CHECK (book (TH1F ("file_executes_2", "file executes", 1, 0, 1)));
86 
87  ANA_CHECK (book (TH1F ("test_property", "test_property", 1, 0, 1)));
88  hist("test_property")->Fill (0.5, m_property);
89 
90  // if (wk()->metaData()->castDouble ("jobOpt") != 42)
91  // RCU_THROW_MSG ("failed to read meta-data from job options");
92 
93  // if (wk()->metaData()->castString ("mymeta") != "test")
94  // RCU_THROW_MSG ("failed to read meta-data from worker");
95 
96  wk()->addOutput (/*m_hist = */new TH1F (m_name.c_str(), m_name.c_str(),
97  50, 0, 50));
98  if (makeOutput)
99  {
100  TFile *file = wk()->getOutputFile ("out");
101  m_tree = new TTree ("tree2", "test output");
102  m_tree->SetDirectory (file);
103  m_tree->Branch (m_name.c_str(), &m_value, (m_name + "/I").c_str());
104  }
105  m_hasInitialize = true;
106  return ::StatusCode::SUCCESS;
107  }
108 
109 
110 
112  execute ()
113  {
114  RCU_CHANGE_INVARIANT (this);
115 
117 
119 
120  RCU_ASSERT_SOFT (wk()->tree()->GetEntries() > wk()->treeEntry());
121  // RCU_ASSERT_SOFT (m_fileName == wk()->inputFile()->GetName());
122 
123  RCU_ASSERT (wk()->tree() != 0);
124  m_branch = wk()->tree()->GetBranch (m_name.c_str());
125  if (m_branch == 0)
126  RCU_THROW_MSG ("failed to find branch " + m_name);
127  m_branch->SetAddress (&m_value);
128 
129  RCU_ASSERT (m_branch != nullptr);
130  RCU_ASSERT (wk() != nullptr);
131  m_branch->GetEntry (wk()->treeEntry());
132  hist(m_name)->Fill (m_value);
133  if (makeOutput)
134  m_tree->Fill ();
135 
136  msg().setLevel (MSG::INFO);
137  if (++ m_calls < 3)
138  ATH_MSG_INFO ("message test");
139 
140  return ::StatusCode::SUCCESS;
141  }
142 
143 
144 
146  finalize ()
147  {
148  RCU_CHANGE_INVARIANT (this);
149 
151 
153  wk()->addOutput (new TH1F ("beta/dir/hist2", "directory test", 10, 0, 10));
154  wk()->addOutputList ("alpha2", new TObjString ("alpha2"));
155  return ::StatusCode::SUCCESS;
156  }
157 
158 
159 
161  getCallbacks ()
162  {
163  if (m_callbacks == nullptr)
164  {
165  m_callbacks = new TH1F ("callbacks2", "callbacks", CB_FINALIZE + 1,
166  0, CB_FINALIZE + 1);
167  wk()->addOutput (m_callbacks);
168  }
169  return m_callbacks;
170  }
171 }
EL::UnitTestAlg2::execute
virtual ::StatusCode execute() override
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AthHistogramming::book
StatusCode book(const TH1 &hist, const std::string &tDir="", const std::string &stream="")
Simplify the booking and registering (into THistSvc) of histograms.
Definition: AthHistogramming.h:303
Job.h
OutputStream.h
EL::UnitTestAlg2::getCallbacks
TH1 * getCallbacks()
EL::UnitTestAlg2::CB_INITIALIZE
@ CB_INITIALIZE
Definition: UnitTestAlg2.h:65
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
Assert.h
EL::UnitTestAlg2::m_tree
TTree * m_tree
description: the histogram we are creating
Definition: UnitTestAlg2.h:121
EL::UnitTestAlg2::m_branch
TBranch * m_branch
description: the branch we are using
Definition: UnitTestAlg2.h:109
ClassImp
ClassImp(EL::UnitTestAlg2) namespace EL
Definition: UnitTestAlg2.cxx:38
EL::UnitTestAlg2::m_name
std::string m_name
description: the name of the variable we are using
Definition: UnitTestAlg2.h:105
EL::UnitTestAlg2::m_hasInitialize
bool m_hasInitialize
whether initialize has been called
Definition: UnitTestAlg2.h:129
RCU_ASSERT_SOFT
#define RCU_ASSERT_SOFT(x)
Definition: Assert.h:167
EL::UnitTestAlg2::testInvariant
void testInvariant() const
effects: test the invariant of this object guarantee: no-fail
EL::UnitTestAlg2::m_callbacks
TH1 * m_callbacks
Definition: UnitTestAlg2.h:135
EL::UnitTestAlg2::m_value
Int_t m_value
description: the value we are reading from the branch
Definition: UnitTestAlg2.h:113
EL::UnitTestAlg2::UnitTestAlg2
UnitTestAlg2(const std::string &name, ISvcLocator *pSvcLocator)
effects: standard constructor.
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
file
TFile * file
Definition: tile_monitor.h:29
EL
This module defines the arguments passed from the BATCH driver to the BATCH worker.
Definition: AlgorithmWorkerData.h:24
TH1::Fill
int Fill(double)
Definition: rootspy.cxx:285
RCU_INVARIANT
#define RCU_INVARIANT(x)
Definition: Assert.h:201
EL::UnitTestAlg2::makeOutput
bool makeOutput
description: whether I create an output n-tuple
Definition: UnitTestAlg2.h:58
EL::UnitTestAlg2::m_calls
unsigned m_calls
Definition: UnitTestAlg2.h:132
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ThrowMsg.h
EL::UnitTestAlg2::initialize
virtual ::StatusCode initialize() override
TH1F
Definition: rootspy.cxx:320
TH1
Definition: rootspy.cxx:268
Worker.h
MsgStreamMacros.h
generate::GetEntries
double GetEntries(TH1D *h, int ilow, int ihi)
Definition: rmsFrac.cxx:20
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
EL::UnitTestAlg2::m_property
float m_property
the float property our owner sets
Definition: UnitTestAlg2.h:93
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
AthHistogramming::hist
TH1 * hist(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered histograms of any type.
Definition: AthHistogramming.cxx:198
UnitTestAlg2.h
RCU_THROW_MSG
#define RCU_THROW_MSG(message)
Definition: PrintMsg.h:58
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
EL::UnitTestAlg2
Definition: UnitTestAlg2.h:36
EL::UnitTestAlg2::CB_FINALIZE
@ CB_FINALIZE
Definition: UnitTestAlg2.h:67
RCU_ASSERT
#define RCU_ASSERT(x)
Definition: Assert.h:222
EL::UnitTestAlg2::finalize
virtual ::StatusCode finalize() override
AthHistogramming::tree
TTree * tree(const std::string &treeName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered TTrees.
Definition: AthHistogramming.cxx:378
MsgStream.h
EL::UnitTestAlg2::CB_EXECUTE
@ CB_EXECUTE
Definition: UnitTestAlg2.h:66
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233