Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17: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 #pragma once
0006 
0007 #include <string>
0008 
0009 #include <JANA/JEventSourceGenerator.h>
0010 #include <JANA/Utils/JTypeInfo.h>
0011 #include <type_traits>
0012 
0013 class JApplication;
0014 
0015 /// This templated class is used to generate JEventSource based objects
0016 /// to handle reading events into JANA. Multiple JEventSourceGenerator
0017 /// objects may exist in a given program, each representing a different
0018 /// input type. For example, one source may be used to read raw data
0019 /// while another may be used to read DST formats. A third may be used
0020 /// to read from a network connection for online applications. For each
0021 /// source specified in a given job, JANA will determine which JEventSource
0022 /// class to use to read from it. It does this via the CheckOpenable method
0023 /// of JEventSourceSourceGenerator.
0024 ///
0025 /// A new instance of the JEventSource subclass will be created for each
0026 /// input source in the job. For example, if two files of the same type,
0027 /// are specified then the JEventSourceGenerator corresponding to that
0028 /// file type will be used to create two JEventSource objects.
0029 ///
0030 /// REQUIREMENTS OF JEventSource SUBCLASS:
0031 /// --------------------------------------
0032 ///   - constructor that accepts (std::string, JApplication*)
0033 ///   - static std::string GetDescription(void) method
0034 ///
0035 ///
0036 /// NOTE ON CheckOpenable:
0037 /// --------------------------------------
0038 ///     This method should "peek" at the source to see if it is one that
0039 ///     it can open. It should return a value between 0 and 1 inclusive
0040 ///     with 0 meaning "cannot open" and 1 meaning "absolutely can open".
0041 ///     The check can be as simple as looking at the source name (e.g.
0042 ///     does it have a specific suffix) or more involved (e.g. opening
0043 ///     the file and checking for a magic header). Note that it should
0044 ///     not be assumed that the source represents a file name. It could
0045 ///     indicate a URL or how to connect to some other non-file source.
0046 ///
0047 
0048 
0049 template <typename T, typename Enable=void>
0050 class JEventSourceGeneratorT:public JEventSourceGenerator{
0051     public:
0052 
0053         JEventSourceGeneratorT(JApplication *app=nullptr):JEventSourceGenerator(app){}
0054         virtual ~JEventSourceGeneratorT(){}
0055 
0056         /// Return name of the source type this will generate
0057         std::string GetType(void) const {
0058             return JTypeInfo::demangle<T>();
0059         }
0060 
0061         /// Return description of the source type this will generate
0062         std::string GetDescription(void) const { return T::GetDescription(); }
0063 
0064         /// Create an instance of the source type this generates
0065         JEventSource* MakeJEventSource( std::string resource_name ){ 
0066 
0067             auto source = new T( resource_name, mApplication ); 
0068             if (mLevel != JEventLevel::None) {
0069                 source->SetLevel(mLevel);
0070             }
0071             source->SetTypeName(JTypeInfo::demangle<T>());
0072             source->SetPluginName(mPluginName);
0073             return source;
0074         }
0075 
0076         /// Check how likely a source of the type this generates is to read
0077         /// the specified source. This mechanism is to allow a single executable
0078         /// to read from multiple file types, each corresponding to a different
0079         /// JEventSource subclass. If you use only a single source type, then
0080         /// there is no need to override this. If you do need this functionality
0081         /// however, then override this in your code with something like:
0082         ///
0083         ///   template<> double JEventSourceGeneratorT<MyType>::CheckOpenable(std::string source) { ... }
0084         ///
0085         double CheckOpenable( std::string /* source */ ){ return 0.01; }
0086 };
0087 
0088 
0089 
0090 
0091 // Specialization for default-constructible event sources
0092 template <typename T>
0093 class JEventSourceGeneratorT<T, std::enable_if_t<std::is_default_constructible_v<T>>> : public JEventSourceGenerator{
0094     public:
0095 
0096         JEventSourceGeneratorT() : JEventSourceGenerator() {}
0097         virtual ~JEventSourceGeneratorT() {}
0098 
0099         /// Return name of the source type this will generate
0100         std::string GetType(void) const {
0101             return JTypeInfo::demangle<T>();
0102         }
0103 
0104         /// Return description of the source type this will generate
0105         std::string GetDescription(void) const { return T::GetDescription(); }
0106 
0107         /// Create an instance of the source type this generates
0108         JEventSource* MakeJEventSource(std::string resource_name) { 
0109 
0110             auto source = new T;
0111             source->SetTypeName(JTypeInfo::demangle<T>());
0112             source->SetResourceName(resource_name);
0113             source->SetApplication(mApplication);
0114             source->SetPluginName(mPluginName);
0115             if (mLevel != JEventLevel::None) {
0116                 source->SetLevel(mLevel);
0117             }
0118             return source;
0119         }
0120 
0121         /// Check how likely a source of the type this generates is to read
0122         /// the specified source. This mechanism is to allow a single executable
0123         /// to read from multiple file types, each corresponding to a different
0124         /// JEventSource subclass. If you use only a single source type, then
0125         /// there is no need to override this. If you do need this functionality
0126         /// however, then override this in your code with something like:
0127         ///
0128         ///   template<> double JEventSourceGeneratorT<MyType>::CheckOpenable(std::string source) { ... }
0129         ///
0130         double CheckOpenable( std::string /* source */ ){ return 0.01; }
0131 };
0132 
0133 
0134