Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions DataProducts/inc/FilterFraction.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// art product to record the selection fraction of a filter. This product could be
// part of the raw data output (trigger prescale) or simulation presampling.
// Original author: Dave Brown (LBNL) 2026
//
#ifndef DataProducts_FilterFraction_hh
#define DataProducts_FilterFraction_hh
#include <cstdint>
namespace mu2e {
class FilterFraction {
public:
enum FilterType { constant=0, nonominal, chained, unknown};
// chained means multiple filtering steps have been chained together
FilterFraction(FilterType type, double nominal, uint64_t nseen, uint64_t npassed) :
type_(type),nominal_(nominal), nseen_(nseen), npassed_(npassed) {}
// use this constructor when there is no nominal selection fraction
FilterFraction(uint64_t nseen, uint64_t npassed) :
type_(nonominal),nominal_(-1.0), nseen_(nseen), npassed_(npassed) {}
// default
FilterFraction(){}
// accessors
FilterType type() const { return type_; }
bool hasNominalValue() const { return type() == nonominal; }
double nominalFraction() const { return nominal_; }
double actualFraction() const { return nseen_ > 0 ? double(npassed_)/double(nseen_) : 0.0; }
uint64_t nSeen() const { return nseen_; }
uint64_t nPassed() const { return npassed_; }
// concatenate multiple subruns in the same path
FilterFraction& operator +=(FilterFraction const& other);
FilterFraction operator + (FilterFraction const& other) const;
// concatenate with an upstream filter. The values must match!
FilterFraction chain(FilterFraction const& upstream) const;
private:
FilterType type_ = unknown; // type of filtering performed
double nominal_ = -1; // nominal fraction (<1) of events expected to be kept by the filter
uint64_t nseen_ = 0; // number of events processed by this filter
uint64_t npassed_ = 0; // number of events passed by this filter
};
}
#endif
22 changes: 22 additions & 0 deletions DataProducts/src/FilterFraction.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "Offline/DataProducts/inc/FilterFraction.hh"
#include <stdexcept>

namespace mu2e {
FilterFraction& FilterFraction::operator +=(FilterFraction const& other) {
if(other.type() != type() || (type()<= nonominal && other.nominalFraction() != nominalFraction()))
throw std::runtime_error("nominal filter fractions conflict");
nseen_ += other.nSeen();
npassed_ += other.nPassed();
return *this;
}
FilterFraction FilterFraction::operator + (FilterFraction const& other) const {
auto retval = *this;
retval += other;
return retval;
}
// concatenate with an upstream filter. The values must match!
FilterFraction FilterFraction::chain(FilterFraction const& upstream) const {
if(upstream.nPassed() != nSeen())throw std::runtime_error("Filter counts conflict");
return FilterFraction(chained,-1.0,upstream.nSeen(),nPassed());
}
}
3 changes: 3 additions & 0 deletions DataProducts/src/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@
#include "Offline/DataProducts/inc/STMChannel.hh"
#include "Offline/DataProducts/inc/STMTestBeamEventInfo.hh"

// Filter
#include "Offline/DataProducts/inc/FilterFraction.hh"

// General
#include "Offline/DataProducts/inc/SurfaceId.hh"
7 changes: 6 additions & 1 deletion DataProducts/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@
<class name="mu2e::STMTestBeamEventInfo" />
<class name="art::Wrapper<mu2e::STMTestBeamEventInfo>" />

<!-- ********* general ********* -->
<!-- ********* filter ********* -->
<class name="mu2e::FilterFraction"/>
<class name="art::Wrapper<mu2e::FilterFraction>" />

<!-- ********* general ********* -->
<class name="mu2e::SurfaceIdDetail"/>
<class name="mu2e::SurfaceIdEnum"/>
<class name="mu2e::SurfaceId"/>


</lcgdict>
17 changes: 13 additions & 4 deletions Filters/src/RandomPrescaleFilter_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "messagefacility/MessageLogger/MessageLogger.h"
#include "Offline/SeedService/inc/SeedService.hh"
#include "CLHEP/Random/RandFlat.h"
#include "Offline/DataProducts/inc/FilterFraction.hh"

namespace mu2e
{
Expand All @@ -39,22 +40,26 @@ namespace mu2e
RandomPrescaleFilter & operator = (RandomPrescaleFilter &&) = delete;

bool filter(art::Event & e) override;
virtual bool endRun(art::Run& run ) override;
bool endSubRun(art::SubRun& subrun ) override;

private:
art::RandomNumberGenerator::base_engine_t& engine_;
CLHEP::RandFlat randflat_;
int debug_;
unsigned nevt_, npass_;
double frac_;
uint64_t nevt_, npass_;
};

