11 #include "nusimdata/SimulationBase/MCParticle.h"
12 #include "nusimdata/SimulationBase/MCTruth.h"
13 #include "nusimdata/SimulationBase/MCNeutrino.h"
22 #include "art/Framework/Core/EDAnalyzer.h"
23 #include "art/Framework/Principal/Event.h"
24 #include "art/Framework/Principal/Handle.h"
25 #include "art/Framework/Services/Registry/ServiceHandle.h"
26 #include "art/Framework/Core/ModuleMacros.h"
27 #include "canvas/Persistency/Common/FindOneP.h"
28 #include "messagefacility/MessageLogger/MessageLogger.h"
29 #include "fhiclcpp/ParameterSet.h"
34 #include "TDirectory.h"
36 #include "TClonesArray.h"
38 #include "TLorentzVector.h"
39 #include "TDatabasePDG.h"
40 #include "TObjArray.h"
42 #include "TTimeStamp.h"
49 #define MAX_TRACKS 30000
58 explicit CellTree(fhicl::ParameterSet
const& pset);
63 void beginRun(
const art::Run& run);
68 void print_vector(ostream& out, vector<double>& v, TString desc,
bool end=
false);
70 void processRaw(
const art::Event& evt);
71 void processCalib(
const art::Event& evt);
72 void processOpHit(
const art::Event& evt);
73 void processOpFlash(
const art::Event& evt);
74 void processSpacePoint(
const art::Event& event, TString
option, ostream& out=
cout);
75 void processSpacePointTruthDepo(
const art::Event& event, TString option, ostream& out=
cout);
76 void processSimChannel(
const art::Event& evt);
77 void processMC(
const art::Event& evt);
78 void processMCTracks();
79 void processTrigger(
const art::Event& evt);
82 void InitProcessMap();
84 bool IsPrimary(
int i) {
return mc_mother[i] == 0 ; }
86 double KE(
float* momentum);
87 TString PDGName(
int pdg);
88 bool DumpMCJSON(
int id, ostream& out);
89 void DumpMCJSON(ostream& out=
cout);
211 CellTree::CellTree(fhicl::ParameterSet
const&
p)
214 dbPDG =
new TDatabasePDG();
226 mcOption = p.get<std::string>(
"mcOption");
232 fSaveMC = p.get<
bool>(
"saveMC");
246 TDirectory* tmpDir = gDirectory;
251 TNamed
version(
"version",
"4.0");
255 TDirectory* subDir =
fOutFile->mkdir(
"Event");
257 fEventTree =
new TTree(
"Sim",
"Event Tree from Simulation");
270 fRaw_wf =
new TClonesArray(
"TH1F");
337 system(
"rm -rf bee");
338 gSystem->MakeDirectory(
"bee");
340 gSystem->MakeDirectory(
"bee/data");
349 TDirectory* tmpDir = gDirectory;
359 gSystem->ChangeDirectory(
"bee");
360 system(
"zip -r bee_upload data");
361 gSystem->ChangeDirectory(
"..");
368 mf::LogInfo(
"CellTree") <<
"begin run";
375 fEvent =
event.id().event();
378 art::Timestamp ts =
event.time();
379 TTimeStamp tts(ts.timeHigh(), ts.timeLow());
391 gSystem->MakeDirectory(TString::Format(
"bee/data/%i",
entryNo).
Data());
393 for (
int i=0; i<nSp; i++) {
396 std::ofstream out(jsonfile.Data());
410 std::ofstream out(jsonfile.Data());
456 for (
int j=0; j<4; j++) {
482 for (
int i=0; i<4; i++) {
496 art::Handle< std::vector<raw::RawDigit> > rawdigit;
501 std::vector< art::Ptr<raw::RawDigit> > wires;
502 art::fill_ptr_vector(wires, rawdigit);
507 for (
auto const& wire: wires) {
508 int chanId = wire->Channel();
511 int nSamples = wire->Samples();
512 std::vector<short> uncompressed(nSamples);
516 for (
int j=1; j<=nSamples; j++) {
517 h->SetBinContent(j, uncompressed[j-1]);
531 art::Handle< std::vector<recob::Wire> > wires_handle;
532 if (! event.getByLabel(
fCalibLabel, wires_handle)) {
536 std::vector< art::Ptr<recob::Wire> > wires;
537 art::fill_ptr_vector(wires, wires_handle);
544 for (
auto const& wire: wires) {
545 std::vector<float> calibwf = wire->Signal();
546 int chanId = wire->Channel();
550 h->SetBinContent(j, calibwf[j]);
562 art::Handle<std::vector<recob::OpHit> > ophit_handle;
567 std::vector<art::Ptr<recob::OpHit> > ophits;
568 art::fill_ptr_vector(ophits, ophit_handle);
571 for(
auto const& oh : ophits){
575 oh_pe.push_back(oh->PE());
583 art::Handle<std::vector<recob::OpFlash> > flash_handle;
588 std::vector<art::Ptr<recob::OpFlash> > flashes;
589 art::fill_ptr_vector(flashes, flash_handle);
595 for(
auto const& flash: flashes){
596 of_t.push_back(flash->Time());
598 TH1F *
h =
new ((*fPEperOpDet)[
a]) TH1F(
"",
"",nOpDet,0,nOpDet);
601 for(
int i=0; i<nOpDet; ++i){
605 h->SetBinContent(i, flash->PE(i));
615 art::Handle< std::vector<sim::SimChannel> > simChannelHandle;
624 for (
auto const& channel : (*simChannelHandle) ) {
625 auto channelNumber = channel.Channel();
630 auto const& timeSlices = channel.TDCIDEMap();
631 for (
auto const& timeSlice : timeSlices ) {
632 auto const& energyDeposits = timeSlice.second;
633 for (
auto const& energyDeposit : energyDeposits ) {
657 art::Handle< std::vector<simb::MCParticle> > particleHandle;
658 if (! event.getByLabel(
"largeant", particleHandle))
return;
659 std::vector< art::Ptr<simb::MCParticle> > particles;
660 art::fill_ptr_vector(particles, particleHandle);
662 art::Handle< std::vector<sim::SimChannel> > simChannelHandle;
663 event.getByLabel(
"largeant", simChannelHandle);
666 art::FindOneP<simb::MCTruth> fo(particleHandle, event,
"largeant");
670 for (
auto const& particle: particles ) {
671 art::Ptr<simb::MCTruth> mctruth = fo.at(i_all);
675 if ( !(mctruth->Origin() == 1 && particle->Mother() == 0) ) {
692 if (
mc_process[i] == 0)
cout <<
"unknown process: " << particle->Process() << endl;
693 mc_id[i] = particle->TrackId();
694 mc_pdg[i] = particle->PdgCode();
698 int Ndaughters = particle->NumberDaughters();
699 vector<int> daughters;
700 for (
int i=0; i<Ndaughters; i++) {
701 daughters.push_back(particle->Daughter(i));
704 size_t numberTrajectoryPoints = particle->NumberTrajectoryPoints();
705 int last = numberTrajectoryPoints - 1;
706 const TLorentzVector& positionStart = particle->Position(0);
707 const TLorentzVector& positionEnd = particle->Position(last);
708 const TLorentzVector& momentumStart = particle->Momentum(0);
709 const TLorentzVector& momentumEnd = particle->Momentum(last);
716 TClonesArray *Lposition =
new TClonesArray(
"TLorentzVector", numberTrajectoryPoints);
718 for(
unsigned int j=0; j<numberTrajectoryPoints; j++) {
719 new ((*Lposition)[j]) TLorentzVector(particle->Position(j));
726 cout <<
"WARNING:: # tracks exceeded MAX_TRACKS " <<
MAX_TRACKS << endl;
734 art::Handle< std::vector<simb::MCTruth> > mctruthListHandle;
735 event.getByLabel(
"generator",mctruthListHandle);
736 std::vector<art::Ptr<simb::MCTruth> > mclist;
737 art::fill_ptr_vector(mclist, mctruthListHandle);
738 art::Ptr<simb::MCTruth> mctruth;
740 if (mclist.size()>0) {
741 mctruth = mclist.at(0);
742 if (mctruth->NeutrinoSet()) {
743 simb::MCNeutrino
nu = mctruth->GetNeutrino();
760 const TLorentzVector& position = nu.Nu().Position(0);
761 const TLorentzVector& momentum = nu.Nu().Momentum(0);
782 art::Handle< std::vector<recob::SpacePoint> > sp_handle;
783 art::Handle< std::vector<recob::PointCharge> > pc_handle;
784 bool sp_exists =
event.getByLabel(option.Data(), sp_handle);
785 bool pc_exists =
event.getByLabel(option.Data(), pc_handle);
787 cout <<
"WARNING: no label " << option << endl;
790 std::vector< art::Ptr<recob::SpacePoint> > sps;
791 std::vector< art::Ptr<recob::PointCharge> > pcs;
792 art::fill_ptr_vector(sps, sp_handle);
794 art::fill_ptr_vector(pcs, pc_handle);
795 if (sps.size() != pcs.size()) {
796 cout <<
"WARNING: SpacePoint and PointCharge length mismatch" << endl;
800 double x=0,
y=0,
z=0,
q=0, nq=1;
801 vector<double> vx, vy, vz, vq, vnq;
803 for (uint i=0; i < sps.size(); i++ ) {
805 x = sps[i]->XYZ()[0];
806 y = sps[i]->XYZ()[1];
807 z = sps[i]->XYZ()[2];
808 if (pc_exists && pcs[i]->hasCharge()) {
809 q = pcs[i]->charge();
820 out << fixed << setprecision(1);
823 out <<
'"' <<
"runNo" <<
'"' <<
":" <<
'"' <<
fRun <<
'"' <<
"," << endl;
824 out <<
'"' <<
"subRunNo" <<
'"' <<
":" <<
'"' <<
fSubRun <<
'"' <<
"," << endl;
825 out <<
'"' <<
"eventNo" <<
'"' <<
":" <<
'"' <<
fEvent <<
'"' <<
"," << endl;
827 TString geomName(
fGeometry->DetectorName().c_str());
828 if (geomName.Contains(
"35t")) { geomName =
"dune35t"; }
829 else if (geomName.Contains(
"protodune")) { geomName =
"protodune"; }
830 else if (geomName.Contains(
"workspace")) { geomName =
"dune10kt_workspace"; }
831 else if (geomName.Contains(
"icarus")) { geomName =
"icarus"; }
832 else { geomName =
"uboone"; }
833 out <<
'"' <<
"geom" <<
'"' <<
":" <<
'"' << geomName <<
'"' <<
"," << endl;
840 out << fixed << setprecision(0);
844 out <<
'"' <<
"type" <<
'"' <<
":" <<
'"' << option <<
'"' << endl;
852 art::Handle< std::vector<sim::SimEnergyDeposit> > sed_handle;
857 std::vector< art::Ptr<sim::SimEnergyDeposit> > sed;
858 art::fill_ptr_vector(sed, sed_handle);
859 int size = sed.size();
860 double x=0,
y=0,
z=0,
q=0, nq=1;
861 vector<double> vx, vy, vz, vq, vnq;
863 for (
int i=0; i <
size; i++ ) {
865 x = sed[i]->MidPointX();
866 y = sed[i]->MidPointY();
867 z = sed[i]->MidPointZ();
868 q = sed[i]->NumElectrons();
869 if (
q<0)
q = sed[i]->Energy()*25000;
871 if (
q<1000)
continue;
879 out << fixed << setprecision(1);
882 out <<
'"' <<
"runNo" <<
'"' <<
":" <<
'"' <<
fRun <<
'"' <<
"," << endl;
883 out <<
'"' <<
"subRunNo" <<
'"' <<
":" <<
'"' <<
fSubRun <<
'"' <<
"," << endl;
884 out <<
'"' <<
"eventNo" <<
'"' <<
":" <<
'"' <<
fEvent <<
'"' <<
"," << endl;
886 TString geomName(
fGeometry->DetectorName().c_str());
887 if (geomName.Contains(
"35t")) { geomName =
"dune35t"; }
888 else if (geomName.Contains(
"protodune")) { geomName =
"protodune"; }
889 else if (geomName.Contains(
"workspace")) { geomName =
"dune10kt_workspace"; }
890 else if (geomName.Contains(
"icarus")) { geomName =
"icarus"; }
891 else { geomName =
"uboone"; }
892 out <<
'"' <<
"geom" <<
'"' <<
":" <<
'"' << geomName <<
'"' <<
"," << endl;
898 out << fixed << setprecision(0);
902 out <<
'"' <<
"type" <<
'"' <<
":" <<
'"' << option <<
'"' << endl;
911 out <<
'"' << desc <<
'"' <<
":[";
912 for (
int i=0; i<
N; i++) {
919 if (!end) out <<
",";
941 vector<int> children;
943 for (
int j=0; j<nChildren; j++) {
952 vector<int> siblings;
956 siblings.push_back(j);
964 for (
int j=0; j<nSiblings; j++) {
976 art::Handle< std::vector<raw::Trigger>> triggerListHandle;
977 std::vector<art::Ptr<raw::Trigger>> triggerlist;
979 art::fill_ptr_vector(triggerlist, triggerListHandle);
984 if (triggerlist.size()){
1002 if (!
KeepMC(i))
return false;
1007 vector<int> saved_daughters;
1008 for (
int j=0; j<nDaughter; j++) {
1013 saved_daughters.push_back(daughter_id);
1017 vector<double> vx, vy, vz;
1021 int nPoints = traj->GetEntries();
1023 for(
int j=0; j<nPoints; j++) {
1024 TLorentzVector* pos = (TLorentzVector*)(*traj)[j];
1025 vx.push_back(pos->X());
1026 vy.push_back(pos->Y());
1027 vz.push_back(pos->Z());
1031 out << fixed << setprecision(1);
1034 out <<
"\"id\":" <<
id <<
",";
1035 out <<
"\"text\":" <<
"\"" <<
PDGName(
mc_pdg[i]) <<
" " << e <<
" MeV\",";
1036 out <<
"\"data\":{";
1043 out <<
"\"children\":[";
1044 int nSavedDaughter = saved_daughters.size();
1045 if (nSavedDaughter == 0) {
1047 out <<
"\"icon\":" <<
"\"jstree-file\"";
1052 for (
int j=0; j<nSavedDaughter; j++) {
1054 if (j!=nSavedDaughter-1) {
1068 vector<int> primaries;
1074 primaries.push_back(i);
1078 int size = primaries.size();
1080 for (
int i=0; i<
size; i++) {
1092 TLorentzVector particle(momentum);
1093 return particle.E()-particle.M();
1100 double thresh_KE_em = 5.;
1101 double thresh_KE_np = 50;
1111 if (e>=thresh_KE_em)
return true;
1115 if (e>=thresh_KE_np)
return true;
1124 TParticlePDG *
p =
dbPDG->GetParticle(pdg);
1127 int z = (pdg - 1e9) / 10000;
1128 int a = (pdg - 1e9 - z*1e4) / 10;
1130 if (z == 18) name =
"Ar";
1132 else if (z == 17) name =
"Cl";
1133 else if (z == 19) name =
"Ca";
1134 else if (z == 16) name =
"S";
1135 else if (z == 15) name =
"P";
1136 else if (z == 14) name =
"Si";
1137 else if (z == 1) name =
"H";
1138 else if (z == 2) name =
"He";
1140 else return Form(
"%i", pdg);
1141 return Form(
"%s-%i", name.Data(),
a);
1143 return Form(
"%i", pdg);
1146 return p->GetName();
1195 processMap[
"CHIPSNuclearCaptureAtRest"] = 22;
float mc_startMomentum[MAX_TRACKS][4]
std::vector< std::vector< int > > trackChildren
process_name opflash particleana ie ie ie z
process_name opflashCryo1 flashfilter analyze
vector< int > fSIMIDE_channelIdY
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
std::map< int, int > trackIndex
vector< int > fSIMIDE_trackId
process_name opflash particleana ie x
std::string fSimChannelLabel
float mc_endXYZT[MAX_TRACKS][4]
void processRaw(const art::Event &evt)
void processSpacePointTruthDepo(const art::Event &event, TString option, ostream &out=cout)
std::vector< std::vector< int > > trackSiblings
vector< int > of_multiplicity
Definition of basic raw digits.
std::size_t size(FixedBins< T, C > const &) noexcept
Information about charge reconstructed in the active volume.
vector< float > fSIMIDE_y
void processSimChannel(const art::Event &evt)
art::ServiceHandle< geo::Geometry const > fGeometry
std::string fRawDigitLabel
vector< float > fSIMIDE_x
bool DumpMCJSON(int id, ostream &out)
int mc_mother[MAX_TRACKS]
vector< unsigned short > fSIMIDE_tdc
std::map< int, int > savedMCTrackIdMap
float mc_endMomentum[MAX_TRACKS][4]
std::string fOpFlashLabel
int mc_process[MAX_TRACKS]
void analyze(const art::Event &evt)
void print_vector(ostream &out, vector< double > &v, TString desc, bool end=false)
process_name opflash particleana ie ie y
vector< double > oh_bgtime
void processOpFlash(const art::Event &evt)
vector< double > oh_trigtime
Collect all the RawData header files together.
TObjArray * fMC_trackPosition
std::vector< int > fRaw_channelId
auto end(FixedBins< T, C > const &) noexcept
vector< float > fSIMIDE_z
unsigned int fTriggerbits
std::string fSimEnergyDepositLabel
void processOpHit(const art::Event &evt)
std::vector< std::vector< int > > mc_daughters
void beginRun(const art::Run &run)
unsigned int fTriggernumber
double KE(float *momentum)
std::vector< int > fCalib_channelId
void processCalib(const art::Event &evt)
contains information for a single step in the detector simulation
void processMC(const art::Event &evt)
process_name largeant stream1 can override from command line with o or output physics producers generator N
std::vector< std::string > fSpacePointLabels
object containing MC truth information necessary for making RawDigits and doing back tracking ...
TClonesArray * fPEperOpDet
vector< float > fSIMIDE_numElectrons
std::string fTriggerLabel
std::map< std::string, int > processMap
Declaration of basic channel signal object.
float mc_startXYZT[MAX_TRACKS][4]
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
void processSpacePoint(const art::Event &event, TString option, ostream &out=cout)
vector< float > of_peTotal
art framework interface to geometry description
BEGIN_PROLOG could also be cout
void processTrigger(const art::Event &evt)
std::vector< std::vector< int > > trackParents