Skip to content

Commit

Permalink
Merge pull request #813 from brownd1978/dsfilter
Browse files Browse the repository at this point in the history
Small cleanups.  Create a pre-mixing filter for comics
  • Loading branch information
kutschke authored Jun 15, 2022
2 parents 39cb6f9 + 7e74ba7 commit 7ae8500
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 58 deletions.
31 changes: 31 additions & 0 deletions Analyses/fcl/TargetStopsDumper.fcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Andrei Gaponenko, 2015

#include "Offline/fcl/minimalMessageService.fcl"
#include "Offline/fcl/standardServices.fcl"

process_name : TGTstops

source : { module_type : RootInput }

services : {
message: @local::default_message
TFileService: { fileName: "TargetStops.root" }
}

physics: {

analyzers: {
genCountLogger: { module_type: GenEventCountReader }

stoppedMuonDumper: {
module_type: StoppedParticlesDumper
dumpSimParticleLeaves: true
inputCollection: "TargetStopFilter"
writeProperTime : false
writeParticleCodes : true
}
}

an: [ genCountLogger, stoppedMuonDumper ]
end_paths: [an]
}
187 changes: 187 additions & 0 deletions Filters/src/CosmicMixingFilter_module.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Select cosmic events for mixing: this requires a 'signal-like' particle in the tracker
// Original author; Dave Brown (LBNL) 2022

#include <string>
#include <map>
#include <sstream>

