Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_danahddm.cc
Go to the documentation of this file.
1 // JEventProcessor_danahddm.cc
2 //
3 //
4 // JANA event processor plugin writes out hddm event to file
5 //
6 //
7 // David Lawrence, 7-May-2010
8 
9 #include <JANA/JApplication.h>
10 #include <HDDM/DEventSourceHDDM.h>
12 
14 
15 
16 // hddm output file name, use hddm:FILENAME configuration parameter to override
17 static string hddmFileName = "dana_events.hddm";
18 
19 
20 // mutex for serializing writing to file
21 static pthread_mutex_t hddmMutex = PTHREAD_MUTEX_INITIALIZER;
22 
23 // Make us a plugin
24 // for initializing plugins
25 extern "C" {
26  void InitPlugin(JApplication *app) {
27  InitJANAPlugin(app);
28  app->AddProcessor(new JEventProcessor_danahddm(), true);
29  }
30 } // "extern C"
31 
32 //-------------------------------
33 // Constructor
34 //-------------------------------
36 {
37 
38  jout << std::endl << " Default JEventProcessor_danahddm invoked"
39  << std::endl << std::endl;
40 
41  // Check for hddm:FILENAME output file name parameter
42  gPARMS->SetDefaultParameter("hddm:FILENAME",hddmFileName);
43  jout << std::endl << " hddm output file name is " << hddmFileName
44  << std::endl << std::endl;
45 
46  HDDM_USE_COMPRESSION = true;
47  gPARMS->SetDefaultParameter("HDDM:USE_COMPRESSION", HDDM_USE_COMPRESSION,
48  "Turn on/off compression of the output HDDM stream."
49  " Set to \"0\" to turn off (it's on by default)");
51  gPARMS->SetDefaultParameter("HDDM:USE_INTEGRITY_CHECKS",
53  "Turn on/off automatic integrity checking on the"
54  " output HDDM stream."
55  " Set to \"0\" to turn off (it's on by default)");
56  file = NULL;
57  fout = NULL;
58  Nevents_written = 0;
59 }
60 
61 //-------------------------------
62 // Destructor
63 //-------------------------------
65 {
66 }
67 
68 //-------------------------------
69 // init
70 //-------------------------------
72 {
73  return NOERROR;
74 }
75 
76 //-------------------------------
77 // brun
78 //-------------------------------
79 jerror_t JEventProcessor_danahddm::brun(JEventLoop *loop, int32_t runnumber)
80 {
81  // get write lock
82  pthread_mutex_lock(&hddmMutex);
83 
84  // If file is already open, don't reopen it. Just keep adding to it.
85  if (file)
86  {
87  pthread_mutex_unlock(&hddmMutex);
88  return NOERROR;
89  }
90 
91  // We wait until here to open the output so that we can check if the
92  // input is hddm. If it's not, tell the user and exit immediately
93  JEvent& event = loop->GetJEvent();
94  JEventSource *source = event.GetJEventSource();
95  DEventSourceHDDM *hddm_source = dynamic_cast<DEventSourceHDDM*>(source);
96  if (! hddm_source) {
97  std::cerr << " This program MUST be used with an HDDM file as input!" << std::endl;
98  exit(-1);
99  }
100 
101  // If we got here, it must be an HDDM source. Open a new file.
102  file = new std::ofstream(hddmFileName.c_str());
103  fout = new hddm_s::ostream(*file);
104  Nevents_written = 0;
105 
106  // enable on-the-fly bzip2 compression on output stream
107  if (HDDM_USE_COMPRESSION) {
108  jout << " Enabling bz2 compression of output HDDM file stream"
109  << std::endl;
110  fout->setCompression(hddm_s::k_bz2_compression);
111  }
112  else {
113  jout << " HDDM compression disabled" << std::endl;
114  }
115 
116  // enable a CRC data integrity check at the end of each event record
118  jout << " Enabling CRC data integrity check in output HDDM file stream"
119  << std::endl;
120  fout->setIntegrityChecks(hddm_s::k_crc32_integrity);
121  }
122  else {
123  jout << " HDDM integrity checks disabled" << std::endl;
124  }
125 
126  // unlock
127  pthread_mutex_unlock(&hddmMutex);
128 
129  return NOERROR;
130 }
131 
132 //-------------------------------
133 // evnt
134 //-------------------------------
135 jerror_t JEventProcessor_danahddm::evnt(JEventLoop *loop, uint64_t eventnumber)
136 {
137  JEvent& event = loop->GetJEvent();
138  JEventSource *source = event.GetJEventSource();
139  DEventSourceHDDM *hddm_source = dynamic_cast<DEventSourceHDDM*>(source);
140  if (! hddm_source) {
141  std::cerr << " This program MUST be used only with HDDM files as inputs!"
142  << std::endl;
143  exit(-1);
144  }
145  hddm_s::HDDM *hddm = (hddm_s::HDDM*)event.GetRef();
146  if (! hddm)
147  return NOERROR;
148 
149  // Delete any data in the reconView branch of the event.
150  hddm->getPhysicsEvent().deleteReconViews();
151 
152  // Fill in reconstructed banks, replacing any that are already there
153  hddm_s::ReconViewList revs = hddm->getPhysicsEvent().addReconViews();
154  Add_DTrackTimeBased(loop, revs.begin());
155 
156  // get write lock
157  pthread_mutex_lock(&hddmMutex);
158 
159  // Write event to file and update counter
160  *fout << *hddm;
161  Nevents_written++;
162 
163  // unlock
164  pthread_mutex_unlock(&hddmMutex);
165 
166  return NOERROR;
167 }
168 
169 //-------------------------------
170 // erun
171 //-------------------------------
173 {
174  return NOERROR;
175 }
176 
177 //-------------------------------
178 // fini
179 //-------------------------------
181 {
182  if (fout)
183  delete fout;
184  if (file) {
185  delete file;
186  std::cout << std::endl << "Closed HDDM file" << std::endl;
187  }
188  std::cout << " " << Nevents_written << " event written to "
189  << hddmFileName << std::endl;
190 
191  return NOERROR;
192 }
193 
194 //-------------------------------
195 // Add_DTrackTimeBased
196 //-------------------------------
198  hddm_s::ReconViewList::iterator riter)
199 {
200  // Get objects to write out
201  vector<const DTrackTimeBased*> tracktimebaseds;
202  loop->Get(tracktimebaseds);
203  if (tracktimebaseds.size() == 0)
204  return;
205 
206  // Allocate memory for all time based tracks
207  unsigned int ntbts = tracktimebaseds.size();
208  hddm_s::TracktimebasedList tbts = riter->addTracktimebaseds(ntbts);
209  for (unsigned int i=0; i < ntbts; i++) {
210  const DTrackTimeBased *tbt_dana = tracktimebaseds[i];
211  DVector3 pos = tbt_dana->position();
212  DVector3 mom = tbt_dana->momentum();
213 
214  tbts(i).setFOM(tbt_dana->FOM);
215  tbts(i).setCandidateid(tbt_dana->candidateid);
216  tbts(i).setTrackid(tbt_dana->trackid);
217  tbts(i).setId(tbt_dana->id);
218  tbts(i).setChisq(tbt_dana->chisq);
219  tbts(i).setNdof(tbt_dana->Ndof);
220 
221  hddm_s::MomentumList tmoms = tbts(i).addMomenta();
222  hddm_s::PropertiesList tpros = tbts(i).addPropertiesList();
223  hddm_s::OriginList torig = tbts(i).addOrigins();
224  hddm_s::ErrorMatrixList terrs = tbts(i).addErrorMatrixs();
225  hddm_s::TrackingErrorMatrixList tters =
226  tbts(i).addTrackingErrorMatrixs();
227 
228  tmoms().setE(tbt_dana->energy());
229  tmoms().setPx(mom.x());
230  tmoms().setPy(mom.y());
231  tmoms().setPz(mom.z());
232 
233  tpros().setCharge((int)tbt_dana->charge());
234  tpros().setMass(tbt_dana->mass());
235 
236  torig().setT(0.0);
237  torig().setVx(pos.x());
238  torig().setVy(pos.y());
239  torig().setVz(pos.z());
240 
241  string vals = DMatrixDSymToString(*(tbt_dana->errorMatrix()));
242  terrs().setNcols(7);
243  terrs().setNrows(7);
244  terrs().setType("DMatrixDSym");
245  terrs().setVals(vals.c_str());
246 
247  tters().setNcols(5);
248  tters().setNrows(5);
249  tters().setType("DMatrixDSym");
250  tters().setVals(DMatrixDSymToString(*(tbt_dana->TrackingErrorMatrix())));
251  }
252 }
253 
254 //-------------------------------
255 // DMatrixDSymToString
256 //-------------------------------
258 {
259  // Convert the given symmetric matrix into a single string that
260  // can be used in an HDDM file.
261 
262  stringstream ss;
263  for (int irow=0; irow<mat.GetNrows(); irow++) {
264  for (int icol=irow; icol<mat.GetNcols(); icol++) {
265  ss << mat[irow][icol] << " ";
266  }
267  }
268 
269  return ss.str();
270 }
void Add_DTrackTimeBased(JEventLoop *loop, hddm_s::ReconViewList::iterator riter)
float chisq
Chi-squared for the track (not chisq/dof!)
static pthread_mutex_t hddmMutex
string DMatrixDSymToString(const DMatrixDSym &mat)
jerror_t fini(void)
Called after last event of last event source has been processed.
TVector3 DVector3
Definition: DVector3.h:14
double energy(void) const
oid_t trackid
id of DTrackWireBased corresponding to this track
const DVector3 & position(void) const
jerror_t brun(JEventLoop *loop, int32_t runnumber)
Called everytime a new run number is detected.
TMatrixDSym DMatrixDSym
Definition: DMatrixDSym.h:13
static string hddmFileName
oid_t candidateid
id of DTrackCandidate corresponding to this track
jerror_t init(void)
Called once at program start.
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
InitPlugin_t InitPlugin
int Ndof
Number of degrees of freedom in the fit.
double charge(void) const
const DVector3 & momentum(void) const
shared_ptr< const TMatrixFSym > errorMatrix(void) const
jerror_t evnt(JEventLoop *loop, uint64_t eventnumber)
Called every event.
shared_ptr< const TMatrixFSym > TrackingErrorMatrix(void) const
Definition: DTrackingData.h:20
double mass(void) const