-
Notifications
You must be signed in to change notification settings - Fork 8
/
DEventSourceHDDM.cc
3288 lines (2850 loc) · 113 KB
/
DEventSourceHDDM.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id: DEventSourceHDDM.cc 19023 2015-07-14 20:23:27Z beattite $
//
// Author: David Lawrence June 24, 2004
//
// changes: Wed Jun 20 17:08:13 EDT 2007 B. Zihlmann
// modify TOF section to add several new variables incuding the
// GEANT particle type to the Truth hits and the hit and track-hit
// list.
//
// Oct 3, 2012 Yi Qiang: add functions for Cherenkov RICH detector
// Oct 11, 2012 Yi Qiang: complete functions for Cherenkov detector
// Oct 8, 2013 Yi Qiang: added dedicated object for RICH Truth Hit
// July 5, 2014 R.T.Jones: changed over from c to c++ API for hddm
// June 22, 2015 J. Stevens: changed RICH -> DIRC and remove CERE
// May 7, 2017 R. Dzhygadlo: added DDIRCTruthPmtHit DDIRCTruthBarHit
// Oct 20, 2017 A. Somov: Added fields for the DPSHit/DPSCHit
// Jan 16, 2024 A. Somov: Added functions for ECAL
//
// DEventSourceHDDM methods
//
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
#include <JANA/JFactory_base.h>
#include <JANA/JEventLoop.h>
#include <JANA/JEvent.h>
#include <DANA/DStatusBits.h>
#include <JANA/JGeometryXML.h>
#include "BCAL/DBCALGeometry.h"
#include "PAIR_SPECTROMETER/DPSGeometry.h"
#include <DVector2.h>
#include <DEventSourceHDDM.h>
#include <FDC/DFDCGeometry.h>
#include <FCAL/DFCALGeometry.h>
#include <FCAL/DFCALHit.h>
#include <CCAL/DCCALGeometry.h>
#include <CCAL/DCCALHit.h>
#include <ECAL/DECALGeometry.h>
#include <ECAL/DECALHit.h>
//------------------------------------------------------------------
// Binary predicate used to sort hits
//------------------------------------------------------------------
class MCTrackHitSort{
public:
bool operator()(DMCTrackHit* const &thit1,
DMCTrackHit* const &thit2) const {
return thit1->z < thit2->z;
}
};
bool MCTrackHitSort_C(DMCTrackHit* const &thit1,
DMCTrackHit* const &thit2) {
return thit1->z < thit2->z;
}
//----------------
// Constructor
//----------------
DEventSourceHDDM::DEventSourceHDDM(const char* source_name)
: JEventSource(source_name)
{
/// Constructor for DEventSourceHDDM object
ifs = new ifstream(source_name);
ifs->get();
ifs->unget();
if (ifs->rdbuf()->in_avail() > 30) {
class nonstd_streambuf: public std::streambuf {
public: char *pub_gptr() {return gptr();}
};
void *buf = (void*)ifs->rdbuf();
std::string head(((nonstd_streambuf*)buf)->pub_gptr(), 30);
std::string expected = " class=\"s\" ";
std::string also_supported = " class=\"mc_s\" ";
if (head.find(expected) == head.npos &&
head.find(also_supported) == head.npos)
{
std::string msg("Unexpected header found in input HDDM stream: ");
throw std::runtime_error(msg + head + source_name);
}
}
fin = new hddm_s::istream(*ifs);
initialized = false;
dapp = NULL;
bfield = NULL;
geom = NULL;
dRunNumber = -1;
if( (!gPARMS->Exists("JANA_CALIB_CONTEXT")) && (getenv("JANA_CALIB_CONTEXT")==NULL) ){
cout << "============================================================" << endl;
cout << " WARNING: JANA_CALIB_CONTEXT not set. " << endl;
cout << "You are reading from an HDDM file which is most likely" << endl;
cout << "MC data. In most cases, you will want to set this parameter" << endl;
cout << "to get proper reconstruction." << endl;
cout << "(usually something like \"variation=mc\")" << endl;
cout << "============================================================" << endl;
}
}
//----------------
// Destructor
//----------------
DEventSourceHDDM::~DEventSourceHDDM()
{
if (fin)
delete fin;
if (ifs)
delete ifs;
}
//----------------
// GetEvent
//----------------
jerror_t DEventSourceHDDM::GetEvent(JEvent &event)
{
/// Implementation of JEventSource virtual function
if (!fin)
return EVENT_SOURCE_NOT_OPEN;
// Each open HDDM file takes up about 1M of memory so it's
// worthwhile to close it as soon as we can.
else if (!ifs->good()) {
delete fin;
fin = NULL;
delete ifs;
ifs = NULL;
return NO_MORE_EVENTS_IN_SOURCE;
}
hddm_s::HDDM *record = new hddm_s::HDDM();
while (record->getPhysicsEvents().size() == 0) {
if (! (*fin >> *record)) {
delete fin;
fin = NULL;
delete ifs;
ifs = NULL;
return NO_MORE_EVENTS_IN_SOURCE;
}
}
++Nevents_read;
int event_number = -1;
int run_number = -1;
if(!record->getPhysicsEvents().empty()) {
// Get event/run numbers from HDDM
hddm_s::PhysicsEvent &pe = record->getPhysicsEvent(0);
event_number = pe.getEventNo();
run_number = pe.getRunNo();
}
// Copy the reference info into the JEvent object
event.SetJEventSource(this);
event.SetEventNumber(event_number);
event.SetRunNumber(run_number);
event.SetRef(record);
event.SetStatusBit(kSTATUS_HDDM);
event.SetStatusBit(kSTATUS_FROM_FILE);
event.SetStatusBit(kSTATUS_PHYSICS_EVENT);
return NOERROR;
}
//----------------
// FreeEvent
//----------------
void DEventSourceHDDM::FreeEvent(JEvent &event)
{
hddm_s::HDDM *record = (hddm_s::HDDM*)event.GetRef();
delete record;
}
//----------------
// GetObjects
//----------------
jerror_t DEventSourceHDDM::GetObjects(JEvent &event, JFactory_base *factory)
{
/// This gets called through the virtual method of the
/// JEventSource base class. It creates the objects of the type
/// on which factory is based. It uses the hddm_s::HDDM* object
/// kept in the ref field of the JEvent object passed.
// We must have a factory to hold the data
if (!factory)
throw RESOURCE_UNAVAILABLE;
// HDDM doesn't exactly support tagged factories, but the tag
// can be used to direct filling of the correct factory.
string tag = (factory->Tag()==NULL)? "" : factory->Tag();
// The ref field of the JEvent is just the HDDM object pointer.
hddm_s::HDDM *record = (hddm_s::HDDM*)event.GetRef();
if (!record)
throw RESOURCE_UNAVAILABLE;
// Get pointer to the B-field object and Geometry object
JEventLoop *loop = event.GetJEventLoop();
if (initialized == false && loop) {
initialized = true;
dRunNumber = event.GetRunNumber();
dapp = dynamic_cast<DApplication*>(loop->GetJApplication());
if (dapp) {
jcalib = dapp->GetJCalibration(event.GetRunNumber());
// Make sure jcalib is set
if (!jcalib) {
_DBG_ << "ERROR - no jcalib set!" <<endl;
return RESOURCE_UNAVAILABLE;
}
// Get constants and do basic check on number of elements
vector< map<string, float> > tvals;
if(jcalib->Get("FDC/strip_calib", tvals))
throw JException("Could not load CCDB table: FDC/strip_calib");
if (tvals.size() != 192) {
_DBG_ << "ERROR - strip calibration vectors are not the right size!"
<< endl;
return VALUE_OUT_OF_RANGE;
}
map<string,float>::iterator iter;
for (iter=tvals[0].begin(); iter!=tvals[0].end(); iter++) {
// Copy values into tables. We preserve the order since
// that is how it was originally done in hitFDC.c
for (unsigned int i=0; i<tvals.size(); i++) {
map<string, float> &row = tvals[i];
uscale[i]=row["qru"];
vscale[i]=row["qrv"];
}
}
}
// load BCAL geometry
vector<const DBCALGeometry *> BCALGeomVec;
loop->Get(BCALGeomVec);
if(BCALGeomVec.size() == 0)
throw JException("Could not load DBCALGeometry object!");
dBCALGeom = BCALGeomVec[0];
// load PS geometry
vector<const DPSGeometry*> psGeomVect;
loop->Get(psGeomVect);
if (psGeomVect.size() < 1)
return OBJECT_NOT_AVAILABLE;
psGeom = psGeomVect[0];
}
// Warning: This class is not completely thread-safe and can fail if running
// running in multithreaded mode over files with events from multiple runs
// It is expected that simulated data will rarely contain events from multiple
// runs, as this is an intermediate format in the simulation chain, so for
// now we just insert a sanity check, and push the problem to the future
if(dRunNumber != event.GetRunNumber()) {
jerr << endl
<< "WARNING: DEventSourceHDDM cannot currently handle HDDM files containing" << endl
<< "events with multiple runs! If you encounter this error message," << endl
<< "please contact the GlueX Offline Software Group: halld-offline@jlab.org" << endl
<< endl;
exit(-1);
}
//Get target center
//multiple reader threads can access this object: need lock
bool locNewRunNumber = false;
unsigned int locRunNumber = event.GetRunNumber();
LockRead();
{
locNewRunNumber = (dTargetCenterZMap.find(locRunNumber) == dTargetCenterZMap.end());
}
UnlockRead();
if(locNewRunNumber)
{
DApplication* dapp = dynamic_cast<DApplication*>(loop->GetJApplication());
DGeometry* locGeometry = dapp->GetDGeometry(loop->GetJEvent().GetRunNumber());
double locTargetCenterZ = 0.0;
locGeometry->GetTargetZ(locTargetCenterZ);
JGeometryXML *jgeom = dynamic_cast<JGeometryXML *>(locGeometry);
hddm_s::GeometryList geolist = record->getGeometrys();
if (jgeom != 0 && geolist.size() > 0) {
std::string md5sim = geolist(0).getMd5simulation();
std::string md5smear = geolist(0).getMd5smear();
std::string md5recon = jgeom->GetChecksum();
geolist(0).setMd5reconstruction(md5recon);
if (md5sim != md5smear) {
jerr << std::endl
<< "WARNING: simulation geometry checksum does not match"
<< " that shown for the mcsmear step."
<< std::endl;
}
else if (md5sim != md5recon) {
jerr << endl
<< "WARNING: simulation geometry checksum does not match"
<< " the geometry being used for reconstruction."
<< std::endl;
}
}
vector<double> locBeamPeriodVector;
if(loop->GetCalib("PHOTON_BEAM/RF/beam_period", locBeamPeriodVector))
throw runtime_error("Could not load CCDB table: PHOTON_BEAM/RF/beam_period");
double locBeamBunchPeriod = locBeamPeriodVector[0];
LockRead();
{
dTargetCenterZMap[locRunNumber] = locTargetCenterZ;
dBeamBunchPeriodMap[locRunNumber] = locBeamBunchPeriod;
}
UnlockRead();
}
// Get name of data class we're trying to extract
string dataClassName = factory->GetDataClassName();
if (dataClassName == "DPSHit")
return Extract_DPSHit(record,
dynamic_cast<JFactory<DPSHit>*>(factory), tag);
if (dataClassName == "DPSTruthHit")
return Extract_DPSTruthHit(record,
dynamic_cast<JFactory<DPSTruthHit>*>(factory), tag);
if (dataClassName == "DPSCHit")
return Extract_DPSCHit(record,
dynamic_cast<JFactory<DPSCHit>*>(factory), tag);
if (dataClassName == "DPSCTruthHit")
return Extract_DPSCTruthHit(record,
dynamic_cast<JFactory<DPSCTruthHit>*>(factory), tag);
if (dataClassName == "DRFTime")
return Extract_DRFTime(record,
dynamic_cast<JFactory<DRFTime>*>(factory), loop);
if (dataClassName == "DTAGMHit")
return Extract_DTAGMHit(record,
dynamic_cast<JFactory<DTAGMHit>*>(factory), tag);
if (dataClassName == "DTAGHHit")
return Extract_DTAGHHit(record,
dynamic_cast<JFactory<DTAGHHit>*>(factory), tag);
if (dataClassName == "DMCTrackHit")
return Extract_DMCTrackHit(record,
dynamic_cast<JFactory<DMCTrackHit>*>(factory), tag);
if (dataClassName == "DMCReaction")
return Extract_DMCReaction(record,
dynamic_cast<JFactory<DMCReaction>*>(factory), tag, loop);
if (dataClassName == "DMCThrown")
return Extract_DMCThrown(record,
dynamic_cast<JFactory<DMCThrown>*>(factory), tag);
if (dataClassName == "DBCALTruthShower")
return Extract_DBCALTruthShower(record,
dynamic_cast<JFactory<DBCALTruthShower>*>(factory), tag);
if (dataClassName == "DBCALSiPMSpectrum")
return Extract_DBCALSiPMSpectrum(record,
dynamic_cast<JFactory<DBCALSiPMSpectrum>*>(factory), tag);
if (dataClassName == "DBCALTruthCell")
return Extract_DBCALTruthCell(record,
dynamic_cast<JFactory<DBCALTruthCell>*>(factory), tag);
if (dataClassName == "DBCALSiPMHit")
return Extract_DBCALSiPMHit(record,
dynamic_cast<JFactory<DBCALSiPMHit>*>(factory), tag);
if (dataClassName == "DBCALDigiHit")
return Extract_DBCALDigiHit(record,
dynamic_cast<JFactory<DBCALDigiHit>*>(factory), tag);
if (dataClassName == "DBCALIncidentParticle")
return Extract_DBCALIncidentParticle(record,
dynamic_cast<JFactory<DBCALIncidentParticle>*>(factory), tag);
if (dataClassName == "DBCALTDCDigiHit")
return Extract_DBCALTDCDigiHit(record,
dynamic_cast<JFactory<DBCALTDCDigiHit>*>(factory), tag);
if (dataClassName == "DCDCHit")
return Extract_DCDCHit(loop, record,
dynamic_cast<JFactory<DCDCHit>*>(factory) , tag);
if (dataClassName == "DFDCHit")
return Extract_DFDCHit(record,
dynamic_cast<JFactory<DFDCHit>*>(factory), tag);
if (dataClassName == "DFCALTruthShower")
return Extract_DFCALTruthShower(record,
dynamic_cast<JFactory<DFCALTruthShower>*>(factory), tag);
if (dataClassName == "DFCALHit")
return Extract_DFCALHit(record,
dynamic_cast<JFactory<DFCALHit>*>(factory), tag,
event.GetJEventLoop());
if (dataClassName == "DECALTruthShower")
return Extract_DECALTruthShower(record,
dynamic_cast<JFactory<DECALTruthShower>*>(factory), tag);
if (dataClassName == "DECALHit")
return Extract_DECALHit(record,
dynamic_cast<JFactory<DECALHit>*>(factory), tag,
event.GetJEventLoop());
if (dataClassName == "DCCALTruthShower")
return Extract_DCCALTruthShower(record,
dynamic_cast<JFactory<DCCALTruthShower>*>(factory), tag);
if (dataClassName == "DCCALHit")
return Extract_DCCALHit(record,
dynamic_cast<JFactory<DCCALHit>*>(factory), tag,
event.GetJEventLoop());
if (dataClassName == "DMCTrajectoryPoint" && tag == "")
return Extract_DMCTrajectoryPoint(record,
dynamic_cast<JFactory<DMCTrajectoryPoint>*>(factory), tag);
if (dataClassName == "DTOFTruth")
return Extract_DTOFTruth(record,
dynamic_cast<JFactory<DTOFTruth>*>(factory), tag);
// TOF is a special case: TWO factories are needed at the same time
// DTOFHit and DTOFHitMC
if (dataClassName == "DTOFHit") {
JFactory_base* factory2 = loop->GetFactory("DTOFHitMC", tag.c_str());
return Extract_DTOFHit(record,
dynamic_cast<JFactory<DTOFHit>*>(factory),
dynamic_cast<JFactory<DTOFHitMC>*>(factory2), tag);
}
if (dataClassName == "DTOFHitMC") {
JFactory_base* factory2 = loop->GetFactory("DTOFHit", tag.c_str());
return Extract_DTOFHit(record,
dynamic_cast<JFactory<DTOFHit>*>(factory2),
dynamic_cast<JFactory<DTOFHitMC>*>(factory), tag);
}
if (dataClassName == "DSCHit")
return Extract_DSCHit(record,
dynamic_cast<JFactory<DSCHit>*>(factory), tag);
if (dataClassName == "DSCTruthHit")
return Extract_DSCTruthHit(record,
dynamic_cast<JFactory<DSCTruthHit>*>(factory), tag);
if (dataClassName == "DFMWPCTruthHit")
return Extract_DFMWPCTruthHit(record,
dynamic_cast<JFactory<DFMWPCTruthHit>*>(factory), tag);
if (dataClassName == "DFMWPCTruth")
return Extract_DFMWPCTruth(record,
dynamic_cast<JFactory<DFMWPCTruth>*>(factory), tag);
if (dataClassName == "DFMWPCHit")
return Extract_DFMWPCHit(record,
dynamic_cast<JFactory<DFMWPCHit>*>(factory), tag);
if (dataClassName == "DCTOFTruth")
return Extract_DCTOFTruth(record,
dynamic_cast<JFactory<DCTOFTruth>*>(factory), tag);
if (dataClassName == "DCTOFHit")
return Extract_DCTOFHit(record,
dynamic_cast<JFactory<DCTOFHit>*>(factory), tag);
if (dataClassName == "DDIRCTruthBarHit")
return Extract_DDIRCTruthBarHit(record,
dynamic_cast<JFactory<DDIRCTruthBarHit>*>(factory), tag);
if (dataClassName == "DDIRCTruthPmtHit")
return Extract_DDIRCTruthPmtHit(record,
dynamic_cast<JFactory<DDIRCTruthPmtHit>*>(factory), tag);
if (dataClassName == "DDIRCPmtHit")
return Extract_DDIRCPmtHit(record,
dynamic_cast<JFactory<DDIRCPmtHit>*>(factory), tag, event.GetJEventLoop());
// extract CereTruth and CereRichHit hits, yqiang Oct 3, 2012
// removed CereTruth (merged into MCThrown), added CereHit, yqiang Oct 10 2012
if (dataClassName == "DCereHit")
return Extract_DCereHit(record,
dynamic_cast<JFactory<DCereHit>*>(factory), tag);
if (dataClassName == "DTPOLHit")
return Extract_DTPOLHit(record,
dynamic_cast<JFactory<DTPOLHit>*>(factory), tag);
if (dataClassName == "DTPOLTruthHit")
return Extract_DTPOLTruthHit(record,
dynamic_cast<JFactory<DTPOLTruthHit>*>(factory), tag);
return OBJECT_NOT_AVAILABLE;
}
//------------------
// Extract_DRFTime
//------------------
jerror_t DEventSourceHDDM::Extract_DRFTime(hddm_s::HDDM *record,
JFactory<DRFTime> *factory, JEventLoop* locEventLoop)
{
if (factory==NULL)
return OBJECT_NOT_AVAILABLE;
string tag = (factory->Tag())? factory->Tag() : "";
vector<DRFTime*> locRFTimes;
// loop over RF-time records
const hddm_s::RFtimeList &rftimes = record->getRFtimes();
hddm_s::RFtimeList::iterator iter;
for (iter = rftimes.begin(); iter != rftimes.end(); ++iter)
{
if (iter->getJtag() != tag)
continue;
DRFTime *locRFTime = new DRFTime;
locRFTime->dTime = iter->getTsync();
locRFTime->dTimeVariance = 0.0; //SET ME!!
locRFTimes.push_back(locRFTime);
}
if(!locRFTimes.empty())
{
//found in the file, copy into factory and return
factory->CopyTo(locRFTimes);
return NOERROR;
}
//Not found in the file, so either:
//Experimental data & it's missing: bail
//MC data: generate it
vector<const DBeamPhoton*> locMCGENPhotons;
locEventLoop->Get(locMCGENPhotons, "MCGEN");
if(locMCGENPhotons.empty())
return OBJECT_NOT_AVAILABLE; //Experimental data & it's missing: bail
//Is MC data. Either:
//No tag: return t = 0.0, but true t is 0.0 +/- n*locBeamBunchPeriod: must select the correct beam bunch
//TRUTH tag: get exact t from DBeamPhoton tag MCGEN
if(tag == "TRUTH")
{
DRFTime *locRFTime = new DRFTime;
locRFTime->dTime = locMCGENPhotons[0]->time();
locRFTime->dTimeVariance = 0.0;
locRFTimes.push_back(locRFTime);
}
else
{
double locBeamBunchPeriod = 0.0;
int locRunNumber = locEventLoop->GetJEvent().GetRunNumber();
LockRead();
{
locBeamBunchPeriod = dBeamBunchPeriodMap[locRunNumber];
}
UnlockRead();
//start with true RF time, increment/decrement by multiples of locBeamBunchPeriod ns until closest to 0
double locTime = locMCGENPhotons[0]->time();
int locNumRFBuckets = int(locTime/locBeamBunchPeriod);
locTime -= double(locNumRFBuckets)*locBeamBunchPeriod;
while(locTime > 0.5*locBeamBunchPeriod)
locTime -= locBeamBunchPeriod;
while(locTime < -0.5*locBeamBunchPeriod)
locTime += locBeamBunchPeriod;
DRFTime *locRFTime = new DRFTime;
locRFTime->dTime = locTime;
locRFTime->dTimeVariance = 0.0;
locRFTimes.push_back(locRFTime);
}
// Copy into factories
factory->CopyTo(locRFTimes);
return NOERROR;
}
//------------------
// Extract_DMCTrackHit
//------------------
jerror_t DEventSourceHDDM::Extract_DMCTrackHit(hddm_s::HDDM *record,
JFactory<DMCTrackHit> *factory, string tag)
{
/// Copies the data from the given hddm_s structure. This is called
/// from JEventSourceHDDM::GetObjects. If factory is NULL, this
/// returns OBJECT_NOT_AVAILABLE immediately.
if (factory == NULL)
return OBJECT_NOT_AVAILABLE;
if (tag != "")
return OBJECT_NOT_AVAILABLE;
// The following routines will create DMCTrackHit objects and add them
// to data.
vector<DMCTrackHit*> data;
GetCDCTruthHits(record, data);
GetFDCTruthHits(record, data);
GetBCALTruthHits(record, data);
GetTOFTruthHits(record, data);
GetCherenkovTruthHits(record, data);
GetFCALTruthHits(record, data);
GetECALTruthHits(record, data);
GetSCTruthHits(record, data);
// It has happened that some CDC hits have "nan" for the drift time
// in a peculiar event Alex Somov came across. This ultimately caused
// a seg. fault in MCTrackHitSort_C. I hate doing this since it
// is treating the symptom rather than the cause, but nonetheless,
// it patches up the problem for now until there is time to revisit
// it later.
for (unsigned int i=0; i < data.size(); i++)
if (!isfinite(data[i]->z))
data[i]->z = -1000.0;
// sort hits by z
sort(data.begin(), data.end(), MCTrackHitSort_C);
// Some systems will use negative phis. Force them all to
// be in the 0 to 2pi range
for (unsigned int i=0; i < data.size(); i++) {
DMCTrackHit *mctrackhit = data[i];
if (mctrackhit->phi < 0.0)
mctrackhit->phi += 2.0*M_PI;
}
// Copy into factory
factory->CopyTo(data);
return NOERROR;
}
//-------------------
// GetCDCTruthHits
//-------------------
jerror_t DEventSourceHDDM::GetCDCTruthHits(hddm_s::HDDM *record,
vector<DMCTrackHit*>& data)
{
const hddm_s::CdcTruthPointList &points = record->getCdcTruthPoints();
hddm_s::CdcTruthPointList::iterator iter;
for (iter = points.begin(); iter != points.end(); ++iter) {
DMCTrackHit *mctrackhit = new DMCTrackHit;
mctrackhit->r = iter->getR();
mctrackhit->phi = iter->getPhi();
mctrackhit->z = iter->getZ();
mctrackhit->track = iter->getTrack();
mctrackhit->primary = iter->getPrimary();
mctrackhit->ptype = iter->getPtype();
mctrackhit->system = SYS_CDC;
const hddm_s::TrackIDList &ids = iter->getTrackIDs();
mctrackhit->itrack = (ids.size())? ids.begin()->getItrack() : 0;
data.push_back(mctrackhit);
}
return NOERROR;
}
//-------------------
// GetFDCTruthHits
//-------------------
jerror_t DEventSourceHDDM::GetFDCTruthHits(hddm_s::HDDM *record,
vector<DMCTrackHit*>& data)
{
const hddm_s::FdcTruthPointList &points = record->getFdcTruthPoints();
hddm_s::FdcTruthPointList::iterator iter;
for (iter = points.begin(); iter != points.end(); ++iter) {
float x = iter->getX();
float y = iter->getY();
DMCTrackHit *mctrackhit = new DMCTrackHit;
mctrackhit->r = sqrt(x*x + y*y);
mctrackhit->phi = atan2(y,x);
mctrackhit->z = iter->getZ();
mctrackhit->track = iter->getTrack();
mctrackhit->primary = iter->getPrimary();
mctrackhit->ptype = iter->getPtype();
mctrackhit->system = SYS_FDC;
const hddm_s::TrackIDList &ids = iter->getTrackIDs();
mctrackhit->itrack = (ids.size())? ids.begin()->getItrack() : 0;
data.push_back(mctrackhit);
}
return NOERROR;
}
//-------------------
// GetBCALTruthHits
//-------------------
jerror_t DEventSourceHDDM::GetBCALTruthHits(hddm_s::HDDM *record,
vector<DMCTrackHit*>& data)
{
const hddm_s::BcalTruthShowerList &showers = record->getBcalTruthShowers();
hddm_s::BcalTruthShowerList::iterator iter;
for (iter = showers.begin(); iter != showers.end(); ++iter) {
DMCTrackHit *mctrackhit = new DMCTrackHit;
mctrackhit->r = iter->getR();
mctrackhit->phi = iter->getPhi();
mctrackhit->z = iter->getZ();
mctrackhit->track = iter->getTrack();
mctrackhit->primary = iter->getPrimary();
mctrackhit->ptype = iter->getPtype();
mctrackhit->system = SYS_BCAL;
const hddm_s::TrackIDList &ids = iter->getTrackIDs();
mctrackhit->itrack = (ids.size())? ids.begin()->getItrack() : 0;
data.push_back(mctrackhit);
}
return NOERROR;
}
//-------------------
// GetTOFTruthHits
//-------------------
jerror_t DEventSourceHDDM::GetTOFTruthHits(hddm_s::HDDM *record,
vector<DMCTrackHit*>& data)
{
const hddm_s::FtofTruthPointList &points = record->getFtofTruthPoints();
hddm_s::FtofTruthPointList::iterator iter;
for (iter = points.begin(); iter != points.end(); ++iter) {
float x = iter->getX();
float y = iter->getY();
DMCTrackHit *mctrackhit = new DMCTrackHit;
mctrackhit->r = sqrt(x*x + y*y);
mctrackhit->phi = atan2(y,x);
mctrackhit->z = iter->getZ();
mctrackhit->track = iter->getTrack();
mctrackhit->primary = iter->getPrimary();
mctrackhit->ptype = iter->getPtype(); // save GEANT particle type
mctrackhit->system = SYS_TOF;
const hddm_s::TrackIDList &ids = iter->getTrackIDs();
mctrackhit->itrack = (ids.size())? ids.begin()->getItrack() : 0;
data.push_back(mctrackhit);
}
return NOERROR;
}
//-------------------
// GetCherenkovTruthHits
// modified by yqiang, Oct 10 2012
//-------------------
jerror_t DEventSourceHDDM::GetCherenkovTruthHits(hddm_s::HDDM *record,
vector<DMCTrackHit*>& data)
{
const hddm_s::CereTruthPointList &points = record->getCereTruthPoints();
hddm_s::CereTruthPointList::iterator iter;
for (iter = points.begin(); iter != points.end(); ++iter) {
float x = iter->getX();
float y = iter->getY();
DMCTrackHit *mctrackhit = new DMCTrackHit;
mctrackhit->r = sqrt(x*x + y*y);
mctrackhit->phi = atan2(y,x);
mctrackhit->z = iter->getZ();
mctrackhit->track = iter->getTrack();
mctrackhit->primary = iter->getPrimary();
mctrackhit->ptype = iter->getPtype(); // save GEANT particle typ()e
mctrackhit->system = SYS_CHERENKOV;
const hddm_s::TrackIDList &ids = iter->getTrackIDs();
mctrackhit->itrack = (ids.size())? ids.begin()->getItrack() : 0;
data.push_back(mctrackhit);
}
return NOERROR;
}
//-------------------
// GetFCALTruthHits
//-------------------
jerror_t DEventSourceHDDM::GetFCALTruthHits(hddm_s::HDDM *record,
vector<DMCTrackHit*>& data)
{
const hddm_s::FcalTruthShowerList &points = record->getFcalTruthShowers();
hddm_s::FcalTruthShowerList::iterator iter;
for (iter = points.begin(); iter != points.end(); ++iter) {
float x = iter->getX();
float y = iter->getY();
DMCTrackHit *mctrackhit = new DMCTrackHit;
mctrackhit->r = sqrt(x*x + y*y);
mctrackhit->phi = atan2(y,x);
mctrackhit->z = iter->getZ();
mctrackhit->track = iter->getTrack();
mctrackhit->primary = iter->getPrimary();
mctrackhit->ptype = iter->getPtype();
mctrackhit->system = SYS_FCAL;
const hddm_s::TrackIDList &ids = iter->getTrackIDs();
mctrackhit->itrack = (ids.size())? ids.begin()->getItrack() : 0;
data.push_back(mctrackhit);
}
return NOERROR;
}
//-------------------
// GetECALTruthHits
//-------------------
jerror_t DEventSourceHDDM::GetECALTruthHits(hddm_s::HDDM *record,
vector<DMCTrackHit*>& data)
{
const hddm_s::EcalTruthShowerList &points = record->getEcalTruthShowers();
hddm_s::EcalTruthShowerList::iterator iter;
for (iter = points.begin(); iter != points.end(); ++iter) {
float x = iter->getX();
float y = iter->getY();
DMCTrackHit *mctrackhit = new DMCTrackHit;
mctrackhit->r = sqrt(x*x + y*y);
mctrackhit->phi = atan2(y,x);
mctrackhit->z = iter->getZ();
mctrackhit->track = iter->getTrack();
mctrackhit->primary = iter->getPrimary();
mctrackhit->ptype = iter->getPtype();
mctrackhit->system = SYS_ECAL;
const hddm_s::TrackIDList &ids = iter->getTrackIDs();
mctrackhit->itrack = (ids.size())? ids.begin()->getItrack() : 0;
data.push_back(mctrackhit);
}
return NOERROR;
}
//-------------------
// GetCCALTruthHits
//-------------------
jerror_t DEventSourceHDDM::GetCCALTruthHits(hddm_s::HDDM *record,
vector<DMCTrackHit*>& data)
{
const hddm_s::CcalTruthShowerList &points = record->getCcalTruthShowers();
hddm_s::CcalTruthShowerList::iterator iter;
for (iter = points.begin(); iter != points.end(); ++iter) {
float x = iter->getX();
float y = iter->getY();
DMCTrackHit *mctrackhit = new DMCTrackHit;
mctrackhit->r = sqrt(x*x + y*y);
mctrackhit->phi = atan2(y,x);
mctrackhit->z = iter->getZ();
mctrackhit->track = iter->getTrack();
mctrackhit->primary = iter->getPrimary();
mctrackhit->ptype = iter->getPtype();
mctrackhit->system = SYS_CCAL;
const hddm_s::TrackIDList &ids = iter->getTrackIDs();
mctrackhit->itrack = (ids.size())? ids.begin()->getItrack() : 0;
data.push_back(mctrackhit);
}
return NOERROR;
}
//-------------------
// GetSCTruthHits
//-------------------
jerror_t DEventSourceHDDM::GetSCTruthHits(hddm_s::HDDM *record,
vector<DMCTrackHit*>& data)
{
const hddm_s::StcTruthPointList &points = record->getStcTruthPoints();
hddm_s::StcTruthPointList::iterator iter;
for (iter = points.begin(); iter != points.end(); ++iter) {
DMCTrackHit *mctrackhit = new DMCTrackHit;
mctrackhit->r = iter->getR();
mctrackhit->phi = iter->getPhi();
mctrackhit->z = iter->getZ();
mctrackhit->track = iter->getTrack();
mctrackhit->primary = iter->getPrimary();
mctrackhit->ptype = iter->getPtype(); // save GEANT particle type
mctrackhit->system = SYS_START;
const hddm_s::TrackIDList &ids = iter->getTrackIDs();
mctrackhit->itrack = (ids.size())? ids.begin()->getItrack() : 0;
data.push_back(mctrackhit);
}
return NOERROR;
}
//------------------
// Extract_DBCALSiPMHit
//------------------
jerror_t DEventSourceHDDM::Extract_DBCALSiPMHit(hddm_s::HDDM *record,
JFactory<DBCALSiPMHit> *factory, string tag)
{
/// Copies the data from the given hddm_s structure. This is called
/// from JEventSourceHDDM::GetObjects. If factory is NULL, this
/// returns OBJECT_NOT_AVAILABLE immediately.
if (factory == NULL)
return OBJECT_NOT_AVAILABLE;
if (tag != "")
return OBJECT_NOT_AVAILABLE;
vector<DBCALSiPMHit*> data;
const hddm_s::BcalSiPMUpHitList &uphits = record->getBcalSiPMUpHits();
hddm_s::BcalSiPMUpHitList::iterator uiter;
for (uiter = uphits.begin(); uiter != uphits.end(); ++uiter) {
DBCALSiPMHit *response = new DBCALSiPMHit;
response->module = uiter->getModule();
response->layer = uiter->getLayer();
response->sector = uiter->getSector();
response->E = uiter->getE();
response->t = uiter->getT();
response->end = DBCALGeometry::kUpstream;
response->cellId = dBCALGeom->cellId(uiter->getModule(),
uiter->getLayer(),
uiter->getSector());
data.push_back(response);
}
const hddm_s::BcalSiPMDownHitList &downhits = record->getBcalSiPMDownHits();
hddm_s::BcalSiPMDownHitList::iterator diter;
for (diter = downhits.begin(); diter != downhits.end(); ++diter) {
DBCALSiPMHit *response = new DBCALSiPMHit;
response->module = diter->getModule();
response->layer = diter->getLayer();
response->sector = diter->getSector();
response->E = diter->getE();
response->t = diter->getT();
response->end = DBCALGeometry::kDownstream;
response->cellId = dBCALGeom->cellId(diter->getModule(),
diter->getLayer(),
diter->getSector());
data.push_back(response);
}
// Copy into factory
factory->CopyTo(data);
return NOERROR;
}
//------------------
// Extract_DBCALDigiHit
//------------------
jerror_t DEventSourceHDDM::Extract_DBCALDigiHit(hddm_s::HDDM *record,
JFactory<DBCALDigiHit> *factory, string tag)
{
/// Copies the data from the given hddm_s structure. This is called
/// from JEventSourceHDDM::GetObjects. If factory is NULL, this
/// returns OBJECT_NOT_AVAILABLE immediately.
if (factory == NULL)
return OBJECT_NOT_AVAILABLE;
if (tag != "")
return OBJECT_NOT_AVAILABLE;
vector<DBCALDigiHit*> data;
const hddm_s::BcalfADCDigiHitList &digihits = record->getBcalfADCDigiHits();
hddm_s::BcalfADCDigiHitList::iterator iter;
for (iter = digihits.begin(); iter != digihits.end(); ++iter) {
DBCALDigiHit *response = new DBCALDigiHit;
response->module = iter->getModule();
response->layer = iter->getLayer();
response->sector = iter->getSector();
response->pulse_integral = (uint32_t)iter->getPulse_integral();
response->pulse_peak = 0;
if(iter->getBcalfADCPeaks().size() > 0) {
response->pulse_peak = iter->getBcalfADCPeak().getPeakAmp();
}
response->pulse_time = (uint32_t)iter->getPulse_time();
response->pedestal = 1;
response->QF = 1;
response->nsamples_integral = 1;
response->nsamples_pedestal = 1;
response->datasource = 3;
response->end = (iter->getEnd() == 0)? DBCALGeometry::kUpstream :
DBCALGeometry::kDownstream;
data.push_back(response);
}
// Copy into factory
factory->CopyTo(data);
return NOERROR;
}
//------------------
// Extract_DBCALIncidentParticle
//------------------
jerror_t DEventSourceHDDM::Extract_DBCALIncidentParticle(hddm_s::HDDM *record,
JFactory<DBCALIncidentParticle> *factory,
string tag)
{
/// Copies the data from the given hddm_s structure. This is called
/// from JEventSourceHDDM::GetObjects. If factory is NULL, this
/// returns OBJECT_NOT_AVAILABLE immediately.
if (factory == NULL)
return OBJECT_NOT_AVAILABLE;
if (tag != "")
return OBJECT_NOT_AVAILABLE;