Skip to content

Commit e7afd47

Browse files
committed
DPL Analysis: Add TDatabasePDG as an O2 service using ServiceSpec
1 parent c9426c3 commit e7afd47

8 files changed

Lines changed: 97 additions & 4 deletions

File tree

Analysis/Tutorials/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,9 @@ o2_add_dpl_workflow(conditional-expressions
234234
JOB_POOL analysis
235235
PUBLIC_LINK_LIBRARIES O2::Framework O2::AnalysisCore O2::AnalysisDataModel
236236
COMPONENT_NAME AnalysisTutorial)
237+
238+
o2_add_dpl_workflow(using-pdg
239+
SOURCES src/usingPDGService.cxx
240+
JOB_POOL analysis
241+
PUBLIC_LINK_LIBRARIES O2::Framework O2::AnalysisCore O2::AnalysisDataModel
242+
COMPONENT_NAME AnalysisTutorial)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
#include "Framework/AnalysisTask.h"
13+
#include "AnalysisCore/MC.h"
14+
#include "TDatabasePDG.h"
15+
#include "Framework/runDataProcessing.h"
16+
17+
using namespace o2;
18+
using namespace o2::framework;
19+
20+
struct UsePdgDatabase {
21+
Service<TDatabasePDG> pdg;
22+
OutputObj<TH1F> particleCharges{TH1F("charges", ";charge;entries", 201, -10.1, 10.1)};
23+
24+
void process(aod::McCollision const&, aod::McParticles const& particles)
25+
{
26+
for (auto& particle : particles) {
27+
auto pdgInfo = pdg->GetParticle(particle.pdgCode());
28+
if (pdgInfo != nullptr) {
29+
particleCharges->Fill(pdgInfo->Charge());
30+
} else {
31+
LOGF(warn, "[%d] unknown particle with PDG code %d", particle.globalIndex(), particle.pdgCode());
32+
}
33+
}
34+
}
35+
};
36+
37+
WorkflowSpec defineDataProcessing(ConfigContext const& cfgc)
38+
{
39+
return WorkflowSpec{
40+
adaptAnalysisTask<UsePdgDatabase>(cfgc)};
41+
}

Framework/Core/include/Framework/AnalysisManagers.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "Framework/ConfigContext.h"
2525
#include "Framework/RootConfigParamHelpers.h"
2626
#include "Framework/ExpressionHelpers.h"
27+
#include "Framework/CommonServices.h"
2728

