154 std::unique_ptr< std::vector<gar::rec::TPCCluster> > TPCClusterCol(
new std::vector<gar::rec::TPCCluster>);
155 std::unique_ptr< std::vector<gar::rec::Track> > trkCol(
new std::vector<gar::rec::Track>);
156 std::unique_ptr< art::Assns<gar::rec::TPCCluster,gar::rec::Track> > TPCClusterTrkAssns(new ::art::Assns<gar::rec::TPCCluster,gar::rec::Track>);
159 auto tccdHandle = e.
getValidHandle< std::vector<gar::sdp::CaloDeposit> >(tpcedeptag);
160 auto const& tccds = *tccdHandle;
166 auto const *magFieldService = gar::providerFrom<mag::MagneticFieldService>();
167 G4ThreeVector zerovec(0,0,0);
168 G4ThreeVector magfield = magFieldService->FieldAtPoint(zerovec);
171 std::unordered_map<int, const simb::MCParticle * > TrkIDMap;
178 <<
"Attempting to identify muon hits without MC particles";
180 for (
auto const &mcp : *mcphandle)
182 TrkIDMap[mcp.TrackId()] = &mcp;
189 for (
auto const& cd : tccds)
191 const int trackid = cd.TrackID();
194 auto mcpt = TrkIDMap.find(trackid);
195 if (mcpt == TrkIDMap.end())
continue;
196 if (
std::abs(mcpt->second->PdgCode()) != 13)
continue;
201 float fcpos[3] = {0., 0., 0.};
206 float covmat[6] = {0,0,0,0,0,0};
208 if(energy <= 0.)
continue;
210 TPCClusterCol->emplace_back(energy,
222 auto muIDHandle = e.
getValidHandle< std::vector<gar::sdp::CaloDeposit> >(muidedeptag);
223 auto const& muids = *muIDHandle;
225 for (
auto const& cd : muids)
228 const int trackid = cd.TrackID();
231 auto mcpt = TrkIDMap.find(trackid);
232 if (mcpt == TrkIDMap.end())
continue;
233 if (
std::abs(mcpt->second->PdgCode()) != 13)
continue;
238 float fcpos[3] = {0., 0., 0.};
243 if(energy <= 0.)
continue;
245 float covmat[6] = {0,0,0,0,0,0};
247 TPCClusterCol->emplace_back(energy,
263 std::vector<std::vector<gar::rec::TPCCluster>> TPCClusterColTime;
264 std::vector<gar::rec::TPCCluster> TPCClusterColTimeBlock;
265 std::sort(TPCClusterCol->begin(),TPCClusterCol->end(),
267 {
return a.
Time() <
b.Time(); } );
268 size_t ntpcclus = TPCClusterCol->size();
270 if (ntpcclus!=0&&debug) std::cout<<
"Time start and end: "<<TPCClusterCol->at(0).Time()<<
" "<<TPCClusterCol->at(TPCClusterCol->size()-1).Time()<<
std::endl;
275 startt=TPCClusterCol->at(0).Time();
276 for(
size_t i=0; i<ntpcclus; i++)
279 if(TPCClusterCol->at(i).Time()-startt<=
fTimeBunch)
281 TPCClusterColTimeBlock.push_back(TPCClusterCol->at(i));
285 TPCClusterColTime.push_back(TPCClusterColTimeBlock);
286 startt=TPCClusterCol->at(i).Time();
287 TPCClusterColTimeBlock.clear();
288 TPCClusterColTimeBlock.push_back(TPCClusterCol->at(i));
293 if(TPCClusterColTimeBlock.size()!=0) TPCClusterColTime.push_back(TPCClusterColTimeBlock);
299 for(
size_t c=0;
c<TPCClusterColTime.size();
c++)
302 std::cout <<
"l"<<
c<<
" = { ";
303 for (
size_t d=0;
d<TPCClusterColTime.at(c).size();
d++) {
304 std::cout << TPCClusterColTime.at(c).at(
d).Time() <<
", ";
311 for(
size_t t=0;
t<TPCClusterColTime.size();
t++)
315 std::sort(TPCClusterColTime.at(
t).begin(),TPCClusterColTime.at(
t).end(),
317 {
return a.
Position()[2] <
b.Position()[2]; } );
320 size_t ntpcclust = TPCClusterColTime.at(
t).size();
329 std::list<size_t> unusedTPC;
330 std::list<size_t> TPCplane;
331 std::vector<std::list<size_t>> unusedTPCplanes;
337 startz=TPCClusterColTime.at(
t).at(0).Position()[2];
338 for(
size_t i=0; i<ntpcclust; i++)
340 unusedTPC.push_back(i);
342 if(TPCClusterColTime.at(
t).at(i).Position()[2]-startz<=fZCut3)
344 TPCplane.push_back(i);
348 unusedTPCplanes.push_back(TPCplane);
349 startz=TPCClusterColTime.at(
t).at(i).Position()[2];
351 TPCplane.push_back(i);
356 if(TPCplane.size()!=0) unusedTPCplanes.push_back(TPCplane);
360 for(
size_t i=0; i<ntpcclust; i++)
362 unusedTPC.push_back(i);
367 std::cout <<
"Plane division: p = { ";
368 for (
size_t n : unusedTPC) {
369 std::cout << TPCClusterColTime.at(
t).at(
n).Time() <<
", ";
373 for(
size_t c=0; c<unusedTPCplanes.size(); c++)
375 std::cout <<
"p"<<c<<
" = { ";
376 for (
size_t n : unusedTPCplanes.at(c)) {
377 std::cout << TPCClusterColTime.at(
t).at(
n).Time() <<
", ";
389 std::vector<size_t> besttpcclusindex;
391 for (
size_t i=0; i<unusedTPCplanes.size(); ++i)
393 for (
size_t it : unusedTPCplanes.at(i))
396 for (
size_t j=i+1; j<unusedTPCplanes.size(); ++j)
398 for (
size_t jt : unusedTPCplanes.at(j))
402 for (
size_t k=j+1;
k<unusedTPCplanes.size(); ++
k)
404 for (
size_t kt : unusedTPCplanes.at(
k))
408 std::vector<gar::rec::TPCCluster> triplet;
409 triplet.push_back(TPCClusterCol->at(it));
410 triplet.push_back(TPCClusterCol->at(jt));
411 triplet.push_back(TPCClusterCol->at(kt));
417 std::vector<size_t> tpcclusindex;
421 int tpcclusindexb = -1;
426 for (
size_t kt2 : unusedTPC)
428 const float *k2pos = TPCClusterCol->at(kt2).
Position();
432 if ((TMath::Abs(zcur - k2pos[2]) >
fZCut2) &&
433 (tpcclusindexb > -1) &&
436 tpcclusindex.push_back(tpcclusindexb);
438 sumr2 += dbest*dbest;
447 if (retcode != 0)
continue;
448 if (dist >
fRCut)
continue;
455 if (kt2 == *std::prev(unusedTPC.end(),1) && tpcclusindexb > -1)
457 tpcclusindex.push_back(tpcclusindexb);
458 sumr2 += dbest*dbest;
462 if (tpcclusindex.size() > bestnpts ||
463 ((tpcclusindex.size() == bestnpts) &&
464 (sumr2 < bestsumr2)))
466 bestnpts = tpcclusindex.size();
468 besttpcclusindex = tpcclusindex;
477 for(
size_t i=0;i<bestnpts;i++)
479 unusedTPC.remove(besttpcclusindex.at(i));
480 for(
size_t j=0;j<unusedTPCplanes.size();j++)
482 unusedTPCplanes.at(j).remove(besttpcclusindex.at(i));
488 std::cout<<
"bestnpts: "<<bestnpts<<
std::endl;
489 for(
size_t j=0;j<unusedTPCplanes.size();j++)
491 std::cout<<
"unusedTPCplane"<<j<<
" : "<<unusedTPCplanes.at(j).size()<<
std::endl;
493 std::cout<<
"unusedTPC: "<<unusedTPC.size()<<std::endl<<
std::endl;
500 std::vector<gar::rec::TPCCluster> tcv;
501 for (
size_t i=0;i<besttpcclusindex.size(); ++i)
503 tcv.push_back(TPCClusterCol->at(besttpcclusindex.at(i)));
509 trkCol->push_back(btt);
511 auto const trackpointer = trackPtrMaker(trkCol->size()-1);
512 for (
size_t i=0; i<besttpcclusindex.size(); ++i)
514 auto const tpccluspointer = tpcclusPtrMaker(besttpcclusindex.at(i));
515 TPCClusterTrkAssns->addSingle(tpccluspointer,trackpointer);
521 if (debug) std::cout<<
"Not able to find a new track, stop cycle, done="<<done<<
std::endl;
529 if(debug) std::cout<<
"Found this many tracks: "<<trkCol->size()<<std::endl<<std::endl<<
std::endl;
const float * TrackParBeg() const
Handle< PROD > getHandle(SelectorBase const &) const
static int DistXYZ(const float *trackpar, const float *Xpoint, const float *xyz, float &retDist)
bool fUseOnlyTrueMuonHits
whether to cheat and use true muon hits
float fRCut
Road in the YZ plane to add hits on a circle.
float fTimeBunch
Time bunch spread, in ns.
std::string fInputEdepInstanceTPC
Input instance for TPC edeps.
std::string fInputEdepLabel
Input label for edeps.
const float * Vertex() const
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
std::string fG4ModuleLabel
Use this to get the MCParticles.
gar::rec::Track CreateTrack()
float fSmearX
amount by which to smear X, in cm
bool fIncludeMuIDhits
Include MuID hits as TPCClusters.
std::string fInputEdepInstanceMuID
Input instance for MuID edeps.
constexpr double dist(const TReal *x, const TReal *y, const unsigned int dimension)
const float * Position() const
void digitizeCaloHitsMu2e(gar::sdp::CaloDeposit cd, float *fcpos, float &energy, float &time)
int makepatrectrack(std::vector< gar::rec::TPCCluster > &trackTPCClusters, gar::rec::TrackPar &trackpar)
float fZCut2
Cut to ensure TPC clusters are on the same plane.
cet::coded_exception< error, detail::translate > exception
QTextStream & endl(QTextStream &s)