// art includes.
#include "fhiclcpp/ParameterSet.h"
#include "fhiclcpp/types/Atom.h"
#include "fhiclcpp/types/Sequence.h"
#include "fhiclcpp/types/OptionalTable.h"
#include "cetlib_except/exception.h"
#include "art/Framework/Core/EDFilter.h"
#include "art/Framework/Principal/Event.h"
#include "art/Framework/Principal/Run.h"
#include "art/Framework/Principal/Handle.h"
#include "Offline/DataProducts/inc/PDGCode.hh"
#include "messagefacility/MessageLogger/MessageLogger.h"
#include "Offline/MCDataProducts/inc/StepPointMC.hh"
#include "Offline/MCDataProducts/inc/StrawGasStep.hh"
#include "Offline/MCDataProducts/inc/CaloShowerStep.hh"
#include "Offline/MCDataProducts/inc/CrvStep.hh"
#include "Offline/MCDataProducts/inc/SimParticle.hh"
#include "Offline/RecoDataProducts/inc/RobustHelix.hh"
#include "Offline/ConditionsService/inc/ConditionsHandle.hh"
#include "Offline/ConditionsService/inc/AcceleratorParams.hh"
#include "Offline/TrkReco/inc/TrkUtilities.hh"
#include "Offline/GeometryService/inc/GeomHandle.hh"
#include "Offline/GeometryService/inc/DetectorSystem.hh"
#include "Offline/BFieldGeom/inc/BFieldManager.hh"
#include "Offline/GlobalConstantsService/inc/GlobalConstantsHandle.hh"
#include "Offline/GlobalConstantsService/inc/ParticleDataList.hh"
#include <map>
namespace mu2e {

class CosmicMixingFilter : public art::EDFilter {
public:

struct TrkHitCount {
unsigned nhits_;
uint16_t minplane_, maxplane_;
std::set<unsigned> planes_;
};

struct TimeCutConfig {
using Name=fhicl::Name;
using Comment=fhicl::Comment;
fhicl::Atom<double> maxTime { Name("MaximumTime"), Comment("Maximum time for good step (ns since POT)")};
fhicl::Atom<double> minTime { Name("MinimumTime"), Comment("Minimum time for good step (ns since POT)")};
};

struct Config {
using Name=fhicl::Name;
using Comment=fhicl::Comment;

fhicl::Atom<art::InputTag> trkSteps { Name("StrawGasSteps"), Comment("StrawGasStep collection") };
fhicl::Atom<int> diagLevel { Name("DiagLevel"), Comment("Diag printout level"),0 };
fhicl::Atom<double> minTrkStepEnergy { Name("MinimumTrkStepEnergy"), Comment("Minimum Trk step energy"), 0.0 };
fhicl::Atom<double> minPartMom { Name("MinimumPartMom"), Comment("Minimum particle momentum") };
fhicl::Atom<double> maxPartMom { Name("MaximumPartMom"), Comment("Maximum particle momentum")};
fhicl::Atom<unsigned> minNTrkSteps { Name("MinimumTrkSteps"), Comment("Minimum number of good tracker steps"), 10};
fhicl::Atom<unsigned> minNTrkPlanes { Name("MinimumTrkPlanes"), Comment("Minimum number of good tracker planes")};
fhicl::Atom<unsigned> minTrkPlaneSpan { Name("MinimumTrkPlaneSpan"), Comment("Minimum gap between good hittracker planes")};
fhicl::Atom<double> maxImpact { Name("MaxImpact"), Comment("Maximum impact parameter, based on tracker info")};
fhicl::OptionalTable<TimeCutConfig> timeCutConfig { fhicl::Name("TimeCutConfig") };

};

using Parameters = art::EDFilter::Table<Config>;
explicit CosmicMixingFilter(const Parameters& conf);
bool filter(art::Event& event) override;
void endJob() override;

private:
bool timeCut(double time) const; // particle time
art::InputTag trkStepCol_;
int diagLevel_;
double minTrkE_, minPartM_, maxPartM_;
unsigned minNTrk_, minNPlanes_, minPlaneSpan_;
double maxImpact_;
bool timecut_;
double minTime_, maxTime_;
unsigned nEvt_, nPassed_;
};

//================================================================
CosmicMixingFilter::CosmicMixingFilter(const Parameters& conf) : art::EDFilter{conf}
, trkStepCol_(conf().trkSteps())
, diagLevel_(conf().diagLevel())
, minTrkE_(conf().minTrkStepEnergy())
, minPartM_(conf().minPartMom())
, maxPartM_(conf().maxPartMom())
, minNTrk_(conf().minNTrkSteps())
, minNPlanes_(conf().minNTrkPlanes())
, minPlaneSpan_(conf().minTrkPlaneSpan())
, maxImpact_(conf().maxImpact())
, timecut_(false)
, minTime_(0.0), maxTime_(0.0)
, nEvt_(0)
, nPassed_(0)
{
consumes<StrawGasStepCollection>(trkStepCol_);
TimeCutConfig tc;
if(conf().timeCutConfig(tc)) {
timecut_ = true;
minTime_ = tc.minTime();
maxTime_ = tc.maxTime();
}
}

// input must be a physical time!
bool CosmicMixingFilter::timeCut(double ptime) const { return (!timecut_) || (ptime > minTime_ && ptime < maxTime_); }

bool CosmicMixingFilter::filter(art::Event& event) {
ConditionsHandle<AcceleratorParams> accPar("ignored");
double mbtime = accPar->deBuncherPeriod;
GlobalConstantsHandle<ParticleDataList> pdt;
GeomHandle<DetectorSystem> det;
GeomHandle<BFieldManager> bfmgr;
bool selecttrk(false);
++nEvt_;
// Count Trk step from same particle
using CT = std::map<const SimParticle*,TrkHitCount>;
CT counttrk;
auto sgscolH = event.getValidHandle<StrawGasStepCollection>(trkStepCol_);
for(const auto& sgs : *sgscolH ) {
double mom = sgs.momentum().R();
if(sgs.ionizingEdep() > minTrkE_ &&
mom > minPartM_ && mom < maxPartM_ &&
timeCut(fmod(sgs.time(),mbtime))) {
auto ifndhit = counttrk.find(sgs.simParticle().get());
if(ifndhit == counttrk.end()){
TrkHitCount thc;
thc.nhits_ = 1;
thc.planes_.insert(sgs.strawId().plane());
thc.minplane_ = thc.maxplane_ = sgs.strawId().plane();
counttrk.insert(CT::value_type(sgs.simParticle().get(),thc));
} else {
ifndhit->second.nhits_++;
ifndhit->second.planes_.insert(sgs.strawId().plane());
ifndhit->second.minplane_ = std::min(ifndhit->second.minplane_,sgs.strawId().plane());
ifndhit->second.maxplane_ = std::max(ifndhit->second.maxplane_,sgs.strawId().plane());
}
}
}
for(auto itrk=counttrk.begin();itrk != counttrk.end();itrk++){
if(diagLevel_ > 0)std::cout << "NHits " << itrk->second.nhits_ << " Nplanes " << itrk->second.planes_.size() << " plane span " << itrk->second.maxplane_-itrk->second.minplane_ << std::endl;
if(itrk->second.nhits_ >= minNTrk_
&& itrk->second.planes_.size() >= minNPlanes_
&& unsigned(abs(itrk->second.maxplane_-itrk->second.minplane_)) >= minPlaneSpan_){
// test the impact parameter. Use info from the first sgs
auto sp = itrk->first;
double charge = pdt->particle(sp->pdgId()).charge();
RobustHelix rh;
for(const auto& sgs : *sgscolH ) {
if(sgs.simParticle().get() == sp){
CLHEP::Hep3Vector pos = sgs.position();
CLHEP::Hep3Vector mom = sgs.momvec();
double bz = bfmgr->getBField(det->toMu2e(pos)).z();
TrkUtilities::RobustHelixFromMom(
pos, mom,
charge,
bz,
rh);
if(diagLevel_ > 0)std::cout << "Impact Parameter " << fabs(rh.rcent() - rh.radius()) << std::endl;
if(fabs(rh.rcent()-rh.radius()) < maxImpact_) {
selecttrk = true;
break;
}
}
}
}
if(selecttrk)break;
}
if(selecttrk)nPassed_++;
return selecttrk;
}

void CosmicMixingFilter::endJob() {
mf::LogInfo("Summary")
<<"CosmicMixingFilter_module: passed "
<<nPassed_<<" / "<<nEvt_<<" events\n";
}

}
DEFINE_ART_MODULE(mu2e::CosmicMixingFilter);
26 changes: 9 additions & 17 deletions Filters/src/DetectorStepFilter_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#include "Offline/MCDataProducts/inc/CaloShowerStep.hh"
#include "Offline/MCDataProducts/inc/CrvStep.hh"
#include "Offline/MCDataProducts/inc/SimParticle.hh"
#include "Offline/Mu2eUtilities/inc/SimParticleTimeOffset.hh"
#include "Offline/ConditionsService/inc/ConditionsHandle.hh"
#include "Offline/ConditionsService/inc/AcceleratorParams.hh"
#include <map>
namespace mu2e {

Expand All @@ -34,7 +35,6 @@ namespace mu2e {
struct TimeCutConfig {
using Name=fhicl::Name;
using Comment=fhicl::Comment;

fhicl::Atom<double> maxTime { Name("MaximumTime"), Comment("Maximum time for good step (ns since POT)")};
fhicl::Atom<double> minTime { Name("MinimumTime"), Comment("Minimum time for good step (ns since POT)")};
};
Expand Down Expand Up @@ -68,9 +68,6 @@ namespace mu2e {

fhicl::OptionalTable<TimeCutConfig> timeCutConfig { fhicl::Name("TimeCutConfig") };

// the following should not be needed soon
fhicl::Sequence<art::InputTag> SPTO { Name("TimeOffsets"), Comment("Sim Particle Time Offset Maps"), std::vector<art::InputTag> () };

};

using Parameters = art::EDFilter::Table<Config>;
Expand All @@ -80,7 +77,7 @@ namespace mu2e {

private:
bool goodParticle(SimParticle const& simp) const; // select particles whose steps to count
bool flashCut(double time) const; // cut particle time outside of flash
bool timeCut(double time) const; // particle time
double minTrkE_, minCaloE_, minCrvE_;
double minPartM_, maxPartM_;
bool or_;
Expand All @@ -92,7 +89,6 @@ namespace mu2e {
double maxSumCaloE_;
bool timecut_;
double minTime_, maxTime_;
SimParticleTimeOffset toff_; // time offsets
unsigned nEvt_, nPassed_;
};

Expand All @@ -113,7 +109,6 @@ namespace mu2e {
, maxSumCaloE_(conf().maxSumCaloStepE())
, timecut_(false)
, minTime_(0.0), maxTime_(0.0)
, toff_(conf().SPTO())
, nEvt_(0)
, nPassed_(0)
{
Expand All @@ -130,11 +125,11 @@ namespace mu2e {
}

// input must be a physical time!
bool DetectorStepFilter::flashCut(double ptime) const { return (!timecut_) || ptime > minTime_ || ptime < maxTime_; }
bool DetectorStepFilter::timeCut(double ptime) const { return (!timecut_) || (ptime > minTime_ && ptime < maxTime_); }

bool DetectorStepFilter::filter(art::Event& event) {
if(timecut_)toff_.updateMap(event);

ConditionsHandle<AcceleratorParams> accPar("ignored");
double mbtime = accPar->deBuncherPeriod;
bool selecttrk(false), selectcalo(false), selectcrv(false);
++nEvt_;
// Count Trk step from same particle
Expand All @@ -144,11 +139,10 @@ namespace mu2e {
auto sgscolH = event.getValidHandle<StrawGasStepCollection>(trkcoltag);
for(const auto& sgs : *sgscolH ) {
double mom = sgs.momentum().R();
double ptime = sgs.time() + toff_.totalTimeOffset(sgs.simParticle());
if(sgs.ionizingEdep() > minTrkE_ &&
mom > minPartM_ && mom < maxPartM_ &&
goodParticle(*sgs.simParticle()) &&
flashCut(ptime)) {
timeCut(fmod(sgs.time(),mbtime))) {
auto ifnd = counttrk.find(sgs.simParticle().get());
if(ifnd == counttrk.end())
counttrk.insert(CT::value_type(sgs.simParticle().get(),1));
Expand All @@ -170,11 +164,10 @@ namespace mu2e {
CES caloESum;
auto csscolH = event.getValidHandle<CaloShowerStepCollection>(calocoltag);
for(const auto& css : *csscolH ) {
double ptime = css.time() + toff_.totalTimeOffset(css.simParticle());
if(css.energyDepBirks() > minCaloE_ &&
css.momentumIn() > minPartM_ && css.momentumIn() < maxPartM_ &&
goodParticle(*css.simParticle()) &&
flashCut(ptime)) {
timeCut(fmod(css.time(),mbtime))) {
auto ifnd = caloESum.find(css.simParticle().get());
if(ifnd == caloESum.end())
caloESum.insert(CES::value_type(css.simParticle().get(),css.energyDepBirks()));
Expand All @@ -197,11 +190,10 @@ namespace mu2e {
auto crvscolH = event.getValidHandle<CrvStepCollection>(crvcoltag);
for(const auto& crvs : *crvscolH ) {
double mom = crvs.startMom().R();
double ptime = crvs.startTime() + toff_.totalTimeOffset(crvs.simParticle());
if(crvs.visibleEDep() > minCrvE_ &&
mom > minPartM_ && mom < maxPartM_ &&
goodParticle(*crvs.simParticle()) &&
flashCut(ptime)) {
timeCut(fmod(crvs.startTime(),mbtime))) {
auto ifnd = countcrv.find(crvs.simParticle().get());
if(ifnd == countcrv.end())
countcrv.insert(CC::value_type(crvs.simParticle().get(),1));
Expand Down
Loading

0 comments on commit 7ae8500

Please sign in to comment.