Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:19

0001 // Copyright 2024, Jefferson Science Associates, LLC.
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 // Author: Nathan Brei
0004 
0005 #include <JANA/JEventProcessor.h>
0006 
0007 #include <podio/podioVersion.h>
0008 #if podio_VERSION_MAJOR == 0 && podio_VERSION_MINOR < 99
0009 #include <podio/ROOTFrameWriter.h>
0010 namespace podio {
0011 using ROOTWriter = podio::ROOTFrameWriter;
0012 }
0013 #else
0014 #include <podio/ROOTWriter.h>
0015 #endif
0016 
0017 class PodioFileWriter : public JEventProcessor {
0018 
0019 private:
0020     Parameter<std::vector<std::string>> m_collection_names {this, 
0021         "podio:output_collections", 
0022         {}, 
0023         "Comma-separated collection names to write to file"};
0024 
0025     Parameter<std::string> m_output_filename {this, 
0026         "podio:output_file", 
0027         "output.root", 
0028         "PODIO output filename"};
0029 
0030     Parameter<std::string> m_output_category {this, 
0031         "podio:output_category",
0032         "events",
0033         "Name of branch to store data in the output file"};
0034 
0035     std::unique_ptr<podio::ROOTWriter> m_writer;
0036 
0037 
0038 public:
0039     PodioFileWriter() {
0040         SetTypeName(NAME_OF_THIS);
0041         SetCallbackStyle(CallbackStyle::ExpertMode);
0042     }
0043 
0044     void Init() override {
0045         m_writer = std::make_unique<podio::ROOTWriter>(*m_output_filename);
0046     }
0047 
0048     void Finish() override {
0049         m_writer->finish();
0050     }
0051 
0052     void ProcessParallel(const JEvent& event) override {
0053 
0054         for (const auto& collection_name : *m_collection_names) {
0055 
0056             // Trigger construction of everything specified in m_output_collections, in case it hadn't yet
0057             // Note that the frame might contain additional collections not specified in m_collection_names.
0058             // We are always writing these out as well for the sake of data integrity. If we don't, the file
0059             // may contain dangling references and segfault upon reading.
0060             event.GetCollectionBase(collection_name);
0061         }
0062     }
0063 
0064     void Process(const JEvent& event) override {
0065 
0066         auto* frame = event.GetSingle<podio::Frame>();
0067         // This will throw if no PODIO frame is found. 
0068         // As long as _some_ PODIO data has been inserted somewhere upstream, the frame will be present.
0069 
0070         m_writer->writeFrame(*frame, *m_output_category);
0071         // The user is responsible for setting the event/run numbers somewhere in their data model, so that
0072         // our output file can be correctly read. JANA doesn't/shouldn't know where that is!
0073         // The user should probably do this in either a JEventSource or a JEventUnfolder.
0074     }
0075 
0076 };
0077 
0078 
0079 extern "C" {
0080 void InitPlugin(JApplication* app) {
0081     InitJANAPlugin(app);
0082     app->Add(new PodioFileWriter());
0083 }
0084 }
0085 
0086 
0087