Skip to content

Commit 150773e

Browse files
committed
Change the test with histograms to be a transformer instead of a producer
1 parent b54c06f commit 150773e

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

test/k4FWCoreTest/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ add_test_with_env(FunctionalProducerRuntimeCollections options/ExampleFunctional
129129
add_test_with_env(FunctionalTransformerRuntimeCollections options/ExampleFunctionalTransformerRuntimeCollections.py)
130130
add_test_with_env(FunctionalTransformerRuntimeEmpty options/ExampleFunctionalTransformerRuntimeEmpty.py)
131131
add_test_with_env(FunctionalTransformerRuntimeCollectionsMultiple options/ExampleFunctionalTransformerRuntimeCollectionsMultiple.py)
132-
add_test_with_env(FunctionalProducerHist options/ExampleFunctionalProducerHist.py)
132+
add_test_with_env(FunctionalTransformerHist options/ExampleFunctionalTransformerHist.py)
133133

134134
add_test(NAME FunctionalCheckFiles COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/options/CheckOutputFiles.py)
135135
set_tests_properties(FunctionalCheckFiles PROPERTIES DEPENDS "FunctionalFile;FunctionalMTFile;FunctionalMultipleFile;FunctionalOutputCommands;FunctionalProducerAbsolutePath;FunctionalTransformerRuntimeEmpty;FunctionalMix;FunctionalMixIOSvc;FunctionalProducerHist")

test/k4FWCoreTest/options/CheckOutputFiles.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ def check_collections(filename, names):
120120
mix_collections,
121121
)
122122

123-
f = ROOT.TFile.Open("functional_producer_hist.root")
123+
f = ROOT.TFile.Open("functional_transformer_hist.root")
124124
for i in range(2):
125125
if (
126126
str(f.GetListOfKeys()[i])
127-
!= f"Name: ExampleFunctionalProducer{i+1} Title: ExampleFunctionalProducer{i+1}"
127+
!= f"Name: ExampleFunctionalTransformerHist{i+1} Title: ExampleFunctionalTransformerHist{i+1}"
128128
):
129129
raise RuntimeError(
130-
"Directory structure does not match expected for functional_producer_hist.root"
130+
"Directory structure does not match expected for functional_transformer_hist.root"
131131
)

test/k4FWCoreTest/options/ExampleFunctionalProducerHist.py renamed to test/k4FWCoreTest/options/ExampleFunctionalTransformerHist.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
# This is an example using two producers that create histograms and persist them to a ROOT file
2121

2222
from Gaudi.Configuration import INFO
23-
from Configurables import ExampleFunctionalProducerHist
23+
from Configurables import ExampleFunctionalProducer, ExampleFunctionalTransformerHist
2424
from k4FWCore import ApplicationMgr
2525
from Configurables import RootHistSvc
2626
from Configurables import Gaudi__Histograming__Sink__Root as RootHistoSink
2727
from Configurables import HiveWhiteBoard, HiveSlimEventLoopMgr, AvalancheSchedulerSvc
2828

2929
# Multithreaded also works fine with histograms
30-
multithreaded = True
30+
multithreaded = False
3131
threads = 2
3232
slots = 3
3333
if multithreaded:
@@ -46,20 +46,33 @@
4646
scheduler.ShowControlFlow = True
4747

4848

