13 #include "TPrincipal.h" 20 #include <Fit/Fitter.h> 29 fCaloAlg(pset.get<
fhicl::ParameterSet>(
"CalorimetryAlg")),
30 fParentModule(parentModule),fReader(
""){
48 std::vector<std::string> weightFileBnames=pset.
get<std::vector<std::string> >(
"WeightFiles");
50 cet::search_path searchPath(
"FW_SEARCH_PATH");
51 for(
auto fileIter=weightFileBnames.begin();fileIter!=weightFileBnames.end();++fileIter){
52 std::string fileWithPath;
53 if(!searchPath.find_file(*fileIter,fileWithPath)){
56 throw cet::exception(
"MVAPID")<<
"Unable to find weight file "<<*fileIter<<
" in search path " 57 <<searchPath.to_string()<<std::endl;
63 std::cerr<<
"Mismatch in number of MVA methods and weight files!"<<std::endl;
67 for(
unsigned int iMethod=0;iMethod!=
fMVAMethods.size();++iMethod){
78 const double fiducialDist = 5.0;
80 if(pos.X() > (
fDetMinX + fiducialDist) && pos.X() < (
fDetMaxX - fiducialDist)
81 && pos.Y() > (
fDetMinY + fiducialDist) && pos.Y() < (
fDetMaxY - fiducialDist)
82 && pos.Z() > (
fDetMinZ + fiducialDist) && pos.Z() < (
fDetMaxZ - fiducialDist))
101 for(
unsigned int t=0; t<geom->
TotalNTPC(); t++)
131 std::string
id = std::string(plane.ID());
132 int pcryo =
id.find(
"C");
133 int ptpc =
id.find(
"T");
134 int pplane =
id.find(
"P");
135 std::string scryo =
id.substr(pcryo+2,2);
136 std::string stpc =
id.substr(ptpc+2,2);
137 std::string splane =
id.substr(pplane+2,2);
138 int icryo = std::stoi(scryo);
139 int itpc = std::stoi(stpc);
140 int iplane = std::stoi(splane);
141 planeKey = icryo * geom->
NTPC(0) * geom->
Nplanes(0, 0) + itpc * geom->
Nplanes(0, 0) + iplane;
142 fNormToWiresY.insert(std::make_pair(planeKey, -plane.Wire(0).Direction().Z()));
143 fNormToWiresZ.insert(std::make_pair(planeKey, plane.Wire(0).Direction().Y()));
154 for(
auto trackIter=
fTracks.begin();trackIter!=
fTracks.end();++trackIter){
157 std::vector<double> eVals,eVecs;
161 if(eVals[0] < 0.0001)
164 evalRatio=std::sqrt(eVals[1]*eVals[1]+eVals[2]*eVals[2])/eVals[0];
166 double coreHaloRatio,concentration,conicalness;
167 this->
_Var_Shape(sortedObj,coreHaloRatio,concentration,conicalness);
196 if(dEdxPenultimate < 0.1)
209 for(
auto showerIter=
fShowers.begin();showerIter!=
fShowers.end();++showerIter){
212 std::vector<double> eVals,eVecs;
221 evalRatio=std::sqrt(eVals[1]*eVals[1]+eVals[2]*eVals[2])/eVals[0];
224 this->
SortShower(*showerIter,isStoppingReco,sortedObj);
226 double coreHaloRatio,concentration,conicalness;
227 this->
_Var_Shape(sortedObj,coreHaloRatio,concentration,conicalness);
257 if(dEdxPenultimate < 0.1)
285 auto const* detProp = lar::providerFrom<detinfo::DetectorPropertiesService>();
291 for (
unsigned int iHit = 0; iHit < hitsHandle->size(); ++iHit){
293 fHits.push_back(hit);
299 for (
unsigned int iTrack = 0; iTrack < tracksHandle->size(); ++iTrack){
307 for (
unsigned int iShower = 0; iShower < showersHandle->size(); ++iShower){
315 for (
unsigned int iSP = 0; iSP < spHandle->size(); ++iSP){
324 for(
unsigned int iSP = 0; iSP <
fSpacePoints.size(); ++iSP)
333 for(
unsigned int iTrack = 0; iTrack <
fTracks.size(); ++iTrack){
336 const std::vector< art::Ptr<recob::Hit> > trackHits = findTracksToHits.at(iTrack);
338 for (
unsigned int iHit=0; iHit<trackHits.size(); ++iHit)
348 for(
unsigned int iShower = 0; iShower <
fShowers.size(); ++iShower){
350 const std::vector< art::Ptr<recob::Hit> > showerHits = findShowersToHits.at(iShower);
352 for (
unsigned int iHit=0; iHit<showerHits.size(); ++iHit)
366 if(partHandle->size()==0||partHandle->at(0).TrackId()!=1){
367 std::cout<<
"Error, ID of first track in largeant list is not 0"<<std::endl;
377 sortedTrack.
hitMap.clear();
378 TVector3 trackPoint,trackDir;
379 this->
LinFit(track,trackPoint,trackDir);
381 TVector3 nearestPointStart, nearestPointEnd;
387 nearestPointStart = trackPoint+trackDir*(trackDir.Dot(track->
Vertex<TVector3>()-trackPoint)/trackDir.Mag2());
388 nearestPointEnd = trackPoint+trackDir*(trackDir.Dot(track->
End<TVector3>()-trackPoint)/trackDir.Mag2());
392 nearestPointStart = trackPoint+trackDir*(trackDir.Dot(track->
End<TVector3>()-trackPoint)/trackDir.Mag2());
393 nearestPointEnd = trackPoint+trackDir*(trackDir.Dot(track->
Vertex<TVector3>()-trackPoint)/trackDir.Mag2());
399 if(track->
End<TVector3>().Z() >= track->
Vertex<TVector3>().
Z()){
400 nearestPointStart = trackPoint+trackDir*(trackDir.Dot(track->
Vertex<TVector3>()-trackPoint)/trackDir.Mag2());
401 nearestPointEnd = trackPoint+trackDir*(trackDir.Dot(track->
End<TVector3>()-trackPoint)/trackDir.Mag2());
405 nearestPointStart = trackPoint+trackDir*(trackDir.Dot(track->
End<TVector3>()-trackPoint)/trackDir.Mag2());
406 nearestPointEnd = trackPoint+trackDir*(trackDir.Dot(track->
Vertex<TVector3>()-trackPoint)/trackDir.Mag2());
410 if(trackDir.Z() <= 0){
411 trackDir.SetX(-trackDir.X());
412 trackDir.SetY(-trackDir.Y());
413 trackDir.SetZ(-trackDir.Z());
417 sortedTrack.
start=nearestPointStart;
418 sortedTrack.
end=nearestPointEnd;
419 sortedTrack.
dir=trackDir;
420 sortedTrack.
length=(nearestPointEnd-nearestPointStart).Mag();
424 for(
auto hitIter=hits.begin();hitIter!=hits.end();++hitIter){
429 TVector3 nearestPoint = trackPoint+trackDir*(trackDir.Dot(TVector3(sp->
XYZ())-trackPoint)/trackDir.Mag2());
430 double lengthAlongTrack=(nearestPointStart-nearestPoint).Mag();
439 sortedShower.
hitMap.clear();
443 TVector3 showerEnd(0, 0, 0);
444 double furthestHitFromStart = -999.9;
445 for(
auto hitIter=hits.begin();hitIter!=hits.end();++hitIter){
449 if((TVector3(sp->
XYZ()) - shower->
ShowerStart()).Mag() > furthestHitFromStart)
451 showerEnd = TVector3(sp->
XYZ());
452 furthestHitFromStart = (TVector3(sp->
XYZ()) - shower->
ShowerStart()).Mag();
456 TVector3 showerPoint,showerDir;
459 TVector3 nearestPointStart, nearestPointEnd;
465 nearestPointStart = showerPoint+showerDir*(showerDir.Dot(shower->
ShowerStart()-showerPoint)/showerDir.Mag2());
466 nearestPointEnd = showerPoint+showerDir*(showerDir.Dot(showerEnd-showerPoint)/showerDir.Mag2());
471 nearestPointStart = showerPoint+showerDir*(showerDir.Dot(showerEnd-showerPoint)/showerDir.Mag2());
472 nearestPointEnd = showerPoint+showerDir*(showerDir.Dot(shower->
ShowerStart()-showerPoint)/showerDir.Mag2());
479 nearestPointStart = showerPoint+showerDir*(showerDir.Dot(shower->
ShowerStart()-showerPoint)/showerDir.Mag2());
480 nearestPointEnd = showerPoint+showerDir*(showerDir.Dot(showerEnd-showerPoint)/showerDir.Mag2());
484 nearestPointStart = showerPoint+showerDir*(showerDir.Dot(showerEnd-showerPoint)/showerDir.Mag2());
485 nearestPointEnd = showerPoint+showerDir*(showerDir.Dot(shower->
ShowerStart()-showerPoint)/showerDir.Mag2());
489 if(showerDir.Z() <= 0){
490 showerDir.SetX(-showerDir.X());
491 showerDir.SetY(-showerDir.Y());
492 showerDir.SetZ(-showerDir.Z());
496 sortedShower.
start=nearestPointStart;
497 sortedShower.
end=nearestPointEnd;
499 sortedShower.
dir=showerDir;
500 sortedShower.
length=(nearestPointEnd-nearestPointStart).Mag();
502 for(
auto hitIter=hits.begin();hitIter!=hits.end();++hitIter){
507 TVector3 nearestPoint = showerPoint+showerDir*(showerDir.Dot(TVector3(sp->
XYZ())-showerPoint)/showerDir.Mag2());
508 double lengthAlongShower=(nearestPointStart-nearestPoint).Mag();
516 TPrincipal* principal =
new TPrincipal(3,
"D");
521 for(
auto hitIter=
hits.begin();hitIter!=
hits.end();++hitIter){
529 principal->MakePrincipals();
531 for(
unsigned int i=0;i<3;++i){
532 eVals.push_back(principal->GetEigenValues()->GetMatrixArray()[i]);
535 for(
unsigned int i=0;i<9;++i){
536 eVecs.push_back(principal->GetEigenVectors()->GetMatrixArray()[i]);
540 double& coreHaloRatio,
double& concentration,
541 double& conicalness){
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;
550 double totalCharge=0;
551 double totalChargeStart=0;
552 double totalChargeEnd=0;
557 unsigned int nHits = 0;
560 double chargeConStart=0;
561 double chargeConEnd=0;
562 unsigned int nHitsConStart=0;
563 unsigned int nHitsConEnd=0;
566 for(
auto hitIter=track.
hitMap.begin();hitIter!=track.
hitMap.end();++hitIter){
570 double distFromTrackFit = ((TVector3(sp->
XYZ()) - track.
start).Cross(track.
dir)).Mag();
574 if(distFromTrackFit < MoliereRadiusFraction * MoliereRadius)
575 chargeCore += hitIter->second->Integral();
577 chargeHalo += hitIter->second->Integral();
579 totalCharge += hitIter->second->Integral();
581 chargeCon += hitIter->second->Integral() /
std::max(1.
E-2,distFromTrackFit);
582 if(hitIter->first/track.
length<conFracRange){
583 chargeConStart+=distFromTrackFit*distFromTrackFit*hitIter->second->Integral();
585 totalChargeStart+=hitIter->second->Integral();
587 else if(1.-hitIter->first/track.
length<conFracRange){
588 chargeConEnd+=distFromTrackFit*distFromTrackFit*hitIter->second->Integral();
590 totalChargeEnd+=hitIter->second->Integral();
595 coreHaloRatio=chargeHalo/TMath::Max(1.0
E-3, chargeCore);
596 coreHaloRatio=TMath::Min(100.0, coreHaloRatio);
597 concentration=chargeCon/totalCharge;
598 if(nHitsConStart>=conMinHits&&nHitsConEnd>=conMinHits&&totalChargeEnd>minCharge&&sqrt(chargeConStart)>minCharge&&totalChargeStart>minCharge){
599 conicalness=(sqrt(chargeConEnd)/totalChargeEnd) / (sqrt(chargeConStart)/totalChargeStart);
608 double trackLength=(track.
end-track.
start).Mag();
614 double trackLength=(track.
end-track.
start).Mag();
623 unsigned int nHits=0;
626 for (
auto hitIter = track.
hitMap.begin(); hitIter!=track.
hitMap.end(); ++hitIter ){
628 if(hitIter->first<start)
continue;
629 if(hitIter->first>=end)
break;
635 double xComponent, pitch3D;
648 xComponent = yzPitch * dir[0] / sqrt(dir[1] * dir[1] + dir[2] * dir[2]);
649 pitch3D = sqrt(xComponent * xComponent + yzPitch * yzPitch);
658 return nHits?totaldEdx/nHits:0;
667 for(
auto spIter=sp.begin();spIter!=sp.end();++spIter){
668 TVector3 point=(*spIter)->XYZ();
669 grFit.SetPoint(iPt++,point.X(),point.Y(),point.Z());
673 ROOT::Fit::Fitter fitter;
677 ROOT::Math::Functor fcn(sdist,6);
680 TVector3 trackStart=track->
Vertex<TVector3>();
681 TVector3 trackEnd=track->
End<TVector3>();
682 trackDir=(trackEnd-trackStart).Unit();
684 TVector3 x0=trackStart-trackDir;
687 double pStart[6] = {x0.X(),u.X(),x0.Y(),u.Y(),x0.Z(),u.Z()};
689 fitter.SetFCN(fcn,pStart);
691 bool ok = fitter.FitFCN();
693 trackPoint.SetXYZ(x0.X(),x0.Y(),x0.Z());
694 trackDir.SetXYZ(u.X(),u.Y(),u.Z());
695 trackDir=trackDir.Unit();
699 const ROOT::Fit::FitResult & result = fitter.Result();
700 const double * parFit = result.GetParams();
701 trackPoint.SetXYZ(parFit[0],parFit[2],parFit[4]);
702 trackDir.SetXYZ(parFit[1],parFit[3],parFit[5]);
703 trackDir=trackDir.Unit();
714 for(
auto spIter=sp.begin();spIter!=sp.end();++spIter){
715 TVector3 point=(*spIter)->XYZ();
716 grFit.SetPoint(iPt++,point.X(),point.Y(),point.Z());
720 ROOT::Fit::Fitter fitter;
724 ROOT::Math::Functor fcn(sdist,6);
730 TVector3 x0=showerStart-showerDir;
731 TVector3 u=showerDir;
733 double pStart[6] = {x0.X(),u.X(),x0.Y(),u.Y(),x0.Z(),u.Z()};
735 fitter.SetFCN(fcn,pStart);
737 bool ok = fitter.FitFCN();
739 showerPoint.SetXYZ(x0.X(),x0.Y(),x0.Z());
740 showerDir.SetXYZ(u.X(),u.Y(),u.Z());
741 showerDir=showerDir.Unit();
745 const ROOT::Fit::FitResult & result = fitter.Result();
746 const double * parFit = result.GetParams();
747 showerPoint.SetXYZ(parFit[0],parFit[2],parFit[4]);
748 showerDir.SetXYZ(parFit[1],parFit[3],parFit[5]);
749 showerDir=showerDir.Unit();
std::vector< art::Ptr< recob::Shower > > fShowers
double CalcSegmentdEdxDist(const SortedObj &track, double start, double end)
const TVector3 & ShowerStart() const
anab::MVAPIDResult fResHolder
std::map< art::Ptr< recob::Hit >, art::Ptr< recob::SpacePoint > > fHitsToSpacePoints
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)
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::Track >, std::vector< art::Ptr< recob::SpacePoint > > > fTracksToSpacePoints
double dEdx_AREA(art::Ptr< recob::Hit > hit, double pitch, double T0=0) const
std::vector< std::string > fMVAMethods
double CalcSegmentdEdxFrac(const SortedObj &track, double start, double end)
geo::WireID WireID() const
Initial tdc tick for hit.
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)
double MaxX() const
Returns the world x coordinate of the end of the box.
std::string fSpacePointLabel
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.
std::vector< art::Ptr< recob::Hit > > fHits
void RunPCA(std::vector< art::Ptr< recob::Hit > > &hits, std::vector< double > &eVals, std::vector< double > &eVecs)
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)
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
std::map< art::Ptr< recob::Shower >, std::vector< art::Ptr< recob::Hit > > > fShowersToHits
unsigned int Nplanes(unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wire planes in the specified TPC.
void PrepareEvent(const art::Event &event)
std::map< art::Ptr< recob::Track >, std::vector< art::Ptr< recob::Hit > > > fTracksToHits
T get(std::string const &key) const
const art::EDProducer * fParentModule
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
Access to track position at different points.
MVAAlg(fhicl::ParameterSet const &pset, const art::EDProducer *parentModule)
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.
const Double32_t * XYZ() const
TLorentzVector fVertex4Vect
const TVector3 & Direction() const
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.
std::map< double, const art::Ptr< recob::Hit > > hitMap
std::map< art::Ptr< recob::Shower >, std::vector< art::Ptr< recob::SpacePoint > > > fShowersToSpacePoints
Utility object to perform functions of association.
void reconfigure(fhicl::ParameterSet const &p)
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
double MaxZ() const
Returns the world z coordinate of the end of the box.
void _Var_Shape(const SortedObj &track, double &coreHaloRatio, double &concentration, double &conicalness)
std::vector< art::Ptr< recob::Track > > fTracks
Point_t const & End() const
Access to track position at different points.
std::map< int, double > fNormToWiresY
std::map< art::Ptr< recob::SpacePoint >, art::Ptr< recob::Hit > > fSpacePointsToHits
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
TPCGeo const & TPC(unsigned int const tpc=0, unsigned int const cstat=0) const
Returns the specified TPC.
TPCID_t TPC
Index of the TPC within its cryostat.
std::map< std::string, double > mvaOutput
void FitAndSortTrack(art::Ptr< recob::Track > track, int &isStoppingReco, SortedObj &sortedObj)
double MinY() const
Returns the world y coordinate of the start of the box.
double CalcSegmentdEdxDistAtEnd(const mvapid::MVAAlg::SortedObj &track, double distAtEnd)
std::map< int, double > fNormToWiresZ
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
std::vector< std::string > fWeightFiles