Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hd_eventfilter/MyProcessor.cc
Go to the documentation of this file.
1 // Author: David Lawrence June 25, 2004
2 //
3 //
4 // MyProcessor.cc
5 //
6 
7 #include <iostream>
8 using namespace std;
9 
10 #include "MyProcessor.h"
11 
12 #include <JANA/JEvent.h>
13 
14 #include <HDDM/DEventSourceHDDM.h>
15 #include <TRACKING/DMCThrown.h>
16 
17 #include <TRIGGER/DMCTrigger.h>
18 
19 //------------------------------------------------------------------
20 // init -Open output file here (e.g. a ROOT file)
21 //------------------------------------------------------------------
22 jerror_t MyProcessor::init(void)
23 {
24  // open HDDM file
25  filename = "filtered.hddm";
26  ofs = new ofstream(filename.c_str());
27  if (!ofs->is_open()) {
28  fout = 0;
29  return UNRECOVERABLE_ERROR;
30  }
31  fout = new hddm_s::ostream(*ofs);
32 
33  HDDM_USE_COMPRESSION = false;
34  gPARMS->SetDefaultParameter("HDDM:USE_COMPRESSION", HDDM_USE_COMPRESSION,
35  "Turn on/off compression of the output HDDM stream."
36  " Set to \"1\" to turn on (it's off by default)");
38  gPARMS->SetDefaultParameter("HDDM:USE_INTEGRITY_CHECKS",
40  "Turn on/off automatic integrity checking on the"
41  " output HDDM stream."
42  " Set to \"1\" to turn on (it's off by default)");
43 
44  // enable on-the-fly bzip2 compression on output stream
46  jout << " Enabling bz2 compression of output HDDM file stream"
47  << std::endl;
48  fout->setCompression(hddm_s::k_bz2_compression);
49  }
50  else {
51  jout << " HDDM compression disabled" << std::endl;
52  }
53 
54  // enable a CRC data integrity check at the end of each event record
56  jout << " Enabling CRC data integrity check in output HDDM file stream"
57  << std::endl;
58  fout->setIntegrityChecks(hddm_s::k_crc32_integrity);
59  }
60  else {
61  jout << " HDDM integrity checks disabled" << std::endl;
62  }
63 
64  Nevents_written = 0;
65 
66  return NOERROR;
67 }
68 
69 //------------------------------------------------------------------
70 // evnt -Fill histograms here
71 //------------------------------------------------------------------
72 jerror_t MyProcessor::evnt(JEventLoop *loop, uint64_t eventnumber)
73 {
74  JEvent& event = loop->GetJEvent();
75  JEventSource *source = event.GetJEventSource();
76  DEventSourceHDDM *hddm_source = dynamic_cast<DEventSourceHDDM*>(source);
77  if (!hddm_source) {
78  cerr << " This program MUST be used with an HDDM file as input!" << endl;
79  exit(-1);
80  }
81  hddm_s::HDDM *hddm = (hddm_s::HDDM*)event.GetRef();
82  if (!hddm)
83  return NOERROR;
84 
85 
86  // Initialize write_out flag. We set this to true to write the event
87  // out and false to ignore it. Initialize it to false so that we can
88  // pick the conditions needed to keep it.
89  bool write_out=false;
90 
91  // Here we do whatever calculations are needed to determine if we keep
92  // the event. Since this is basically a skeleton meant as an example
93  // we do a trivial check on the momentum of the thrown particles.
94  // In practice, one could request objects that require full reconstruction
95  // as well so that filters could be built on those quantities as well.
96 
97  //---------------------- Filter Code Start ----------------------
98  // Get data
99  vector<const DMCThrown*> mcthrowns;
100  loop->Get(mcthrowns);
101 
102  vector<const DMCTrigger*> triggers;
103  loop->Get(triggers);
104 
105  // Loop over thrown tracks
106  for (unsigned int i=0; i < mcthrowns.size(); i++) {
107 
108  // keep tracks with at least 1 thrown particle greater than 1GeV/c
109  //const DMCThrown *mcthrown = mcthrowns[i];
110  //if (mcthrown->momentum().Mag() > 1.0)
111  // write_out = true;
112 
113  }
114 
115  // Loop over triggers
116  for(unsigned int i=0;i<triggers.size();i++){
117  const DMCTrigger *trigger = triggers[i];
118 
119  if (trigger->L1a_fired) {
120  write_out = true;
121  break;
122  }
123  if (trigger->L1b_fired) {
124  write_out = true;
125  break;
126  }
127  }
128 
129  //----------------------- Filter Code End -----------------------
130 
131  // If write_out flag is set, write this event to our output file
132  // otherwise, just flush the memory.
133  //
134  // WARNING: If a plugin is used with this program that tries to
135  // access objects in the s_HDDM_t structure (pretty much every
136  // plugin out there), it will likely seg. fault due to the
137  // memory already being freed!
138  if (write_out) {
139  *fout << *hddm;
140  Nevents_written++;
141  }
142  else {
143  hddm->clear();
144  }
145 
146  return NOERROR;
147 }
148 
149 //------------------------------------------------------------------
150 // fini -Close output file here
151 //------------------------------------------------------------------
152 jerror_t MyProcessor::fini(void)
153 {
154  if (fout) {
155  delete fout;
156  fout = 0;
157  }
158  if (ofs) {
159  ofs->close();
160  delete ofs;
161  cout << endl << "Closed HDDM output file" << endl;
162  }
163  cout << " " << Nevents_written << " events written to " << filename << endl;
164 
165  return NOERROR;
166 }
jerror_t init(void)
TString filename
bool L1b_fired
Definition: DMCTrigger.h:19
bool L1a_fired
Definition: DMCTrigger.h:18
jerror_t fini(void)
Called everytime run number changes, provided brun has been called.
bool HDDM_USE_COMPRESSION
bool HDDM_USE_INTEGRITY_CHECKS
jerror_t evnt(JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.