Skip to content

Commit

Permalink
Merge pull request #281 from JeffersonLab/nbrei_omni
Browse files Browse the repository at this point in the history
Develop TimesliceExample
nathanwbrei authored Apr 10, 2024
2 parents 523c0a5 + bbadc01 commit e988cd3
Showing 83 changed files with 2,252 additions and 768 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -63,3 +63,5 @@ cmake-build*/
src/examples/PodioExample/datamodel/*
src/examples/PodioExample/src/*
src/examples/PodioExample/podio_generated_files.cmake

podio_build/
2 changes: 0 additions & 2 deletions src/examples/PodioExample/DatamodelGlue.h
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
#include <datamodel/EventInfo.h>
#include <datamodel/EventInfoCollection.h>

#if podio_VERSION < PODIO_VERSION(0, 17, 0)
/// Legacy PODIO support
template <typename T>
struct PodioTypeMap {
@@ -38,7 +37,6 @@ struct PodioTypeMap<EventInfo> {
using mutable_t = MutableEventInfo;
using collection_t = EventInfoCollection;
};
#endif


template<typename ... Ts>
16 changes: 8 additions & 8 deletions src/examples/PodioExample/PodioExample.cc
Original file line number Diff line number Diff line change
@@ -26,10 +26,10 @@ void create_hits_file() {
eventinfos1.push_back(eventinfo1);

ExampleHitCollection hits1;
hits1.push_back(ExampleHit(22, -1, -1, 0, 100));
hits1.push_back(ExampleHit(49, 1, 1, 0, 15.5));
hits1.push_back(ExampleHit(47, 1, 2, 0, 0.5));
hits1.push_back(ExampleHit(42, 2, 1, 0, 4.0));
hits1.push_back(ExampleHit(22, -1, -1, 0, 100, 0));
hits1.push_back(ExampleHit(49, 1, 1, 0, 15.5, 0));
hits1.push_back(ExampleHit(47, 1, 2, 0, 0.5, 0));
hits1.push_back(ExampleHit(42, 2, 1, 0, 4.0, 0));

podio::Frame event1;
event1.put(std::move(hits1), "hits");
@@ -43,10 +43,10 @@ void create_hits_file() {
eventinfos2.push_back(eventinfo2);

ExampleHitCollection hits2;
hits2.push_back(ExampleHit(42, 5, -5, 5, 7.6));
hits2.push_back(ExampleHit(618, -3, -5, 1, 99.9));
hits2.push_back(ExampleHit(27, -10, 10, 10, 22.2));
hits2.push_back(ExampleHit(28, -9, 11, 10, 7.8));
hits2.push_back(ExampleHit(42, 5, -5, 5, 7.6, 0));
hits2.push_back(ExampleHit(618, -3, -5, 1, 99.9, 0));
hits2.push_back(ExampleHit(27, -10, 10, 10, 22.2, 0));
hits2.push_back(ExampleHit(28, -9, 11, 10, 7.8, 0));

podio::Frame event2;
event2.put(std::move(hits2), "hits");
1 change: 1 addition & 0 deletions src/examples/PodioExample/layout.yaml
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ datatypes :
- double y // y-coordinate
- double z // z-coordinate
- double energy // measured energy deposit
- uint64_t time // ticks since start of timeframe

ExampleCluster :
Description : "Cluster"
5 changes: 3 additions & 2 deletions src/examples/TimesliceExample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -3,12 +3,13 @@
if (USE_PODIO)
set (TimesliceExample_SOURCES
TimesliceExample.cc
CollectionTabulators.cc
)

add_library(TimesliceExample SHARED ${TimesliceExample_SOURCES})
target_link_libraries(TimesliceExample jana2 podio::podio PodioExampleDatamodel PodioExampleDatamodelDict podio::podioRootIO)
target_link_libraries(TimesliceExample PodioExampleDatamodel PodioExampleDatamodelDict podio::podioRootIO)
set_target_properties(TimesliceExample PROPERTIES PREFIX "" SUFFIX ".so" OUTPUT_NAME "TimesliceExample")
install(TARGETS TimesliceExample DESTINATION programs)
install(TARGETS TimesliceExample DESTINATION plugins)

else()
message(STATUS "Skipping examples/TimesliceExample because USE_PODIO=Off")
41 changes: 41 additions & 0 deletions src/examples/TimesliceExample/CollectionTabulators.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

#include "CollectionTabulators.h"

JTablePrinter TabulateClusters(const ExampleClusterCollection* c) {

JTablePrinter t;
t.AddColumn("clusterId");
t.AddColumn("energy");
t.AddColumn("hits");
t.AddColumn("clusters");

for (auto cluster : *c) {
std::ostringstream oss;
for (auto hit : cluster.Hits()) {
oss << hit.id() << " ";
}
std::ostringstream oss2;
for (auto cluster : cluster.Clusters()) {
oss2 << cluster.id() << " ";
}
t | cluster.id() | cluster.energy() | oss.str() | oss2.str();
}
return t;
}


JTablePrinter TabulateHits(const ExampleHitCollection* c) {

JTablePrinter t;
t.AddColumn("hitId");
t.AddColumn("cellId");
t.AddColumn("x");
t.AddColumn("y");
t.AddColumn("energy");
t.AddColumn("time");

for (auto hit : *c) {
t | hit.id() | hit.cellID() | hit.x() | hit.y() | hit.energy() | hit.time();
}
return t;
}
10 changes: 10 additions & 0 deletions src/examples/TimesliceExample/CollectionTabulators.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "DatamodelGlue.h"
#include <JANA/Utils/JTablePrinter.h>

JTablePrinter TabulateClusters(const ExampleClusterCollection* c);

JTablePrinter TabulateHits(const ExampleHitCollection* c);


18 changes: 0 additions & 18 deletions src/examples/TimesliceExample/MyDataModel.h

This file was deleted.

37 changes: 19 additions & 18 deletions src/examples/TimesliceExample/MyEventFactory.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@



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

#pragma once
#include "MyDataModel.h"
#include <JANA/JFactoryT.h>

#include <DatamodelGlue.h>
#include <JANA/Omni/JOmniFactory.h>


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

int init_call_count = 0;
int change_run_call_count = 0;
int process_call_count = 0;
PodioInput<ExampleCluster> m_protoclusters_in {this, "evt_protoclusters"};
PodioOutput<ExampleCluster> m_clusters_out {this, "clusters"};

MyClusterFactory() {

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

void Init() override {
++init_call_count;
void Configure() {
}

void ChangeRun(const std::shared_ptr<const JEvent>&) override {
++change_run_call_count;
void ChangeRun(int32_t /*run_nr*/) {
}

