15 #include "canvas/Persistency/Common/FindManyP.h" 16 #include "canvas/Persistency/Common/FindOneP.h" 18 #include "TPrincipal.h" 19 #include <Fit/Fitter.h> 24 : fCaloAlg(pset.
get<
fhicl::ParameterSet>(
"CalorimetryAlg")), fReader(
"")
43 std::vector<std::string> weightFileBnames = pset.
get<std::vector<std::string>>(
"WeightFiles");
46 for (
auto fileIter = weightFileBnames.begin(); fileIter != weightFileBnames.end(); ++fileIter) {
48 if (!searchPath.find_file(*fileIter, fileWithPath)) {
51 throw cet::exception(
"MVAPID") <<
"Unable to find weight file " << *fileIter
52 <<
" in search path " << searchPath.to_string() <<
std::endl;
58 std::cerr <<
"Mismatch in number of MVA methods and weight files!" <<
std::endl;
62 for (
unsigned int iMethod = 0; iMethod !=
fMVAMethods.size(); ++iMethod) {
70 const double fiducialDist = 5.0;
72 if (pos.X() > (
fDetMinX + fiducialDist) && pos.X() < (
fDetMaxX - fiducialDist) &&
73 pos.Y() > (
fDetMinY + fiducialDist) && pos.Y() < (
fDetMaxY - fiducialDist) &&
118 int pcryo =
id.find(
"C");
119 int ptpc =
id.find(
"T");
120 int pplane =
id.find(
"P");
124 int icryo = std::stoi(scryo);
125 int itpc = std::stoi(stpc);
126 int iplane = std::stoi(splane);
130 std::make_pair(planeKey, -plane.Wire(0).Direction().Z()));
132 std::make_pair(planeKey, plane.Wire(0).Direction().Y()));
138 std::vector<anab::MVAPIDResult>&
result,
142 auto const clockData =
148 for (
auto trackIter =
fTracks.begin(); trackIter !=
fTracks.end(); ++trackIter) {
151 std::vector<double> eVals, eVecs;
155 if (eVals[0] < 0.0001)
158 evalRatio = std::sqrt(eVals[1] * eVals[1] + eVals[2] * eVals[2]) / eVals[0];
160 double coreHaloRatio, concentration, conicalness;
161 this->
_Var_Shape(sortedObj, coreHaloRatio, concentration, conicalness);
176 if (dEdxPenultimate < 0.1)
189 for (
auto showerIter =
fShowers.begin(); showerIter !=
fShowers.end(); ++showerIter) {
192 std::vector<double> eVals, eVecs;
198 if (eVals[0] < 0.0001)
201 evalRatio = std::sqrt(eVals[1] * eVals[1] + eVals[2] * eVals[2]) / eVals[0];
203 this->
SortShower(*showerIter, isStoppingReco, sortedObj);
205 double coreHaloRatio, concentration, conicalness;
206 this->
_Var_Shape(sortedObj, coreHaloRatio, concentration, conicalness);
215 (*showerIter)->ID() + 1000;
223 if (dEdxPenultimate < 0.1)
257 for (
unsigned int iHit = 0; iHit < hitsHandle->size(); ++iHit) {
259 fHits.push_back(hit);
265 for (
unsigned int iTrack = 0; iTrack < tracksHandle->size(); ++iTrack) {
273 for (
unsigned int iShower = 0; iShower < showersHandle->size(); ++iShower) {
281 for (
unsigned int iSP = 0; iSP < spHandle->size(); ++iSP) {
290 for (
unsigned int iSP = 0; iSP <
fSpacePoints.size(); ++iSP) {
298 for (
unsigned int iTrack = 0; iTrack <
fTracks.size(); ++iTrack) {
301 const std::vector<art::Ptr<recob::Hit>> trackHits = findTracksToHits.at(iTrack);
303 for (
unsigned int iHit = 0; iHit < trackHits.size(); ++iHit) {
312 for (
unsigned int iShower = 0; iShower <
fShowers.size(); ++iShower) {
314 const std::vector<art::Ptr<recob::Hit>> showerHits = findShowersToHits.at(iShower);
316 for (
unsigned int iHit = 0; iHit < showerHits.size(); ++iHit) {
329 if (partHandle->size() == 0 || partHandle->at(0).TrackId() != 1) {
330 std::cout <<
"Error, ID of first track in largeant list is not 0" <<
std::endl;
343 sortedTrack.
hitMap.clear();
344 TVector3 trackPoint, trackDir;
345 this->
LinFit(track, trackPoint, trackDir);
347 TVector3 nearestPointStart, nearestPointEnd;
356 trackDir * (trackDir.Dot(track->
Vertex<TVector3>() - trackPoint) / trackDir.Mag2());
357 nearestPointEnd = trackPoint + trackDir * (trackDir.Dot(track->
End<TVector3>() - trackPoint) /
364 trackDir * (trackDir.Dot(track->
End<TVector3>() - trackPoint) / trackDir.Mag2());
367 trackDir * (trackDir.Dot(track->
Vertex<TVector3>() - trackPoint) / trackDir.Mag2());
373 if (track->
End<TVector3>().Z() >=
374 track->
Vertex<TVector3>().
Z()) {
377 trackDir * (trackDir.Dot(track->
Vertex<TVector3>() - trackPoint) / trackDir.Mag2());
378 nearestPointEnd = trackPoint + trackDir * (trackDir.Dot(track->
End<TVector3>() - trackPoint) /
385 trackDir * (trackDir.Dot(track->
End<TVector3>() - trackPoint) / trackDir.Mag2());
388 trackDir * (trackDir.Dot(track->
Vertex<TVector3>() - trackPoint) / trackDir.Mag2());
392 if (trackDir.Z() <= 0) {
393 trackDir.SetX(-trackDir.X());
394 trackDir.SetY(-trackDir.Y());
395 trackDir.SetZ(-trackDir.Z());
399 sortedTrack.
start = nearestPointStart;
400 sortedTrack.
end = nearestPointEnd;
401 sortedTrack.
dir = trackDir;
402 sortedTrack.
length = (nearestPointEnd - nearestPointStart).Mag();
404 std::vector<art::Ptr<recob::Hit>> hits =
fTracksToHits[track];
406 for (
auto hitIter = hits.begin(); hitIter != hits.end(); ++hitIter) {
411 TVector3 nearestPoint =
412 trackPoint + trackDir * (trackDir.Dot(TVector3(sp->
XYZ()) - trackPoint) / trackDir.Mag2());
413 double lengthAlongTrack = (nearestPointStart - nearestPoint).Mag();
425 sortedShower.
hitMap.clear();
429 TVector3 showerEnd(0, 0, 0);
430 double furthestHitFromStart = -999.9;
431 for (
auto hitIter = hits.begin(); hitIter != hits.end(); ++hitIter) {
435 if ((TVector3(sp->
XYZ()) - shower->
ShowerStart()).Mag() > furthestHitFromStart) {
436 showerEnd = TVector3(sp->
XYZ());
437 furthestHitFromStart = (TVector3(sp->
XYZ()) - shower->
ShowerStart()).Mag();
441 TVector3 showerPoint, showerDir;
444 TVector3 nearestPointStart, nearestPointEnd;
453 showerDir * (showerDir.Dot(shower->
ShowerStart() - showerPoint) / showerDir.Mag2());
455 showerPoint + showerDir * (showerDir.Dot(showerEnd - showerPoint) / showerDir.Mag2());
460 showerPoint + showerDir * (showerDir.Dot(showerEnd - showerPoint) / showerDir.Mag2());
463 showerDir * (showerDir.Dot(shower->
ShowerStart() - showerPoint) / showerDir.Mag2());
472 showerDir * (showerDir.Dot(shower->
ShowerStart() - showerPoint) / showerDir.Mag2());
474 showerPoint + showerDir * (showerDir.Dot(showerEnd - showerPoint) / showerDir.Mag2());
479 showerPoint + showerDir * (showerDir.Dot(showerEnd - showerPoint) / showerDir.Mag2());
482 showerDir * (showerDir.Dot(shower->
ShowerStart() - showerPoint) / showerDir.Mag2());
486 if (showerDir.Z() <= 0) {
487 showerDir.SetX(-showerDir.X());
488 showerDir.SetY(-showerDir.Y());
489 showerDir.SetZ(-showerDir.Z());
493 sortedShower.
start = nearestPointStart;
494 sortedShower.
end = nearestPointEnd;
495 sortedShower.
dir = showerDir;
496 sortedShower.
length = (nearestPointEnd - nearestPointStart).Mag();
498 for (
auto hitIter = hits.begin(); hitIter != hits.end(); ++hitIter) {
503 TVector3 nearestPoint =
505 showerDir * (showerDir.Dot(TVector3(sp->
XYZ()) - showerPoint) / showerDir.Mag2());
506 double lengthAlongShower = (nearestPointStart - nearestPoint).Mag();
507 sortedShower.
hitMap.insert(
513 std::vector<double>& eVals,
514 std::vector<double>& eVecs)
516 TPrincipal* principal =
new TPrincipal(3,
"D");
518 for (
auto hitIter = hits.begin(); hitIter != hits.end(); ++hitIter) {
526 principal->MakePrincipals();
528 for (
unsigned int i = 0; i < 3; ++i) {
529 eVals.push_back(principal->GetEigenValues()->GetMatrixArray()[i]);
532 for (
unsigned int i = 0; i < 9; ++i) {
533 eVecs.push_back(principal->GetEigenVectors()->GetMatrixArray()[i]);
538 double& coreHaloRatio,
539 double& concentration,
543 static const unsigned int conMinHits = 3;
544 static const double minCharge = 0.1;
545 static const double conFracRange = 0.2;
546 static const double MoliereRadius = 10.1;
547 static const double MoliereRadiusFraction = 0.2;
549 double totalCharge = 0;
550 double totalChargeStart = 0;
551 double totalChargeEnd = 0;
553 double chargeCore = 0;
554 double chargeHalo = 0;
555 double chargeCon = 0;
556 unsigned int nHits = 0;
559 double chargeConStart = 0;
560 double chargeConEnd = 0;
561 unsigned int nHitsConStart = 0;
562 unsigned int nHitsConEnd = 0;
564 for (
auto hitIter = track.
hitMap.begin(); hitIter != track.
hitMap.end(); ++hitIter) {
568 double distFromTrackFit = ((TVector3(sp->
XYZ()) - track.
start).Cross(track.
dir)).Mag();
572 if (distFromTrackFit < MoliereRadiusFraction * MoliereRadius)
573 chargeCore += hitIter->second->Integral();
575 chargeHalo += hitIter->second->Integral();
577 totalCharge += hitIter->second->Integral();
579 chargeCon += hitIter->second->Integral() /
std::max(1.
E-2, distFromTrackFit);
580 if (hitIter->first / track.
length < conFracRange) {
581 chargeConStart += distFromTrackFit * distFromTrackFit * hitIter->second->Integral();
583 totalChargeStart += hitIter->second->Integral();
585 else if (1. - hitIter->first / track.
length < conFracRange) {
586 chargeConEnd += distFromTrackFit * distFromTrackFit * hitIter->second->Integral();
588 totalChargeEnd += hitIter->second->Integral();
593 coreHaloRatio = chargeHalo / TMath::Max(1.0
E-3, chargeCore);
594 coreHaloRatio = TMath::Min(100.0, coreHaloRatio);
595 concentration = chargeCon / totalCharge;
596 if (nHitsConStart >= conMinHits && nHitsConEnd >= conMinHits && totalChargeEnd > minCharge &&
597 sqrt(chargeConStart) > minCharge && totalChargeStart > minCharge) {
598 conicalness = (sqrt(chargeConEnd) / totalChargeEnd) / (sqrt(chargeConStart) / totalChargeStart);
613 double trackLength = (track.
end - track.
start).Mag();
614 return CalcSegmentdEdxDist(clock_data, det_prop, track, start * trackLength, end * trackLength);
624 double trackLength = (track.
end - track.
start).Mag();
625 return CalcSegmentdEdxDist(clock_data, det_prop, track, trackLength - distAtEnd, trackLength);
637 double totaldEdx = 0;
638 unsigned int nHits = 0;
641 for (
auto hitIter = track.
hitMap.begin(); hitIter != track.
hitMap.end(); ++hitIter) {
643 if (hitIter->first < start)
continue;
644 if (hitIter->first >= end)
break;
652 double xComponent, pitch3D;
666 xComponent = yzPitch * dir[0] / sqrt(dir[1] * dir[1] + dir[2] * dir[2]);
667 pitch3D = sqrt(xComponent * xComponent + yzPitch * yzPitch);
676 return nHits ? totaldEdx / nHits : 0;
686 unsigned int iPt = 0;
687 for (
auto spIter = sp.begin(); spIter != sp.end(); ++spIter) {
688 TVector3 point = (*spIter)->XYZ();
689 grFit.SetPoint(iPt++, point.X(), point.Y(), point.Z());
693 ROOT::Fit::Fitter fitter;
697 ROOT::Math::Functor fcn(sdist, 6);
700 TVector3 trackStart = track->
Vertex<TVector3>();
701 TVector3 trackEnd = track->
End<TVector3>();
702 trackDir = (trackEnd - trackStart).Unit();
704 TVector3 x0 = trackStart - trackDir;
705 TVector3 u = trackDir;
707 double pStart[6] = {x0.X(), u.X(), x0.Y(), u.Y(), x0.Z(), u.Z()};
709 fitter.SetFCN(fcn, pStart);
711 bool ok = fitter.FitFCN();
713 trackPoint.SetXYZ(x0.X(), x0.Y(), x0.Z());
714 trackDir.SetXYZ(u.X(), u.Y(), u.Z());
715 trackDir = trackDir.Unit();
719 const ROOT::Fit::FitResult&
result = fitter.Result();
720 const double* parFit = result.GetParams();
721 trackPoint.SetXYZ(parFit[0], parFit[2], parFit[4]);
722 trackDir.SetXYZ(parFit[1], parFit[3], parFit[5]);
723 trackDir = trackDir.Unit();
730 TVector3& showerPoint,
737 unsigned int iPt = 0;
738 for (
auto spIter = sp.begin(); spIter != sp.end(); ++spIter) {
739 TVector3 point = (*spIter)->XYZ();
740 grFit.SetPoint(iPt++, point.X(), point.Y(), point.Z());
744 ROOT::Fit::Fitter fitter;
748 ROOT::Math::Functor fcn(sdist, 6);
754 TVector3 x0 = showerStart - showerDir;
755 TVector3 u = showerDir;
757 double pStart[6] = {x0.X(), u.X(), x0.Y(), u.Y(), x0.Z(), u.Z()};
759 fitter.SetFCN(fcn, pStart);
761 bool ok = fitter.FitFCN();
763 showerPoint.SetXYZ(x0.X(), x0.Y(), x0.Z());
764 showerDir.SetXYZ(u.X(), u.Y(), u.Z());
765 showerDir = showerDir.Unit();
769 const ROOT::Fit::FitResult&
result = fitter.Result();
770 const double* parFit = result.GetParams();
771 showerPoint.SetXYZ(parFit[0], parFit[2], parFit[4]);
772 showerDir.SetXYZ(parFit[1], parFit[3], parFit[5]);
773 showerDir = showerDir.Unit();
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
std::vector< art::Ptr< recob::Shower > > fShowers
const TVector3 & ShowerStart() const
anab::MVAPIDResult fResHolder
double CalcSegmentdEdxDistAtEnd(const detinfo::DetectorClocksData &clock_data, const detinfo::DetectorPropertiesData &det_prop, const mvapid::MVAAlg::SortedObj &track, double distAtEnd)
const calo::CalorimetryAlg fCaloAlg
unsigned int TotalNTPC() const
Returns the total number of TPCs in the detector.
int LinFit(const art::Ptr< recob::Track > track, TVector3 &trackPoint, TVector3 &trackDir)
void RunPCA(std::vector< art::Ptr< recob::Hit >> &hits, std::vector< double > &eVals, std::vector< double > &eVecs)
IteratorBox< plane_iterator,&GeometryCore::begin_plane,&GeometryCore::end_plane > IteratePlanes() const
Enables ranged-for loops on all planes of the detector.
std::map< art::Ptr< recob::Hit >, art::Ptr< recob::SpacePoint > > fHitsToSpacePoints
std::vector< std::string > fMVAMethods
geo::WireID WireID() const
double MinX() const
Returns the world x coordinate of the start of the box.
CryostatID_t Cryostat
Index of cryostat.
int IsInActiveVol(const TVector3 &pos)
std::map< art::Ptr< recob::Track >, std::vector< art::Ptr< recob::Hit > > > fTracksToHits
double MaxX() const
Returns the world x coordinate of the end of the box.
std::string fSpacePointLabel
void PrepareEvent(const art::Event &event, const detinfo::DetectorClocksData &clockData)
MVAAlg(fhicl::ParameterSet const &pset)
void RunPID(art::Event &evt, std::vector< anab::MVAPIDResult > &result, art::Assns< recob::Track, anab::MVAPIDResult, void > &trackAssns, art::Assns< recob::Shower, anab::MVAPIDResult, void > &showerAssns)
geo::Length_t WirePitch(geo::PlaneID const &planeid) const
Returns the distance between two consecutive wires.
art framework interface to geometry description
std::vector< art::Ptr< recob::Hit > > fHits
int LinFitShower(const art::Ptr< recob::Shower > shower, TVector3 &showerPoint, TVector3 &showerDir)
void SortShower(art::Ptr< recob::Shower > shower, int &isStoppingReco, mvapid::MVAAlg::SortedObj &sortedShower)
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
double dEdx(float dqdx, float Efield)
unsigned int Nplanes(unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wire planes in the specified TPC.
std::map< double, const art::Ptr< recob::Hit > > hitMap
std::map< int, double > fNormToWiresY
T get(std::string const &key) const
std::vector< art::Ptr< recob::SpacePoint > > fSpacePoints
Geometry information for a single wire plane.The plane is represented in the geometry by a solid whic...
Point_t const & Vertex() const
double MinZ() const
Returns the world z coordinate of the start of the box.
bool CreateAssn(PRODUCER const &prod, art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t indx=UINT_MAX)
Creates a single one-to-one association.
TLorentzVector fVertex4Vect
const TVector3 & Direction() const
std::map< art::Ptr< recob::Shower >, std::vector< art::Ptr< recob::Hit > > > fShowersToHits
static int max(int a, int b)
PlaneID_t Plane
Index of the plane within its TPC.
Detector simulation of raw signals on wires.
double MaxY() const
Returns the world y coordinate of the end of the box.
std::string fTrackingLabel
unsigned int NTPC(unsigned int cstat=0) const
Returns the total number of TPCs in the specified cryostat.
double CalcSegmentdEdxFrac(const detinfo::DetectorClocksData &clock_data, const detinfo::DetectorPropertiesData &det_prop, const SortedObj &track, double start, double end)
Contains all timing reference information for the detector.
std::map< art::Ptr< recob::Track >, std::vector< art::Ptr< recob::SpacePoint > > > fTracksToSpacePoints
double MaxZ() const
Returns the world z coordinate of the end of the box.
double dEdx_AREA(detinfo::DetectorClocksData const &clock_data, detinfo::DetectorPropertiesData const &det_prop, recob::Hit const &hit, double pitch, double T0=0) const
void _Var_Shape(const SortedObj &track, double &coreHaloRatio, double &concentration, double &conicalness)
std::vector< art::Ptr< recob::Track > > fTracks
int trigger_offset(DetectorClocksData const &data)
const Double32_t * XYZ() const
Point_t const & End() const
TPCGeo const & TPC(unsigned int const tpc=0, unsigned int const cstat=0) const
Returns the specified TPC.
std::map< art::Ptr< recob::Shower >, std::vector< art::Ptr< recob::SpacePoint > > > fShowersToSpacePoints
auto const & get(AssnsNode< L, R, D > const &r)
TPCID_t TPC
Index of the TPC within its cryostat.
std::map< std::string, double > mvaOutput
std::map< art::Ptr< recob::SpacePoint >, art::Ptr< recob::Hit > > fSpacePointsToHits
void FitAndSortTrack(art::Ptr< recob::Track > track, int &isStoppingReco, SortedObj &sortedObj)
std::map< int, double > fNormToWiresZ
double MinY() const
Returns the world y coordinate of the start of the box.
double CalcSegmentdEdxDist(const detinfo::DetectorClocksData &clock_data, const detinfo::DetectorPropertiesData &det_prop, const SortedObj &track, double start, double end)
cet::coded_exception< error, detail::translate > exception
QTextStream & endl(QTextStream &s)
std::vector< std::string > fWeightFiles