ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtTabEnergyLoss.cxx
Go to the documentation of this file.
1 #include "AtTabEnergyLoss.h"
2 
3 #include "AtCSVReader.h" // for CSVRow, CSVRange, CSVIterator
4 #include "AtDataManip.h" // for GetTB
5 #include "AtE12014.h" // for E12014
6 #include "AtHit.h" // for AtHit, AtHit::XYZPoint, AtHit::X...
7 #include "AtMap.h" // for AtMap, AtMap::InhibitType, AtMap...
8 #include "AtRawEvent.h" // for AtRawEvent
9 #include "AtViewerManager.h" // for AtViewerManager
10 #include "AtViewerManagerSubject.h" // for AtTreeEntry, AtBranch
11 
12 #include <FairLogger.h> // for Logger, LOG
13 
14 #include <TCanvas.h> // for TCanvas
15 #include <TF1.h> // for TF1, TF1::EAddToList, TF1::EAddT...
16 #include <TH1.h> // for TH1F
17 #include <THStack.h> // for THStack
18 #include <TMath.h> // for Sqrt, Sq, CeilNint
19 #include <TString.h> // for TString, operator<<
20 
21 #include <algorithm> // for max
22 #include <cmath> // for abs, sqrt
23 #include <iostream> // for ifstream
24 #include <string> // for getline, string
25 
28 
29 void AtTabEnergyLoss::SetStyle(std::array<TH1Ptr, 2> &hists, THStack &stack)
30 {
31  for (int i = 0; i < hists.size(); ++i) {
32  hists[i]->SetLineColor(fHistColors[i]);
33  hists[i]->SetMarkerColor(fHistColors[i]);
34  hists[i]->SetMarkerStyle(21);
35  hists[i]->SetDirectory(nullptr);
36 
37  stack.Add(hists[i].get());
38  }
39 }
40 
42  : AtTabCanvas("dE/dx", 2, 2), fRawEvent(AtViewerManager::Instance()->GetRawEventBranch()),
43  fFissionEvent(fissionBranch), fEntry(AtViewerManager::Instance()->GetCurrentEntry()), fBinWidth(100),
44  fSigmaFromHit(2), fTBtoAvg(10), fRatioFunc(std::make_unique<TF1>("ratioFunc", "pol0", 0, 1, TF1::EAddToList::kNo)),
45  fProxyFunc(std::make_unique<TF1>("proxyFunc", "pol0", 0, 1, TF1::EAddToList::kNo)),
46  fZFunc(std::make_unique<TF1>("zFunc", "pol0", 0, 1, TF1::EAddToList::kNo))
47 {
48 
49  double maxLength = TMath::Sqrt(TMath::Sq(1000) + TMath::Sq(25));
50  int nBins = TMath::CeilNint(maxLength / fBinWidth.Get());
51 
52  // Want bins = number of TB across detector
53  int minBin = 0;
54  int numBins = 512; // AtTools::GetDriftTB(1000);
55  int maxBin = 512; // 1000;
56 
57  dEdx[0] = std::make_unique<TH1F>("dEdx1", "dEdX Frag 1", nBins, 0, maxLength);
58  dEdx[1] = std::make_unique<TH1F>("dEdx2", "dEdX Frag 2", nBins, 0, maxLength);
59  SetStyle(dEdx, dEdxStack);
60 
61  dEdxZ[0] = std::make_unique<TH1F>("dEdxZ1", "dEdX Frag 1", nBins, 0, maxLength);
62  dEdxZ[1] = std::make_unique<TH1F>("dEdxZ2", "dEdX Frag 2", nBins, 0, maxLength);
63  SetStyle(dEdxZ, dEdxStackZ);
64 
66  // fSumQ[0] = std::make_unique<TH1F>("qSum_0", "Q Sum Frag 1", 512, 0, 512);
67  // fSumQ[1] = std::make_unique<TH1F>("qSum_1", "Q Sum Frag 2", 512, 0, 512);
68  fSumQ[0] = std::make_unique<TH1F>("qSum_0", "Q Sum Frag 1", numBins, minBin, maxBin);
69  fSumQ[1] = std::make_unique<TH1F>("qSum_1", "Q Sum Frag 2", numBins, minBin, maxBin);
70  SetStyle(fSumQ, dEdxStackSum);
71 
72  fSumFit[0] = std::make_unique<TH1F>("fitSum_0", "Fit Sum Frag 1", numBins, minBin, maxBin);
73  fSumFit[1] = std::make_unique<TH1F>("fitSum_1", "Fit Sum Frag 2", numBins, minBin, maxBin);
74  SetStyle(fSumFit, dEdxStackFit);
75 
76  fRatioQ = std::make_unique<TH1F>("ratioQ", "Ratio of Q Sum", numBins, minBin, maxBin);
77  fRatioFit = std::make_unique<TH1F>("ratioFit", "Ratio of Fit Sum", numBins, minBin, maxBin);
78  fProxy = std::make_unique<TH1F>("proxy", "Z Proxy", numBins, minBin, maxBin);
79  fZHist = std::make_unique<TH1F>("zHist", "Z of light fragment", numBins, minBin, maxBin);
80 
81  fVetoPads = {{0, 1, 1, 6}, {0, 1, 1, 7}, {0, 1, 1, 9}, {0, 1, 1, 10}, {0, 1, 1, 12}, {0, 1, 1, 39},
82  {0, 1, 1, 40}, {0, 1, 1, 41}, {0, 1, 1, 44}, {0, 1, 1, 43}, {0, 1, 1, 46}, {0, 1, 3, 13}};
83 
84  std::ifstream file("/mnt/projects/hira/e12014/tpcSharedInfo/e12014_zap.csv");
85  if (!file.is_open())
86  LOG(fatal) << "File not open";
87 
88  std::string header;
89  std::getline(file, header);
90 
91  for (auto &row : CSVRange<int>(file)) {
92  fVetoPads.push_back({row[0], row[1], row[2], row[3]});
93  }
94 
95  fEntry.Attach(this);
96 }
97 
99 {
100  fEntry.Detach(this);
101 }
102 
104 
106 {
107  if (sub == &fEntry) {
108  Update();
109  UpdateCanvas();
110  }
111 }
112 
113 void AtTabEnergyLoss::Update()
114 {
115  for (auto &hist : dEdx)
116  hist->Reset();
117  for (auto &hist : dEdxZ)
118  hist->Reset();
119  for (auto &hist : fSumQ)
120  hist->Reset();
121  for (auto &hist : fSumFit)
122  hist->Reset();
123  fRatioQ->Reset();
124  fRatioFit->Reset();
125  fProxy->Reset();
126  fZHist->Reset();
127 
128  if (fFissionEvent.Get() == nullptr) {
129  LOG(info) << "Cannot find AtFissionEvent in branch " << fFissionEvent.GetBranch().GetBranchName();
130  return;
131  }
132 
133  setdEdX();
134 
135  // Fill fSumQ and fSumFit
136  FillSums();
137  FillRatio();
138 
139  fCanvas->cd(1);
140  dEdxStackSum.Draw("nostack;hist");
141  fCanvas->cd(2);
142  dEdxStackFit.Draw("nostack;hist");
143 
144  // dEdxStack.Draw("nostack,X0,ep1");
145  // dEdxStackZ.Draw("nostack,X0,ep1");
146 
147  fCanvas->cd(3);
148  fZHist->Draw("hist");
149  fZFunc->Draw("SAME");
150  // fRatioFunc->Draw("SAME");
151 
152  fCanvas->cd(4);
153  fRatioFit->Draw("hist");
154  fRatioFunc->Draw("SAME");
155 }
156 
157 float AtTabEnergyLoss::GetZ(int Zcn, float R)
158 {
159  return Zcn / (2 * R) * (R - 1 + std::sqrt(1 - R * R));
160 }
161 void AtTabEnergyLoss::FillRatio()
162 {
163  // Get the hit to use as the start of the ratio filling. We want the index that is closest to
164  // the pad plane (location is closest to zero)
165  int index = (fTrackStart[0] < fTrackStart[1]) ? 0 : 1;
166  int tbFinal = AtTools::GetTB(fTrackStart[index]);
167  int tbIni = tbFinal - fTBtoAvg.Get();
168  LOG(info) << "Starting ratio from " << fTrackStart[index] << "(mm) " << tbFinal << "(TB)";
169 
170  // Get the track index that has the higher charge. This should be the numberator in the division
171  int trackInd = fSumQ[0]->GetBinContent(tbIni + 1) > fSumQ[1]->GetBinContent(tbIni + 1) ? 0 : 1;
172 
173  fRatioQ->Divide(fSumQ[trackInd].get(), fSumQ[(trackInd + 1) % 2].get());
174  fRatioFit->Divide(fSumFit[trackInd].get(), fSumFit[(trackInd + 1) % 2].get());
175 
176  // Fill the proxy function
177  for (int i = 0; i < fSumFit[0]->GetNbinsX(); ++i) {
178  auto de1 = fSumFit[0]->GetBinContent(i);
179  auto de2 = fSumFit[1]->GetBinContent(i);
180  if (de1 + de2 == 0)
181  continue;
182  auto proxy = std::abs(de1 - de2) / (de1 + de2);
183  fProxy->SetBinContent(i, proxy);
184  fZHist->SetBinContent(i, GetZ(85, proxy));
185  }
186 
187  // Get the average of the ratio
188  double ratioAvg = 0, proxyAvg = 0;
189 
190  for (int i = tbIni; i < tbFinal; ++i) {
191  ratioAvg += fRatioFit->GetBinContent(i + 1);
192  proxyAvg += fProxy->GetBinContent(i + 1);
193  }
194  ratioAvg /= fTBtoAvg.Get();
195  proxyAvg /= fTBtoAvg.Get();
196 
197  LOG(info) << "Average ratio: " << ratioAvg << " Z avg: " << GetZ(85, proxyAvg) << "/" << 85 - GetZ(85, proxyAvg);
198 
199  // Set range of functions
200  fRatioFunc->SetRange(tbIni, tbFinal);
201  fZFunc->SetRange(tbIni, tbFinal);
202  fProxyFunc->SetRange(tbIni, tbFinal);
203 
204  // Set value of functions
205  fRatioFunc->SetParameter(0, ratioAvg);
206 
207  fZFunc->SetParameter(0, GetZ(85, proxyAvg));
208  fProxyFunc->SetParameter(0, proxyAvg);
209 }
210 
211 void AtTabEnergyLoss::FillSums(float threshold)
212 {
213  if (fRawEvent.Get() == nullptr)
214  return;
215  for (int i = 0; i < 2; ++i) {
216  fFirstHit[i] = nullptr;
217  fTrackStart[i] = 0;
218 
219  // Fill fSumQ
220  E12014::FillChargeSum(fSumQ[i].get(), fFissionEvent->GetFragHits(i), *fRawEvent, threshold);
221 
222  // Fill fSumFit
223  E12014::FillHitSum(*fSumFit[i], fFissionEvent->GetFragHits(i), threshold, 4500);
224 
225  for (auto &hit : fFissionEvent->GetFragHits(i)) {
226  auto inhibitType = AtViewerManager::Instance()->GetMap()->IsInhibited(hit->GetPadNum());
227  if (inhibitType != AtMap::InhibitType::kNone)
228  continue;
229 
230  // Update the first hit (want highest TB)
231  auto hitLocation = hit->GetPosition().Z() - fSigmaFromHit.Get() * hit->GetPositionSigma().Z();
232  if (fFirstHit[i] == nullptr) {
233  fFirstHit[i] = hit;
234  } else if (hitLocation <
235  fFirstHit[i]->GetPosition().Z() - fSigmaFromHit.Get() * fFirstHit[i]->GetPositionSigma().Z()) {
236  fFirstHit[i] = hit;
237  }
238 
239  // Update hit location
240 
241  if (fTrackStart[i] < hitLocation) {
242  LOG(debug) << "Setting start of " << i << " to " << hit->GetPosition() << " at " << hit->GetPadNum();
243  fTrackStart[i] = hitLocation;
244  }
245  }
246  }
247 }
248 void AtTabEnergyLoss::setdEdX()
249 {
250  for (int i = 0; i < 2; ++i) {
251 
252  for (auto &hit : fFissionEvent->GetFragHits(i))
253  if (hit->GetPosition().z() > 0 && hit->GetPosition().Z() > fFissionEvent->GetVertex().Z()) {
254  dEdx[i]->Fill(getHitDistanceFromVertex(*hit), hit->GetCharge());
255  dEdxZ[i]->Fill(getHitDistanceFromVertexAlongZ(*hit), hit->GetCharge());
256  }
257 
258  for (int bin = 0; bin < dEdx[i]->GetNbinsX(); ++bin) {
259  dEdx[i]->SetBinError(bin, TMath::Sqrt(dEdx[i]->GetBinContent(bin)));
260  dEdxZ[i]->SetBinError(bin, TMath::Sqrt(dEdxZ[i]->GetBinContent(bin)));
261  }
262  }
263 }
264 
265 double AtTabEnergyLoss::getHitDistanceFromVertex(const AtHit &hit)
266 {
267  auto p = hit.GetPosition();
268  auto position = XYZPoint(p.x(), p.y(), p.z());
269  auto diff = position - fFissionEvent->GetVertex();
270  return TMath::Sqrt(diff.Mag2());
271 }
272 
273 double AtTabEnergyLoss::getHitDistanceFromVertexAlongZ(const AtHit &hit)
274 {
275  auto p = hit.GetPosition();
276  auto diff = p.z() - fFissionEvent->GetVertex().z();
277  return diff;
278 }
CSVRange
Range class for CSVIterator.
Definition: AtCSVReader.h:102
AtTabCanvas::fCanvas
TCanvas * fCanvas
Definition: AtTabCanvas.h:22
AtRawEvent.h
AtTabEnergyLoss::Update
void Update(DataHandling::AtSubject *sub) override
Definition: AtTabEnergyLoss.cxx:105
AtTabEnergyLoss::fFirstHit
std::array< AtHit *, 2 > fFirstHit
Definition: AtTabEnergyLoss.h:75
AtTabEnergyLoss::fZFunc
std::unique_ptr< TF1 > fZFunc
Definition: AtTabEnergyLoss.h:79
AtTabEnergyLoss::fSumFit
std::array< TH1Ptr, 2 > fSumFit
Definition: AtTabEnergyLoss.h:67
DataHandling::AtBranch
Subject for the branch in the FairRoot tree.
Definition: AtViewerManagerSubject.h:38
AtTabEnergyLoss::fVetoPads
std::vector< AtPadReference > fVetoPads
Definition: AtTabEnergyLoss.h:81
AtFissionEvent::GetVertex
XYZPoint GetVertex() const
Definition: AtFissionEvent.cxx:64
AtTabEnergyLoss::~AtTabEnergyLoss
~AtTabEnergyLoss()
Definition: AtTabEnergyLoss.cxx:98
AtTabEnergyLoss::dEdxStackFit
THStack dEdxStackFit
Definition: AtTabEnergyLoss.h:66
XYZVector
ROOT::Math::XYZVector XYZVector
Definition: AtFindVertex.h:20
AtTabInfoFairRoot::GetBranch
const DataHandling::AtBranch & GetBranch() const
Definition: AtTabInfo.h:117
AtTools::GetTB
double GetTB(double z, const AtDigiPar *par=nullptr)
Get TB that corresponds to the passed z position [mm].
Definition: AtDataManip.cxx:21
AtTabEnergyLoss::dEdxStack
THStack dEdxStack
Definition: AtTabEnergyLoss.h:56
XYZVector
ROOT::Math::XYZVector XYZVector
Definition: AtTabEnergyLoss.cxx:26
AtTabEnergyLoss::fTrackStart
std::array< float, 2 > fTrackStart
Definition: AtTabEnergyLoss.h:74
AtTabEnergyLoss::fSumQ
std::array< TH1Ptr, 2 > fSumQ
Definition: AtTabEnergyLoss.h:64
AtTabEnergyLoss::fFissionEvent
AtTabInfoFairRoot< AtFissionEvent > fFissionEvent
Definition: AtTabEnergyLoss.h:43
DataHandling::AtSubject::Detach
void Detach(AtObserver *observer)
Detach an observer to stop getting notified when this subject changes.
Definition: AtDataSubject.h:37
AtViewerManager::Instance
static AtViewerManager * Instance()
Definition: AtViewerManager.cxx:43
AtTabEnergyLoss::XYZPoint
ROOT::Math::XYZPoint XYZPoint
Definition: AtTabEnergyLoss.h:38
AtTabEnergyLoss::fProxy
TH1Ptr fProxy
Definition: AtTabEnergyLoss.h:71
AtE12014.h
XYZPoint
ROOT::Math::XYZPoint XYZPoint
Definition: AtPatternCircle2D.h:18
AtTabEnergyLoss::InitTab
void InitTab() override
Definition: AtTabEnergyLoss.cxx:103
AtTabEnergyLoss.h
AtTabInfoFairRoot::Get
T * Get()
Definition: AtTabInfo.h:102
AtTabEnergyLoss::fRatioFunc
std::unique_ptr< TF1 > fRatioFunc
Definition: AtTabEnergyLoss.h:77
AtDataManip.h
AtViewerManagerSubject.h
AtTabEnergyLoss::fHistColors
const std::array< Color_t, 2 > fHistColors
Definition: AtTabEnergyLoss.h:54
AtTabEnergyLoss::GetZ
static float GetZ(int Zcn, float proxy)
Definition: AtTabEnergyLoss.cxx:157
AtHit::GetPosition
const XYZPoint & GetPosition() const
Definition: AtHit.h:79
AtTabEnergyLoss::dEdxStackSum
THStack dEdxStackSum
Definition: AtTabEnergyLoss.h:63
AtHit.h
AtViewerManager::GetMap
AtMap * GetMap()
Definition: AtViewerManager.h:66
AtTabEnergyLoss::fZHist
TH1Ptr fZHist
Definition: AtTabEnergyLoss.h:72
AtMap::InhibitType::kNone
@ kNone
E12014::FillHitSum
static std::set< int > FillHitSum(TH1 &hist, const std::vector< AtHit * > &hits, int threshold=0, float saturationThreshold=std::numeric_limits< float >::max())
Definition: AtE12014.cxx:94
AtTabEnergyLoss::fSigmaFromHit
DataHandling::AtSimpleType< float > fSigmaFromHit
Definition: AtTabEnergyLoss.h:50
AtTabEnergyLoss::dEdxStackZ
THStack dEdxStackZ
Definition: AtTabEnergyLoss.h:59
AtTabEnergyLoss::fProxyFunc
std::unique_ptr< TF1 > fProxyFunc
Definition: AtTabEnergyLoss.h:78
AtTabEnergyLoss::fBinWidth
DataHandling::AtSimpleType< float > fBinWidth
Definition: AtTabEnergyLoss.h:46
AtTabEnergyLoss::fTBtoAvg
DataHandling::AtSimpleType< int > fTBtoAvg
Definition: AtTabEnergyLoss.h:51
AtTabEnergyLoss::fRatioFit
TH1Ptr fRatioFit
Definition: AtTabEnergyLoss.h:70
E12014::FillChargeSum
static void FillChargeSum(TH1 *hist, const std::vector< AtHit * > &hits, AtRawEvent &event, int threshold=0, std::string qName="Qreco")
Definition: AtE12014.cxx:65
DataHandling::AtSimpleType::Get
T Get() const
Definition: AtDataSubject.h:57
AtTabCanvas
Abstract class for a tab composed of a single TCanvas.
Definition: AtTabCanvas.h:20
AtViewerManager
Definition: AtViewerManager.h:33
DataHandling::AtSubject::Attach
void Attach(AtObserver *observer)
Attach an observer to get notified when this subject changes.
Definition: AtDataSubject.cxx:12
AtMap.h
DataHandling::AtSubject
Definition: AtDataSubject.h:24
AtTabEnergyLoss::dEdx
std::array< TH1Ptr, 2 > dEdx
Definition: AtTabEnergyLoss.h:57
AtTabEnergyLoss::fRatioQ
TH1Ptr fRatioQ
Definition: AtTabEnergyLoss.h:69
AtTabCanvas::UpdateCanvas
void UpdateCanvas()
Definition: AtTabCanvas.cxx:24
AtViewerManager.h
AtCSVReader.h
AtTabEnergyLoss::AtTabEnergyLoss
AtTabEnergyLoss(DataHandling::AtBranch &fissionBranch)
Definition: AtTabEnergyLoss.cxx:41
AtTabEnergyLoss::fEntry
DataHandling::AtTreeEntry & fEntry
Definition: AtTabEnergyLoss.h:45
AtTabEnergyLoss::dEdxZ
std::array< TH1Ptr, 2 > dEdxZ
Definition: AtTabEnergyLoss.h:60
AtFissionEvent::GetFragHits
std::vector< AtHit * > GetFragHits() const
Definition: AtFissionEvent.cxx:86
DataHandling::AtBranch::GetBranchName
TString GetBranchName() const
Definition: AtViewerManagerSubject.cxx:32
AtTabEnergyLoss::fRawEvent
AtTabInfoFairRoot< AtRawEvent > fRawEvent
Definition: AtTabEnergyLoss.h:42
AtHit
Point in space with charge.
Definition: AtHit.h:27
XYZPoint
ROOT::Math::XYZPoint XYZPoint
Definition: AtTabEnergyLoss.cxx:27
AtMap::IsInhibited
AtMap::InhibitType IsInhibited(Int_t PadNum)
Definition: AtMap.h:98