Skip to content

Commit 9e6c9a5

Browse files
authored
DPL Analysis: Add TDatabasePDG as an O2 service using ServiceSpec (#6782)
* DPL Analysis: Add TDatabasePDG as an O2 service using ServiceSpec * fix dispatch
1 parent 9bc07e5 commit 9e6c9a5

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+
CommonAnalysisServices::addAnalysisService<T>(specs);
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "Framework/ServiceSpec.h"
1515
#include "Framework/TypeIdHelpers.h"
1616

17+
class TDatabasePDG;
18+
1719
namespace o2::framework
1820
{
1921

@@ -69,6 +71,18 @@ struct CommonServices {
6971
static std::vector<ServiceSpec> requiredServices();
7072
};
7173

74+
struct CommonAnalysisServices {
75+
static ServiceSpec databasePDGSpec();
76+
77+
template <typename T>
78+
static void addAnalysisService(std::vector<ServiceSpec>& specs)
79+
{
80+
if constexpr (std::is_same_v<T, TDatabasePDG>) {
81+
specs.push_back(databasePDGSpec());
82+
}
83+
}
84+
};
85+
7286
} // namespace o2::framework
7387

7488
#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: 14 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>
@@ -530,5 +531,18 @@ std::vector<ServiceSpec> CommonServices::defaultServices(int numThreads)
530531
return specs;
531532
}
532533

534+
o2::framework::ServiceSpec CommonAnalysisServices::databasePDGSpec()
535+
{
536+
return ServiceSpec{
537+
.name = "database-pdg",
538+
.init = [](ServiceRegistry&, DeviceState&, fair::mq::ProgOptions&) -> ServiceHandle {
539+
auto* ptr = new TDatabasePDG();
540+
ptr->ReadPDGTable();
541+
return ServiceHandle{TypeIdHelpers::uniqueId<TDatabasePDG>(), ptr, ServiceKind::Serial, "database-pdg"};
542+
},
543+
.configure = CommonServices::noConfiguration(),
544+
.exit = [](ServiceRegistry&, void* service) { reinterpret_cast<TDatabasePDG*>(service)->Delete(); },
545+
.kind = ServiceKind::Serial};
546+
}
533547
} // namespace o2::framework
534548
#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)