void Process(const std::shared_ptr<const JEvent>& event) override {
++process_call_count;
void Execute(int32_t /*run_nr*/, uint64_t /*evt_nr*/) {

auto cs = std::make_unique<ExampleClusterCollection>();

for (auto protocluster : *m_protoclusters_in()) {
auto cluster = MutableExampleCluster(protocluster.energy() + 1000);
cluster.addClusters(protocluster);
cs->push_back(cluster);
}

auto protos = event->Get<MyCluster>("protos");
// TODO: Output something sensible
m_clusters_out() = std::move(cs);
}
};

73 changes: 59 additions & 14 deletions src/examples/TimesliceExample/MyEventProcessor.h
Original file line number Diff line number Diff line change
@@ -3,32 +3,77 @@
// Subject to the terms in the LICENSE file found in the top-level directory.

#pragma once
#include "MyDataModel.h"

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

struct ExampleEventProcessor : public JEventProcessor {
#include <set>



struct MyEventProcessor : public JEventProcessor {

PodioInput<ExampleHit> m_ts_hits_in {this, "hits", JEventLevel::Timeslice};
PodioInput<ExampleCluster> m_ts_protoclusters_in {this, "ts_protoclusters", JEventLevel::Timeslice};
PodioInput<ExampleCluster> m_evt_protoclusters_in {this, "evt_protoclusters", JEventLevel::Event};
PodioInput<ExampleCluster> m_evt_clusters_in {this, "clusters", JEventLevel::Event};

Input<podio::Frame> m_evt_frame_in {this, "", JEventLevel::Event};
Input<podio::Frame> m_ts_frame_in {this, "", JEventLevel::Timeslice};

std::unique_ptr<podio::ROOTFrameWriter> m_writer = nullptr;
std::set<int> m_seen_event_nrs;
int m_expected_timeslice_count;

std::mutex m_mutex;

ExampleTimesliceProcessor() {
SetEventLevel(JEvent::Level::Event);
MyEventProcessor() {
SetLevel(JEventLevel::Event);
SetTypeName("MyEventProcessor");
}

void Init() {
m_writer = std::make_unique<podio::ROOTFrameWriter>("output.root");
m_expected_timeslice_count = GetApplication()->GetParameterValue<int>("jana:nevents");
}

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

auto ts_nr = event->GetParent(JEventLevel::Timeslice).GetEventNumber();
m_seen_event_nrs.insert(event->GetEventNumber());

std::lock_guard<std::mutex> guard(m_mutex);
m_writer->writeFrame(*(m_evt_frame_in().at(0)), "events");
if (event->GetEventIndex() == 0) {
m_writer->writeFrame(*(m_ts_frame_in().at(0)), "timeslices");
}

LOG_DEBUG(GetLogger())
<< "MyEventProcessor: Event " << event->GetEventNumber() << " from Timeslice " << ts_nr
<< "\nTimeslice-level hits\n"
<< TabulateHits(m_ts_hits_in())
<< "\nTimeslice-level protoclusters\n"
<< TabulateClusters(m_ts_protoclusters_in())
<< "\nEvent-level protoclusters\n"
<< TabulateClusters(m_evt_protoclusters_in())
<< "\nEvent-level clusters\n"
<< TabulateClusters(m_evt_clusters_in())
<< LOG_END;
}

void Finish() {
m_writer->finish();
for (int tsnr=0; tsnr<m_expected_timeslice_count; ++tsnr) {
for (int chnr=0; chnr < 3; ++chnr) {
int evtnr = 100*tsnr + chnr;

auto outputs = event->Get<MyOutput>();
// assert(outputs.size() == 4);
// assert(outputs[0]->z == 25.6f);
// assert(outputs[1]->z == 26.5f);
// assert(outputs[2]->z == 27.4f);
// assert(outputs[3]->z == 28.3f);
LOG << " Contents of event " << event->GetEventNumber() << LOG_END;
for (auto output : outputs) {
LOG << " " << output->evt << ":" << output->sub << " " << output->z << LOG_END;
if (!m_seen_event_nrs.contains(evtnr)) {
LOG << "MyEventProcessor: Missing event #" << evtnr << LOG_END;
}
}
}
LOG << " DONE with contents of event " << event->GetEventNumber() << LOG_END;
}
};

30 changes: 15 additions & 15 deletions src/examples/TimesliceExample/MyTimesliceFactory.h
Original file line number Diff line number Diff line change
@@ -2,35 +2,35 @@
// Subject to the terms in the LICENSE file found in the top-level directory.

#pragma once
#include "MyDataModel.h"

#include <DatamodelGlue.h>
#include <JANA/Omni/JOmniFactory.h>
#include <JANA/JFactoryT.h>


struct MyProtoClusterFactory : public JFactoryT<MyCluster> {
struct MyTimesliceFactory : public JOmniFactory<MyTimesliceFactory> {

int init_call_count = 0;
int change_run_call_count = 0;
int process_call_count = 0;
PodioInput<ExampleHit> hits_in {this, "hits"};
PodioOutput<ExampleCluster> clusters_out {this, "protoclusters"};

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

void Init() override {
++init_call_count;
void Configure() {
}

void ChangeRun(const std::shared_ptr<const JEvent>&) override {
++change_run_call_count;
void ChangeRun(int32_t /*run_nr*/) {
}

void Process(const std::shared_ptr<const JEvent>& event) override {
++process_call_count;
void Execute(int32_t /*run_nr*/, uint64_t /*evt_nr*/) {

auto protos = event->Get<MyCluster>("protos");
// TODO: Output something sensible
auto cs = std::make_unique<ExampleClusterCollection>();
for (auto hit : *hits_in()) {
auto cluster = MutableExampleCluster(hit.energy());
cluster.addHits(hit);
cs->push_back(cluster);
}
clusters_out() = std::move(cs);
}
};

Loading

0 comments on commit e988cd3

Please sign in to comment.