Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /jana2/src/libraries/JANA/JEvent.cc was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 
0002 #include "JANA/Utils/JEventLevel.h"
0003 #include <JANA/JEvent.h>
0004 #include <JANA/Services/JComponentManager.h>
0005 #include <cstdint>
0006 #include <sstream>
0007 
0008 
0009 JEvent::JEvent() : mInspector(this){
0010 }
0011 
0012 JEvent::JEvent(JApplication* app) : mInspector(this) {
0013     // Furnish the JEvent with the parameter values and factory generators provided to the JApplication
0014     app->Initialize();
0015     app->GetService<JComponentManager>()->configure_event(*this);
0016 }
0017 
0018 JEvent::~JEvent() {
0019     mFactorySet.Clear();
0020 }
0021 
0022 /// GetFactory() should be used with extreme care because it subverts the JEvent abstraction.
0023 /// Most historical uses of GetFactory are far better served by JMultifactory
0024 JFactory* JEvent::GetFactory(const std::string& object_name, const std::string& tag) const {
0025     auto databundle = mFactorySet.GetDatabundle(object_name, tag);
0026     if (databundle != nullptr) {
0027         return databundle->GetFactory();
0028     }
0029     return nullptr;
0030 }
0031 
0032 /// GetAllFactories() should be used with extreme care because it subverts the JEvent abstraction.
0033 /// Most historical uses of GetFactory are far better served by JMultifactory
0034 std::vector<JFactory*> JEvent::GetAllFactories() const {
0035     return mFactorySet.GetAllFactories();
0036 }
0037 
0038 bool JEvent::HasParent(JEventLevel level) const {
0039     for (const auto& pair : mParents) {
0040         if (pair.first == level) return true;
0041     }
0042     return false;
0043 }
0044 
0045 const JEvent& JEvent::GetParent(JEventLevel level) const {
0046     for (const auto& pair : mParents) {
0047         if (pair.first == level) return *pair.second.first;
0048     }
0049     throw JException("Unable to find parent at level %s", 
0050                         toString(level).c_str());
0051 }
0052 
0053 void JEvent::SetParent(JEvent* parent) {
0054     JEventLevel level = parent->GetLevel();
0055     for (const auto& pair : mParents) {
0056         if (pair.first == level) throw JException("Event already has a parent at level %s", 
0057                                                     toString(parent->GetLevel()).c_str());
0058     }
0059     mParents.push_back({level, {parent, parent->GetEventNumber()}});
0060     parent->mReferenceCount.fetch_add(1);
0061     MakeEventStamp();
0062 }
0063 
0064 
0065 void JEvent::MakeEventStamp() const {
0066     std::ostringstream ss;
0067     ss << toChar(mFactorySet.GetLevel()) << ":" << mEventNumber;
0068     if (!mParents.empty()) {
0069         ss << "(";
0070         size_t parent_count = mParents.size();
0071         for (size_t i=0; i<parent_count; ++i) {
0072             auto parent = mParents[i].second;
0073             ss << parent.first->GetEventStamp();
0074             if (i != parent_count-1) {
0075                 ss << ",";
0076             }
0077         }
0078         ss << ")";
0079     }
0080     mEventStamp = ss.str();
0081 };
0082 
0083 const std::string& JEvent::GetEventStamp() const {
0084     if (mEventStamp.empty()) {
0085         MakeEventStamp();
0086     }
0087     return mEventStamp;
0088 }
0089 
0090 void JEvent::SetParentNumber(JEventLevel level, uint64_t number) {
0091     for (const auto& pair : mParents) {
0092         if (pair.first == level) {
0093             throw JException("Event already has a parent at level %s", toString(level).c_str());
0094         }
0095     }
0096     mParents.push_back({level, {nullptr, number}});
0097     MakeEventStamp();
0098 }
0099 
0100 
0101 uint64_t JEvent::GetParentNumber(JEventLevel level) const {
0102     for (const auto& pair : mParents) {
0103         if (pair.first == level) {
0104             return pair.second.second;
0105         }
0106     }
0107     return 0;
0108 }
0109 
0110 
0111 JEvent* JEvent::ReleaseParent(JEventLevel level) {
0112     if (mParents.size() == 0) {
0113         throw JException("ReleaseParent failed: child has no parents!");
0114     }
0115     auto pair = mParents.back();
0116     if (pair.first != level) {
0117         throw JException("JEvent::ReleaseParent called out of level order: Caller expected %s, but parent was actually %s", 
0118                 toString(level).c_str(), toString(pair.first).c_str());
0119     }
0120     mParents.pop_back();
0121     auto remaining_refs = pair.second.first->mReferenceCount.fetch_sub(1);
0122     if (remaining_refs < 1) { // Remember, this was fetched _before_ the last subtraction
0123         throw JException("Parent refcount has gone negative!");
0124     }
0125     if (remaining_refs == 1) {
0126         return pair.second.first; 
0127         // Parent is no longer shared. Transfer back to arrow
0128     }
0129     else {
0130         return nullptr; // Parent is still shared by other children
0131     }
0132 }
0133 
0134 std::vector<JEvent*> JEvent::ReleaseAllParents() {
0135     std::vector<JEvent*> released_parents;
0136 
0137     for (auto it : mParents) {
0138         auto remaining_refs = it.second.first->mReferenceCount.fetch_sub(1);
0139         if (remaining_refs == 1) {
0140             released_parents.push_back(it.second.first);
0141         }
0142     }
0143     mParents.clear();
0144     return released_parents;
0145 }
0146 
0147 void JEvent::TakeRefToSelf() {
0148     mReferenceCount++;
0149 }
0150 
0151 int JEvent::ReleaseRefToSelf() {
0152     int remaining_refs = mReferenceCount.fetch_sub(1);
0153     remaining_refs -= 1; // fetch_sub post increments
0154     if (remaining_refs < 0) {
0155         throw JException("JEvent's own refcount has gone negative!");
0156     }
0157     return remaining_refs;
0158 }
0159 
0160 int JEvent::GetChildCount() {
0161     return mReferenceCount;
0162 }
0163 
0164 void JEvent::Clear(bool processed_successfully) {
0165     if (processed_successfully && mEventSource != nullptr) {
0166         mEventSource->DoFinishEvent(*this);
0167         mIsWarmedUp = true;
0168     }
0169     mFactorySet.Clear();
0170     mInspector.Reset();
0171     mCallGraph.Reset();
0172 }
0173 
0174 void JEvent::Finish() {
0175     mFactorySet.Finish();
0176 }
0177 
0178