Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Analysis/Tutorials/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,9 @@ o2_add_dpl_workflow(conditional-expressions
JOB_POOL analysis
PUBLIC_LINK_LIBRARIES O2::Framework O2::AnalysisCore O2::AnalysisDataModel
COMPONENT_NAME AnalysisTutorial)

o2_add_dpl_workflow(using-pdg
SOURCES src/usingPDGService.cxx
JOB_POOL analysis
PUBLIC_LINK_LIBRARIES O2::Framework O2::AnalysisCore O2::AnalysisDataModel
COMPONENT_NAME AnalysisTutorial)
41 changes: 41 additions & 0 deletions Analysis/Tutorials/src/usingPDGService.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

#include "Framework/AnalysisTask.h"
#include "AnalysisCore/MC.h"
#include "TDatabasePDG.h"
#include "Framework/runDataProcessing.h"

using namespace o2;
using namespace o2::framework;

struct UsePdgDatabase {
Service<TDatabasePDG> pdg;
OutputObj<TH1F> particleCharges{TH1F("charges", ";charge;entries", 201, -10.1, 10.1)};

void process(aod::McCollision const&, aod::McParticles const& particles)
{
for (auto& particle : particles) {
auto pdgInfo = pdg->GetParticle(particle.pdgCode());
if (pdgInfo != nullptr) {
particleCharges->Fill(pdgInfo->Charge());
} else {
LOGF(warn, "[%d] unknown particle with PDG code %d", particle.globalIndex(), particle.pdgCode());
}
}
}
};

WorkflowSpec defineDataProcessing(ConfigContext const& cfgc)
{
return WorkflowSpec{
adaptAnalysisTask<UsePdgDatabase>(cfgc)};
}
15 changes: 14 additions & 1 deletion Framework/Core/include/Framework/AnalysisManagers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Framework/ConfigContext.h"
#include "Framework/RootConfigParamHelpers.h"
#include "Framework/ExpressionHelpers.h"
#include "Framework/CommonServices.h"

namespace o2::framework
{
Expand Down Expand Up @@ -344,6 +345,12 @@ class has_instance

template <typename T>
struct ServiceManager {
template <typename ANY>
static bool add(std::vector<ServiceSpec>&, ANY&)
{
return false;
}

template <typename ANY>
static bool prepare(InitContext&, ANY&)
{
Expand All @@ -353,13 +360,19 @@ struct ServiceManager {

template <typename T>
struct ServiceManager<Service<T>> {
static bool add(std::vector<ServiceSpec>& specs, Service<T>&)
{
CommonAnalysisServices::addAnalysisService<T>(specs);
return true;
}

static bool prepare(InitContext& context, Service<T>& service)
{
if constexpr (has_instance<T>::value) {
service.service = &(T::instance()); // Sigh...
return true;
} else {
service.service = context.services().get<T>();
service.service = &(context.services().get<T>());
return true;
}
return false;
Expand Down
6 changes: 5 additions & 1 deletion Framework/Core/include/Framework/AnalysisTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,9 @@ DataProcessorSpec adaptAnalysisTask(ConfigContext const& ctx, Args&&... args)

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

std::vector<ServiceSpec> requiredServices = CommonServices::defaultServices();
homogeneous_apply_refs([&requiredServices](auto& x) { return ServiceManager<std::decay_t<decltype(x)>>::add(requiredServices, x); }, *task.get());

auto algo = AlgorithmSpec::InitCallback{[task = task, expressionInfos](InitContext& ic) mutable {
homogeneous_apply_refs([&ic](auto&& x) { return OptionManager<std::decay_t<decltype(x)>>::prepare(ic, x); }, *task.get());
homogeneous_apply_refs([&ic](auto&& x) { return ServiceManager<std::decay_t<decltype(x)>>::prepare(ic, x); }, *task.get());
Expand Down Expand Up @@ -840,7 +843,8 @@ DataProcessorSpec adaptAnalysisTask(ConfigContext const& ctx, Args&&... args)
inputs,
outputs,
algo,
options};
options,
requiredServices};
return spec;
}

Expand Down
14 changes: 14 additions & 0 deletions Framework/Core/include/Framework/CommonServices.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "Framework/ServiceSpec.h"
#include "Framework/TypeIdHelpers.h"

class TDatabasePDG;

namespace o2::framework
{

Expand Down Expand Up @@ -69,6 +71,18 @@ struct CommonServices {
static std::vector<ServiceSpec> requiredServices();
};

struct CommonAnalysisServices {
static ServiceSpec databasePDGSpec();

template <typename T>
static void addAnalysisService(std::vector<ServiceSpec>& specs)
{
if constexpr (std::is_same_v<T, TDatabasePDG>) {
specs.push_back(databasePDGSpec());
}
}
};

} // namespace o2::framework

#endif // O2_FRAMEWORK_COMMONSERVICES_H_
4 changes: 3 additions & 1 deletion Framework/Core/include/Framework/runDataProcessing.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ void defaultConfiguration(std::vector<o2::framework::DispatchPolicy>& dispatchPo
void defaultConfiguration(std::vector<o2::framework::ResourcePolicy>& resourcePolicies) {}
void defaultConfiguration(std::vector<o2::framework::ServiceSpec>& services)
{
services = o2::framework::CommonServices::defaultServices();
if (services.empty()) {
services = o2::framework::CommonServices::defaultServices();
}
}

/// Workflow options which are required by DPL in order to work.
Expand Down
14 changes: 14 additions & 0 deletions Framework/Core/src/CommonServices.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "../src/DataProcessingStatus.h"
#include "ArrowSupport.h"
#include "DPLMonitoringBackend.h"
#include "TDatabasePDG.h"

#include <Configuration/ConfigurationInterface.h>
#include <Configuration/ConfigurationFactory.h>
Expand Down Expand Up @@ -528,5 +529,18 @@ std::vector<ServiceSpec> CommonServices::defaultServices(int numThreads)
return specs;
}

o2::framework::ServiceSpec CommonAnalysisServices::databasePDGSpec()
{
return ServiceSpec{
.name = "database-pdg",
.init = [](ServiceRegistry&, DeviceState&, fair::mq::ProgOptions&) -> ServiceHandle {
auto* ptr = new TDatabasePDG();
ptr->ReadPDGTable();
return ServiceHandle{TypeIdHelpers::uniqueId<TDatabasePDG>(), ptr, ServiceKind::Serial, "database-pdg"};
},
.configure = CommonServices::noConfiguration(),
.exit = [](ServiceRegistry&, void* service) { reinterpret_cast<TDatabasePDG*>(service)->Delete(); },
.kind = ServiceKind::Serial};
}
} // namespace o2::framework
#pragma GCC diagnostic pop
1 change: 0 additions & 1 deletion Framework/Core/src/DeviceSpecHelpers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,6 @@ void DeviceSpecHelpers::dataProcessorSpecs2DeviceSpecs(const WorkflowSpec& workf
unsigned short resourcesMonitoringInterval,
std::string const& channelPrefix)
{

std::vector<LogicalForwardInfo> availableForwardsInfo;
std::vector<DeviceConnectionEdge> logicalEdges;
std::vector<DeviceConnectionId> connections;
Expand Down