2829
namespace o2::framework
2930
{
@@ -344,6 +345,12 @@ class has_instance
344345

345346
template <typename T>
346347
struct ServiceManager {
348+
template <typename ANY>
349+
static bool add(std::vector<ServiceSpec>&, ANY&)
350+
{
351+
return false;
352+
}
353+
347354
template <typename ANY>
348355
static bool prepare(InitContext&, ANY&)
349356
{
@@ -353,13 +360,19 @@ struct ServiceManager {
353360

354361
template <typename T>
355362
struct ServiceManager<Service<T>> {
363+
static bool add(std::vector<ServiceSpec>& specs, Service<T>&)
364+
{
365+
specs.push_back(CommonAnalysisServices::addAnalysisService<T>());
366+
return true;
367+
}
368+
356369
static bool prepare(InitContext& context, Service<T>& service)
357370
{
358371
if constexpr (has_instance<T>::value) {
359372
service.service = &(T::instance()); // Sigh...
360373
return true;
361374
} else {
362-
service.service = context.services().get<T>();
375+
service.service = &(context.services().get<T>());
363376
return true;
364377
}
365378
return false;

Framework/Core/include/Framework/AnalysisTask.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,9 @@ DataProcessorSpec adaptAnalysisTask(ConfigContext const& ctx, Args&&... args)
779779

780780
homogeneous_apply_refs([&outputs, &hash](auto& x) { return OutputManager<std::decay_t<decltype(x)>>::appendOutput(outputs, x, hash); }, *task.get());
781781

782+
std::vector<ServiceSpec> requiredServices = CommonServices::defaultServices();
783+
homogeneous_apply_refs([&requiredServices](auto& x) { return ServiceManager<std::decay_t<decltype(x)>>::add(requiredServices, x); }, *task.get());
784+
782785
auto algo = AlgorithmSpec::InitCallback{[task = task, expressionInfos](InitContext& ic) mutable {
783786
homogeneous_apply_refs([&ic](auto&& x) { return OptionManager<std::decay_t<decltype(x)>>::prepare(ic, x); }, *task.get());
784787
homogeneous_apply_refs([&ic](auto&& x) { return ServiceManager<std::decay_t<decltype(x)>>::prepare(ic, x); }, *task.get());
@@ -840,7 +843,8 @@ DataProcessorSpec adaptAnalysisTask(ConfigContext const& ctx, Args&&... args)
840843
inputs,
841844
outputs,
842845
algo,
843-
options};
846+
options,
847+
requiredServices};
844848
return spec;
845849
}
846850

Framework/Core/include/Framework/CommonServices.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ struct CommonServices {
6969
static std::vector<ServiceSpec> requiredServices();
7070
};
7171

72+
struct CommonAnalysisServices {
73+
static ServiceSpec databasePDGSpec();
74+
75+
template <typename T>
76+
static ServiceSpec addAnalysisService();
77+
};
78+
7279
} // namespace o2::framework
7380

7481
#endif // O2_FRAMEWORK_COMMONSERVICES_H_

Framework/Core/include/Framework/runDataProcessing.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ void defaultConfiguration(std::vector<o2::framework::DispatchPolicy>& dispatchPo
9090
void defaultConfiguration(std::vector<o2::framework::ResourcePolicy>& resourcePolicies) {}
9191
void defaultConfiguration(std::vector<o2::framework::ServiceSpec>& services)
9292
{
93-
services = o2::framework::CommonServices::defaultServices();
93+
if (services.empty()) {
94+
services = o2::framework::CommonServices::defaultServices();
95+
}
9496
}
9597

9698
/// Workflow options which are required by DPL in order to work.

Framework/Core/src/CommonServices.cxx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "../src/DataProcessingStatus.h"
3434
#include "ArrowSupport.h"
3535
#include "DPLMonitoringBackend.h"
36+
#include "TDatabasePDG.h"
3637

3738
#include <Configuration/ConfigurationInterface.h>
3839
#include <Configuration/ConfigurationFactory.h>
@@ -528,5 +529,25 @@ std::vector<ServiceSpec> CommonServices::defaultServices(int numThreads)
528529
return specs;
529530
}
530531

532+
o2::framework::ServiceSpec CommonAnalysisServices::databasePDGSpec()
533+
{
534+
return ServiceSpec{
535+
.name = "database-pdg",
536+
.init = [](ServiceRegistry&, DeviceState&, fair::mq::ProgOptions&) -> ServiceHandle {
537+
auto* ptr = new TDatabasePDG();
538+
ptr->ReadPDGTable();
539+
return ServiceHandle{TypeIdHelpers::uniqueId<TDatabasePDG>(), ptr, ServiceKind::Serial, "database-pdg"};
540+
},
541+
.configure = CommonServices::noConfiguration(),
542+
.exit = [](ServiceRegistry&, void* service) { reinterpret_cast<TDatabasePDG*>(service)->Delete(); },
543+
.kind = ServiceKind::Serial};
544+
}
545+
546+
template <>
547+
o2::framework::ServiceSpec CommonAnalysisServices::addAnalysisService<TDatabasePDG>()
548+
{
549+
return databasePDGSpec();
550+
}
551+
531552
} // namespace o2::framework
532553
#pragma GCC diagnostic pop

Framework/Core/src/DeviceSpecHelpers.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,6 @@ void DeviceSpecHelpers::dataProcessorSpecs2DeviceSpecs(const WorkflowSpec& workf
764764
unsigned short resourcesMonitoringInterval,
765765
std::string const& channelPrefix)
766766
{
767-
768767
std::vector<LogicalForwardInfo> availableForwardsInfo;
769768
std::vector<DeviceConnectionEdge> logicalEdges;
770769
std::vector<DeviceConnectionId> connections;

0 commit comments

Comments
 (0)