GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
TimeWalkCal.hh
Go to the documentation of this file.
1#ifndef GAMRSORT_TIMEWALKCAL_HH
2#define GAMRSORT_TIMEWALKCAL_HH
3
4#include "Sorter.hh"
5
6#include <memory>
7#include <sstream>
8#include <string>
9#include <vector>
10
11#include <TH2.h>
12#include <TTreeReader.h>
13#include <TTreeReaderArray.h>
14#include <TTreeReaderValue.h>
15
16#include <nucleus/Photon.hh>
17
18namespace GamR {
19namespace Sort {
20namespace Type {
21
22template <class Det, size_t E, size_t T>
24private:
25 Sorter *p;
26 std::map<std::pair<int, int>, std::shared_ptr<TH2D>> ptrPkMatrix;
27 std::map<std::pair<int, int>, std::shared_ptr<TH2D>> ptrBgMatrix;
28 std::shared_ptr<TH2D> ptrPkMatrixSum;
29 std::shared_ptr<TH2D> ptrBgMatrixSum;
30 std::shared_ptr<GamR::Nucleus::Photon> ptrRefPhoton;
31 std::vector<int> detIds;
32 std::string gid;
33
34public:
35 TimeWalkCal(Sorter *parent, std::vector<std::string> opts) : p(parent)
36 {
37 if (opts.size() < 8)
38 throw std::length_error("Invalid arguments, expect: group ref_E ref_p "
39 "ref_P ref_b ref_B id1 id2 ... idn");
40
41 std::stringstream ss;
42 std::copy(opts.begin(), opts.end(), std::ostream_iterator<std::string>(ss, "\n"));
43
44 ss >> gid;
45 float energy;
46 ss >> energy;
47 float pklow;
48 ss >> pklow;
49 float pkhigh;
50 ss >> pkhigh;
51 float bglow;
52 ss >> bglow;
53 float bghigh;
54 ss >> bghigh;
55 ptrRefPhoton = std::make_shared<GamR::Nucleus::Photon>(energy, pklow, pkhigh, bglow, bghigh);
56 ptrRefPhoton->SetName("TC_REF");
57 p->WriteObject(ptrRefPhoton.get());
58
59 int id;
60 while (ss >> id) {
61 detIds.push_back(id);
62 };
63
64 // Create the histogram objects
65 ptrPkMatrixSum = p->GetMatrix("P_TCAL");
66 ptrBgMatrixSum = p->GetMatrix("B_TCAL");
67
68 ptrPkMatrixSum->SetBins(2048, 0, 2048, 2048, -1024, 1024);
69 ptrBgMatrixSum->SetBins(2048, 0, 2048, 2048, -1024, 1024);
70
71 std::stringstream ssTitle;
72 for (const auto &i : detIds) {
73 for (const auto &j : detIds) {
74 if (i == j) {
75 continue;
76 }
77 ssTitle.str("");
78 ss.clear();
79 ss.str("");
80 ss << i << "_" << j;
81 ptrPkMatrix[{i, j}] = p->GetMatrix(("P" + ss.str()));
82 ptrBgMatrix[{i, j}] = p->GetMatrix(("B" + ss.str()));
83
84 ssTitle << "$E_{" << i << "}=" << ptrRefPhoton->GetEnergy() << "$ Gate: $";
85 ssTitle << ptrRefPhoton->GetGate()->GetLow() << "\\to";
86 ssTitle << ptrRefPhoton->GetGate()->GetHigh();
87 ssTitle << "$;Gamma Energy, E_{" << j;
88 ssTitle << "} [keV];$\\Delta t = t_{" << j << "}-t_{" << i << "}$";
89
90 ptrPkMatrix[{i, j}]->SetTitle(ssTitle.str().c_str());
91 ptrPkMatrix[{i, j}]->SetBins(2048, 0, 2048, 2048, -1024, 1024);
92 // ptrPkMatrix[{i,j}] -> SetCanExtend(TH1::kYaxis); // better have a
93 // knob
94 // todo find out how many bins to actually use
95
96 ptrBgMatrix[{i, j}]->SetTitle(ssTitle.str().c_str());
97 ptrBgMatrix[{i, j}]->SetBins(2048, 0, 2048, 2048, -1024, 1024);
98 }
99 }
100 } // Sorter::TimeWalk
101
102 void operator()(TTreeReader &R)
103 {
104 std::string group = "DetGroup._" + gid;
105 TTreeReaderValue<std::vector<Det>> rdrDets(R, group.c_str());
106 while (R.Next() && !p->Done()) {
107 std::vector<Det> vecDets = *rdrDets;
108 p->Tick();
109 if (vecDets.size() < 2) {
110 continue;
111 }
112 for (auto &deti : vecDets) {
113 for (auto &detj : vecDets) {
114 if (deti.ID == detj.ID) {
115 continue;
116 }
117 Bool_t pkgated = ptrRefPhoton->GetGate()->Pass(deti.template GetCal<E>());
118 Bool_t bggated = ptrRefPhoton->GetGateBG()->Pass(deti.template GetCal<E>());
119
120 if ((pkgated || bggated) && (deti.template Get<T>() > 0) && (detj.template Get<T>() > 0)) {
121 // tdiff = T2 - T1 ALWAYS.
122 auto tdiff = detj.template Get<T>() - deti.template Get<T>(); // uncalibrated, think, document
123 if (pkgated) {
124 ptrPkMatrix[{deti.ID, detj.ID}]->Fill(detj.template GetCal<E>(), tdiff);
125 ptrPkMatrixSum->Fill(detj.template GetCal<E>(), tdiff);
126 } else if (bggated) {
127 ptrBgMatrix[{deti.ID, detj.ID}]->Fill(detj.template GetCal<E>(), tdiff);
128 ptrBgMatrixSum->Fill(detj.template GetCal<E>(), tdiff);
129 }
130 } // gated
131 } // fold 2
132 } // fold 1
133 } // all events
134 } // Sorter::TimeWalk::operator()
135};
136
137} // namespace Type
138} // namespace Sort
139} // namespace GamR
140
141#endif
void ss(std::vector< int > indexes, TCanvas *canvas, Option_t *option)
void operator()(TTreeReader &R)
TimeWalkCal(Sorter *parent, std::vector< std::string > opts)
Definition Gain.cc:19