Back to home page

EIC code displayed by LXR

 
 

    


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

0001 
0002 // Copyright 2020, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 
0006 #include <JANA/JApplication.h>
0007 #include <JANA/JLogger.h>
0008 
0009 #include "GroupedEventProcessor.h"
0010 #include "GroupedEventSource.h"
0011 #include "BlockingGroupedEventSource.h"
0012 
0013 
0014 /// The producer thread generates and feeds TridasEvents to the BlockingEventSource.
0015 void producer_thread(BlockingGroupedEventSource* evt_src, JApplication* app, int starting_event_number = 1) {
0016 
0017     int event_number = starting_event_number;
0018     std::vector<TridasEvent*> event_batch;
0019 
0020     for (int group_number = 1; group_number < 6; ++group_number) {
0021 
0022         // Emit groups of 5 TRIDAS events apiece
0023 
0024         for (int i=1; i<=5; ++i) {
0025             auto event = new TridasEvent;
0026             event->event_number = event_number++;
0027             event->run_number = 22;
0028             event->should_keep = false;
0029             event_batch.push_back(event);
0030         }
0031 
0032         // Note: In general, the producer really shouldn't be responsible for setting group number, because they
0033         // can really mess that up. For now we are just doing this so that we can easily see the 'barrier' in the logs
0034 
0035         LOG << "Calling SubmitAndWait for events " << event_number-5 << ".." << event_number-1 << LOG_END;
0036 
0037         // SubmitAndWait causes producer to block until GroupedEventProcessor finishes
0038         evt_src->SubmitAndWait(event_batch);
0039 
0040         // After this point, all of the events in event_batch have been processed!
0041         LOG << "SubmitAndWait returned! Producer thread may now access results for events "
0042             << event_number-5 << ".." << event_number-1 << LOG_END;
0043 
0044         // Note: Just for demonstration purposes, we are setting should_keep=false in the producer and having JANA
0045         // set it to true in the GroupedEventProcessor. More realistically, a JFactory would make a decision.
0046 
0047         // Verify that JANA indeed processed all of the events, and that it didn't free the TridasEvents afterwards!
0048         for (int i=0; i<5; ++i) {
0049             assert(event_batch[i]->should_keep);
0050             delete event_batch[i];
0051         }
0052         event_batch.clear();
0053     }
0054 
0055     // Stop execution once producer is completely finished
0056     app->Quit();
0057 }
0058 
0059 
0060 
0061 extern "C"{
0062 void InitPlugin(JApplication *app) {
0063 
0064     InitJANAPlugin(app);
0065 
0066     app->SetParameterValue("jana:extended_report", false);
0067 
0068     app->Add(new GroupedEventProcessor());
0069 
0070     auto evt_src = new BlockingGroupedEventSource();
0071 
0072     app->Add(evt_src);
0073 
0074     // Launch a separate thread which generates TRIDAS events and submits them to the event source
0075     new std::thread([=](){ producer_thread(evt_src, app); });
0076 
0077     // We can run multiple producer threads, which will correctly interleave execution within JANA
0078     new std::thread([=](){ producer_thread(evt_src, app, 100); });
0079 
0080 
0081 }
0082 } // "C"