Back to home page

EIC code displayed by LXR

 
 

    


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

0001 
0002 #include <JANA/JEvent.h>
0003 #include <JANA/Services/JComponentManager.h>
0004 
0005 
0006 JEvent::JEvent() : mInspector(this){
0007     mFactorySet = new JFactorySet();
0008 }
0009 
0010 JEvent::JEvent(JApplication* app) : mInspector(this) {
0011     // Furnish the JEvent with the parameter values and factory generators provided to the JApplication
0012     app->Initialize();
0013     app->GetService<JComponentManager>()->configure_event(*this);
0014 }
0015 
0016 JEvent::~JEvent() {
0017     if (mFactorySet != nullptr) {
0018         // Prevent memory leaks of factory contents
0019         mFactorySet->Clear();
0020         // We mustn't call EndRun() or Finish() here because that would give us an excepting destructor
0021     }
0022     delete mFactorySet;
0023 }
0024 
0025 void JEvent::SetFactorySet(JFactorySet* factorySet) {
0026     delete mFactorySet;
0027     mFactorySet = factorySet;
0028 #if JANA2_HAVE_PODIO
0029     // Maintain the index of PODIO factories
0030     for (JFactory* factory : mFactorySet->GetAllFactories()) {
0031         if (dynamic_cast<JFactoryPodio*>(factory) != nullptr) {
0032             auto tag = factory->GetTag();
0033             auto it = mPodioFactories.find(tag);
0034             if (it != mPodioFactories.end()) {
0035                 throw JException("SetFactorySet failed because PODIO factory tag '%s' is not unique", tag.c_str());
0036             }
0037             mPodioFactories[tag] = factory;
0038         }
0039     }
0040 #endif
0041 }
0042 
0043 
0044 /// GetFactory() should be used with extreme care because it subverts the JEvent abstraction.
0045 /// Most historical uses of GetFactory are far better served by JMultifactory
0046 JFactory* JEvent::GetFactory(const std::string& object_name, const std::string& tag) const {
0047     return mFactorySet->GetFactory(object_name, tag);
0048 }
0049 
0050 /// GetAllFactories() should be used with extreme care because it subverts the JEvent abstraction.
0051 /// Most historical uses of GetFactory are far better served by JMultifactory
0052 std::vector<JFactory*> JEvent::GetAllFactories() const {
0053     return mFactorySet->GetAllFactories();
0054 }
0055 
0056 bool JEvent::HasParent(JEventLevel level) const {
0057     for (const auto& pair : mParents) {
0058         if (pair.first == level) return true;
0059     }
0060     return false;
0061 }
0062 
0063 const JEvent& JEvent::GetParent(JEventLevel level) const {
0064     for (const auto& pair : mParents) {
0065         if (pair.first == level) return *pair.second;
0066     }
0067     throw JException("Unable to find parent at level %s", 
0068                         toString(level).c_str());
0069 }
0070 
0071 void JEvent::SetParent(JEvent* parent) {
0072     JEventLevel level = parent->GetLevel();
0073     for (const auto& pair : mParents) {
0074         if (pair.first == level) throw JException("Event already has a parent at level %s", 
0075                                                     toString(parent->GetLevel()).c_str());
0076     }
0077     mParents.push_back({level, parent});
0078     parent->mReferenceCount.fetch_add(1);
0079 }
0080 
0081 JEvent* JEvent::ReleaseParent(JEventLevel level) {
0082     if (mParents.size() == 0) {
0083         throw JException("ReleaseParent failed: child has no parents!");
0084     }
0085     auto pair = mParents.back();
0086     if (pair.first != level) {
0087         throw JException("JEvent::ReleaseParent called out of level order: Caller expected %s, but parent was actually %s", 
0088                 toString(level).c_str(), toString(pair.first).c_str());
0089     }
0090     mParents.pop_back();
0091     auto remaining_refs = pair.second->mReferenceCount.fetch_sub(1);
0092     if (remaining_refs < 1) { // Remember, this was fetched _before_ the last subtraction
0093         throw JException("Parent refcount has gone negative!");
0094     }
0095     if (remaining_refs == 1) {
0096         return pair.second; 
0097         // Parent is no longer shared. Transfer back to arrow
0098     }
0099     else {
0100         return nullptr; // Parent is still shared by other children
0101     }
0102 }
0103 
0104 void JEvent::Release() {
0105     auto remaining_refs = mReferenceCount.fetch_sub(1);
0106     if (remaining_refs < 0) {
0107         throw JException("JEvent's own refcount has gone negative!");
0108     }
0109 }
0110 
0111 void JEvent::Clear() {
0112     if (mEventSource != nullptr) {
0113         mEventSource->DoFinishEvent(*this);
0114     }
0115     mFactorySet->Clear();
0116     mInspector.Reset();
0117     mCallGraph.Reset();
0118     mReferenceCount = 1;
0119     mIsWarmedUp = true;
0120 }
0121 
0122 void JEvent::Finish() {
0123     mFactorySet->Finish();
0124 }
0125 
0126