1#ifndef STRIP_SUM_SCATTER_HPP
2#define STRIP_SUM_SCATTER_HPP
9#include "InitUtils.hpp"
11#include "PlottingUtils.hpp"
14#include <TApplication.h>
20#include <TGraphErrors.h>
236 return Double_t(
leftdE[strip - 1]) + Double_t(
rightdE[strip - 1]);
248 for (Int_t s = 0; s < 18; s++)
253 for (Int_t s = 0; s < 18; s++)
408 static void PlaneXY(
const Double_t *total, Int_t reac, Double_t &x,
414 static Int_t
YLoOf(Int_t reac);
417 static Int_t
YHiOf(Int_t reac);
439 static void SetStripNoise(
const Double_t *mean,
const Double_t *sigma);
442 static Double_t
JumpMin(Int_t reac);
446 static Double_t s_stripSigma[18];
447 static Double_t s_stripMean[18];
448 std::map<Int_t, TH2F *> m_scatter;
449 std::vector<TraceEvt> m_reservoir;
454 std::vector<Long64_t> m_normedAt;
455 std::vector<Long64_t> m_tagged;
459 std::vector<std::vector<Long64_t>> m_taggedVar;
460 std::vector<std::vector<Long64_t>> m_normedVar;
461 std::vector<TString> m_variantNames;
464 std::vector<Long64_t> m_cutCounts;
465 std::vector<Long64_t> m_preCounts;
468 void WriteCutReport()
const;
472 const TString kSimCacheName =
"StripSumScatter_simcache.root";
474 static Int_t ReacIndex(Int_t reac);
476 Bool_t TryLoadCache(
const TString &cacheName,
const TString &fingerprint);
477 void WriteCache(
const TString &cacheName,
const TString &fingerprint);
480 void AllocateScatters();
484 void ReprojectFromReservoir();
489 void InteractiveOverlay(Int_t reac);
491 static void EnableEventBranches(TChain *chain);
492 static Bool_t AllStripsFired(
const EnergyView &ev);
499 static Bool_t MeasureBeamNoise(TChain *chain,
const BeamEllipses &be,
500 Double_t *strip_mean, Double_t *strip_sigma);
503 static Bool_t BeamUpstreamOf(
const EnergyView &ev, Int_t reac);
504 static Bool_t BeamUpstreamOf(
const EnergyView &ev, Int_t reac,
506 static Double_t SumRange(
const Double_t *total, Int_t lo, Int_t hi);
510 Int_t strip, Double_t nsigma);
517 Int_t sx, Int_t sy, Double_t nsigma);
520 static BeamFit2D FindBeamGate(TChain *chain, Int_t sx, Int_t sy,
521 const TString &tag,
const TString &subdir,
525 static BeamGate1D FindStripGate(TChain *chain, Int_t strip,
526 const TString &tag,
const TString &subdir,
527 Double_t nsigma, Double_t center);
529 static void DrawTraceSet(
const std::vector<TGraph *> &traces, Int_t color);
530 static TGraph *TraceFromTotal(
const Double_t *total);
541 std::vector<TGraph *> traces;
547 static void DrawRegionTraces(
const TString &save_name,
const TString &subdir,
548 const std::vector<TGraph *> &beam,
549 const std::vector<TraceClass> &classes,
550 Double_t y_min, Double_t y_max,
552 Bool_t beam_sigma_measured = kTRUE);
553 static void DrawRegionTraces(
const TString &save_name,
const TString &subdir,
554 const std::vector<TGraph *> &beam,
555 const std::vector<TGraph *> &aa,
556 const std::vector<TGraph *> &an, Double_t y_min,
557 Double_t y_max,
const char *y_title,
558 Bool_t beam_sigma_measured = kTRUE,
559 const char *an_label =
nullptr);
562 static Int_t SimClassColor(
const TString &base);
564 static void DrawRegionMeanTraces(
const TString &save_name,
565 const TString &subdir,
566 const std::vector<TGraph *> &beam,
567 const std::vector<TGraph *> &aa,
568 const std::vector<TGraph *> &an,
569 Double_t y_min, Double_t y_max,
570 const char *y_title);
572 static void TraceYRange(
const std::vector<TGraph *> &beam,
573 const std::vector<TGraph *> &aa,
574 const std::vector<TGraph *> &an, Double_t &y_min,
587 struct EventLevelVerdict {
588 Bool_t gate = kFALSE;
589 Bool_t pileup = kFALSE;
590 Bool_t noise = kFALSE;
591 Bool_t smooth_ok = kTRUE;
592 Bool_t
event = kFALSE;
597 static EventLevelVerdict JudgeEventLevel(
const BeamGate1D &gate,
599 Int_t gate_strip, Double_t step_z,
603 static Bool_t AnyVariantKeeps(
605 const std::vector<std::pair<TString, TagThresholds>> &variants,
606 const std::vector<Bool_t> &varies_event, Bool_t nom_event,
607 std::vector<Bool_t> &var_event);
610 static void TagUnderVariants(
611 const EnergyView &ev, Double_t step_z, Int_t kReacMin, Int_t kReacMax,
612 const std::vector<std::pair<TString, TagThresholds>> &variants,
613 const std::vector<Bool_t> &var_event, std::vector<Bool_t> &pass,
618 static Int_t TagNominal(
const EnergyView &ev,
const Double_t totals[18],
619 Int_t kReacMin, Int_t kReacMax, Int_t nReacStrips,
623 static void AllocateRunScatters(Int_t run, Int_t kReacMin, Int_t kReacMax,
624 Int_t kXBins, Int_t kYBins,
625 std::vector<TH2F *> &scatters);
626 static TCutG *PromptCut(TCanvas *c,
const char *name,
const char *label);
627 static void SaveRegionCuts(Int_t reac, TCutG *cut_an, TCutG *cut_aa);
628 static TCutG *LoadRegionCut(
const char *name, Int_t reac);
633 static void SavitzkyGolay(
const Double_t *in, Double_t *out);
637 static TGraph *SmoothedTraceFromTotal(
const Double_t *total);
640 SimFingerprint(
const std::vector<RemixSim::SimFileSpec> &specs);
642 static void YBounds(Double_t *y_lo, Double_t *y_hi);
643 static TString PrettyLabel(
const TString &tag);
651 const std::vector<Bool_t> &pass);
713 static Bool_t SimBeamGains(Double_t *gain);
715 static void RunIndexedParallel(Int_t n, Int_t workers,
716 const std::function<
void(Int_t)> &task);
726 std::vector<SingleRunFillResult> fills;
727 std::vector<Bool_t> filled;
728 Int_t next_merge = 0;
729 std::mutex merge_mutex;
731 Long64_t totalGated = 0;
732 Long64_t totalSeen = 0;
736 void FitGateRun(Int_t i, std::vector<SingleRunFitResult> &fits,
737 const std::vector<Int_t> &runOrder,
738 const std::vector<TString> &labelVec,
739 const std::vector<TChain *> &chainVec);
742 void MergeRunFill(Int_t t, std::vector<SingleRunFillResult> &fills,
743 Int_t &nBeamKept, Long64_t &totalGated,
744 Long64_t &totalSeen);
747 void FillScatterFile(Int_t t,
const std::vector<FillTask> &tasks,
748 const std::vector<Int_t> &runOrder,
749 const std::vector<TString> &labelVec,
750 const std::vector<SingleRunFitResult> &fits,
754 static TGraph *SimPopScatter(
const TString &file, Int_t reac,
755 const Double_t *gain, Long64_t max_points);
756 static std::vector<TGraph *>
757 SimPopTraces(
const TString &file,
const Double_t *gain, Long64_t max_traces);
758 void SimTraceOverlay();
759 static Bool_t LoadSimCache(
const TString &fp,
760 std::map<Int_t, std::vector<TGraph *>> &by_strip);
762 WriteSimCache(
const TString &fp,
763 const std::map<Int_t, std::vector<TGraph *>> &by_strip);
TagCut
The conditions of the reaction tag, in the order they are applied.
@ kCUT_CLIFF
The fall happened in the last step (TAIL_CLIFF_*).
@ kCUT_UPSTREAM
A strip before the reaction was not beam-like.
@ kCUT_TAIL_RISE
A rise in the tail (TAIL_RISE_NSIGMA).
@ kCUT_CROSS
Read below the beam before POST_CROSS_MIN_STRIP.
@ kCUT_RERISE
Back at the beam, then above it again (TAIL_RERISE_*).
@ kCUT_POST_ABOVE
The excess did not persist (POST_ABOVE_*).
@ kCUT_END_STRIP
The end strip not END_STRIP_NSIGMA below the beam.
@ kCUT_JUMP
Jump at the reaction strip below the gate.
@ kCUT_OTHER_TAG
Passed, but the event's one tag went to another strip.
@ kCUT_REAC_LEVEL
Reaction-strip deposit below 1 + the gate.
PreCut
The event-level cuts applied before any reaction is asked about, in order; sequential like TagCut.
@ kPRE_ALL_STRIPS
A strip did not fire.
@ kPRE_BOTH_MULT
Both-ends multiplicity (BOTH_MULT_MAX).
@ kPRE_SMOOTH
A strip-to-strip step above SMOOTHNESS_NSIGMA.
@ kPRE_GATE
Failed a beam gate.
const char *const kPreCutName[kNPRECUTS]
Short labels for PreCut, indexed by it.
const char *const kTagCutName[kNTAGCUTS]
Short labels for TagCut, indexed by it.
static TString CacheName()
Filename of the scatter cache for this configuration.
static Bool_t IsNoise(const EnergyView &ev)
static Int_t YLoOf(Int_t reac)
First strip of the post-trigger window summed onto y.
static Int_t YHiOf(Int_t reac)
Last strip of the post-trigger window, inclusive.
static TagThresholds NominalThresholds()
The thresholds the configuration sets.
static void SetStripNoise(const Double_t *mean, const Double_t *sigma)
Install the per-strip beam means and sigmas.
static Bool_t IsPileup(const EnergyView &ev)
The event-level pileup and noise cuts (PILEUP_* / NOISE_*), in sigma of the measured beam spread.
static Bool_t IsSmooth(const EnergyView &ev, Double_t nsigma)
The event-level smoothness cut: MaxStepNSigma() at or under nsigma, or the cut is off (nsigma <= 0).
static Double_t JumpMin(Int_t reac)
Minimum jump for a tag: REAC_JUMP_NSIGMA * StripSigma(reac).
static Double_t StripSigma(Int_t strip)
Width of the beam's deposit on a strip, from the gated beam sample of the first run (MeasureBeamNoise...
void Run()
Build or load the scatters, tag reactions, and draw everything.
static std::vector< SelectionStep > DescribeSelection()
The whole selection as the configuration sets it, in order.
static TagCut TailReason(const EnergyView &ev, Int_t reac, const TagThresholds &T)
The tail-shape part of RejectReason: kTAG_PASS or the first failing tail condition,...
void DrawAllTaggedTraces(const TString &subdir)
Region-trace overlays of every tagged event, one figure per reaction strip, against the beam band.
StripSumScatter()
Construct with empty scatters and no cache loaded.
static Int_t ResolveTag(const EnergyView &ev, const std::vector< Bool_t > &pass)
The one strip an event is tagged at, among those whose tag passed (pass[ReacIndex]),...
~StripSumScatter()
Frees the scatters and the reservoir.
static Bool_t Condition(TagCut c, const EnergyView &ev, Int_t reac, const TagThresholds &T)
One tag condition on its own: whether c passes for reac under T, true where it does not apply at that...
static Bool_t ConditionActive(TagCut c, const TagThresholds &T)
Whether the configuration has condition c in force.
static TagCut RejectReason(const EnergyView &ev, Int_t reac)
Which condition of the tag an event fails first at a strip.
Bool_t Prepare()
Measure the beam, then load the scatter cache or build it.
static Double_t MaxStepNSigma(const EnergyView &ev)
The largest strip-to-strip step of the trace, |E(s) - E(s-1)| over strips 2 to the last in sigma of s...
static Double_t StripMean(Int_t strip)
Mean of the beam's deposit on a strip over the same sample; the level every cut measures from.
static void PlaneXY(const Double_t *total, Int_t reac, Double_t &x, Double_t &y)
Where an event sits in the scatter plane for a given reaction strip.
void SimTagReport()
Run the data selection over every simulated population: the event-level cuts (all but the beam gate,...
static std::vector< std::pair< TString, TagThresholds > > ThresholdVariants()
The cut-variation set: each active threshold shifted up and down by its configured step (CUT_VARIATIO...
The fixed window every strip-sum scatter is built over.
const Double_t kXMin
Lower x bound of the build window.
const Double_t kXMax
Upper x bound; x sums 16 strips.
const Double_t kYMin
Lower y bound.
const Double_t kYMax
Upper y bound; y sums the post-trigger strips only.
The classification ellipses defining a pure-beam event.
BeamFit2D s15_s16
Alternative exit ellipse.
BeamFit2D s1_grid
Alternative entrance ellipse on strip 1 and the grid.
BeamFit2D s0_s1
Entrance ellipse on strips 0 and 1.
Bool_t ok
Whether the fits succeeded.
Bool_t use_s15_s16
Which exit ellipse is in force.
BeamFit2D s16_s17
Exit ellipse on strips 16 and 17.
BeamFit2D s1_s2
Alternative entrance ellipse, per PURE_BEAM_GATE.
A fitted 2-D Gaussian beam spot.
The event-level gate: one strip's fitted beam peak and width.
A view over one event's energies, decoding raw ADC into calibrated units.
The events files grouped into the units the beam gates are fitted on.
One entry of a simulation's events_MeV tree.
One step of the event selection, as StripSumScatter::DescribeSelection lists them: in the order the c...
Int_t tag_cut
The TagCut it counts under, or -1.
TString id
Node identifier, [a-z0-9_].
Bool_t on
False when the configuration switches the step off.
Stage
Where in the selection the step sits.
Int_t pre_cut
The PreCut it counts under, or -1.
TString detail
One line: the condition to pass, with the numbers in.
TString name
Short name, as in the cut report.
One simulated population to overlay on the data.
TString label
Legend label.
TString file
Simulation ROOT file.
One run's filled scatters, reservoir and normalisation counts.
std::vector< Long64_t > normed_at
Per-strip denominator: beam particles incident on that strip.
std::vector< std::vector< Long64_t > > tagged_var
Events tagged under each threshold variant, [variant][ReacIndex], in the order of StripSumScatter::Th...
Long64_t seen
Events examined.
Long64_t normed
Events surviving every cut applied before reaction tagging: the beam gates, the pileup,...
SingleRunFillResult()
Construct with zeroed counters.
std::vector< Long64_t > cut_counts
Sequential per-condition counts, [ReacIndex(reac) * kNTAGCUTS + cut], for the tag-cut report.
Long64_t gated
Events passing the beam gates.
std::vector< Long64_t > tagged
Events tagged at each reaction strip, indexed the same way.
std::vector< Long64_t > pre_counts
Sequential event-level counts, indexed by PreCut.
std::vector< std::vector< Long64_t > > normed_var
The per-strip denominator under each variant, indexed the same way: a variant of the beam selection m...
std::vector< TH2F * > scatters
Private clones, one per reaction strip.
std::vector< TraceEvt > reservoir
Tagged events from this run.
One run's fitted beam gates.
SingleRunFitResult()
Construct unfitted.
Bool_t ok
Whether the fits succeeded.
BeamGate1D gate
The strip gate.
BeamEllipses pure_beam
Entrance and exit ellipses.
Every threshold the tag applies, as one value set.
Double_t noise_nsigma
NOISE_NSIGMA; 0 = off.
Double_t smooth_nsigma
Event level; 0 = off.
Int_t tail_fall_from_strip
Double_t post_above_nsigma
Double_t end_strip_nsigma
Double_t tail_rise_nsigma
Double_t gate_nsigma
GATE_NSIGMA; 0 = off.
Double_t pileup_nsigma
PILEUP_NSIGMA; 0 = off.
Double_t tail_return_nsigma
Double_t tail_rerise_nsigma
Double_t cross_min_strip
The strip the trace must reach without reading below the beam; an absolute strip index held as a doub...
One tagged event, kept in the reservoir.
Double_t Total(Int_t strip) const
A strip's deposit: the sum of its ends, or the unsegmented value.
Float_t leftdE[16]
Left end of strips 1-16, index strip - 1.
Float_t rightdE[16]
Right end of strips 1-16, index strip - 1.
Int_t both_mult
Split strips (1-16) with both ends above threshold.
Float_t strip0dE
Strip 0, unsegmented.
void TotalsAdc(Double_t *out) const
Every strip's raw ADC deposit, for code that wants an array of 18.
ULong64_t seed_ts
Timestamp of the grid hit that seeded the event, from the events tree.
Bool_t beam_flat
Whether the trace looked flat, i.e. beam-like.
Float_t strip17dE
Strip 17, unsegmented.
UInt_t reac_mask
Bit per reaction strip this event was tagged at.
void Totals(Double_t *out) const
Every strip's deposit, for code that wants an array of 18.
Double_t TotalAdc(Int_t strip) const
A strip's raw ADC deposit, summed the same way.