Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimesliceExample: Wire factories externally #289

Merged
merged 13 commits into from
Apr 19, 2024
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
15 changes: 12 additions & 3 deletions src/examples/PodioExample/DatamodelGlue.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@

#include <podio/podioVersion.h>

#include <datamodel/ExampleHit.h>
#include <datamodel/ExampleHitCollection.h>
#include <datamodel/ExampleCluster.h>
#include <datamodel/ExampleClusterCollection.h>
#include <datamodel/EventInfo.h>
#include <datamodel/EventInfoCollection.h>
#include <datamodel/TimesliceInfoCollection.h>

/// Legacy PODIO support
template <typename T>
Expand All @@ -38,6 +36,12 @@ struct PodioTypeMap<EventInfo> {
using collection_t = EventInfoCollection;
};

template <>
struct PodioTypeMap<TimesliceInfo> {
using mutable_t = MutableTimesliceInfo;
using collection_t = TimesliceInfoCollection;
};


template<typename ... Ts>
struct Overload : Ts ... {
Expand All @@ -51,6 +55,9 @@ void visitPodioType(const std::string& podio_typename, F& helper, ArgsT... args)
if (podio_typename == "EventInfo") {
return helper.template operator()<EventInfo>(std::forward<ArgsT>(args)...);
}
if (podio_typename == "TimesliceInfo") {
return helper.template operator()<TimesliceInfo>(std::forward<ArgsT>(args)...);
}
else if (podio_typename == "ExampleHit") {
return helper.template operator()<ExampleHit>(std::forward<ArgsT>(args)...);
}
Expand Down Expand Up @@ -81,6 +88,8 @@ struct VisitExampleDatamodel {
const auto podio_typename = collection.getTypeName();
if (podio_typename == "EventInfoCollection") {
return visitor(static_cast<const EventInfoCollection &>(collection));
} else if (podio_typename == "TimesliceInfoCollection") {
return visitor(static_cast<const TimesliceInfoCollection &>(collection));
} else if (podio_typename == "ExampleHitCollection") {
return visitor(static_cast<const ExampleHitCollection &>(collection));
} else if (podio_typename == "ExampleClusterCollection") {
Expand Down
4 changes: 2 additions & 2 deletions src/examples/PodioExample/PodioExample.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

void create_hits_file() {

EventInfo eventinfo1(7, 22);
EventInfo eventinfo1(7, 0, 22);
EventInfoCollection eventinfos1;
eventinfos1.push_back(eventinfo1);

Expand All @@ -38,7 +38,7 @@ void create_hits_file() {
podio::ROOTFrameWriter writer("hits.root");
writer.writeFrame(event1, "events");

EventInfo eventinfo2(8, 22);
EventInfo eventinfo2(8, 0, 22);
EventInfoCollection eventinfos2;
eventinfos2.push_back(eventinfo2);

Expand Down
8 changes: 8 additions & 0 deletions src/examples/PodioExample/layout.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ datatypes :
Author : "N. Brei"
Members :
- int EventNumber // event number
- int TimesliceNumber // timeslice number
- int RunNumber // run number

TimesliceInfo:
Description : "Timeslice info"
Author : "N. Brei"
Members :
- int TimesliceNumber // timeslice number
- int RunNumber // run number

ExampleHit :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
#include <JANA/Omni/JOmniFactory.h>


struct MyEventFactory : public JOmniFactory<MyEventFactory> {
struct MyClusterFactory : public JOmniFactory<MyClusterFactory> {

PodioInput<ExampleCluster> m_protoclusters_in {this, "evt_protoclusters"};
PodioOutput<ExampleCluster> m_clusters_out {this, "clusters"};


MyEventFactory() {
SetLevel(JEventLevel::Event);
}

void Configure() {
}

Expand Down
80 changes: 0 additions & 80 deletions src/examples/TimesliceExample/MyEventProcessor.h

This file was deleted.

50 changes: 50 additions & 0 deletions src/examples/TimesliceExample/MyFileReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024, Jefferson Science Associates, LLC.
// Subject to the terms in the LICENSE file found in the top-level directory.

#pragma once

#include <DatamodelGlue.h>
#include <JANA/JEventSource.h>
#include "CollectionTabulators.h"


struct MyFileReader : public JEventSource {

PodioOutput<ExampleHit> m_hits_out {this, "hits"};

MyFileReader() {
SetTypeName(NAME_OF_THIS);
}

void Open() override { }

void GetEvent(std::shared_ptr<JEvent> event) override {

auto event_nr = event->GetEventNumber();

auto hits_out = std::make_unique<ExampleHitCollection>();

// ExampleHit(unsigned long long cellID, double x, double y, double z, double energy, std::uint64_t time);
hits_out->push_back(ExampleHit(event_nr, 0, 22, 22, 22, 0));
hits_out->push_back(ExampleHit(event_nr, 0, 49, 49, 49, 1));
hits_out->push_back(ExampleHit(event_nr, 0, 7.6, 7.6, 7.6, 2));

LOG_DEBUG(GetLogger()) << "MySource: Emitted " << GetLevel() << " " << event->GetEventNumber() << "\n"
<< TabulateHits(hits_out.get())
<< LOG_END;

m_hits_out() = std::move(hits_out);

// Furnish this event with info object
if (GetLevel() == JEventLevel::Timeslice) {
TimesliceInfoCollection info;
info.push_back(TimesliceInfo(event_nr, 0)); // event nr, run nr
event->InsertCollection<TimesliceInfo>(std::move(info), "ts_info");
}
else {
EventInfoCollection info;
info.push_back(EventInfo(event_nr, 0, 0)); // event nr, timeslice nr, run nr
event->InsertCollection<EventInfo>(std::move(info), "evt_info");
}
}
};
36 changes: 36 additions & 0 deletions src/examples/TimesliceExample/MyFileReaderGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include <JANA/JEventSourceGenerator.h>
#include "MyFileReader.h"


class MyFileReaderGenerator : public JEventSourceGenerator {

JEventSource* MakeJEventSource(std::string resource_name) override {

auto source = new MyFileReader;

// Check if the string "timeslices" appears anywhere in our filename.
// If so, we assume the file contains timeslices, otherwise it contains physics events.
// Another approach might be to peek at the file's contents
if (resource_name.find("timeslices") != std::string::npos) {
source->SetLevel(JEventLevel::Timeslice);
}
else {
source->SetLevel(JEventLevel::PhysicsEvent);
}
return source;
}

double CheckOpenable(std::string resource_name) override {
// In theory, we should check whether PODIO can open the file and
// whether it contains an 'events' or 'timeslices' tree. If not, return 0.
if (resource_name.find(".root") != std::string::npos) {
return 0.01;
}
else {
return 0;
}
}
};


76 changes: 76 additions & 0 deletions src/examples/TimesliceExample/MyFileWriter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

// Copyright 2024, Jefferson Science Associates, LLC.
// Subject to the terms in the LICENSE file found in the top-level directory.

#pragma once

#include <DatamodelGlue.h>
#include <podio/ROOTFrameWriter.h>
#include "CollectionTabulators.h"
#include <JANA/JEventProcessor.h>

#include <set>



struct MyFileWriter : public JEventProcessor {

// Trigger the creation of clusters
PodioInput<ExampleCluster> m_evt_clusters_in {this, "clusters"};

// Retrieve the PODIO frame so we can write it directly
Input<podio::Frame> m_evt_frame_in {this, "", JEventLevel::PhysicsEvent};

// TODO: Support optional inputs
// Input<podio::Frame> m_ts_frame_in {this, "", JEventLevel::Timeslice};

std::unique_ptr<podio::ROOTFrameWriter> m_writer = nullptr;
std::mutex m_mutex;

MyFileWriter() {
SetTypeName(NAME_OF_THIS);
}

void Init() {
m_writer = std::make_unique<podio::ROOTFrameWriter>("output.root");
}

void Process(const std::shared_ptr<const JEvent>& event) {

std::lock_guard<std::mutex> guard(m_mutex);
if (event->HasParent(JEventLevel::Timeslice)) {

auto& ts = event->GetParent(JEventLevel::Timeslice);
auto ts_nr = ts.GetEventNumber();

if (event->GetEventIndex() == 0) {
// m_writer->writeFrame(*(m_ts_frame_in().at(0)), "timeslices");
auto ts_frame_in = ts.Get<podio::Frame>();
m_writer->writeFrame(*(ts_frame_in.at(0)), "timeslices");
}

LOG_DEBUG(GetLogger())
<< "Event " << event->GetEventNumber() << " from Timeslice " << ts_nr
<< "\nClusters\n"
<< TabulateClusters(m_evt_clusters_in())
<< LOG_END;
}
else {

LOG_DEBUG(GetLogger())
<< "Event " << event->GetEventNumber()
<< "\nClusters\n"
<< TabulateClusters(m_evt_clusters_in())
<< LOG_END;
}

m_writer->writeFrame(*(m_evt_frame_in().at(0)), "events");

}

void Finish() {
m_writer->finish();
}
};


Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@
#include <JANA/Omni/JOmniFactory.h>


struct MyTimesliceFactory : public JOmniFactory<MyTimesliceFactory> {
struct MyProtoclusterFactory : public JOmniFactory<MyProtoclusterFactory> {

PodioInput<ExampleHit> hits_in {this, "hits"};
PodioOutput<ExampleCluster> clusters_out {this, "protoclusters"};

MyTimesliceFactory() {
SetLevel(JEventLevel::Timeslice);
}

void Configure() {
}

Expand Down
38 changes: 0 additions & 38 deletions src/examples/TimesliceExample/MyTimesliceSource.h

This file was deleted.

Loading
Loading