28 #include "art_root_io/TFileService.h" 29 #include "art_root_io/TFileDirectory.h" 30 #include "canvas/Persistency/Common/FindManyP.h" 31 #include "cetlib_except/exception.h" 47 #define setHistTitles(hist,xtitle,ytitle) hist->GetXaxis()->SetTitle(xtitle); hist->GetYaxis()->SetTitle(ytitle); 48 #define appendToHistTitle(hist,str) hist->SetTitle((std::string(hist->GetTitle())+std::string(str)).c_str()); 51 const std::array<size_t,13>
tpcMapping = {{0,4,1,0,0,5,2,0,0,6,3,0,0}};
151 fLife = tfs->make<TH1F>(
"Life",
"Electron Lifetime for all APAs", 50, 0, 15);
153 fLifeVTPC = tfs->make<TH2F>(
"LifeVTPC",
"Electron Lifetime v. APA", 6,0.5,6.5,50, 0, 15.);
155 fLifeInv = tfs->make<TH1F>(
"LifeInv",
"LifeInv", 50, 0, 1);
156 fFracSelHits = tfs->make<TH1F>(
"FracSelHits",
"FracSelHits", 50, 0, 1);
157 fChiDOF = tfs->make<TH1F>(
"ChiDOF",
"ChiDOF", 80, 0, 80);
158 fLifeInvCA = tfs->make<TH1F>(
"LifeInvCA",
"LifeInv C to A", 50, 0, 1);
159 fLifeInvAC = tfs->make<TH1F>(
"LifeInvAC",
"LifeInv A to C", 50, 0, 1);
161 fLifeInv_E = tfs->make<TProfile>(
"LifeInv_E",
"LifeInv_E", 20, 0, 40);
162 fLifeInv_Angle = tfs->make<TProfile>(
"LifeInv_Angle",
"LifeInv_Angle", 15, -1.5, 1.5);
166 for(
unsigned short tpc = 0; tpc < 12; ++tpc) {
175 fDriftTime = tfs->make<TH1F>(
"DriftTime",
"Drift Time", 500, 0.,10.);
177 fDriftTimeVTPC = tfs->make<TH2F>(
"DriftTimeVTPC",
"Drift Time v. APA", 6,0.5,6.5,500,0.,10.);
180 fSNR = tfs->make<TH1F>(
"SNR",
"Signal to Noise Ratio", 400, 0.,200);
182 fSNRVTPC = tfs->make<TH2F>(
"SNRVTPC",
"Signal to Noise Ratio v. APA", 6,0.5,6.5,400,0.,200.);
185 fAmplitudes = tfs->make<TH1F>(
"Amplitudes",
"Hit Amplitude", 4100, 0.,4100.);
187 fNoise = tfs->make<TH1F>(
"Noise",
"Wire Noise < 10 ADC", 2000, 0.,10.);
189 fNoiseWide = tfs->make<TH1F>(
"NoiseWide",
"Wire Noise", 2000, 0.,1000.);
195 fSNRVTPC->GetXaxis()->SetBinLabel(
apa+1,apaLabels.at(
apa).c_str());
205 fChgCuts = pset.
get<std::vector<float>>(
"ChgCuts", {0, 5});
219 std::ofstream purfile;
222 purfile<<
"Run, tpc, lifetime, error, count, S/N, num S/N clusters, drift time\n";
224 for(
unsigned short tpc = 0; tpc < 12; ++tpc) {
234 if(1/bigLifeInv[tpc] != 0) life = 1 / bigLifeInv[tpc];
242 size_t apa = tpcMapping.at(tpc);
243 if (apa == 0)
continue;
245 float driftTime = -1.;
246 for (
int iBin=nBinsY; iBin >=0; iBin--)
261 TCanvas * canvas =
new TCanvas(
"canvas_SPLifetime");
263 canvas->SetBottomMargin(0.13);
264 canvas->SetRightMargin(0.12);
270 unsigned fileNum = 1;
274 static const std::string patternString =
"run(\\d+)_(\\d+)_dl(\\d+)";
275 static const std::regex
pattern(patternString);
276 std::smatch patternMatches;
277 const bool foundMatch = std::regex_search(
fInFilename,patternMatches,pattern);
280 std::cout <<
"filename: " <<
fInFilename <<
" match: " << patternMatches[0] <<
" run: " << patternMatches[1] <<
" file: " << patternMatches[2] <<
" dl: " << patternMatches[3] <<
std::endl;
281 runNum = std::stoi(patternMatches[1]);
282 fileNum = std::stoi(patternMatches[2]);
283 dlNum = std::stoi(patternMatches[3]);
289 std::cout <<
"Error: couldn't parse input filename: '"<<
fInFilename<<
"' with regex '"<<patternString<<
"'\n";
293 std::stringstream infileNameStrStream;
294 infileNameStrStream <<
"run";
296 infileNameStrStream <<
std::setw(0) <<
'_';
297 infileNameStrStream <<
std::setw(4) << fileNum;
298 const std::string identifierNoDL = infileNameStrStream.str();
299 infileNameStrStream <<
std::setw(0) <<
"_dl";
300 infileNameStrStream <<
std::setw(2) << dlNum;
301 const std::string identifierDL = infileNameStrStream.str();
303 std::cout <<
"identifierDL: " << identifierDL <<
std::endl;
304 std::cout <<
"identifierNoDL: " << identifierNoDL <<
std::endl;
305 const std::string identifierTitle =
" " + identifierDL;
316 std::string summary_filename = identifierNoDL+
"_purity_summary.json";
317 std::string filelist_filename = identifierNoDL+
"_purity_FileList.json";
319 std::ofstream summaryfile;
320 summaryfile.open(summary_filename);
321 summaryfile <<
"[\n {\n \"run\": \"" << identifierDL <<
"\",\n" 322 <<
" \"Type\": \"purity\"\n }\n]\n";
326 std::ofstream filelistfile;
327 filelistfile.open(filelist_filename);
328 filelistfile <<
"[\n {\n \"Category\": \"Purity Monitor\",\n \"Files\": {\n \"Cluster Drift Time\": \"";
330 imageFileName =
"driftVTPC_";
331 imageFileName += identifierNoDL;
332 imageFileName +=
".png";
337 canvas->SaveAs(imageFileName.c_str());
338 filelistfile << imageFileName<<
",";
340 imageFileName =
"driftVTPC_zoom_";
341 imageFileName += identifierNoDL;
342 imageFileName +=
".png";
344 fDriftTimeVTPC->SetTitle((originalTitle+
" From 0 to 4 ms").c_str());
347 canvas->SaveAs(imageFileName.c_str());
349 filelistfile << imageFileName;
351 filelistfile <<
"\",\n";
354 filelistfile <<
" \"Purity\": \"";
355 imageFileName =
"purity_";
356 imageFileName += identifierNoDL;
357 imageFileName +=
".png";
360 canvas->SaveAs(imageFileName.c_str());
361 filelistfile << imageFileName<<
",";
363 canvas->SetLogz(
false);
364 imageFileName =
"purityVTPC_";
365 imageFileName += identifierNoDL;
366 imageFileName +=
".png";
369 fLifeVTPC->GetXaxis()->SetLabelSize(0.050);
371 canvas->SaveAs(imageFileName.c_str());
372 filelistfile << imageFileName;
375 filelistfile <<
"\",\n";
378 filelistfile <<
" \"SNR\": \"";
379 imageFileName =
"snr_";
380 imageFileName += identifierNoDL;
381 imageFileName +=
".png";
382 canvas->SetLogy(
true);
386 canvas->SaveAs(imageFileName.c_str());
387 canvas->SetLogy(
false);
388 filelistfile << imageFileName<<
",";
390 imageFileName =
"snrVTPC_";
391 imageFileName += identifierNoDL;
392 imageFileName +=
".png";
395 fSNRVTPC->GetXaxis()->SetLabelSize(0.050);
397 canvas->SaveAs(imageFileName.c_str());
398 filelistfile << imageFileName<<
",";
400 canvas->SetLogy(
true);
401 imageFileName =
"noise_";
402 imageFileName += identifierNoDL;
403 imageFileName +=
".png";
406 canvas->SaveAs(imageFileName.c_str());
407 filelistfile << imageFileName<<
",";
409 imageFileName =
"noiseWide_";
410 imageFileName += identifierNoDL;
411 imageFileName +=
".png";
414 canvas->SaveAs(imageFileName.c_str());
415 filelistfile << imageFileName<<
",";
417 imageFileName =
"amplitude_";
418 imageFileName += identifierNoDL;
419 imageFileName +=
".png";
422 canvas->SaveAs(imageFileName.c_str());
423 filelistfile << imageFileName<<
",";
425 imageFileName =
"amplitude_zoom_";
426 imageFileName += identifierNoDL;
427 imageFileName +=
".png";
429 fAmplitudes->SetTitle((originalTitle+
" From 0 to 500 ADC").c_str());
432 canvas->SaveAs(imageFileName.c_str());
433 filelistfile << imageFileName;
434 canvas->SetLogy(
false);
436 filelistfile <<
"\"\n }\n }\n]\n";
437 filelistfile.close();
472 for(
unsigned int icl = 0; icl < clsVecHandle->size(); ++icl) {
479 if(sTick > eTick)
std::swap(sTick, eTick);
480 float dTick = eTick - sTick;
482 std::vector<art::Ptr<recob::Hit> > clsHits;
483 clsHitsFind.get(icl, clsHits);
484 if(clsHits.size() == 0)
continue;
485 unsigned short tpc = clsHits[0]->WireID().TPC;
488 unsigned short nhist = 1 + (
unsigned short)(dTick /
fTicksPerBin);
490 if(clsHits.size() <
fMinHits)
continue;
501 std::vector<double> tck(nhist), ave(nhist), cnt(nhist),
err(nhist);
502 std::vector<float> minChg(nhist, 0);
503 std::vector<float> maxChg(nhist, 10000);
504 for(
unsigned short nit = 0; nit < 2; ++nit) {
505 for(
unsigned short ihist = 0; ihist < nhist; ++ihist) {
512 for(
auto& pht : clsHits) {
513 unsigned short ihist = (pht->PeakTime() - sTick) /
fTicksPerBin;
514 if(ihist > nhist - 1)
continue;
515 float chg = pht->Integral();
516 if(chg < minChg[ihist] || chg > maxChg[ihist])
continue;
517 tck[ihist] += pht->PeakTime() - sTick;
519 err[ihist] += chg * chg;
522 for(
unsigned short ihist = 0; ihist < nhist; ++ihist) if(cnt[ihist] > 0) {
523 tck[ihist] /= cnt[ihist];
524 ave[ihist] /= cnt[ihist];
528 for(
unsigned short ihist = 0; ihist < nhist; ++ihist) {
530 if(cnt[ihist] < 5)
continue;
531 maxChg[ihist] =
fChgCuts[1] * ave[ihist];
532 minChg[ihist] =
fChgCuts[0] * ave[ihist];
537 for(
unsigned short ihist = 0; ihist < nhist; ++ihist) {
538 if(cnt[ihist] < 3)
continue;
539 double arg = err[ihist] - cnt[ihist] * ave[ihist] * ave[ihist];
545 err[ihist] = sqrt(arg / (cnt[ihist] - 1));
547 err[ihist] /= sqrt(cnt[ihist]);
553 unsigned short nok = 0;
554 for(
auto& icnt : cnt)
if(icnt > 4) ++nok;
574 double xx, yy, wght, arg;
577 for(
unsigned short ihist = 0; ihist < nhist; ++ihist) {
578 if(cnt[ihist] < 3)
continue;
579 if(err[ihist] == 0)
continue;
580 xx = (tck[ihist] - tck[0]) * msPerTick;
581 yy = log(ave[ihist]);
583 arg = ave[ihist] / err[ihist];
588 sumx2 += wght * xx * xx;
589 sumxy += wght * xx * yy;
590 sumy2 += wght * yy * yy;
594 double delta = sum * sumx2 - sumx * sumx;
595 if(delta == 0.)
continue;
596 double A = (sumx2 * sumy - sumx * sumxy) / delta;
598 double B = (sumxy * sum - sumx * sumy) / delta;
601 double ndof = fitcnt - 2;
602 double varnce = (sumy2 + A*A*sum + B*B*sumx2 - 2 * (A*sumy + B*sumxy - A*B*sumx)) / ndof;
603 if(varnce == 0)
continue;
612 for(
unsigned short ihist = 0; ihist < nhist; ++ihist) {
613 if(cnt[ihist] < 3)
continue;
614 if(err[ihist] == 0)
continue;
615 xx = (tck[ihist] - tck[0]) * msPerTick;
616 yy = exp(A - xx * lifeInv);
617 arg = (yy - ave[ihist]) / err[ihist];
629 fLife->Fill(1./lifeInv);
630 fLifeVTPC->Fill(tpcMapping.at(tpc),1./lifeInv);
633 for(
auto& hcnt : cnt) selHits += hcnt;
634 selHits /= (double)(clsHits.size());
640 std::vector<std::pair<unsigned int, float>> chanPedRMS;
642 for(
unsigned int witer = 0; witer < wireVecHandle->size(); ++witer) {
645 for(
const auto& range : signalROI.
get_ranges()) {
646 const std::vector<float>& signal = range.data();
648 if(signal.size() != 1)
continue;
650 float rms = signal[0];
651 if(rms < 0.001) rms = 0.001;
652 chanPedRMS.push_back(std::make_pair(thisWire->
Channel(),
rms));
658 for(
unsigned int icl = 0; icl < clsVecHandle->size(); ++icl) {
667 std::vector<art::Ptr<recob::Hit> > clsHits;
668 clsHitsFind.get(icl, clsHits);
670 unsigned short tpc = clsHits[0]->WireID().TPC;
674 for(
auto&
hit : clsHits) {
676 for(
auto& chrms : chanPedRMS) {
677 if(chrms.first !=
hit->Channel())
continue;
678 pedRMS = chrms.second;
681 if(pedRMS < 0)
continue;
682 float snr =
hit->PeakAmplitude() / pedRMS;
690 fSNRVTPC->Fill(tpcMapping.at(tpc),snr);
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
double rms(sqlite3 *db, std::string const &table_name, std::string const &column_name)
#define appendToHistTitle(hist, str)
void respondToOpenInputFile(art::FileBlock const &infileblock) override
float StartWire() const
Returns the wire coordinate of the start of the cluster.
const range_list_t & get_ranges() const
Returns the internal list of non-void ranges.
float EndTick() const
Returns the tick coordinate of the end of the cluster.
geo::PlaneID Plane() const
Returns the plane ID this cluster lies on.
EDAnalyzer(fhicl::ParameterSet const &pset)
float StartAngle() const
Returns the starting angle of the cluster.
Q_EXPORT QTSManip setprecision(int p)
art framework interface to geometry description
double signalToNoiseCnt[12]
#define DEFINE_ART_MODULE(klass)
void swap(Handle< T > &a, Handle< T > &b)
raw::ChannelID_t Channel() const
Returns the ID of the channel (or InvalidChannelID)
T get(std::string const &key) const
std::string const & fileName() const
const std::array< std::string, 6 > apaLabels
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
const RegionsOfInterest_t & SignalROI() const
Returns the list of regions of interest.
const std::array< size_t, 13 > tpcMapping
TProfile * fLifeInv_Angle
unsigned int signalToNoiseClsCnt[12]
PlaneID_t Plane
Index of the plane within its TPC.
Definition of data types for geometry description.
void err(const char *fmt,...)
Detector simulation of raw signals on wires.
Q_EXPORT QTSManip setw(int w)
std::string fClusterModuleLabel
std::vector< float > fChgCuts
Declaration of signal hit object.
SPLifetime & operator=(SPLifetime const &)=delete
Declaration of basic channel signal object.
#define setHistTitles(hist, xtitle, ytitle)
void reconfigure(fhicl::ParameterSet const &p)
SPLifetime(fhicl::ParameterSet const &p)
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
void analyze(art::Event const &e) override
float StartTick() const
Returns the tick coordinate of the start of the cluster.
Q_EXPORT QTSManip setfill(int f)
std::string to_string(ModuleType const mt)
QTextStream & endl(QTextStream &s)
float EndWire() const
Returns the wire coordinate of the end of the cluster.