From 7c99cf7bbc999d0b4300d9e042d8ac2edad525c6 Mon Sep 17 00:00:00 2001
From: Wouter Deconinck <wouter.deconinck@umanitoba.ca>
Date: Fri, 13 Aug 2021 10:13:58 +0000
Subject: [PATCH] Benchmarks for mrich: number of hits

---
 benchmarks/pid/config.yml                 | 35 ++++++++---
 benchmarks/pid/dummy_test.sh              |  6 --
 benchmarks/pid/scripts/mrich_analysis.cxx | 76 +++++++++++++++++++++++
 3 files changed, 102 insertions(+), 15 deletions(-)
 delete mode 100755 benchmarks/pid/dummy_test.sh
 create mode 100644 benchmarks/pid/scripts/mrich_analysis.cxx

diff --git a/benchmarks/pid/config.yml b/benchmarks/pid/config.yml
index 6e6d226d..e424d369 100644
--- a/benchmarks/pid/config.yml
+++ b/benchmarks/pid/config.yml
@@ -1,12 +1,29 @@
-#stages:
-#  #- simulate
-#  - benchmarks
-pid_test_1_dummy_test:
+sim:backward:
+  extends: .det_benchmark
+  stage: simulate
+  script:
+    - npsim --runType batch --numberOfEvents 100 --compactFile ${DETECTOR_PATH}/${JUGGLER_DETECTOR}.xml --enableGun --gun.energy "5*GeV" --gun.particle "${PARTICLE}" --gun.thetaMin 130*deg --gun.thetaMax 177*deg --gun.distribution "cos(theta)" --outputFile sim_output/sim_pid_backward_${PARTICLE}_5GeV.root
+    - rootls -t sim_output/sim_pid_backward_${PARTICLE}_5GeV.root
+  parallel:
+    matrix:
+      - PARTICLE: ["e-", "pi+", "proton"]
+
+bench:mrich:
+  extends: .det_benchmark
   stage: benchmarks
+  needs: ["sim:backward"]
+  script:
+    - mkdir -p results/pid/backward/mrich/
+    - root -t -x -q -b "benchmarks/pid/scripts/mrich_analysis.cxx+(\"sim_output/sim_pid_backward_${PARTICLE}_5GeV.root\", \"${PARTICLE}\")"
+  parallel:
+    matrix:
+      - PARTICLE: ["e-", "pi+", "proton"]
+
+collect_results:pid:
+  extends: .det_benchmark
+  stage: collect
+  needs: 
+    - ["bench:mrich"]
   script:
-    - bash benchmarks/pid/dummy_test.sh
-  artifacts:
-    paths: 
-      - results/
-  allow_failure: true
+    - ls -lrht
 
diff --git a/benchmarks/pid/dummy_test.sh b/benchmarks/pid/dummy_test.sh
deleted file mode 100755
index 97f97bbc..00000000
--- a/benchmarks/pid/dummy_test.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/bin/bash
-
-echo "PID Dummy Test..."
-echo "passes."
-
-exit 0
diff --git a/benchmarks/pid/scripts/mrich_analysis.cxx b/benchmarks/pid/scripts/mrich_analysis.cxx
new file mode 100644
index 00000000..f8ca59f4
--- /dev/null
+++ b/benchmarks/pid/scripts/mrich_analysis.cxx
@@ -0,0 +1,76 @@
+////////////////////////////////////////
+// Read reconstruction ROOT output file
+// Plot variables
+////////////////////////////////////////
+
+#include "ROOT/RDataFrame.hxx"
+#include <iostream>
+#include <fmt/core.h>
+
+#include "dd4pod/Geant4ParticleCollection.h"
+#include "dd4pod/PhotoMultiplierHitCollection.h"
+
+#include "TCanvas.h"
+#include "TStyle.h"
+#include "TMath.h"
+#include "TH1.h"
+#include "TF1.h"
+#include "TH1D.h"
+
+using ROOT::RDataFrame;
+using namespace ROOT::VecOps;
+
+void mrich_analysis(const char* input_fname = "sim_output/sim_pid_backward_e-_5GeV.root", const char* input_pname = "e-")
+{
+  // Setting for graphs
+  gROOT->SetStyle("Plain");
+  gStyle->SetOptFit(1);
+  gStyle->SetLineWidth(2);
+  gStyle->SetPadTickX(1);
+  gStyle->SetPadTickY(1);
+  gStyle->SetPadGridX(1);
+  gStyle->SetPadGridY(1);
+  gStyle->SetPadLeftMargin(0.14);
+  gStyle->SetPadRightMargin(0.14);
+
+  ROOT::EnableImplicitMT();
+  ROOT::RDataFrame d0("events", input_fname);
+
+  // Number of hits
+  auto nhits = [] (const std::vector<dd4pod::PhotoMultiplierHitData>& evt) {return (int) evt.size(); };
+
+  // Define variables
+  auto d1 = d0.Define("nhits", nhits, {"MRICHHits"});
+
+  // Define Histograms
+  auto hNhits =
+      d1.Histo1D({"hNhits", "Number of hits per events; Number of hits; Events",
+                  100, 0.0, 2000.0},
+                 "nhits");
+  auto hXYhits =
+      d1.Histo2D({"hXYhits", "Hit positions for events; Horizontal position [mm]; Vertical position [mm]",
+                  1000, -1000.0, +1000.0, 1000, -1000.0, +1000.0},
+                 "MRICHHits.position.x", "MRICHHits.position.y");
+
+  // Event Counts
+  auto nevents_thrown = d1.Count();
+  std::cout << "Number of Thrown Events: " << (*nevents_thrown) << "\n";
+
+  // Draw Histograms
+  {
+    TCanvas* c1 = new TCanvas("c1", "c1", 700, 500);
+    auto h = hXYhits->DrawCopy();
+    c1->SaveAs(fmt::format("results/pid/backward/mrich/mrich_{}_hits_xy.png",input_pname).c_str());
+    c1->SaveAs(fmt::format("results/pid/backward/mrich/mrich_{}_hits_xy.pdf",input_pname).c_str());
+  }
+  {
+    TCanvas* c2 = new TCanvas("c2", "c2", 700, 500);
+    c2->SetLogy(1);
+    auto h = hNhits->DrawCopy();
+    //h->GetYaxis()->SetTitleOffset(1.4);
+    h->SetLineWidth(2);
+    h->SetLineColor(kBlue);
+    c2->SaveAs(fmt::format("results/pid/backward/mrich/mrich_{}_nhits.png",input_pname).c_str());
+    c2->SaveAs(fmt::format("results/pid/backward/mrich/mrich_{}_nhits.pdf",input_pname).c_str());
+  }
+}
-- 
GitLab