-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi0TestSelection_module.cc
More file actions
1252 lines (1045 loc) · 43.5 KB
/
pi0TestSelection_module.cc
File metadata and controls
1252 lines (1045 loc) · 43.5 KB
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
////////////////////////////////////////////////////////////////////////
// Class: pi0TestSelection
// Plugin Type: analyzer (art v2_07_03)
// File: pi0TestSelection_module.cc
//TODO follow new calibration as done in PDSPAnalyser (after understanding current reconsturction)
//? output raw CNN outputs?
////////////////////////////////////////////////////////////////////////
#include "art/Framework/Core/EDAnalyzer.h"
#include "art/Framework/Core/ModuleMacros.h"
#include "art/Framework/Principal/Event.h"
#include "art/Framework/Principal/Handle.h"
#include "art/Framework/Principal/Run.h"
#include "art/Framework/Principal/SubRun.h"
#include "art_root_io/TFileService.h"
#include "canvas/Utilities/InputTag.h"
#include "canvas/Persistency/Common/FindManyP.h"
#include "fhiclcpp/ParameterSet.h"
#include "messagefacility/MessageLogger/MessageLogger.h"
#include "larsim/MCCheater/BackTrackerService.h"
#include "larsim/MCCheater/ParticleInventoryService.h"
#include "lardata/DetectorInfoServices/DetectorClocksService.h"
#include "lardata/DetectorInfoServices/DetectorPropertiesService.h"
#include "lardataobj/RecoBase/Hit.h"
#include "lardataobj/RecoBase/Track.h"
#include "lardataobj/RecoBase/Shower.h"
#include "lardataobj/RecoBase/PFParticle.h"
#include "nusimdata/SimulationBase/MCParticle.h"
#include "nusimdata/SimulationBase/MCTruth.h"
#include "lardataobj/AnalysisBase/CosmicTag.h"
#include "lardataobj/AnalysisBase/T0.h"
#include "lardataobj/AnalysisBase/Calorimetry.h"
#include "lardataobj/RecoBase/OpFlash.h"
#include "larcore/Geometry/Geometry.h"
#include "larreco/RecoAlg/TrackMomentumCalculator.h"
//protoDUNE analysis headers
#include "protoduneana/Utilities/ProtoDUNETrackUtils.h"
#include "protoduneana/Utilities/ProtoDUNEShowerUtils.h"
#include "protoduneana/Utilities/ProtoDUNETruthUtils.h"
#include "protoduneana/Utilities/ProtoDUNEPFParticleUtils.h"
#include "protoduneana/Utilities/ProtoDUNEBeamlineUtils.h"
#include "protoduneana/Utilities/ProtoDUNECalibration.h"
//dunetpc headers
#include "dune/DuneObj/ProtoDUNEBeamEvent.h"
//ROOT includes
#include <TTree.h>
#include <TH1D.h>
#include "Math/Vector3D.h"
//Use this to get CNN output
#include "lardata/ArtDataHelper/MVAReader.h"
#include <algorithm>
namespace protoana {
class pi0TestSelection;
}
class protoana::pi0TestSelection : public art::EDAnalyzer {
public:
explicit pi0TestSelection(fhicl::ParameterSet const & p);
// The compiler-generated destructor is fine for non-base
// classes without bare pointers or other resource use.
// Plugins should not be copied or assigned.
pi0TestSelection(pi0TestSelection const &) = delete;
pi0TestSelection(pi0TestSelection &&) = delete;
pi0TestSelection & operator = (pi0TestSelection const &) = delete;
pi0TestSelection & operator = (pi0TestSelection &&) = delete;
virtual void beginJob() override;
virtual void endJob() override;
// Required functions.
void analyze(art::Event const & e) override;
// Custom functions.
int PandoraIdentification(const recob::PFParticle &daughterPFP, const art::Event &evt);
std::vector<double> CNNScoreCalculator(anab::MVAReader<recob::Hit,4> &hitResults, const std::vector< art::Ptr< recob::Hit > > &hits, unsigned int &n);
std::vector<double> StartHitQuantityCalculator(TVector3 &hitStart, TVector3 &hit, TVector3 &direction);
std::vector<std::vector<double> > StartHits(unsigned int n_hits, art::FindManyP<recob::SpacePoint> spFromHits, TVector3 &showerStart, TVector3 &direction);
double ShowerEnergyCalculator(const std::vector<art::Ptr<recob::Hit> > &hits, const detinfo::DetectorPropertiesData &detProp, art::FindManyP<recob::SpacePoint> &spFromHits);
void reset();
void AnalyseDaughterPFP(const recob::PFParticle &daughterPFP, const art::Event &evt, const detinfo::DetectorPropertiesData &detProp, anab::MVAReader<recob::Hit,4> &hitResults);
void AnalyseBeamPFP(const recob::PFParticle &beam, const art::Event &evt);
void AnalyseMCTruth(const recob::PFParticle &daughter, const art::Event &evt, const detinfo::DetectorClocksData &clockData);
void AnalyseMCTruthBeam(const art::Event &evt);
void FillG4NTuple(const simb::MCParticle* &particle, const int &number);
void CollectG4Particle(const int &Pdg, const int start, const int stop);
void AnalyseFromBeam(const art::Event &evt, const detinfo::DetectorClocksData &clockData, const detinfo::DetectorPropertiesData &detProp, anab::MVAReader<recob::Hit,4> &hitResults, std::vector<recob::PFParticle> pfpVec);
private:
enum G4Mode{PI0=1, DIPHOTON=2, ALL=3, NONE=0}; // determines what MC particles are retrieved from the truth table
// fcl parameters, order matters!
protoana::ProtoDUNECalibration calibration_SCE;
std::string fCalorimetryTag;
std::string fTrackerTag;
std::string fShowerTag;
std::string fHitTag;
std::string fPFParticleTag;
std::string fGeneratorTag;
protoana::ProtoDUNEBeamlineUtils fBeamlineUtils; // get BeamLineUtils class... <consider removing>
art::ServiceHandle<geo::Geometry> geom;
art::ServiceHandle<cheat::BackTrackerService> bt_serv;
art::ServiceHandle< cheat::ParticleInventoryService > pi_serv;
bool fPi0Only;
bool fHitSpacePoints;
G4Mode fRetrieveG4;
//Initialise protodune analysis utility classes
protoana::ProtoDUNEPFParticleUtils pfpUtil;
protoana::ProtoDUNETrackUtils trkUtil;
protoana::ProtoDUNETruthUtils truthUtil;
protoana::ProtoDUNEShowerUtils showerUtil;
protoana::ProtoDUNETrackUtils trackUtil;
// local variables
TTree *fOutTree = new TTree;
// meta-data
double totalEvents; // number of events processed
double beamEvents; // number of events with beam particles
std::vector<int> pdgCodes; // particle pdg codes
// track-shower identification
std::vector<int> pandoraTags; // track/shower like tag from pandora
std::vector<double> emScore;
std::vector<double> trackScore;
std::vector<double> CNNScore; // CNN score per shower
// shower start position
std::vector<double> startPosX;
std::vector<double> startPosY;
std::vector<double> startPosZ;
// shower direction
std::vector<double> dirX;
std::vector<double> dirY;
std::vector<double> dirZ;
// shower angle
std::vector<double> coneAngle;
//shower length
std::vector<double> length;
// hit/energy quantities
std::vector<int> nHits; // number of collection plane hits
std::vector<double> energy; // reco shower energy in ???
// quantity used to calculate the number of start hits
std::vector<std::vector<double>> hitRadial;
std::vector<std::vector<double>> hitLongitudinal;
std::vector<std::vector<double>> spacePointX;
std::vector<std::vector<double>> spacePointY;
std::vector<std::vector<double>> spacePointZ;
// beam start position
double beamStartPosX;
double beamStartPosY;
double beamStartPosZ;
// beam end position
double beamEndPosX;
double beamEndPosY;
double beamEndPosZ;
int trueBeamPdg;
std::vector<int> trueDaughterPdg;
double trueBeamEnergy;
double trueBeamMass;
// true beam start positions
double trueBeamStartPosX;
double trueBeamStartPosY;
double trueBeamStartPosZ;
// true beam end positions
double trueBeamEndPosX;
double trueBeamEndPosY;
double trueBeamEndPosZ;
// true daughter start positions
std::vector<double> trueDaughterStartPosX;
std::vector<double> trueDaughterStartPosY;
std::vector<double> trueDaughterStartPosZ;
// true daughter end positions
std::vector<double> trueDaughterEndPosX;
std::vector<double> trueDaughterEndPosY;
std::vector<double> trueDaughterEndPosZ;
// true daughter momentum
std::vector<double> trueDaughterMomentumX;
std::vector<double> trueDaughterMomentumY;
std::vector<double> trueDaughterMomentumZ;
std::vector<double> trueDaughterEnergy; // mc shower energy in GeV
std::vector<double> trueDaughterMass;
// true parent start positions
std::vector<double> trueParentStartPosX;
std::vector<double> trueParentStartPosY;
std::vector<double> trueParentStartPosZ;
// true parent end positions
std::vector<double> trueParentEndPosX;
std::vector<double> trueParentEndPosY;
std::vector<double> trueParentEndPosZ;
// true parent momentum
std::vector<double> trueParentMomentumX;
std::vector<double> trueParentMomentumY;
std::vector<double> trueParentMomentumZ;
std::vector<double> trueParentEnergy; // mc shower energy in GeV
std::vector<double> trueParentMass;
std::vector<int> trueParentPdg;
std::vector<int> G4ParticlePdg;
std::vector<double> G4ParticleEnergy;
std::vector<double> G4ParticleMass;
std::vector<double> G4ParticleStartPosX;
std::vector<double> G4ParticleStartPosY;
std::vector<double> G4ParticleStartPosZ;
std::vector<double> G4ParticleEndPosX;
std::vector<double> G4ParticleEndPosY;
std::vector<double> G4ParticleEndPosZ;
std::vector<double> G4ParticleMomX;
std::vector<double> G4ParticleMomY;
std::vector<double> G4ParticleMomZ;
std::vector<int> G4ParticleNum;
std::vector<int> G4ParticleMother;
std::vector<int> matchedG4DaughterPdg;
std::vector<int> matchedG4ParentPdg;
std::vector<int> PFPNum;
std::vector<int> PFPMother;
unsigned int eventID;
unsigned int run;
unsigned int subRun;
int beam;
};
protoana::pi0TestSelection::pi0TestSelection(fhicl::ParameterSet const & p)
:
EDAnalyzer(p),
calibration_SCE(p.get<fhicl::ParameterSet>("CalibrationParsSCE")),
fCalorimetryTag(p.get<std::string>("CalorimetryTag")),
fTrackerTag(p.get<std::string>("TrackerTag")),
fShowerTag(p.get<std::string>("ShowerTag")),
fHitTag(p.get<std::string>("HitTag")),
fPFParticleTag(p.get<std::string>("PFParticleTag")),
fGeneratorTag(p.get<std::string>("GeneratorTag")),
fBeamlineUtils(p.get<fhicl::ParameterSet>("BeamlineUtils")),
fPi0Only(p.get<bool>("Pi0Only")),
fHitSpacePoints(p.get<bool>("RetrieveSpacePoints")),
fRetrieveG4(static_cast<G4Mode>(p.get<int>("RetrieveG4")))
{ }
// shower energy calculation, taken from Jake Calcutt's PDPSPAnalyser
double protoana::pi0TestSelection::ShowerEnergyCalculator(const std::vector<art::Ptr<recob::Hit> > &hits, const detinfo::DetectorPropertiesData &detProp, art::FindManyP<recob::SpacePoint> &spFromHits)
{
std::vector<double> x_vec, y_vec, z_vec; // parameterised hit position vector for each hit
double total_y = 0;
int n_good_y = 0;
std::vector<art::Ptr<recob::Hit>> good_hits;
for(unsigned int i = 0; i < hits.size(); i++)
{
auto hit = hits[i];
// skip any hits not on the collection plane (shouldn't be anyways)
if(hit->View() != 2)
{
continue;
}
good_hits.push_back(hit);
double shower_hit_x = detProp.ConvertTicksToX(hit->PeakTime(), hit->WireID().Plane, hit->WireID().TPC, 0);
double shower_hit_z = geom->Wire(hit->WireID()).GetCenter().Z();
x_vec.push_back(shower_hit_x);
z_vec.push_back(shower_hit_z);
std::vector<art::Ptr<recob::SpacePoint>> sps = spFromHits.at(i);
if (!sps.empty())
{
y_vec.push_back(sps[0]->XYZ()[1]);
total_y += y_vec.back();
n_good_y++;
}
else
{
y_vec.push_back(-999.);
}
}
double total_energy = 0;
if(n_good_y < 1)
{
std::cout << "could not reconstruct energy" << std::endl;
total_energy = -999;
}
else
{
for(unsigned int j = 0; j < good_hits.size(); j++)
{
auto good_hit = good_hits[j];
if(good_hit->View() != 2)
{
continue;
}
if (y_vec[j] < -100.)
{
y_vec[j] = total_y / n_good_y;
}
total_energy += calibration_SCE.HitToEnergy(good_hit, x_vec[j], y_vec[j], z_vec[j]);
}
}
return total_energy;
}
// track/shower identification done thorugh pandora, returns 11 for a shower and 13 for a track
int protoana::pi0TestSelection::PandoraIdentification(const recob::PFParticle &daughterPFP, const art::Event &evt)
{
// determine if they are track like or shower like using pandora
// then fill a vector containing this data: 11 = shower 13 = track
if(pfpUtil.IsPFParticleShowerlike(daughterPFP, evt, fPFParticleTag, fShowerTag))
{
return 11;
}
else if(pfpUtil.IsPFParticleTracklike(daughterPFP, evt, fPFParticleTag, fTrackerTag))
{
return 13;
}
else
{
return -999;
}
return -1;
}
// Calculates the CNN score of the event. Used to determine if a an event is track or shower like.
std::vector<double> protoana::pi0TestSelection::CNNScoreCalculator(anab::MVAReader<recob::Hit,4> &hitResults, const std::vector< art::Ptr< recob::Hit > > &hits, unsigned int &n)
{
std::vector<double> output{};
double score = 0;
double mean_em = 0;
double mean_track = 0;
double cnn_track = 0;
double cnn_em = 0;
// Calculate the score per hit than take the average
for(unsigned int h = 0; h < n; h++)
{
std::array<float,4> cnn_out = hitResults.getOutput( hits[h] );
cnn_track = cnn_out[ hitResults.getIndex("track") ];
cnn_em = cnn_out[ hitResults.getIndex("em") ];
mean_em += cnn_em;
mean_track += cnn_track;
}
mean_em = (n > 0) ? ( mean_em / n ) : -999; // posts -999 if there were no hits
mean_track = (n > 0) ? ( mean_track / n ) : -999;
score = (n > 0) ? (mean_em / (mean_em + mean_track) ) : -999;
output.push_back(score);
output.push_back(mean_em);
output.push_back(mean_track);
return output;
}
// calculates the quantities for determining hits close to the shower start
std::vector<double> protoana::pi0TestSelection::StartHitQuantityCalculator(TVector3 &hitStart, TVector3 &hit, TVector3 &direction)
{
std::vector<double> output{}; // make the return type a pair!
TVector3 cross = (hit - hitStart).Cross(direction); // rsin(theta) compare to cylinder radius
double dot = (hit - hitStart).Dot(direction); // rcos(theta) compare to cylinder length
output.push_back(cross.Mag());
output.push_back(dot);
return output;
}
std::vector<std::vector<double> > protoana::pi0TestSelection::StartHits(unsigned int n_hits, art::FindManyP<recob::SpacePoint> spFromHits, TVector3 &showerStart, TVector3 &direction)
{
std::vector<std::vector<double> > out;
// calculates quantities needed to compute the start hits
std::vector<double> hitRad; // magnitudes of cross product of hit positions and shower direction
std::vector<double> hitLong; // dot product of of hit positions and shower direction
for(unsigned int n = 0; n < n_hits; n++)
{
std::vector<art::Ptr<recob::SpacePoint>> sps = spFromHits.at(n); // get nth space point
if(!sps.empty())
{
TVector3 hitPoint(sps[0]->XYZ()[0], sps[0]->XYZ()[1], sps[0]->XYZ()[2]); // create space point position vector
std::vector<double> startHitQuantities = StartHitQuantityCalculator(showerStart, hitPoint, direction); // get start hit quantities
hitRad.push_back(startHitQuantities[0]);
hitLong.push_back(startHitQuantities[1]);
}
else
{
hitRad.push_back(-999);
hitLong.push_back(-999);
}
}
out.push_back(hitRad);
out.push_back(hitLong);
return out;
}
// Clears the various analyser outputs at the start of a new event to remove the previous events contents
void protoana::pi0TestSelection::reset()
{
pdgCodes.clear();
pandoraTags.clear();
emScore.clear();
trackScore.clear();
CNNScore.clear();
startPosX.clear();
startPosY.clear();
startPosZ.clear();
dirX.clear();
dirY.clear();
dirZ.clear();
coneAngle.clear();
length.clear();
energy.clear();
nHits.clear();
hitRadial.clear();
hitLongitudinal.clear();
spacePointX.clear();
spacePointY.clear();
spacePointZ.clear();
trueDaughterPdg.clear();
trueDaughterMass.clear();
trueDaughterEnergy.clear();
trueDaughterStartPosX.clear();
trueDaughterStartPosY.clear();
trueDaughterStartPosZ.clear();
trueDaughterEndPosX.clear();
trueDaughterEndPosY.clear();
trueDaughterEndPosZ.clear();
trueDaughterMomentumX.clear();
trueDaughterMomentumY.clear();
trueDaughterMomentumZ.clear();
trueParentPdg.clear();
trueParentMass.clear();
trueParentEnergy.clear();
trueParentStartPosX.clear();
trueParentStartPosY.clear();
trueParentStartPosZ.clear();
trueParentEndPosX.clear();
trueParentEndPosY.clear();
trueParentEndPosZ.clear();
trueParentMomentumX.clear();
trueParentMomentumY.clear();
trueParentMomentumZ.clear();
G4ParticlePdg.clear();
G4ParticleMass.clear();
G4ParticleEnergy.clear();
G4ParticleStartPosX.clear();
G4ParticleStartPosY.clear();
G4ParticleStartPosZ.clear();
G4ParticleMomX.clear();
G4ParticleMomY.clear();
G4ParticleMomZ.clear();
G4ParticleEndPosX.clear();
G4ParticleEndPosY.clear();
G4ParticleEndPosZ.clear();
G4ParticleNum.clear();
G4ParticleMother.clear();
matchedG4DaughterPdg.clear();
matchedG4ParentPdg.clear();
PFPNum.clear();
PFPMother.clear();
}
void protoana::pi0TestSelection::FillG4NTuple(const simb::MCParticle* &particle, const int &number)
{
std::cout << "----------------------------------------" << std::endl;
std::cout << "number: " << number << std::endl;
std::cout << "PDG code: " << particle->PdgCode() << std::endl;
std::cout << "Energy: " << particle->E() << std::endl;
G4ParticlePdg.push_back(particle->PdgCode());
G4ParticleEnergy.push_back(particle->E());
G4ParticleMass.push_back(particle->Mass());
TLorentzVector StartPos = particle->Position(0);
G4ParticleStartPosX.push_back(StartPos.X());
G4ParticleStartPosY.push_back(StartPos.Y());
G4ParticleStartPosZ.push_back(StartPos.Z());
TLorentzVector EndPos = particle->EndPosition();
G4ParticleEndPosX.push_back(EndPos.X());
G4ParticleEndPosY.push_back(EndPos.Y());
G4ParticleEndPosZ.push_back(EndPos.Z());
TLorentzVector momentum = particle->Momentum();
G4ParticleMomX.push_back(momentum.X());
G4ParticleMomY.push_back(momentum.Y());
G4ParticleMomZ.push_back(momentum.Z());
G4ParticleNum.push_back(number);
G4ParticleMother.push_back(particle->Mother());
}
void protoana::pi0TestSelection::CollectG4Particle(const int &pdg=0, const int start=-1, const int stop=-1)
{
const sim::ParticleList & plist = pi_serv->ParticleList();
for(auto part = plist.begin(); part != plist.end(); part ++)
{
// don't compute anything till the start point
if(start > -1 && part->first < start)
{
continue;
}
// finish once we process the last particle
if(stop > -1 && part->first > stop)
{
std::cout << "finished at: " << part->first << std::endl;
break;
}
const simb::MCParticle* pPart = part->second;
// run if a specific particle is needed
if(pdg == pPart->PdgCode())
{
FillG4NTuple(pPart, part->first);
std::cout << "number of Daughters: " << pPart->NumberDaughters() << std::endl;
for (int i = pPart->FirstDaughter(); i < pPart->FirstDaughter() + pPart->NumberDaughters(); i++)
{
const simb::MCParticle* daughter = plist.find(i)->second;
FillG4NTuple(daughter, i);
}
}
// run if all particles are needed
if(pdg == 0)
{
FillG4NTuple(pPart, part->first);
}
}
std::cout << "number of G4 particles: " << plist.size() << std::endl;
}
void protoana::pi0TestSelection::AnalyseDaughterPFP(const recob::PFParticle &daughterPFP, const art::Event &evt, const detinfo::DetectorPropertiesData &detProp, anab::MVAReader<recob::Hit,4> &hitResults)
{
// get what pandora thinks the pdg code is
pdgCodes.push_back(daughterPFP.PdgCode());
// determine if they are track like or shower like using pandora
// then fill a vector containing this data: 11 = shower 13 = track
pandoraTags.push_back(PandoraIdentification(daughterPFP, evt));
// number of collection plane hits
auto collection_hits = pfpUtil.GetPFParticleHitsFromPlane_Ptrs( daughterPFP, evt, fPFParticleTag, 2 ); // get collection plane hit objects for the daughter
unsigned int num = collection_hits.size();
nHits.push_back(num);
std::cout << "collection plane hits: " << num << std::endl;
// calculate cnn score
std::vector<double> cnnOutput = CNNScoreCalculator(hitResults, collection_hits, num);
CNNScore.push_back(cnnOutput[0]);
// also output the average and em track score to calculate it in python
emScore.push_back(cnnOutput[1]);
trackScore.push_back(cnnOutput[2]);
const recob::Shower* shower = 0x0; // intilise the forced shower object
std::cout << "Getting shower" << std::endl;
// try assigning the forced shower object
try
{
shower = pfpUtil.GetPFParticleShower(daughterPFP, evt, fPFParticleTag, "pandora2Shower");
if(shower)
{
std::cout << "got shower" << std::endl;
const std::vector<art::Ptr<recob::Hit> > showerHits = showerUtil.GetRecoShowerArtHits(*shower, evt, "pandora2Shower");
art::FindManyP<recob::SpacePoint> spFromHits(showerHits, evt, fHitTag); // get space point objects of the hits
std::cout << "getting start and direction" << std::endl;
TVector3 showerStart = shower->ShowerStart();
TVector3 showerDir = shower->Direction();
std::cout << "got start and direction" << std::endl;
startPosX.push_back(showerStart.X());
startPosY.push_back(showerStart.Y());
startPosZ.push_back(showerStart.Z());
dirX.push_back(showerDir.X());
dirY.push_back(showerDir.Y());
dirZ.push_back(showerDir.Z());
length.push_back(shower->Length());
coneAngle.push_back(shower->OpenAngle());
// calculates quantities needed to compute the start hits <move to function?>
std::vector<double> hitRad; // magnitudes of cross product of hit positions and shower direction
std::vector<double> hitLong; // dot product of of hit positions and shower direction
std::vector<double> spx;
std::vector<double> spy;
std::vector<double> spz;
for(unsigned int n = 0; n < num; n++)
{
std::vector<art::Ptr<recob::SpacePoint>> sps = spFromHits.at(n); // get nth space point
if(!sps.empty())
{
TVector3 hitPoint(sps[0]->XYZ()[0], sps[0]->XYZ()[1], sps[0]->XYZ()[2]); // create space point position vector
std::vector<double> startHitQuantities = StartHitQuantityCalculator(showerStart, hitPoint, showerDir); // get start hit quantities
if(fHitSpacePoints)
{
spx.push_back(hitPoint.X());
spy.push_back(hitPoint.Y());
spz.push_back(hitPoint.Z());
}
else
{
spx.push_back(-999);
spy.push_back(-999);
spz.push_back(-999);
}
hitRad.push_back(startHitQuantities[0]);
hitLong.push_back(startHitQuantities[1]);
}
else
{
spx.push_back(-999);
spy.push_back(-999);
spz.push_back(-999);
hitRad.push_back(-999);
hitLong.push_back(-999);
}
}
hitRadial.push_back(hitRad);
hitLongitudinal.push_back(hitLong);
spacePointX.push_back(spx);
spacePointY.push_back(spy);
spacePointZ.push_back(spz);
// calculate and push back shower energy
energy.push_back(ShowerEnergyCalculator(showerHits, detProp, spFromHits));
}
else
{
std::cout << "couldn't get shower object! Moving on" << std::endl;
startPosX.push_back(-999);
startPosY.push_back(-999);
startPosZ.push_back(-999);
std::vector<double> null (1, -999);
hitRadial.push_back( null );
hitLongitudinal.push_back( null );
spacePointX.push_back( null );
spacePointY.push_back( null );
spacePointZ.push_back( null );
dirX.push_back(-999);
dirY.push_back(-999);
dirZ.push_back(-999);
length.push_back(-999);
coneAngle.push_back(-999);
energy.push_back(-999);
}
}
catch( const cet::exception &e )
{
std::cout << "couldn't get shower object! Moving on" << std::endl;
startPosX.push_back(-999);
startPosY.push_back(-999);
startPosZ.push_back(-999);
dirX.push_back(-999);
dirY.push_back(-999);
dirZ.push_back(-999);
length.push_back(-999);
coneAngle.push_back(-999);
energy.push_back(-999);
}
}
void protoana::pi0TestSelection::AnalyseBeamPFP(const recob::PFParticle &beam, const art::Event &evt)
{
const recob::Track* beamTrack = 0x0; // set to null
beamTrack = pfpUtil.GetPFParticleTrack(beam, evt, fPFParticleTag, fTrackerTag); // get the beam track if it exists
std::cout << "Pandora ID of beam particle: " << PandoraIdentification(beam, evt) << std::endl;
// store beam track info
if(!beamTrack)
{
std::cout<< "no beam track found, moving on" << std::endl;
beamStartPosX = -999;
beamStartPosY = -999;
beamStartPosZ = -999;
beamEndPosX = -999;
beamEndPosY = -999;
beamEndPosZ = -999;
}
else
{
beamStartPosX = beamTrack->Trajectory().Start().X();
beamStartPosY = beamTrack->Trajectory().Start().Y();
beamStartPosZ = beamTrack->Trajectory().Start().Z();
beamEndPosX = beamTrack->Trajectory().End().X();
beamEndPosY = beamTrack->Trajectory().End().Y();
beamEndPosZ = beamTrack->Trajectory().End().Z();
// if the beam enters from the opposite direction
if(beamStartPosZ > beamEndPosZ)
{
beamStartPosX = beamTrack->Trajectory().End().X();
beamStartPosY = beamTrack->Trajectory().End().Y();
beamStartPosZ = beamTrack->Trajectory().End().Z();
beamEndPosX = beamTrack->Trajectory().Start().X();
beamEndPosY = beamTrack->Trajectory().Start().Y();
beamEndPosZ = beamTrack->Trajectory().Start().Z();
}
}
}
void protoana::pi0TestSelection::AnalyseMCTruth(const recob::PFParticle &daughter, const art::Event &evt, const detinfo::DetectorClocksData &clockData)
{
std::cout << "getting shared hits" << std::endl;
// match the MC particle assosiated to the daughter PFParticle by comparing the hit objects
protoana::MCParticleSharedHits match = truthUtil.GetMCParticleByHits( clockData, daughter, evt, fPFParticleTag, fHitTag );
std::cout << "got shared hits" << std::endl;
const simb::MCParticle* mcParticle = match.particle; // get the MCParticle object from the match
const sim::ParticleList & plist = pi_serv->ParticleList(); // get particle list, g4?
if(mcParticle)
{
std::cout << "we have matched the MC particles!" << std::endl;
const simb::MCParticle * parent = plist.Primary(mcParticle->Mother());
std::cout << "parent PID" << std::endl;
std::cout << parent->PdgCode() << std::endl;
const simb::MCParticle * g4Particle = truthUtil.MatchPduneMCtoG4(*mcParticle, evt);
const simb::MCParticle * g4ParticleParent = truthUtil.MatchPduneMCtoG4(*parent, evt);
matchedG4DaughterPdg.push_back(g4Particle->PdgCode());
matchedG4ParentPdg.push_back(g4ParticleParent->PdgCode());
TLorentzVector trueDaughterStartPos = mcParticle->Position(0);
trueDaughterStartPosX.push_back(trueDaughterStartPos.X());
trueDaughterStartPosY.push_back(trueDaughterStartPos.Y());
trueDaughterStartPosZ.push_back(trueDaughterStartPos.Z());
TLorentzVector trueDaughterEndPos = mcParticle->EndPosition();
trueDaughterEndPosX.push_back(trueDaughterEndPos.X());
trueDaughterEndPosY.push_back(trueDaughterEndPos.Y());
trueDaughterEndPosZ.push_back(trueDaughterEndPos.Z());
TLorentzVector trueDaughterMomentum = mcParticle->Momentum();
trueDaughterMomentumX.push_back(trueDaughterMomentum.X());
trueDaughterMomentumY.push_back(trueDaughterMomentum.Y());
trueDaughterMomentumZ.push_back(trueDaughterMomentum.Z());
trueDaughterEnergy.push_back(mcParticle->E());
trueDaughterPdg.push_back(mcParticle->PdgCode());
trueDaughterMass.push_back(mcParticle->Mass());
TLorentzVector trueParentStartPos = parent->Position(0);
trueParentStartPosX.push_back(trueParentStartPos.X());
trueParentStartPosY.push_back(trueParentStartPos.Y());
trueParentStartPosZ.push_back(trueParentStartPos.Z());
TLorentzVector trueParentEndPos = parent->EndPosition();
trueParentEndPosX.push_back(trueParentEndPos.X());
trueParentEndPosY.push_back(trueParentEndPos.Y());
trueParentEndPosZ.push_back(trueParentEndPos.Z());
TLorentzVector trueParentMomentum = parent->Momentum();
trueParentMomentumX.push_back(trueParentMomentum.X());
trueParentMomentumY.push_back(trueParentMomentum.Y());
trueParentMomentumZ.push_back(trueParentMomentum.Z());
trueParentEnergy.push_back(parent->E());
trueParentPdg.push_back(parent->PdgCode());
trueParentMass.push_back(parent->Mass());
}
else
{
std::cout << "MC particle not matched" << std::endl;
trueDaughterStartPosX.push_back(-999);
trueDaughterStartPosY.push_back(-999);
trueDaughterStartPosZ.push_back(-999);
trueDaughterEndPosX.push_back(-999);
trueDaughterEndPosY.push_back(-999);
trueDaughterEndPosZ.push_back(-999);
trueDaughterMomentumX.push_back(-999);
trueDaughterMomentumY.push_back(-999);
trueDaughterMomentumZ.push_back(-999);
trueDaughterEnergy.push_back(-999);
trueDaughterPdg.push_back(-999);
trueDaughterMass.push_back(-999);
trueParentStartPosX.push_back(-999);
trueParentStartPosY.push_back(-999);
trueParentStartPosZ.push_back(-999);
trueParentEndPosX.push_back(-999);
trueParentEndPosY.push_back(-999);
trueParentEndPosZ.push_back(-999);
trueParentMomentumX.push_back(-999);
trueParentMomentumY.push_back(-999);
trueParentMomentumZ.push_back(-999);
trueParentEnergy.push_back(-999);
trueParentPdg.push_back(-999);
trueParentMass.push_back(-999);
matchedG4DaughterPdg.push_back(-999);
matchedG4ParentPdg.push_back(-999);
}
}
void protoana::pi0TestSelection::AnalyseMCTruthBeam(const art::Event &evt)
{
const simb::MCParticle* true_beam_particle = 0x0;
auto mcTruths = evt.getValidHandle<std::vector<simb::MCTruth>>(fGeneratorTag);
true_beam_particle = truthUtil.GetGeantGoodParticle((*mcTruths)[0],evt);
if( !true_beam_particle ){
std::cout << "No true beam particle" << std::endl;
return;
}
else
{
std::cout << "Found true Beam particle" << std::endl;
}
trueBeamPdg = true_beam_particle->PdgCode();
trueBeamMass = true_beam_particle->Mass();
trueBeamEnergy = true_beam_particle->E();
TLorentzVector trueBeamStartPos = true_beam_particle->Position(0);
trueBeamStartPosX = trueBeamStartPos.X();
trueBeamStartPosX = trueBeamStartPos.Y();
trueBeamStartPosX = trueBeamStartPos.Z();
TLorentzVector trueBeamEndPos = true_beam_particle->EndPosition();
trueBeamEndPosX = trueBeamEndPos.X();
trueBeamEndPosX = trueBeamEndPos.Y();
trueBeamEndPosX = trueBeamEndPos.Z();
}
void protoana::pi0TestSelection::AnalyseFromBeam(art::Event const &evt, const detinfo::DetectorClocksData &clockData, const detinfo::DetectorPropertiesData &detProp, anab::MVAReader<recob::Hit,4> &hitResults, std::vector<recob::PFParticle> pfpVec)
{
// Get only Beam particle by checking the Beam slices
std::vector<const recob::PFParticle*> beamParticles = pfpUtil.GetPFParticlesFromBeamSlice(evt, fPFParticleTag);
if(beamParticles.size() == 0)
{
std::cout << "no beam particle, moving on..." << std::endl;
totalEvents ++;
return;
}
if(beamParticles.size() > 1)
{
std::cout << "there shouldn't be more than one beam particle" << std::endl;
}
auto beamParticle = beamParticles[0]; // get the first beam particle
beamEvents ++;
// get track-like beam info, not for pi0 only MC events
if(!fPi0Only)
{
AnalyseBeamPFP(*beamParticle, evt);
}
else
{
// we want to process the beam like a daughter event for pi0 only particle gun tests
AnalyseDaughterPFP(*beamParticle, evt, detProp, hitResults);
beamStartPosX = -999;
beamStartPosY = -999;
beamStartPosZ = -999;
beamEndPosX = -999;
beamEndPosY = -999;
beamEndPosZ = -999;
}
std::cout << beamParticle->Daughters().size() << std::endl;
// analyse each daughter PFParticle from the beam
for( size_t daughterID : beamParticle->Daughters() )
{
const recob::PFParticle * daughterPFP = &(pfpVec.at( daughterID ));
AnalyseDaughterPFP(*daughterPFP, evt, detProp, hitResults);
}
// store any MC reated information here i.e. MC truth info
if(!evt.isRealData())
{
if(!fPi0Only)
{
AnalyseMCTruthBeam(evt);
}
else
{
AnalyseMCTruth(*beamParticle, evt, clockData);
trueBeamPdg = -999;
trueBeamMass = -999;
trueBeamEnergy = -999;
trueBeamStartPosX = -999;
trueBeamStartPosX = -999;
trueBeamStartPosX = -999;
trueBeamEndPosX = -999;
trueBeamEndPosX = -999;
trueBeamEndPosX = -999;
}
// backtrack each daughter PFParticle from the beam
for( size_t daughterID : beamParticle->Daughters() )
{
const recob::PFParticle * daughterPFP = &(pfpVec.at( daughterID ));
AnalyseMCTruth(*daughterPFP, evt, clockData);
}
}
}
void protoana::pi0TestSelection::beginJob()
{
// intiialize output root file
art::ServiceHandle<art::TFileService> tfs;
fOutTree = tfs->make<TTree>("beamana", "");
//Once we are done, write results into the ROOT file
fOutTree->Branch("Run", &run);
fOutTree->Branch("SubRun", &subRun);
fOutTree->Branch("EventID", &eventID);
fOutTree->Branch("totalEvents", &totalEvents);
fOutTree->Branch("beamEvents", &beamEvents);
fOutTree->Branch("beamNum", &beam);
fOutTree->Branch("pdgCode", &pdgCodes);
// track-shower identification
fOutTree->Branch("pandoraTag", &pandoraTags);
fOutTree->Branch("reco_daughter_PFP_emScore_collection", &emScore);
fOutTree->Branch("reco_daughter_PFP_trackScore_collection", &trackScore);
fOutTree->Branch("CNNScore_collection", &CNNScore);
// shower start position
fOutTree->Branch("reco_daughter_allShower_startX", &startPosX);
fOutTree->Branch("reco_daughter_allShower_startY", &startPosY);
fOutTree->Branch("reco_daughter_allShower_startZ", &startPosZ);
// shower direction
fOutTree->Branch("reco_daughter_allShower_dirX", &dirX);
fOutTree->Branch("reco_daughter_allShower_dirY", &dirY);
fOutTree->Branch("reco_daughter_allShower_dirZ", &dirZ);