Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventWriterHDDM.cc
Go to the documentation of this file.
1 #include "DEventWriterHDDM.h"
2 
3 #include <DANA/DApplication.h>
4 #include <JANA/JCalibration.h>
5 
7 {
8  // must be read/used entirely in "HDDMWriter" lock
9  static int locNumEventWriterThreads = 0;
10  return locNumEventWriterThreads;
11 }
12 
13 map<string, pair<ofstream*, hddm_s::ostream*> >& DEventWriterHDDM::Get_HDDMOutputFilePointers(void) const
14 {
15  // must be read/used entirely in "HDDMWriter" lock
16  // cannot do individual file locks, because the map itself can be modified
17  static map<string, pair<ofstream*, hddm_s::ostream*> > locHDDMOutputFilePointers;
18  return locHDDMOutputFilePointers;
19 }
20 
21 DEventWriterHDDM::DEventWriterHDDM(JEventLoop* locEventLoop, string locOutputFileBaseName) : dOutputFileBaseName(locOutputFileBaseName)
22 {
23  japp->WriteLock("HDDMWriter");
24  {
26  }
27  japp->Unlock("HDDMWriter");
28 
29  HDDM_USE_COMPRESSION = true;
30  string locCompressionString = "Turn on/off compression of the output HDDM stream. Set to \"0\" to turn off (it's on by default)";
31  gPARMS->SetDefaultParameter("HDDM:USE_COMPRESSION", HDDM_USE_COMPRESSION, locCompressionString);
32 
34  string locIntegrityString = "Turn on/off automatic integrity checking on the output HDDM stream. Set to \"0\" to turn off (it's on by default)";
35  gPARMS->SetDefaultParameter("HDDM:USE_INTEGRITY_CHECKS", HDDM_USE_INTEGRITY_CHECKS, locIntegrityString);
36 
38  gPARMS->SetDefaultParameter("HDDM:DATAVERSIONSTRING", HDDM_DATA_VERSION_STRING, "");
39 
41  // if we can get the calibration context from the DANA interface, then save this as well
42  DApplication *dapp = dynamic_cast<DApplication*>(locEventLoop->GetJApplication());
43  if (dapp) {
44  JEvent& event = locEventLoop->GetJEvent();
45  JCalibration *jcalib = dapp->GetJCalibration(event.GetRunNumber());
46  if (jcalib) {
47  CCDB_CONTEXT_STRING = jcalib->GetContext();
48  }
49  }
50 
51  CDC_TAG = "Calib";
52  gPARMS->SetDefaultParameter("HDDMOUT:CDCTAG", CDC_TAG, "Tag (string) to use when selecting CDC hits to read out.");
53 
54  FDC_TAG = "";
55  gPARMS->SetDefaultParameter("HDDMOUT:FDCTAG", FDC_TAG, "Tag (string) to use when selecting FDC hits to read out.");
56 
57  TAGM_TAG = "Calib";
58  gPARMS->SetDefaultParameter("HDDMOUT:TAGMTAG", TAGM_TAG, "Tag (string) to use when selecting TAGM hits to read out.");
59 
60  TAGH_TAG = "Calib";
61  gPARMS->SetDefaultParameter("HDDMOUT:TAGHTAG", TAGH_TAG, "Tag (string) to use when selecting TAGH hits to read out.");
62 }
63 
64 bool DEventWriterHDDM::Write_HDDMEvent(JEventLoop* locEventLoop, string locOutputFileNameSubString) const
65 {
66 
67  vector<const DCDCHit*> CDCHits;
68  vector<const DTOFHit*> TOFHits;
69  vector<const DFCALHit*> FCALHits;
70  vector<const DSCHit*> SCHits;
71  vector<const DBCALDigiHit*> BCALDigiHits;
72  vector<const DBCALTDCDigiHit*> BCALTDCDigiHits;
73  vector<const DPSHit*> PSHits;
74  vector<const DPSCHit*> PSCHits;
75  vector<const DFDCHit*> FDCHits;
76  vector<const DTAGHHit*> TAGHHits;
77  vector<const DTAGMHit*> TAGMHits;
78  vector<const DTPOLHit*> TPOLHits;
79  vector<const DRFTime*> RFtimes;
80  vector<const DDIRCPmtHit*> DIRCPmtHits;
81 
82  locEventLoop->Get(CDCHits, CDC_TAG.c_str());
83  locEventLoop->Get(FDCHits, FDC_TAG.c_str());
84  locEventLoop->Get(TOFHits);
85  locEventLoop->Get(FCALHits);
86  locEventLoop->Get(BCALDigiHits);
87  locEventLoop->Get(BCALTDCDigiHits);
88  locEventLoop->Get(SCHits);
89  locEventLoop->Get(PSHits);
90  locEventLoop->Get(PSCHits);
91  locEventLoop->Get(TAGHHits, TAGH_TAG.c_str());
92  locEventLoop->Get(TAGMHits, TAGM_TAG.c_str());
93  locEventLoop->Get(TPOLHits);
94  locEventLoop->Get(RFtimes);
95  locEventLoop->Get(DIRCPmtHits);
96 
97  if(CDCHits.size()== uint(0) && TOFHits.size()==uint(0) && FCALHits.size()==uint(0) && BCALDigiHits.size()==uint(0) && BCALTDCDigiHits.size()==uint(0) && SCHits.size()==uint(0) && PSHits.size()==uint(0) && PSCHits.size()==uint(0) && FDCHits.size()==uint(0) && TAGHHits.size()==uint(0) && TAGMHits.size()==uint(0) && TPOLHits.size()==uint(0) && RFtimes.size()==uint(0) && DIRCPmtHits.size()==uint(0))
98  {
99  return false;
100  }
101 
102  //create an HDDM record to store the Events' hits and set the Event/Run Number
103  hddm_s::HDDM* record = new hddm_s::HDDM;
104  record->addPhysicsEvents();
105  hddm_s::PhysicsEvent* pe = &record->getPhysicsEvent();
106  pe->setEventNo(locEventLoop->GetJEvent().GetEventNumber());
107  pe->setRunNo(locEventLoop->GetJEvent().GetRunNumber());
108  //add a HitView which is necessary to create and save all of the data
109  pe->addHitViews();
110  hddm_s::HitView* hitv = &pe->getHitView();
111 
112 
113  //Because HDDM groups hits by sub-unit of each detector we loop through the hits in each sub-dector and need to see if a sub-unit exists
114  //in hddm so as to avoid duplication of sub-units in HDDM
115  //====================================TPOL================================================
116  for(uint i=0;i<TPOLHits.size();++i)
117  {
118  if(i==0)//if the TPOL has hits then on the first hit we need to build the TPOL
119  {
120  hitv->addTripletPolarimeters();
121  }
122 
123  bool found=false;
124 
125  //get sectors. We get the iterator outside the for loop so when the loop is broken the iterator is at the sector that
126  //has been hit, making adding hits to it trivial
127  hddm_s::TpolSectorList* TPOL_SectorList = &hitv->getTripletPolarimeter().getTpolSectors();
128  hddm_s::TpolSectorList::iterator sectorIterator = TPOL_SectorList->begin();
129 
130  //loop over the unique sectors already created in the HDDM framework
131  for(sectorIterator = TPOL_SectorList->begin(); sectorIterator != TPOL_SectorList->end(); sectorIterator++)
132  {
133  //look to see if the same sub-unit of the sub-detector is hit again. At worst this TPOLHit is new and all sectors already hit must be looped over
134  if(int(TPOLHits[i]->ring) == sectorIterator->getRing() && TPOLHits[i]->sector == sectorIterator->getSector() )
135  {
136  found=true;//we found it!
137  break;//you can stop looping now to shorten the time and leave the iterator at the right spot
138  }
139  }
140 
141  //this part hasn't been hit yet. We need to create it to hold the hit(s)
142  if(found==false)
143  {
144  hitv->getTripletPolarimeter().addTpolSectors();//currently the iterator is at "end", which is exactly where we want to add it
145  sectorIterator = TPOL_SectorList->end()-1;//this might be a little redundant but the newly made sector is at end-1 so we set the iterator here
146  sectorIterator->setRing(TPOLHits[i]->ring);//set the unique identifiers for the new sector
147  sectorIterator->setSector(TPOLHits[i]->sector);
148  }
149  //Now that we either created the sub-unit or found the sub-unit we add a hit to it
150  sectorIterator->addTpolHits();//either the iterator was at the sector that this hit belongs to or it is at the newly created sector
151  hddm_s::TpolHitList* TPOL_HitList = &sectorIterator->getTpolHits();
152  hddm_s::TpolHitList::iterator TPOL_HitIterator = TPOL_HitList->end()-1;//as above we ensure we are at the last element
153  TPOL_HitIterator->setT(TPOLHits[i]->t);//and set the the proper values for the newly formed hit
154  TPOL_HitIterator->setDE(TPOLHits[i]->dE);
155 
156  }
157 
158 
159  //========================================TAGGER===========================================================
160 
161  if(TAGHHits.size() != uint(0) || TAGMHits.size() != uint(0) )//If either the TAGH or TAGM have hits create the Tagger
162  {
163  hitv->addTaggers();
164  }
165 
166  //hodoscope
167  for(uint i=0; i<TAGHHits.size(); ++i)
168  {
169  bool found=false;
170  //look to see if the same sub-unit of the sub-detector is hit again
171  hddm_s::HodoChannelList* TAGH_ChannelList = &hitv->getTagger().getHodoChannels();
172  hddm_s::HodoChannelList::iterator TAGH_ChannelIterator = TAGH_ChannelList->begin();
173 
174  //look to see if the same sub-unit of the sub-detector is hit again
175  for(TAGH_ChannelIterator=TAGH_ChannelList->begin(); TAGH_ChannelIterator != TAGH_ChannelList->end(); TAGH_ChannelIterator++)
176  {
177 
178  if(int(TAGHHits[i]->counter_id) == TAGH_ChannelIterator->getCounterId() )
179  {
180  found=true;
181  break;
182  }
183  }
184 
185  if(found==false)
186  {
187 
188  hitv->getTagger().addHodoChannels();
189  TAGH_ChannelIterator = TAGH_ChannelList->end()-1;
190  TAGH_ChannelIterator->setCounterId(TAGHHits[i]->counter_id);
191  TAGH_ChannelIterator->setE(TAGHHits[i]->E);
192  }
193 
194  //Add hits
195  TAGH_ChannelIterator->addTaggerHits();
196  hddm_s::TaggerHitList* TAGGER_HitList = &TAGH_ChannelIterator->getTaggerHits();
197  hddm_s::TaggerHitList::iterator TAGGER_HitIterator = TAGGER_HitList->end()-1;
198  TAGGER_HitIterator->setNpe(TAGHHits[i]->npe_fadc);
199  TAGGER_HitIterator->setT(TAGHHits[i]->t);
200  TAGGER_HitIterator->setTADC(TAGHHits[i]->time_fadc);
201 
202  }
203 
204  //microscope
205  for(uint i=0;i<TAGMHits.size();++i)
206  {
207  bool found=false;
208 
209  //look to see if the same sub-unit of the sub-detector is hit again
210  hddm_s::MicroChannelList* TAGM_ChannelList= &hitv->getTagger().getMicroChannels();
211  hddm_s::MicroChannelList::iterator TAGM_ChannelIterator=TAGM_ChannelList->begin();
212 
213  for(TAGM_ChannelIterator=TAGM_ChannelList->begin(); TAGM_ChannelIterator != TAGM_ChannelList->end(); TAGM_ChannelIterator++)
214  {
215  if(int(TAGMHits[i]->column) == TAGM_ChannelIterator->getColumn() && int(TAGMHits[i]->row) == TAGM_ChannelIterator->getRow() )
216  {
217  found = true;
218  break;
219  }
220  }
221 
222  if(found == false)
223  {
224  hitv->getTagger().addMicroChannels();
225  if(TAGMHits[i]->column == 0 && TAGMHits[i]->row == 0)
226  {
227  std::cout<<"I found one in the TAGM!"<<std::endl;
228  }
229  TAGM_ChannelIterator = TAGM_ChannelList->end()-1;
230  TAGM_ChannelIterator->setColumn(TAGMHits[i]->column);
231  TAGM_ChannelIterator->setRow(TAGMHits[i]->row);
232  TAGM_ChannelIterator->setE(TAGMHits[i]->E);
233  }
234 
235  TAGM_ChannelIterator->addTaggerHits();
236  hddm_s::TaggerHitList* TAGGER_HitList = &TAGM_ChannelIterator->getTaggerHits();
237  hddm_s::TaggerHitList::iterator TAGGER_HitIterator = TAGGER_HitList->end()-1;
238  TAGGER_HitIterator->setNpe(TAGMHits[i]->npix_fadc);
239  TAGGER_HitIterator->setT(TAGMHits[i]->t);
240  TAGGER_HitIterator->setTADC(TAGMHits[i]->time_fadc);
241 
242  }
243 
244 
245 
246  //====================================FDC==========================================
247 
248  for(uint i=0;i<FDCHits.size();++i)
249  {
250  //if there is at least 1 FDC hit we need an FDC
251  if(i==0)
252  {
253  hitv->addForwardDCs();
254  }
255  //look for the chamber by layer/module (before searching for same strip/wire we must see if the chamber exists already
256  bool foundChamber=false;
257  hddm_s::FdcChamberList* FDC_ChamberList = &hitv->getForwardDC().getFdcChambers();
258  hddm_s::FdcChamberList::iterator FDC_ChamberIterator = FDC_ChamberList->begin();
259 
260  for(FDC_ChamberIterator = FDC_ChamberList->begin(); FDC_ChamberIterator != FDC_ChamberList->end(); FDC_ChamberIterator++)
261  {
262  //it is important to note that in HDDM the Layer that is saved represents the clocking of the chamber. 1=0 degrees 2=60 3=120
263  //in evio Layer is 1-3 where 1/3 are the cathode strips and 2 is the anode wire plane
264  //the clocking can be calculated by accessing the glayer and using a little modular arithmatic
265  if(((FDCHits[i]->gLayer-1)%3)+1 == FDC_ChamberIterator->getLayer() && FDCHits[i]->module == FDC_ChamberIterator->getModule() )
266  {
267  foundChamber = true;
268  break;
269  }
270  }
271 
272  if(foundChamber == false)
273  {
274 
275  hitv->getForwardDC().addFdcChambers();
276  FDC_ChamberIterator = FDC_ChamberList->end()-1;
277  FDC_ChamberIterator->setLayer(((FDCHits[i]->gLayer-1)%3)+1); //This will be dumped as the clocking and is not expected to match the Layer dumped from evio
278  FDC_ChamberIterator->setModule(FDCHits[i]->module);
279 
280  }
281  //now that we found the chamber (or created a new one) we need to see if it was a cathode or anode hit and see if it too is new
282  if(FDCHits[i]->type == DFDCHit::AnodeWire)
283  {
284 
285  //search either the wires or strips depending on the above
286  bool found=false;
287 
288  hddm_s::FdcAnodeWireList* FDC_AnodeWireList= &FDC_ChamberIterator->getFdcAnodeWires();
289  hddm_s::FdcAnodeWireList::iterator FDC_AnodeWireIterator = FDC_AnodeWireList->begin();
290 
291  for(FDC_AnodeWireIterator = FDC_AnodeWireList->begin(); FDC_AnodeWireIterator != FDC_AnodeWireList->end(); FDC_AnodeWireIterator++)
292  {
293  if(int(FDCHits[i]->element) == FDC_AnodeWireIterator->getWire() )
294  {
295  found=true;
296  break;
297  }
298  }
299 
300  if(found == false)
301  {
302  FDC_ChamberIterator->addFdcAnodeWires();
303  FDC_AnodeWireIterator = FDC_AnodeWireList->end()-1;
304  FDC_AnodeWireIterator->setWire(FDCHits[i]->element);
305  }
306 
307  FDC_AnodeWireIterator->addFdcAnodeHits();
308  hddm_s::FdcAnodeHitList* FDC_AnodeWireHitList = &FDC_AnodeWireIterator->getFdcAnodeHits();
309  hddm_s::FdcAnodeHitList::iterator FDC_AnodeWireHitIterator = FDC_AnodeWireHitList->end()-1;
310  FDC_AnodeWireHitIterator->setT(FDCHits[i]->t);
311  FDC_AnodeWireHitIterator->setDE(0);//FDC does not have fADCs on the anodes
312 
313  }
314  else//this is a cathode hit. Agnostic about being half length or not
315  {
316  bool found=false;
317 
318  hddm_s::FdcCathodeStripList* FDC_CathodeStripList = &FDC_ChamberIterator->getFdcCathodeStrips();
319  hddm_s::FdcCathodeStripList::iterator FDC_CathodeStripIterator = FDC_CathodeStripList->begin();
320  //look in the chamber to see if the cathode has already been hit
321  for(FDC_CathodeStripIterator = FDC_CathodeStripList->begin(); FDC_CathodeStripIterator != FDC_CathodeStripList->end(); FDC_CathodeStripIterator++)
322  {
323  if(int(FDCHits[i]->element) == FDC_CathodeStripIterator->getStrip() && FDCHits[i]->plane == FDC_CathodeStripIterator->getPlane() )
324  {
325  found = true;
326  break;
327  }
328  }
329 
330  if(found == false)
331  {
332 
333  FDC_ChamberIterator->addFdcCathodeStrips();
334  FDC_CathodeStripIterator=FDC_CathodeStripList->end()-1;
335  FDC_CathodeStripIterator->setStrip(FDCHits[i]->element);
336  FDC_CathodeStripIterator->setPlane(FDCHits[i]->plane);
337 
338  }
339 
340  FDC_CathodeStripIterator->addFdcCathodeHits();
341  hddm_s::FdcCathodeHitList* FDC_CathodeStripHitList = &FDC_CathodeStripIterator->getFdcCathodeHits();
342  hddm_s::FdcCathodeHitList::iterator FDC_CathodeStripHitIterator = FDC_CathodeStripHitList->end()-1;
343  FDC_CathodeStripHitIterator->setT(FDCHits[i]->t);
344  FDC_CathodeStripHitIterator->setQ(FDCHits[i]->q);
345  FDC_CathodeStripHitIterator->addFdcDigihits();
346  FDC_CathodeStripHitIterator->getFdcDigihits().begin()->setPeakAmp(FDCHits[i]->pulse_height);
347 
348  }
349 
350  }
351 
352 
353 
354  //===============================================PS=============================================
355  //in hddm the PS is in two parts PSFine and PSCoarse
356  for(uint i=0; i<PSHits.size(); ++i)//Do the fine hits first
357  {
358  if(i == 0)
359  {
360  hitv->addPairSpectrometerFines(); //only create PS Fine Tiles if there is a hit in the
361  }
362  bool found=false;
363 
364  hddm_s::PsTileList* PS_TileList = &hitv->getPairSpectrometerFine().getPsTiles();
365  hddm_s::PsTileList::iterator PS_TileIterator = PS_TileList->begin();
366 
367  //check for the same PS tile is hit
368  for(PS_TileIterator = PS_TileList->begin(); PS_TileIterator != PS_TileList->end(); PS_TileIterator++)
369  {
370  if(int(PSHits[i]->arm) == PS_TileIterator->getArm() && PSHits[i]->column == PS_TileIterator->getColumn() )
371  {
372  found=true;
373  break;
374  }
375  }
376 
377  if(found == false)
378  {
379 
380  hitv->getPairSpectrometerFine().addPsTiles();
381  PS_TileIterator = PS_TileList->end()-1;
382  PS_TileIterator->setArm(PSHits[i]->arm);
383  PS_TileIterator->setColumn(PSHits[i]->column);
384  }
385 
386  PS_TileIterator->addPsHits();
387  hddm_s::PsHitList* PS_HitList = &PS_TileIterator->getPsHits();
388  hddm_s::PsHitList::iterator PS_HitIterator = PS_HitList->end()-1;
389  PS_HitIterator->setT(PSHits[i]->t);
390  PS_HitIterator->setDE(PSHits[i]->E);
391 
392  }
393  //--------------------COARSE------------------------------------------
394  for(uint i=0;i<PSCHits.size();++i)//repeat for the coarse hits
395  {
396  if(i==0)
397  {
398  hitv->addPairSpectrometerCoarses();
399  }
400 
401  bool found=false;
402 
403  hddm_s::PscPaddleList* PS_PaddleList = &hitv->getPairSpectrometerCoarse().getPscPaddles();
404  hddm_s::PscPaddleList::iterator PS_PaddleIterator = PS_PaddleList->begin();
405 
406  for(PS_PaddleIterator = PS_PaddleList->begin(); PS_PaddleIterator != PS_PaddleList->end(); PS_PaddleIterator++)
407  {
408  if(int(PSCHits[i]->arm) == PS_PaddleIterator->getArm() && PSCHits[i]->module == PS_PaddleIterator->getModule() )
409  {
410  found = true;
411  break;
412  }
413  }
414 
415  if(found == false)
416  {
417  hitv->getPairSpectrometerCoarse().addPscPaddles();
418  PS_PaddleIterator = PS_PaddleList->end()-1;
419  PS_PaddleIterator->setArm(PSCHits[i]->arm);
420  PS_PaddleIterator->setModule(PSCHits[i]->module);
421  }
422 
423  PS_PaddleIterator->addPscHits();
424  hddm_s::PscHitList* PSC_HitList = &PS_PaddleIterator->getPscHits();
425  hddm_s::PscHitList::iterator pschitit = PSC_HitList->end()-1;
426  pschitit->setT(PSCHits[i]->t);
427 
428  }
429 
430 
431 
432  //================================================Start Counter======================================
433 
434  for(uint i=0; i<SCHits.size(); ++i)
435  {
436  if(i == 0)
437  {
438  hitv->addStartCntrs();//if we have a hit add a start counter
439  }
440  bool found=false;
441 
442  hddm_s::StcPaddleList* SC_CounterList = &hitv->getStartCntr().getStcPaddles();
443  hddm_s::StcPaddleList::iterator SC_CounterIterator = SC_CounterList->begin();
444 
445  //see if the sector has already been hit
446  for(SC_CounterIterator = SC_CounterList->begin(); SC_CounterIterator != SC_CounterList->end(); SC_CounterIterator++)
447  {
448  if(SCHits[i]->sector == SC_CounterIterator->getSector() )
449  {
450  found = true;
451  break;
452  }
453  }
454 
455  if(found == false)
456  {
457  hitv->getStartCntr().addStcPaddles();
458  SC_CounterIterator=SC_CounterList->end()-1;
459  SC_CounterIterator->setSector(SCHits[i]->sector);
460  }
461 
462  SC_CounterIterator->addStcHits();
463  hddm_s::StcHitList* schitl = &SC_CounterIterator->getStcHits();
464  hddm_s::StcHitList::iterator schitit = schitl->end()-1;
465  schitit->setT(SCHits[i]->t);
466  schitit->setDE(SCHits[i]->dE);
467  schitit->addStcDigihits();
468  schitit->getStcDigihits().begin()->setPeakAmp(SCHits[i]->pulse_height);
469  }
470 
471 
472  //============================================BCAL=========================================
473 
474  //The BCAL is unique in that it needs the DigiHits put in HDDM ADC/TDC done separately
475  if(BCALDigiHits.size() != uint(0) || BCALTDCDigiHits.size() != uint(0) )
476  {
477  hitv->addBarrelEMcals(); //still only need one BCAL if we have a hit of some kind
478  }
479  //-------------------------------ADC--------------------------
480  for(uint i=0; i<BCALDigiHits.size(); ++i)
481  {
482  bool found = false;
483 
484  hddm_s::BcalCellList* BCAL_CellList= &hitv->getBarrelEMcal().getBcalCells();
485  hddm_s::BcalCellList::iterator BCAL_CellIterator=BCAL_CellList->begin();
486 
487  //note: these cells being searched are the same ones searched for TDCs
488  for(BCAL_CellIterator = BCAL_CellList->begin(); BCAL_CellIterator != BCAL_CellList->end(); BCAL_CellIterator++)
489  {
490  if(BCALDigiHits[i]->sector == BCAL_CellIterator->getSector() && BCALDigiHits[i]->layer == BCAL_CellIterator->getLayer() && BCALDigiHits[i]->module == BCAL_CellIterator->getModule() )
491  {
492  found = true;
493  break;
494  }
495  }
496 
497  if(found == false)
498  {
499  hitv->getBarrelEMcal().addBcalCells();
500  BCAL_CellIterator = BCAL_CellList->end()-1;
501  BCAL_CellIterator->setLayer(BCALDigiHits[i]->layer);
502  BCAL_CellIterator->setSector(BCALDigiHits[i]->sector);
503  BCAL_CellIterator->setModule(BCALDigiHits[i]->module);
504  }
505 
506  BCAL_CellIterator->addBcalfADCDigiHits();
507  hddm_s::BcalfADCDigiHitList* BCAL_FADCDigiHitList = &BCAL_CellIterator->getBcalfADCDigiHits();
508  hddm_s::BcalfADCDigiHitList::iterator BCAL_FADCDigiHitIterator = BCAL_FADCDigiHitList->end()-1;
509  BCAL_FADCDigiHitIterator->setEnd(BCALDigiHits[i]->end);
510  BCAL_FADCDigiHitIterator->setPulse_time(BCALDigiHits[i]->pulse_time);
511  BCAL_FADCDigiHitIterator->setPulse_integral(BCALDigiHits[i]->pulse_integral);
512  BCAL_FADCDigiHitIterator->addBcalfADCPeaks();
513  BCAL_FADCDigiHitIterator->getBcalfADCPeaks().begin()->setPeakAmp(BCALDigiHits[i]->pulse_peak);
514  }
515 
516  //------------------------TDC-----------------------------
517  for(uint i=0; i<BCALTDCDigiHits.size(); ++i)
518  {
519  bool found = false;
520 
521  hddm_s::BcalCellList* BCAL_CellList = &hitv->getBarrelEMcal().getBcalCells();
522  hddm_s::BcalCellList::iterator BCAL_CellIterator = BCAL_CellList->begin();
523 
524  for(BCAL_CellIterator = BCAL_CellList->begin(); BCAL_CellIterator != BCAL_CellList->end(); BCAL_CellIterator++)
525  {
526  if(BCALTDCDigiHits[i]->sector == uint(BCAL_CellIterator->getSector()) && BCALTDCDigiHits[i]->layer == uint(BCAL_CellIterator->getLayer()) && BCALTDCDigiHits[i]->module == uint(BCAL_CellIterator->getModule()) )
527  {
528  found = true;
529  break;
530  }
531  }
532 
533  if(found == false)
534  {
535  hitv->getBarrelEMcal().addBcalCells();
536  BCAL_CellIterator = BCAL_CellList->end()-1;
537  BCAL_CellIterator->setLayer(BCALTDCDigiHits[i]->layer);
538  BCAL_CellIterator->setSector(BCALTDCDigiHits[i]->sector);
539  BCAL_CellIterator->setModule(BCALTDCDigiHits[i]->module);
540 
541  }
542 
543  BCAL_CellIterator->addBcalTDCDigiHits();
544  hddm_s::BcalTDCDigiHitList* BCAL_TDCDigiHitList = &BCAL_CellIterator->getBcalTDCDigiHits();
545  hddm_s::BcalTDCDigiHitList::iterator BCAL_TDCDigiHitIterator = BCAL_TDCDigiHitList->end()-1;
546  BCAL_TDCDigiHitIterator->setEnd(BCALTDCDigiHits[i]->end);
547  BCAL_TDCDigiHitIterator->setTime(BCALTDCDigiHits[i]->time);
548  }
549 
550 
551  //========================================FCAL=========================================================
552 
553  for(uint i=0; i<FCALHits.size(); ++i)
554  {
555  if(i == 0)
556  {
557  hitv->addForwardEMcals();
558  }
559  bool found = false;
560  //FCAL only has one hit per block per event so we need not search
561  hddm_s::FcalBlockList* FCAL_BlockList = &hitv->getForwardEMcal().getFcalBlocks();
562  hddm_s::FcalBlockList::iterator FCAL_BlockIterator = FCAL_BlockList->begin();
563 
564  for(FCAL_BlockIterator = FCAL_BlockList->begin(); FCAL_BlockIterator != FCAL_BlockList->end(); FCAL_BlockIterator++)
565  {
566  if(FCALHits[i]->row==FCAL_BlockIterator->getRow() && FCALHits[i]->column==FCAL_BlockIterator->getColumn())
567  {
568  found=true;
569  break;
570  }
571  }
572 
573  if(found==false)
574  {
575  hitv->getForwardEMcal().addFcalBlocks();
576  FCAL_BlockIterator=FCAL_BlockList->end()-1;
577  FCAL_BlockIterator->setColumn(FCALHits[i]->column);
578  FCAL_BlockIterator->setRow(FCALHits[i]->row);
579  }
580 
581 
582  FCAL_BlockIterator->addFcalHits();
583  hddm_s::FcalHitList* FCAL_HitList = &FCAL_BlockIterator->getFcalHits();
584  hddm_s::FcalHitList::iterator FCAL_HitIterator = FCAL_HitList->end()-1;
585  FCAL_HitIterator->setT(FCALHits[i]->t);
586  FCAL_HitIterator->setE(FCALHits[i]->E);
587  FCAL_HitIterator->addFcalDigihits();
588  FCAL_HitIterator->getFcalDigihits().begin()->setIntegralOverPeak(FCALHits[i]->intOverPeak);
589  }
590 
591 
592  //=============================================TOF=====================================================
593 
594  for(uint i=0; i<TOFHits.size(); ++i)
595  {
596 
597  if(i == 0)
598  {
599  hitv->addForwardTOFs();//if we have a hit add the TOF
600  }
601 
602  bool found = false;
603  //gotta look for the same plane/bar
604  hddm_s::FtofCounterList* TOF_CounterList = &hitv->getForwardTOF().getFtofCounters();
605  hddm_s::FtofCounterList::iterator TOF_CounterIterator = TOF_CounterList->begin();
606 
607  for(TOF_CounterIterator = TOF_CounterList->begin(); TOF_CounterIterator != TOF_CounterList->end(); TOF_CounterIterator++)
608  {
609  if(TOFHits[i]->bar==TOF_CounterIterator->getBar() && TOFHits[i]->plane==TOF_CounterIterator->getPlane())
610  {
611  found=true;
612  break;
613  }
614  }
615 
616  if(found==false)
617  {
618  hitv->getForwardTOF().addFtofCounters();
619  TOF_CounterIterator=TOF_CounterList->end()-1;
620  TOF_CounterIterator->setPlane(TOFHits[i]->plane);
621  TOF_CounterIterator->setBar(TOFHits[i]->bar);
622 
623  }
624 
625  TOF_CounterIterator->addFtofHits();
626  hddm_s::FtofHitList* ftofhitl=&TOF_CounterIterator->getFtofHits();
627  hddm_s::FtofHitList::iterator ftofhitit=ftofhitl->end()-1;
628  ftofhitit->setEnd(TOFHits[i]->end);
629  ftofhitit->setT(TOFHits[i]->t);//walk corrected time
630  ftofhitit->setDE(TOFHits[i]->dE);
631  ftofhitit->addFtofDigihits();
632  ftofhitit->getFtofDigihits().begin()->setPeakAmp(TOFHits[i]->Amp);
633  }
634 
635 
636  //============================CDC=============================================
637 
638  for(uint i=0; i<CDCHits.size(); ++i)
639  {
640  if(i == 0)
641  {
642  hitv->addCentralDCs(); //if we have a hit then add the CDC
643  }
644  //CDC only has one hit per block per event so we need not search
645  hitv->getCentralDC().addCdcStraws();
646 
647  hddm_s::CdcStrawList* CDC_StrawList = &hitv->getCentralDC().getCdcStraws();
648  hddm_s::CdcStrawList::iterator CDC_StrawIterator = CDC_StrawList->end()-1;
649  CDC_StrawIterator->setRing(CDCHits[i]->ring);
650  CDC_StrawIterator->setStraw(CDCHits[i]->straw);
651 
652  CDC_StrawIterator->addCdcStrawHits();
653  hddm_s::CdcStrawHitList* strawhitl = &CDC_StrawIterator->getCdcStrawHits();
654  hddm_s::CdcStrawHitList::iterator cdcstrawhitit = strawhitl->end()-1;
655  cdcstrawhitit->setQ(CDCHits[i]->q);
656  cdcstrawhitit->setT(CDCHits[i]->t);
657  cdcstrawhitit->addCdcDigihits();
658  cdcstrawhitit->getCdcDigihits().begin()->setPeakAmp(CDCHits[i]->amp);
659  cdcstrawhitit->addCdcHitQFs();
660  cdcstrawhitit->getCdcHitQFs().begin()->setQF(CDCHits[i]->QF);
661  }
662 
663  //========================================RFtime=======================================================
664 
665  for(uint i=0; i<RFtimes.size(); ++i)
666  {
667  hddm_s::RFtimeList nextRF = hitv->addRFtimes();
668  nextRF(0).setJtag(RFtimes[i]->GetTag());
669  nextRF(0).setTsync(RFtimes[i]->dTime);
670  }
671 
672  std::vector<std::string> RFtags = {"TOF", "FDC", "PSC", "TAGH"};
673  hddm_s::RFtimeList mainRF = hitv->getRFtimes();
674  if(mainRF.size() > 0)
675  {
676  for(uint it=0; it < RFtags.size(); ++it)
677  {
678  vector<const DRFTime*> RFsubsys;
679  locEventLoop->Get(RFsubsys, RFtags[it].c_str());
680  for(uint i=0; i < RFsubsys.size(); ++i)
681  {
682  hddm_s::RFsubsystemList nextRF = mainRF(0).addRFsubsystems();
683  nextRF(0).setJtag(RFsubsys[i]->GetTag());
684  nextRF(0).setTsync(RFsubsys[i]->dTime);
685  }
686  }
687  }
688 
689  //========================================DIRC=======================================================
690 
691  for(uint i=0; i<DIRCPmtHits.size(); ++i) {
692  if(i == 0) {
693  hitv->addDIRCs(); //if we have a hit then add the DIRC
694  }
695 
696 
697  hddm_s::DircPmtHitList *pmtHits = &hitv->getDIRC().getDircPmtHits();
698  hddm_s::DircPmtHitList::iterator iter;
699 
700  hitv->getDIRC().addDircPmtHits();
701  iter=pmtHits->end()-1;
702  iter->setCh(DIRCPmtHits[i]->ch);
703  iter->setT(DIRCPmtHits[i]->t);
704  }
705 
706  //*fout << *record; //stream the new record into the file
707 
708  // write the resulting record to the output stream
709  string locOutputFileName = Get_OutputFileName(locOutputFileNameSubString);
710  bool locWriteStatus = Write_HDDMEvent(locOutputFileName, *record);
711  delete record;
712 
713  CDCHits.clear();
714  TOFHits.clear();
715  FCALHits.clear();
716  SCHits.clear();
717  BCALDigiHits.clear();
718  BCALTDCDigiHits.clear();
719  PSHits.clear();
720  PSCHits.clear();
721  FDCHits.clear();
722  TAGHHits.clear();
723  TAGMHits.clear();
724  TPOLHits.clear();
725  RFtimes.clear();
726  DIRCPmtHits.clear();
727 
728  return locWriteStatus;
729 }
730 
731 string DEventWriterHDDM::Get_OutputFileName(string locOutputFileNameSubString) const
732 {
733  string locOutputFileName = dOutputFileBaseName;
734  if (locOutputFileNameSubString != "")
735  locOutputFileName += string("_") + locOutputFileNameSubString;
736  return (locOutputFileName + string(".hddm"));
737 }
738 
739 bool DEventWriterHDDM::Write_HDDMEvent(string locOutputFileName, hddm_s::HDDM& locRecord) const
740 {
741  japp->WriteLock("HDDMWriter");
742  {
743  //check to see if the HDDM file is open
744  if(Get_HDDMOutputFilePointers().find(locOutputFileName) != Get_HDDMOutputFilePointers().end())
745  {
746  //open: get pointer, write event
747  hddm_s::ostream* locOutputHDDMFileStream = Get_HDDMOutputFilePointers()[locOutputFileName].second;
748  japp->Unlock("HDDMWriter");
749  *(locOutputHDDMFileStream) << locRecord;
750  return true;
751  }
752 
753  //not open: open it
754  pair<ofstream*, hddm_s::ostream*> locHDDMFilePointers(NULL, NULL);
755  locHDDMFilePointers.first = new ofstream(locOutputFileName.c_str());
756  if(!locHDDMFilePointers.first->is_open())
757  {
758  //failed to open
759  delete locHDDMFilePointers.first;
760  japp->Unlock("HDDMWriter");
761  return false;
762  }
763  locHDDMFilePointers.second = new hddm_s::ostream(*locHDDMFilePointers.first);
764 
765  // enable on-the-fly bzip2 compression on output stream
767  {
768  jout << " Enabling bz2 compression of output HDDM file stream" << std::endl;
769  locHDDMFilePointers.second->setCompression(hddm_s::k_bz2_compression);
770  }
771  else
772  jout << " HDDM compression disabled" << std::endl;
773 
774  // enable a CRC data integrity check at the end of each event record
776  {
777  jout << " Enabling CRC data integrity check in output HDDM file stream" << std::endl;
778  locHDDMFilePointers.second->setIntegrityChecks(hddm_s::k_crc32_integrity);
779  }
780  else
781  jout << " HDDM integrity checks disabled" << std::endl;
782 
783 
784  //write the event
785  *(locHDDMFilePointers.second) << locRecord;
786 
787  //store the stream pointers
788  Get_HDDMOutputFilePointers()[locOutputFileName] = locHDDMFilePointers;
789  }
790  japp->Unlock("HDDMWriter");
791 
792  return true;
793 }
794 
796 {
797  japp->WriteLock("HDDMWriter");
798  {
800  if(Get_NumEventWriterThreads() > 0)
801  {
802  japp->Unlock("HDDMWriter");
803  return; //not the last thread writing to HDDM files
804  }
805 
806  //last thread writing to HDDM files: close all files and free all memory
807  map<string, pair<ofstream*, hddm_s::ostream*> >::iterator locIterator;
808  for(locIterator = Get_HDDMOutputFilePointers().begin(); locIterator != Get_HDDMOutputFilePointers().end(); ++locIterator)
809  {
810  string locOutputFileName = locIterator->first;
811  if (locIterator->second.second != NULL)
812  delete locIterator->second.second;
813  if (locIterator->second.first != NULL)
814  delete locIterator->second.first;
815  std::cout << "Closed HDDM file " << locOutputFileName << std::endl;
816  }
817  Get_HDDMOutputFilePointers().clear();
818  }
819  japp->Unlock("HDDMWriter");
820 }
821 
822 int32_t DEventWriterHDDM::Convert_UnsignedIntToSigned(uint32_t locUnsignedInt) const
823 {
824  //Convert uint32_t to int32_t
825  //Scheme:
826  //If bit 32 is zero, then the int32_t is the same as the uint32_t: Positive or zero
827  //If bit 32 is one, and at least one other bit is 1, then the int32_t is -1 * uint32_t (after stripping the top bit)
828  //If bit 32 is one, and all other bits are zero, then the int32_t is the minimum int: -(2^31)
829  if((locUnsignedInt & 0x80000000) == 0)
830  return int32_t(locUnsignedInt); //bit 32 is zero: positive or zero
831 
832  //bit 32 is 1. see if there is another bit set
833  int32_t locTopBitStripped = int32_t(locUnsignedInt & uint32_t(0x7FFFFFFF)); //strip the top bit
834  if(locTopBitStripped == 0)
835  return numeric_limits<int32_t>::min(); //no other bit is set: minimum int
836  return -1*locTopBitStripped; //return the negative
837 }
DApplication * dapp
Int_t layer
char string[256]
int & Get_NumEventWriterThreads(void) const
string HDDM_DATA_VERSION_STRING
JApplication * japp
bool Write_HDDMEvent(JEventLoop *locEventLoop, string locOutputFileNameSubString) const
int32_t Convert_UnsignedIntToSigned(uint32_t locUnsignedInt) const
DEventWriterHDDM(JEventLoop *locEventLoop, string locOutputFileBaseName)
map< string, pair< ofstream *, hddm_s::ostream * > > & Get_HDDMOutputFilePointers(void) const
string Get_OutputFileName(string locOutputFileNameSubString) const