RandomPrescaleFilter::RandomPrescaleFilter(const Parameters& conf) :
art::EDFilter{conf},
engine_(createEngine( art::ServiceHandle<SeedService>()->getSeed())),
randflat_( engine_, 0.0, conf().nPrescale() ),
debug_(conf().debugLevel()),
frac_(1.0/double(conf().nPrescale())),
nevt_(0), npass_(0)
{}
{
produces<mu2e::FilterFraction,art::InSubRun>();
}

inline bool RandomPrescaleFilter::filter(art::Event & event)
{
Expand All @@ -64,10 +69,14 @@ namespace mu2e
return retval;
}

bool RandomPrescaleFilter::endRun( art::Run& run ) {
bool RandomPrescaleFilter::endSubRun( art::SubRun& subrun ) {
auto ff = std::make_unique<FilterFraction>(FilterFraction::constant,frac_, nevt_,npass_);
subrun.put(std::move(ff),"",art::fullSubRun());
if(debug_ > 0 && nevt_ > 0){
std::cout << moduleDescription().moduleLabel() << " passed " << npass_ << " events out of " << nevt_ << " for a ratio of " << float(npass_)/float(nevt_) << std::endl;
}
// reset
npass_ = 0; nevt_ = 0;
return true;
}

Expand Down
1 change: 0 additions & 1 deletion Print/fcl/printCosmicLivetime.fcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#
# print products with a moderate amount of output - includes cuts on energy
#
Expand Down
37 changes: 37 additions & 0 deletions Print/fcl/printFilterFraction.fcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# print products with a moderate amount of output - includes cuts on energy
#

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

process_name : print

services : {
message : @local::default_message
GlobalConstantsService : { inputFile : "Offline/GlobalConstantsService/data/globalConstants_01.txt" }
}

physics :{
analyzers: {

printModule : {
module_type : PrintModule
PrintEvent : false
verbose : 0
PrintSubRun : true
FilterFractionPrinter : {
verbose : 1
}
} # printModule


} # analyzers

ana : [ printModule ]
end_paths : [ ana ]

}

services.message.destinations.log.categories.ArtSummary.limit : 0
services.message.destinations.statistics.stats : @local::mf_null
37 changes: 37 additions & 0 deletions Print/inc/FilterFractionPrinter.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Utility class to print FilterFraction
//
#ifndef Print_inc_FilterFractionPrinter_hh
#define Print_inc_FilterFractionPrinter_hh

#include <cstring>
#include <iostream>

#include "Offline/DataProducts/inc/FilterFraction.hh"
#include "Offline/Print/inc/ProductPrinter.hh"
#include "art/Framework/Principal/Handle.h"
#include "canvas/Persistency/Common/Ptr.h"

namespace mu2e {

class FilterFractionPrinter : public ProductPrinter {
public:
FilterFractionPrinter() {}
FilterFractionPrinter(const Config& conf) : ProductPrinter(conf) {}

// all the ways to request a printout
void Print(art::Event const& event, std::ostream& os = std::cout) override;
void PrintSubRun(art::SubRun const& subrun,
std::ostream& os = std::cout) override;
void Print(const art::Handle<FilterFraction>& handle,
std::ostream& os = std::cout);
void Print(const art::ValidHandle<FilterFraction>& handle,
std::ostream& os = std::cout);
void Print(const mu2e::FilterFraction& obj, int ind = -1,
std::ostream& os = std::cout);
void PrintHeader(const std::string& tag, std::ostream& os = std::cout);
private:
};

} // namespace mu2e
#endif
79 changes: 79 additions & 0 deletions Print/src/FilterFractionPrinter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "Offline/Print/inc/FilterFractionPrinter.hh"
#include "art/Framework/Principal/Provenance.h"
#include "canvas/Persistency/Common/Sampled.h"
#include "canvas/Persistency/Provenance/SampledInfo.h"
#include <iomanip>
#include <string>

void mu2e::FilterFractionPrinter::Print(art::Event const& event, std::ostream& os) {}

void mu2e::FilterFractionPrinter::PrintSubRun(art::SubRun const& subrun, std::ostream& os) {
if (verbose() < 1) return;
if (tags().empty()) {
// if a list of instances not specified, print all instances
std::vector<art::Handle<FilterFraction> > ffl =
subrun.getMany<FilterFraction>();
for (auto const& cl : ffl) Print(cl);
// also look for sampled instances
auto ffs = subrun.getMany<art::Sampled<FilterFraction>>();
if(ffs.size() > 0){
for (auto const& ff : ffs){
std::cout << "SampledFilterFraction with tag " << ff->originalInputTag() << std::endl;
auto sinfomh = subrun.getHandle<art::SampledSubRunInfo>("SamplingInput");
if(sinfomh.isValid()){
auto const& sinfom = *sinfomh;
for(auto sinfoit = sinfom.begin(); sinfoit != sinfom.end(); ++sinfoit) {
if(sinfoit->first.find("Cosmic") != std::string::npos){
std::cout << "With SampledSubRunInfo entry for dataset " << sinfoit->first << " Has the following FilterFractions: " << std::endl;
for(auto const& sr : sinfoit->second.ids){
std::cout << sr << " : ";
auto ffp = ff->get(sinfoit->first,sr);
if(!ffp.empty()) Print(*ffp);
}
}
}
}
}
}

} else {
// print requested instances
for (const auto& tag : tags()) {
auto ff = subrun.getValidHandle<FilterFraction>(tag);
Print(ff);
}
}
}

void mu2e::FilterFractionPrinter::Print(
const art::Handle<FilterFraction>& handle, std::ostream& os) {
if (verbose() < 1) return;
// the product tags with all four fields, with underscores
std::string tag = handle.provenance()->productDescription().branchName();
tag.pop_back(); // remove trailing dot
PrintHeader(tag, os);
Print(*handle);
}

void mu2e::FilterFractionPrinter::Print(
const art::ValidHandle<FilterFraction>& handle, std::ostream& os) {
if (verbose() < 1) return;
// the product tags with all four fields, with underscores
std::string tag = handle.provenance()->productDescription().branchName();
tag.pop_back(); // remove trailing dot
PrintHeader(tag, os);
Print(*handle);
}

void mu2e::FilterFractionPrinter::Print(const mu2e::FilterFraction& obj,
int ind, std::ostream& os) {
os << std::setiosflags(std::ios::fixed | std::ios::right);

os << " Type " << obj.type() << " Nominal fraction " << obj.nominalFraction() << " Actual Fraction " << obj.actualFraction() << " N Seen " << obj.nSeen() << std::endl;
}

void mu2e::FilterFractionPrinter::PrintHeader(const std::string& tag,
std::ostream& os) {
if (verbose() < 1) return;
os << "\nProductPrint " << tag << "\n";
}
5 changes: 5 additions & 0 deletions Print/src/PrintModule_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Offline/Print/inc/CaloShowerStepPrinter.hh"
#include "Offline/Print/inc/ComboHitPrinter.hh"
#include "Offline/Print/inc/CosmicLivetimePrinter.hh"
#include "Offline/Print/inc/FilterFractionPrinter.hh"
#include "Offline/Print/inc/CrvCoincidenceClusterPrinter.hh"
#include "Offline/Print/inc/CrvDigiMCPrinter.hh"
#include "Offline/Print/inc/CrvDigiPrinter.hh"
Expand Down Expand Up @@ -77,6 +78,8 @@ class PrintModule : public art::EDAnalyzer {
fhicl::Name("ProtonBunchIntensityPrinter")};
fhicl::Table<ProductPrinter::Config> CosmicLivetimePrinter{
fhicl::Name("CosmicLivetimePrinter")};
fhicl::Table<ProductPrinter::Config> FilterFractionPrinter{
fhicl::Name("FilterFractionPrinter")};
fhicl::Table<ProductPrinter::Config> EventWindowMarkerPrinter{
fhicl::Name("EventWindowMarkerPrinter")};
fhicl::Table<ProductPrinter::Config> genParticlePrinter{
Expand Down Expand Up @@ -190,6 +193,8 @@ mu2e::PrintModule::PrintModule(const Parameters& conf) : art::EDAnalyzer(conf),
conf().ProtonBunchIntensityPrinter()));
_printers.push_back(
make_unique<CosmicLivetimePrinter>(conf().CosmicLivetimePrinter()));
_printers.push_back(
make_unique<FilterFractionPrinter>(conf().FilterFractionPrinter()));
_printers.push_back(
make_unique<EventWindowMarkerPrinter>(conf().EventWindowMarkerPrinter()));
_printers.push_back(
Expand Down
Loading