6 #include "sbnanaobj/StandardRecord/Proxy/SRProxy.h"
33 const std::string& outfile)
36 fSpillCut(nullptr), fSliceCut(nullptr)
42 const std::string& outfile)
82 FILE* f = fopen(fname.c_str(),
"r");
86 int run, subrun, event;
87 fscanf(f,
"%d %d %d", &run, &subrun, &event);
100 TTree::SetMaxTreeSize(1e15);
103 std::cerr <<
"Error: can only call Go() once on a FileReducer" << std::endl;
108 const int Nfiles =
NFiles();
115 TH1* hPOTOut =
new TH1F(
"TotalPOT",
"", 1, 0, 1);
116 TH1* hEventsOut =
new TH1F(
"TotalEvents",
"", 1, 0, 1);
118 std::vector<std::string> fnames;
120 std::map<std::string, std::string> meta;
121 std::set<std::string> meta_mask;
130 assert(!fin->IsZombie());
132 fnames.push_back(fin->GetName());
134 TH1* hPOT = (TH1*)fin->Get(
"TotalPOT");
138 TDirectory* meta_dir = (TDirectory*)fin->Get(
"metadata");
142 TH1* hEvents = (TH1*)fin->Get(
"TotalEvents");
144 hEventsOut->Add(hEvents);
147 Nfiles == 1 ? &
prog : 0,
148 nRecSeen, nRecPassed);
152 prog.SetProgress((fileIdx+1.)/Nfiles);
167 std::cout <<
"Passed " << nRecPassed <<
" / " << nRecSeen <<
" records";
174 long& nRecSeen,
long& nRecPassed)
176 TTree* recTree = (TTree*)fin->Get(
"recTree");
179 const caf::CAFType
type = caf::GetCAFType(recTree);
182 const caf::CAFType outtype = caf::GetCAFType(trOut);
184 std::cerr <<
"FileReducer: Error: dataset contains mixed CAF types (flat vs nested)" << std::endl;
191 nRecSeen, nRecPassed);
194 nRecSeen, nRecPassed);
197 std::cerr <<
"FileReducer: Error: Unrecognized file type: "
198 << fin->GetName() << std::endl;
205 TTree* recTree, TTree*& trOut,
207 long& nRecSeen,
long& nRecPassed)
211 trOut =
new TTree(
"recTree",
"recTree");
214 trOut->Branch(
"rec", &dummy);
218 caf::Proxy<caf::StandardRecord> srProxy(recTree,
"rec");
222 recTree->SetBranchAddress(
"rec", &sr);
226 const int Nentries = recTree->GetEntries();
227 for(
int n = 0;
n < Nentries; ++
n){
229 recTree->LoadTree(
n);
233 !
fEventList.count(std::make_tuple(srProxy.hdr.run,
235 srProxy.hdr.evt)))
continue;
238 const bool passesSpillCut = !
fSpillCut || (*fSpillCut)(&srProxy);
240 if(passesSpillCut || srProxy.hdr.pot > 0 || srProxy.hdr.ngenevt > 0){
241 recTree->GetEntry(
n);
244 trOut->SetBranchAddress(
"rec", &sr);
256 std::vector<int> tocut;
257 for(
unsigned int i = 0; i < srProxy.slc.size(); ++i){
262 for(
auto it = tocut.rbegin(); it != tocut.rend(); ++it){
263 sr->
slc.erase(sr->
slc.begin() + *it);
277 if(
n%100 == 0 && prog) prog->
SetProgress(
double(
n)/Nentries);
284 long& nRecSeen,
long& nRecPassed)
287 std::cerr <<
"FileReducer: Event list not supported for FlatCAFs (yet)" << std::endl;
292 std::cerr <<
"FileReducer: Spill cuts not supported for FlatCAFs" << std::endl;
297 std::cerr <<
"FileReducer: Slice cuts not supported for FlatCAFs" << std::endl;
302 std::cerr <<
"FileReducer: Reduction functions not supported for FlatCAFs" << std::endl;
309 trOut = recTree->CloneTree();
313 recTree->CopyAddresses(trOut);
314 trOut->CopyEntries(recTree);
317 nRecSeen += recTree->GetEntries();
318 nRecPassed += recTree->GetEntries();
324 TTree* globalIn = (TTree*)fin->Get(
"globalTree");
325 if(!globalIn)
return;
330 globalIn->SetBranchAddress(
"global", &pglobal);
332 TTree globalOut(
"globalTree",
"globalTree");
333 globalOut.Branch(
"global",
"caf::SRGlobal", &global);
334 assert(globalIn->GetEntries() == 1);
336 globalIn->GetEntry(0);
343 const std::set<std::string>&
mask,
344 const std::vector<std::string>& fnames)
const
346 for(
const std::string&
m: mask){
347 std::cerr <<
"Warning: metadata parameter '" <<
m <<
"' differs between input files and has been dropped from the output." << std::endl;
365 std::string parents =
"[";
366 for(
const std::string& f: fnames){
367 parents +=
"{\"file_name\": \""+std::string(basename((
char *)f.c_str()))+
"\"}, ";
369 if(!fnames.empty()) parents.resize(parents.size()-2);
372 meta[
"parents"] = parents;
375 if(fnames.size() > 1 && meta.find(
"data_tier") != meta.end()){
376 meta[
"data_tier"] =
"\"concat_caf\"";
380 for(
auto it:
fMetaMap) meta[it.first] = it.second;
int ncrt_tracks
Number of CRT tracks in event.
SRHeader hdr
Header branch: run, subrun, etc.
std::vector< SRMeVPrtl > prtl
If present – information on decay of MeV "Portal" particle.
int nfake_reco
Number of Fake-Reco's in list.
BEGIN_PROLOG could also be cerr
void CopyGlobalTree(TFile *fin, TFile *fout)
bool fGone
Has Go() been called? Can't add more histograms after that.
int nslc
Number of slices in list.
std::vector< SRShower > shw
Vector of trac showers.
void CombineMetadata(std::map< std::string, std::string > &base, const std::map< std::string, std::string > &add, std::set< std::string > &mask)
void HandleNestedTree(TFile *fout, TTree *recTree, TTree *&trOut, Progress *prog, long &nRecSeen, long &nRecPassed)
std::vector< SRSlice > slc
Slice branch.
process_name opflashCryoW ana
size_t nshw
Number of trac showers.
tuple m
now if test mode generate materials, CRT shell, world, gdml header else just generate CRT shell for u...
size_t nprtl
Number of portals.
int ncrt_hits
Number of CRT hits in event.
void HandleFile(TFile *fin, TFile *fout, TTree *&trOut, Progress *prog, long &nRecSeen, long &nRecPassed)
std::map< std::string, std::string > GetCAFMetadata(TDirectory *dir)
std::vector< SRTrueInteraction > nu
Vector of true nu or cosmic.
constexpr mask_t< EnumType > mask(EnumType bit, OtherBits...otherBits)
Returns a mask with all specified bits set.
size_t nstub
Number of stubs.
std::vector< SRCRTHit > crt_hits
CRT hits in event.
std::map< std::string, std::string > fMetaMap
size_t ntrk
Number of panora tracks.
std::vector< SRTrueParticle > true_particles
True particles in spill.
void ClearTrueParticles(caf::StandardRecord *sr)
void WriteCAFMetadata(TDirectory *dir, const std::map< std::string, std::string > &meta)
std::vector< SRTrack > trk
Vector of pandora tracks.
FileReducer(const std::string &wildcard, const std::string &outfile)
void AddSpillCut(const SpillCut &cut)
Only copy records to the output file if they pass this cut.
virtual void Go() override
Load all the registered spectra.
std::vector< SRCRTTrack > crt_tracks
CRT tracks in event.
_Cut< caf::SRSpillProxy > SpillCut
Equivalent of Cut acting on caf::SRSpill. For use in spill-by-spill data quality cuts.
void AddSliceCut(const SliceCut &cut)
Base class for the various types of spectrum loader.
int ntrue_particles
Number of true particles in list.
std::vector< std::function< ReductionFunc > > fReductionFuncs
The StandardRecord is the primary top-level object in the Common Analysis File trees.
void SetProgress(double frac)
Update the progress fraction between zero and one.
std::vector< SRFakeReco > fake_reco
List of fake-reco slices.
size_t nnu
Number of true nu or cosmic.
_Cut< caf::SRSliceProxy > SliceCut
Representation of a cut (selection) to be applied to a caf::StandardRecord object.
void HandleFlatTree(TFile *fout, TTree *recTree, TTree *&trOut, Progress *prog, long &nRecSeen, long &nRecPassed)
SRSliceRecoBranch reco
Slice reco branch: tracks, showers, etc.
std::set< std::tuple< int, int, int > > fEventList
void UpdateMetadata(std::map< std::string, std::string > &meta, const std::set< std::string > &mask, const std::vector< std::string > &fnames) const
A simple ascii-art progress bar.
SRTruthBranch mc
Truth branch for all interactions.
void Huskify(caf::StandardRecord *sr) const
Strip all information out of this record and tag it as a husk.
void SetEventList(const std::string &fname)
If called, only events whose run/subrun/event occur in fname will be retained.
int NFiles() const
Forwards to fFileSource.
BEGIN_PROLOG could also be cout
std::vector< SRStub > stub
Vector of stubs.