|
||||
File indexing completed on 2025-01-30 10:29:38
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 "RandomSource.h" 0007 0008 #include <JANA/JApplication.h> 0009 #include <JANA/JEvent.h> 0010 0011 /// Include headers to any JObjects you wish to associate with each event 0012 #include "Hit.h" 0013 0014 /// There are two different ways of instantiating JEventSources 0015 /// 1. Creating them manually and registering them with the JApplication 0016 /// 2. Creating a corresponding JEventSourceGenerator and registering that instead 0017 /// If you have a list of files as command line args, JANA will use the JEventSourceGenerator 0018 /// to find the most appropriate JEventSource corresponding to that filename, instantiate and register it. 0019 /// For this to work, the JEventSource constructor has to have the following constructor arguments: 0020 0021 RandomSource::RandomSource() : JEventSource() { 0022 SetTypeName(NAME_OF_THIS); // Provide JANA with class name 0023 SetCallbackStyle(CallbackStyle::ExpertMode); 0024 } 0025 0026 void RandomSource::Open() { 0027 0028 /// Open is called exactly once when processing begins. 0029 0030 /// Get any configuration parameters from the JApplication 0031 JApplication* app = GetApplication(); 0032 app->SetDefaultParameter("random_source:max_emit_freq_hz", 0033 m_max_emit_freq_hz, 0034 "Maximum event rate [Hz] for RandomSource"); 0035 0036 /// For opening a file, get the filename via: 0037 // std::string resource_name = GetResourceName(); 0038 /// Open the file here! 0039 } 0040 0041 void RandomSource::Close() { 0042 // Close the file pointer here! 0043 } 0044 0045 JEventSource::Result RandomSource::Emit(JEvent& event) { 0046 0047 /// Calls to GetEvent are synchronized with each other, which means they can 0048 /// read and write state on the JEventSource without causing race conditions. 0049 0050 /// Configure event and run numbers 0051 static size_t current_event_number = 1; 0052 event.SetEventNumber(current_event_number++); 0053 event.SetRunNumber(22); 0054 0055 /// Slow down event source 0056 auto delay_ms = std::chrono::milliseconds(1000/m_max_emit_freq_hz); 0057 std::this_thread::sleep_for(delay_ms); 0058 0059 /// Insert simulated data into event 0060 std::vector<Hit*> hits; 0061 hits.push_back(new Hit(0, 0, 1.0, 0)); 0062 hits.push_back(new Hit(0, 1, 1.0, 0)); 0063 hits.push_back(new Hit(1, 0, 1.0, 0)); 0064 hits.push_back(new Hit(1, 1, 1.0, 0)); 0065 event.Insert(hits); 0066 0067 /// If you are reading a file of events and have reached the end 0068 /// Note that you should close the file handle in Close(), not here. 0069 // return Result::FailureFinished; 0070 0071 /// If you are streaming events and there are no new events in the message queue, 0072 /// tell JANA that GetEvent() was temporarily unsuccessful like this: 0073 // return Result::FailureTryAgain; 0074 0075 return Result::Success; 0076 } 0077 0078 std::string RandomSource::GetDescription() { 0079 0080 /// GetDescription() helps JANA explain to the user what is going on 0081 return ""; 0082 } 0083 0084 0085 template <> 0086 double JEventSourceGeneratorT<RandomSource>::CheckOpenable(std::string resource_name) { 0087 0088 /// CheckOpenable() decides how confident we are that this EventSource can handle this resource. 0089 /// 0.0 -> 'Cannot handle' 0090 /// (0.0, 1.0] -> 'Can handle, with this confidence level' 0091 0092 /// To determine confidence level, feel free to open up the file and check for magic bytes or metadata. 0093 /// Returning a confidence <- {0.0, 1.0} is perfectly OK! 0094 0095 return (resource_name == "random") ? 1.0 : 0.0; 0096 }
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |