Skip to content

Commit 98e9331

Browse files
committed
Add a test producing histograms
1 parent 5db8a8f commit 98e9331

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

test/k4FWCoreTest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +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)
132133

133134
add_test(NAME FunctionalCheckFiles COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/options/CheckOutputFiles.py)
134135
set_tests_properties(FunctionalCheckFiles PROPERTIES DEPENDS "FunctionalFile;FunctionalMTFile;FunctionalMultipleFile;FunctionalOutputCommands;FunctionalProducerAbsolutePath;FunctionalTransformerRuntimeEmpty;FunctionalMix;FunctionalMixIOSvc")
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#
2+
# Copyright (c) 2014-2024 Key4hep-Project.
3+
#
4+
# This file is part of Key4hep.
5+
# See https://key4hep.github.io/key4hep-doc/ for further info.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
# This is an example using two producers that create histograms and persist them to a ROOT file
21+
22+
from Gaudi.Configuration import INFO
23+
from Configurables import ExampleFunctionalProducerHist
24+
from k4FWCore import ApplicationMgr
25+
from Configurables import RootHistSvc
26+
from Configurables import Gaudi__Histograming__Sink__Root as RootHistoSink
27+
from Configurables import HiveWhiteBoard, HiveSlimEventLoopMgr, AvalancheSchedulerSvc
28+
29+
# Multithreaded also works fine with histograms
30+
multithreaded = True
31+
threads = 2
32+
slots = 3
33+
if multithreaded:
34+
whiteboard = HiveWhiteBoard(
35+
"EventDataSvc",
36+
EventSlots=slots,
37+
ForceLeaves=True,
38+
)
39+
slimeventloopmgr = HiveSlimEventLoopMgr(
40+
"HiveSlimEventLoopMgr", SchedulerName="AvalancheSchedulerSvc", OutputLevel=INFO
41+
)
42+
43+
scheduler = AvalancheSchedulerSvc(ThreadPoolSize=threads, OutputLevel=INFO)
44+
scheduler.ShowDataDependencies = True
45+
scheduler.ShowDataFlow = True
46+
scheduler.ShowControlFlow = True
47+
48+
49+
producer1 = ExampleFunctionalProducerHist(
50+
"ExampleFunctionalProducer1", OutputCollection=["dummy1"]
51+
)
52+
producer2 = ExampleFunctionalProducerHist(
53+
"ExampleFunctionalProducer2", OutputCollection=["dummy2"]
54+
)
55+
56+
hist = RootHistSvc("HistogramPersistencySvc")
57+
root_hist_svc = RootHistoSink("RootHistoSink")
58+
root_hist_svc.FileName = "functional_producer_hist.root"
59+
60+
61+
if not multithreaded:
62+
app = ApplicationMgr(
63+
TopAlg=[producer1, producer2],
64+
EvtSel="NONE",
65+
EvtMax=10,
66+
ExtSvc=[root_hist_svc] + ([whiteboard] if multithreaded else []),
67+
OutputLevel=INFO,
68+
HistogramPersistency="ROOT",
69+
)
70+
else:
71+
app = ApplicationMgr(
72+
TopAlg=[producer1, producer2],
73+
EvtSel="NONE",
74+
EvtMax=10,
75+
ExtSvc=[root_hist_svc] + ([whiteboard] if multithreaded else []),
76+
OutputLevel=INFO,
77+
HistogramPersistency="ROOT",
78+
EventLoop=slimeventloopmgr,
79+
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2014-2024 Key4hep-Project.
3+
*
4+
* This file is part of Key4hep.
5+
* See https://key4hep.github.io/key4hep-doc/ for further info.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#include "edm4hep/MCParticleCollection.h"
21+
22+
#include "k4FWCore/Producer.h"
23+
24+
#include "Gaudi/Accumulators/RootHistogram.h"
25+
#include "GaudiKernel/RndmGenerators.h"
26+
27+
#include <string>
28+
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"})) {}
34+
35+
// 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
41+
return {};
42+
}
43+
44+
private:
45+
// 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.}};
47+
};
48+
49+
DECLARE_COMPONENT(ExampleFunctionalProducerHist)

0 commit comments

Comments
 (0)