49-
producer1 = ExampleFunctionalProducerHist(
50-
"ExampleFunctionalProducer1", OutputCollection=["dummy1"]
49+
producer1 = ExampleFunctionalProducer(
50+
"ExampleFunctionalProducer1", OutputCollection=["MCParticles1"]
5151
)
52-
producer2 = ExampleFunctionalProducerHist(
53-
"ExampleFunctionalProducer2", OutputCollection=["dummy2"]
52+
producer2 = ExampleFunctionalProducer(
53+
"ExampleFunctionalProducer2", OutputCollection=["MCParticles2"]
54+
)
55+
56+
transformer1 = ExampleFunctionalTransformerHist(
57+
"ExampleFunctionalTransformerHist1",
58+
InputCollection=["MCParticles1"],
59+
OutputCollection=["dummy1"],
60+
)
61+
62+
transformer2 = ExampleFunctionalTransformerHist(
63+
"ExampleFunctionalTransformerHist2",
64+
InputCollection=["MCParticles2"],
65+
OutputCollection=["dummy2"],
66+
FirstParticle=False,
5467
)
5568

5669
hist = RootHistSvc("HistogramPersistencySvc")
5770
root_hist_svc = RootHistoSink("RootHistoSink")
58-
root_hist_svc.FileName = "functional_producer_hist.root"
71+
root_hist_svc.FileName = "functional_transformer_hist.root"
5972

6073

6174
app = ApplicationMgr(
62-
TopAlg=[producer1, producer2],
75+
TopAlg=[producer1, producer2, transformer1, transformer2],
6376
EvtSel="NONE",
6477
EvtMax=10,
6578
ExtSvc=[root_hist_svc] + ([whiteboard] if multithreaded else []),

test/k4FWCoreTest/src/components/ExampleFunctionalProducerHist.cpp renamed to test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,33 @@
1919

2020
#include "edm4hep/MCParticleCollection.h"
2121

22-
#include "k4FWCore/Producer.h"
22+
#include "k4FWCore/Transformer.h"
2323

2424
#include "Gaudi/Accumulators/RootHistogram.h"
25-
#include "GaudiKernel/RndmGenerators.h"
2625

2726
#include <string>
2827

29-
struct ExampleFunctionalProducerHist final : k4FWCore::Producer<edm4hep::MCParticleCollection()> {
30-
// The pair in KeyValues can be changed from python and it corresponds
31-
// to the name of the output collection
32-
ExampleFunctionalProducerHist(const std::string& name, ISvcLocator* svcLoc)
33-
: Producer(name, svcLoc, {}, KeyValues("OutputCollection", {"MCParticles"})) {}
28+
struct ExampleFunctionalTransformerHist final
29+
: k4FWCore::Transformer<edm4hep::MCParticleCollection(const edm4hep::MCParticleCollection& input)> {
30+
// The pairs in KeyValues can be changed from python and they correspond
31+
// to the name of the input and output collections respectively
32+
ExampleFunctionalTransformerHist(const std::string& name, ISvcLocator* svcLoc)
33+
: Transformer(name, svcLoc, KeyValues("InputCollection", {"MCParticles"}),
34+
KeyValues("OutputCollection", {"MCParticles"})) {}
3435

3536
// This is the function that will be called to produce the data
36-
edm4hep::MCParticleCollection operator()() const override {
37-
// Not thread-safe!
38-
Rndm::Numbers rndu(randSvc(), Rndm::Flat(0, 1));
39-
++m_histograms[rndu()];
40-
// Return an empty collection since we don't care about the collection for this example
37+
edm4hep::MCParticleCollection operator()(const edm4hep::MCParticleCollection& input) const override {
38+
// Fill the histogram with the energy of one particle
39+
++m_histograms[input[0 + !m_firstParticle.value()].getEnergy()];
40+
// Return an empty collection since we don't care about the collection
4141
return {};
4242
}
4343

4444
private:
4545
// This is the histogram that will be filled, 1 is the number of dimensions of the histogram (1D)
46-
mutable Gaudi::Accumulators::RootHistogram<1> m_histograms{this, "Histogram Name", "Histogram Title", {100, 0, 1.}};
46+
mutable Gaudi::Accumulators::RootHistogram<1> m_histograms{this, "Histogram Name", "Histogram Title", {100, 0, 10.}};
47+
48+
Gaudi::Property<bool> m_firstParticle{this, "FirstParticle", true};
4749
};
4850

49-
DECLARE_COMPONENT(ExampleFunctionalProducerHist)
51+
DECLARE_COMPONENT(ExampleFunctionalTransformerHist)

0 commit comments

Comments
 (0)