test_misconfigure.cxx
Go to the documentation of this file.
6 #include "WireCellUtil/Binning.h"
9 
10 #include <iostream>
11 
12 #include "MultiPdf.h"
13 #include "TFile.h"
14 #include "TStyle.h"
15 #include "TH2F.h"
16 
17 
18 using namespace std;
19 using namespace WireCell;
20 using namespace WireCell::Test;
21 using namespace WireCell::Waveform;
22 
23 // Make up a Gaussian charge distribution
24 std::vector<float> blip(Binning bins,
25  double time=1*units::ms, double sigma = 3*units::us, double mag = 10000.0)
26 {
27  int nticks = bins.nbins();
28  std::vector<float> charge(nticks,0);
29  for (int ind=0; ind<nticks; ++ind) {
30  const double t = bins.center(ind);
31  const double rel = (t-time)/sigma;
32  const double val = mag * exp(-0.5*rel*rel);
33  charge[ind] = val;
34  }
35  return charge;
36 }
37 
38 float maximum(const std::vector<float>& wave) {
39  float m=0;
40  for (auto q : wave ) {
41  if (q > m) m = q;
42  }
43  return m;
44 }
45 
46 
47 TH1F* plot_wave(MultiPdf& pdf, const std::vector<float>& wave, std::string name, std::string title)
48 {
49  TH1F* hist = new TH1F(name.c_str(), title.c_str(), wave.size(), 0, wave.size()+1);
50  int tick = 0;
51  for (auto q : wave) {
52  hist->Fill(tick+0.5, q);
53  ++tick;
54  }
55  hist->SetXTitle("tick");
56  hist->SetYTitle("amplitude");
57  hist->Draw("hist");
58  pdf();
59  return hist;
60 }
61 
62 TH2F* plot_ratio(MultiPdf& pdf, TH2F* f1, TH2F* f2)
63 {
64  TH2F* out = (TH2F*)f1->Clone("ratio");
65  out->SetTitle("Ratio - no time shift correction");
66  out->Divide(f2);
67  out->SetXTitle("ticks");
68  out->SetYTitle("channels");
69  out->Draw("colz");
70  pdf();
71  return out;
72 }
73 
74 TH2F* plot_frame(MultiPdf& pdf, IFrame::pointer frame, std::string name, double mtick=-1, double mchan=-1);
75 TH2F* plot_frame(MultiPdf& pdf, IFrame::pointer frame, std::string name, double mtick, double mchan)
76 {
77  auto traces = frame->traces();
78 
79  int maxchan=0, maxtick=0;
80  for (auto trace : (*traces)) {
81  auto& charge = trace->charge();
82  int tick = trace->tbin() + charge.size();
83  if (tick > maxtick) {
84  maxtick = tick;
85  }
86  int chan = trace->channel();
87  if (chan > maxchan) {
88  maxchan = chan;
89  }
90  cerr << name << " " << chan << " " << tick << " " << maximum(charge) << "\n";
91  }
92 
93  TH2F* hist = new TH2F(name.c_str(), name.c_str(),
94  maxtick+1, 0, maxtick+1,
95  maxchan+1, 0, maxchan+1);
96 
97  for (auto trace : (*traces)) {
98  int tick = trace->tbin();
99  int chan = trace->channel();
100  auto& charge = trace->charge();
101  for (float q : charge) {
102  hist->Fill(tick+0.5, chan+0.5, q);
103  ++tick;
104  }
105  }
106  hist->SetXTitle("ticks");
107  hist->SetYTitle("channels");
108  hist->SetContour(50);
109  hist->Draw("colz");
110  if (mtick > 0) {
111  hist->GetXaxis()->SetRangeUser(0, mtick);
112  }
113  if (mchan > 0) {
114  hist->GetYaxis()->SetRangeUser(0, mchan);
115  }
116 
117  pdf();
118  return hist;
119 }
120 
121 
122 int main(int argc, char* argv[])
123 {
125  pm.add("WireCellGen");
126  pm.add("WireCellRoot");
127 
128  int nsamples = 50;
129  double gain, shaping, tick;
130  {
131  IConfigurable::pointer mccfg = Factory::lookup<IConfigurable>("Misconfigure");
132  Configuration cfg = mccfg->default_configuration();
133  cfg["nsamples"] = nsamples;
134  cfg["truncate"] = true;
135  // use defaults locally
136  gain = cfg["from"]["gain"].asDouble();
137  shaping = cfg["from"]["shaping"].asDouble();
138  tick = cfg["tick"].asInt();
139  mccfg->configure(cfg);
140  }
141  IFrameFilter::pointer mc = Factory::lookup<IFrameFilter>("Misconfigure");
142 
143  Response::ColdElec ce(gain, shaping);
144 
145  auto resp = ce.generate(Binning(200, 0, 200*tick));
146  auto resp2 = ce.generate(Binning(400, 0, 400*tick));
147  auto resp3 = ce.generate(Binning(50, 0, 50*tick));
148  auto resp_spec = Waveform::dft(resp);
149  auto resp_spec2 = Waveform::dft(resp2);
150  auto resp_spec3 = Waveform::dft(resp3);
151 
152  ITrace::vector q_traces;
153  ITrace::vector out_traces;
154 
155  int qchannel = 0;
156  int channel = 0;
157  for (int nbins : {50, 100, 200, 400}) {
158  Binning bins(nbins, 0, nbins*tick);
159 
160  auto q1 = blip(bins, 0.25 * nbins * tick);
161  auto q2 = blip(bins, 0.50 * nbins * tick, 0.5*units::us);
162  auto q3 = blip(bins, 0.75 * nbins * tick);
163  auto q4 = Waveform::realseq_t(nbins, 0);
164  q4[nbins/2] = maximum(q1);
165 
166  q_traces.push_back(std::make_shared<SimpleTrace>(qchannel++, 0, q1));
167  q_traces.push_back(std::make_shared<SimpleTrace>(qchannel++, 0, q2));
168  q_traces.push_back(std::make_shared<SimpleTrace>(qchannel++, 0, q3));
169  q_traces.push_back(std::make_shared<SimpleTrace>(qchannel++, 0, q4));
170 
171  auto e1 = linear_convolve(q1, resp);
172  auto e2 = linear_convolve(q2, resp);
173  auto e3 = linear_convolve(q3, resp);
174  auto e4 = linear_convolve(q4, resp);
175 
176  out_traces.push_back(std::make_shared<SimpleTrace>(channel++, 0, e1));
177  out_traces.push_back(std::make_shared<SimpleTrace>(channel++, 0, e2));
178  out_traces.push_back(std::make_shared<SimpleTrace>(channel++, 0, e3));
179  out_traces.push_back(std::make_shared<SimpleTrace>(channel++, 0, e4));
180  }
181 
182  auto qorig = std::make_shared<SimpleFrame>(0, 0.0, q_traces, tick);
183  auto orig = std::make_shared<SimpleFrame>(0, 0.0, out_traces, tick);
184  IFrame::pointer mced, dummy;
185  {
186  bool ok = (*mc)(orig, mced);
187  if (!ok) { return -1; }
188  }
189 
190 
191  gStyle->SetOptStat(0);
192  gStyle->SetOptFit(0);
193  MultiPdf pdf(argv[0]);
194 
195  plot_wave(pdf,resp,"resp","Electronics response waveform - 200 bins");
196  plot_wave(pdf,resp2,"resp2","Electronics response waveform - 400 bins");
197  plot_wave(pdf,resp3,"resp3","Electronics response waveform - 50 bins");
198  plot_wave(pdf, Waveform::real(resp_spec),"respect","Electronics response spectrum - 200 bins");
199  plot_wave(pdf, Waveform::real(resp_spec2),"respect2","Electronics response spectrum - 400 bins");
200  plot_wave(pdf, Waveform::real(resp_spec3),"respect3","Electronics response spectrum - 50 bins");
201 
202  plot_frame(pdf, qorig, "ChargeZoomed", 50., 5.);
203  plot_frame(pdf, orig, "ShapedZoomed", 50., 5.);
204  plot_frame(pdf, mced, "MisconfiguredZoomed", 50, 5);
205 
206  plot_frame(pdf, qorig, "Charge");
207  auto f1 = plot_frame(pdf, orig, "Shaped");
208  auto f2 = plot_frame(pdf, mced, "Misconfigured");
209  plot_ratio(pdf, f1, f2);
210 
211  return 0;
212 }
static QCString name
Definition: declinfo.cpp:673
static const double m
Definition: Units.h:79
realseq_t real(const compseq_t &seq)
Return the real part of the sequence.
Definition: Waveform.cxx:55
Sequence< real_t > realseq_t
Definition: Waveform.h:31
double center(int ind) const
Definition: Binning.h:86
std::string string
Definition: nybbler.cc:12
struct vector vector
const std::string instance
STL namespace.
SynchrotronAndGN f2
cfg
Definition: dbjson.py:29
A functional object caching gain and shape.
Definition: Response.h:165
const double tick
const int nticks
TH2F * plot_ratio(MultiPdf &pdf, TH2F *f1, TH2F *f2)
TH2F * plot_frame(MultiPdf &pdf, IFrame::pointer frame, std::string name, double mtick=-1, double mchan=-1)
std::shared_ptr< IFrameFilter > pointer
Definition: IFrameFilter.h:23
TH1F * plot_wave(MultiPdf &pdf, const std::vector< float > &wave, std::string name, std::string title)
EmPhysicsFactory f1
std::vector< float > blip(Binning bins, double time=1 *units::ms, double sigma=3 *units::us, double mag=10000.0)
Definition: Main.h:22
static const double us
Definition: Units.h:101
static const double ms
Definition: Units.h:100
Json::Value Configuration
Definition: Configuration.h:50
cet::LibraryManager dummy("noplugin")
Plugin * add(const std::string &plugin_name, const std::string &libname="")
Add a plugin. If libname is not given, try to derive it.
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1124
array_xxc dft(const array_xxf &arr)
Definition: Array.cxx:15
int main(int argc, char *argv[])
int nbins() const
Definition: Binning.h:42
float maximum(const std::vector< float > &wave)
realseq_t linear_convolve(Waveform::realseq_t in1, Waveform::realseq_t in2, bool truncate=true)
Definition: Waveform.cxx:159