Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventProcessor_ReactionFilter.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DEventProcessor_ReactionFilter.cc
4 // Created: Mon Nov 21 17:54:40 EST 2016
5 // Creator: pmatt (on Darwin Pauls-MacBook-Pro-2.local 13.4.0 i386)
6 //
7 
9 
10 // Routine used to create our DEventProcessor
11 
12 extern "C"
13 {
14  void InitPlugin(JApplication *locApplication)
15  {
16  InitJANAPlugin(locApplication);
17  locApplication->AddProcessor(new DEventProcessor_ReactionFilter()); //register this plugin
18  locApplication->AddFactoryGenerator(new DFactoryGenerator_ReactionFilter()); //register the factory generator
19  }
20 } // "C"
21 
22 //------------------
23 // init
24 //------------------
26 {
27  // This is called once at program startup.
28 
29  /*
30  //OPTIONAL: Create an EventStore skim.
31  string locSkimFileName = "ReactionFilter.idxa";
32  dEventStoreSkimStream.open(locSkimFileName.c_str());
33  dEventStoreSkimStream << "IDXA" << endl;
34  */
35 
36  return NOERROR;
37 }
38 
39 //------------------
40 // brun
41 //------------------
42 jerror_t DEventProcessor_ReactionFilter::brun(jana::JEventLoop* locEventLoop, int32_t locRunNumber)
43 {
44  // This is called whenever the run number changes
45 
46  return NOERROR;
47 }
48 
49 //------------------
50 // evnt
51 //------------------
52 jerror_t DEventProcessor_ReactionFilter::evnt(jana::JEventLoop* locEventLoop, uint64_t locEventNumber)
53 {
54  // This is called for every event. Use of common resources like writing
55  // to a file or filling a histogram should be mutex protected. Using
56  // locEventLoop->Get(...) to get reconstructed objects (and thereby activating the
57  // reconstruction algorithm) should be done outside of any mutex lock
58  // since multiple threads may call this method at the same time.
59 
60  // DOCUMENTATION:
61  // ANALYSIS library: https://halldweb1.jlab.org/wiki/index.php/GlueX_Analysis_Software
62 
63  /*********************************************************** REQUIRED ***********************************************************/
64 
65  //REQUIRED: To run an analysis, You MUST call one at least of the below code fragments.
66  //JANA is on-demand, so if you don't call one of these, then your analysis won't run.
67 
68  //Recommended: Write surviving particle combinations (if any) to output ROOT TTree
69  //If no cuts are performed by the analysis actions added to a DReaction, then this saves all of its particle combinations.
70  //The event writer gets the DAnalysisResults objects from JANA, performing the analysis.
71  // string is DReaction factory tag: will fill trees for all DReactions that are defined in the specified factory
72  const DEventWriterROOT* locEventWriterROOT = NULL;
73  locEventLoop->GetSingle(locEventWriterROOT);
74  locEventWriterROOT->Fill_DataTrees(locEventLoop, "ReactionFilter");
75 
76  /******************************************************** OPTIONAL: SKIMS *******************************************************/
77 
78  /*
79  //Optional: Create an EventStore skim.
80 
81  // See whether this is MC data or real data
82  vector<const DMCThrown*> locMCThrowns;
83  locEventLoop->Get(locMCThrowns);
84 
85  unsigned int locRunNumber = locEventLoop->GetJEvent().GetRunNumber();
86  unsigned int locUniqueID = locMCThrowns.empty() ? 1 : Get_FileNumber(locEventLoop);
87 
88  // If a particle combo passed the cuts, save the event info in the output file
89  for(size_t loc_i = 0; loc_i < locAnalysisResultsVector.size(); ++loc_i)
90  {
91  const DAnalysisResults* locAnalysisResults = locAnalysisResultsVector[loc_i];
92  if(locAnalysisResults->Get_Reaction()->Get_ReactionName() != "ReactionFilter")
93  continue; // analysis results were for a different reaction
94  if(locAnalysisResults->Get_NumPassedParticleCombos() == 0)
95  continue; // no combos passed
96 
97  //MUST LOCK AROUND MODIFICATION OF MEMBER VARIABLES IN brun() or evnt().
98  japp->WriteLock("ReactionFilter.idxa"); //Lock is unique to this output file
99  {
100  dEventStoreSkimStream << locRunNumber << " " << locEventNumber << " " << locUniqueID << endl;
101  }
102  japp->Unlock("ReactionFilter.idxa");
103  }
104  */
105 
106  return NOERROR;
107 }
108 
109 int DEventProcessor_ReactionFilter::Get_FileNumber(JEventLoop* locEventLoop) const
110 {
111  //Assume that the file name is in the format: *_X.ext, where:
112  //X is the file number (a string of numbers of any length)
113  //ext is the file extension (probably .evio or .hddm)
114 
115  //get the event source
116  JEventSource* locEventSource = locEventLoop->GetJEvent().GetJEventSource();
117  if(locEventSource == NULL)
118  return -1;
119 
120  //get the source file name (strip the path)
121  string locSourceFileName = locEventSource->GetSourceName();
122 
123  //find the last "_" & "." indices
124  size_t locUnderscoreIndex = locSourceFileName.rfind("_");
125  size_t locDotIndex = locSourceFileName.rfind(".");
126  if((locUnderscoreIndex == string::npos) || (locDotIndex == string::npos))
127  return -1;
128 
129  size_t locNumberLength = locDotIndex - locUnderscoreIndex - 1;
130  string locFileNumberString = locSourceFileName.substr(locUnderscoreIndex + 1, locNumberLength);
131 
132  int locFileNumber = -1;
133  istringstream locFileNumberStream(locFileNumberString);
134  locFileNumberStream >> locFileNumber;
135 
136  return locFileNumber;
137 }
138 
139 //------------------
140 // erun
141 //------------------
143 {
144  // This is called whenever the run number changes, before it is
145  // changed to give you a chance to clean up before processing
146  // events from the next run number.
147  return NOERROR;
148 }
149 
150 //------------------
151 // fini
152 //------------------
154 {
155  // Called before program exit after event processing is finished.
156  if(dEventStoreSkimStream.is_open())
157  dEventStoreSkimStream.close();
158  return NOERROR;
159 }
160 
jerror_t evnt(jana::JEventLoop *locEventLoop, uint64_t locEventNumber)
Called every event.
jerror_t erun(void)
Called every time run number changes, provided brun has been called.
jerror_t init(void)
Called once at program start.
jerror_t fini(void)
Called after last event of last event source has been processed.
InitPlugin_t InitPlugin
int Get_FileNumber(JEventLoop *locEventLoop) const
void Fill_DataTrees(JEventLoop *locEventLoop, string locDReactionTag) const
jerror_t brun(jana::JEventLoop *locEventLoop, int32_t locRunNumber)
Called every time a new run number is detected.