ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtE12014.cxx
Go to the documentation of this file.
1 #include "AtE12014.h"
2 
3 #include "AtCSVReader.h"
4 #include "AtContainerManip.h"
5 #include "AtDataManip.h"
6 #include "AtHit.h"
7 #include "AtMap.h"
8 #include "AtPad.h" // for AtPad
9 #include "AtPadArray.h"
10 #include "AtPadReference.h" // for AtPadReference
11 #include "AtRawEvent.h"
12 #include "AtTpcMap.h"
13 
14 #include <FairLogger.h>
15 
16 #include <TF1.h>
17 #include <TH1.h>
18 #include <TString.h>
19 
20 #include <algorithm> // for fill_n, max
21 #include <cstdlib> // for getenv
22 #include <iosfwd> // for ifstream
23 #include <set>
24 std::shared_ptr<AtMap> E12014::fMap;
25 int E12014::fTBMin = 105;
26 int E12014::fThreshold = 1;
27 double E12014::fSatThreshold = 4200;
28 
30 {
31  fMap = std::make_shared<AtTpcMap>();
32  auto mapFile = TString(getenv("VMCWORKDIR")) + "/scripts/e12014_pad_map_size.xml";
33  fMap->ParseXMLMap(mapFile.Data());
34 
35  // Add the inhibited pads
36  AtPadReference ref{.cobo = 2, .asad = 3, .aget = 0, .ch = 0};
37  for (ref.aget = 0; ref.aget < 4; ++ref.aget) {
38  for (ref.ch = 0; ref.ch < 68; ++ref.ch) {
39  fMap->InhibitPad(ref, AtMap::InhibitType::kBadPad);
40  }
41  }
42  std::vector<AtPadReference> badPads = {{0, 1, 1, 6}, {0, 1, 1, 7}, {0, 1, 1, 9}, {0, 1, 1, 10},
43  {0, 1, 1, 12}, {0, 1, 1, 39}, {0, 1, 1, 40}, {0, 1, 1, 41},
44  {0, 1, 1, 44}, {0, 1, 1, 43}, {0, 1, 1, 46}, {0, 1, 3, 13}};
45 
46  for (auto &badPad : badPads)
47  fMap->InhibitPad(badPad, AtMap::InhibitType::kBadPad);
48 
49  // Add the smart-zap region to the map
50  std::ifstream file("/mnt/projects/hira/e12014/tpcSharedInfo/e12014_zap.csv");
51  if (!file.is_open())
52  LOG(error) << "Failed to open smart zap file";
53 
54  // Clear out the header
55  std::string temp;
56  std::getline(file, temp);
57  std::getline(file, temp);
58 
59  for (auto &row : CSVRange<int>(file)) {
60  LOG(debug) << "Inhibiting " << row[4];
61  fMap->InhibitPad(row[4], AtMap::InhibitType::kLowGain);
62  }
63 }
64 
65 void E12014::FillChargeSum(TH1 *hist, const std::vector<AtHit *> &hits, AtRawEvent &event, int threshold,
66  std::string qName)
67 {
68  if (fMap == nullptr)
69  LOG(fatal) << "The map (E12014::fMap) was never set! Please call E12014::CreateMap()";
70 
71  std::set<int> usedPads;
72  for (auto &hit : hits) {
73 
74  // Skip hit if the pad is inhibited or we've already added the charge
75  if (usedPads.count(hit->GetPadNum()) != 0 || fMap->IsInhibited(hit->GetPadNum()) != AtMap::InhibitType::kNone)
76  continue;
77  usedPads.insert(hit->GetPadNum());
78 
79  auto pad = event.GetPad(hit->GetPadNum());
80  if (pad == nullptr)
81  continue;
82 
83  const auto charge = pad->GetAugment<AtPadArray>(qName);
84  if (charge == nullptr)
85  continue;
86 
87  // If we pass all the checks, add the charge to the histogram
88  for (int tb = 20; tb < 500; ++tb)
89  if (charge->GetArray(tb) > threshold)
90  hist->Fill(tb + 0.5, charge->GetArray(tb));
91  }
92 }
93 
94 std::set<int> E12014::FillHitSum(TH1 &hist, const std::vector<AtHit *> &hits, int threshold, float satThresh)
95 {
96  std::vector<double> charge;
97  auto pads = FillHitSum(charge, hits, threshold, satThresh);
98  ContainerManip::SetHistFromData(hist, charge);
99  return pads;
100 }
101 
102 int E12014::FillHitSums(std::vector<double> &exp, std::vector<double> &sim, const std::vector<AtHit *> &expHits,
103  const std::vector<AtHit *> &simHits, int threshold, float satThresh, const AtDigiPar *fPar,
104  std::vector<double> *expADC, AtRawEvent *expEvent)
105 {
106  if (fMap == nullptr)
107  LOG(fatal) << "The map (E12014::fMap) was never set!";
108 
109  exp.clear();
110  exp.resize(512);
111  std::fill_n(exp.begin(), 512, 0);
112  sim.clear();
113  sim.resize(512);
114  std::fill_n(sim.begin(), 512, 0);
115  if (expADC) {
116  expADC->clear();
117  expADC->resize(512);
118  std::fill_n(expADC->begin(), 512, 0);
119  }
120 
121  int numGoodHits = 0;
122  for (auto &expHit : expHits) {
123  if (fMap->IsInhibited(expHit->GetPadNum()) != AtMap::InhibitType::kNone)
124  continue;
125  if (expHit->GetCharge() > satThresh)
126  continue;
127 
128  if (fMap->GetPadSize(expHit->GetPadNum()) != 0)
129  continue;
130 
131  auto funcExp = AtTools::GetHitFunctionTB(*expHit, fPar);
132  if (funcExp == nullptr)
133  continue;
134 
135  // We have a hit we want to save for both exp and fisison.
136  // Find the corresponding simulated hit if it exists.
137  AtHit *simHit = nullptr;
138  for (auto &hit : simHits) {
139  if (expHit->GetPadNum() == hit->GetPadNum()) {
140  simHit = hit;
141  break;
142  }
143  }
144  if (simHit == nullptr)
145  continue;
146  auto funcSim = AtTools::GetHitFunctionTB(*simHit, fPar);
147  if (funcSim == nullptr)
148  continue;
149 
150  AtPad *pad = nullptr;
151  if (expEvent)
152  pad = expEvent->GetPad(expHit->GetPadNum());
153 
154  numGoodHits++;
155  // Get the pad that corresponds to
156  // We now have the sim and exp hits. Fill the arrays
157  for (int tb = fTBMin; tb < 512; ++tb) {
158 
159  auto val = funcExp->Eval(tb);
160  if (val > threshold) {
161  exp[tb] += val;
162  sim[tb] += funcSim->Eval(tb);
163  if (pad && expADC)
164  (*expADC)[tb] += pad->GetADC(tb);
165  }
166  }
167  }
168  return numGoodHits;
169 }
170 
171 void E12014::FillSimSum(std::vector<double> &sim, const std::vector<AtHit *> &simHits)
172 {
173  if (fMap == nullptr)
174  LOG(fatal) << "The map (E12014::fMap) was never set!";
175 
176  sim.clear();
177  sim.resize(512);
178  std::fill_n(sim.begin(), 512, 0);
179 
180  for (auto &expHit : simHits) {
181 
182  auto funcExp = AtTools::GetHitFunctionTB(*expHit);
183  if (funcExp == nullptr)
184  continue;
185 
186  for (int tb = fTBMin; tb < 512; ++tb) {
187  sim[tb] += funcExp->Eval(tb);
188  }
189  }
190 }
191 
192 void E12014::FillHits(std::vector<double> &exp, std::vector<double> &sim, const std::vector<AtHit *> &expHits,
193  const std::vector<AtHit *> &simHits, float satThresh)
194 {
195  if (fMap == nullptr)
196  LOG(fatal) << "The map (E12014::fMap) was never set!";
197 
198  exp.clear();
199  sim.clear();
200 
201  for (auto &expHit : expHits) {
202  if (fMap->IsInhibited(expHit->GetPadNum()) != AtMap::InhibitType::kNone)
203  continue;
204  if (expHit->GetCharge() > satThresh)
205  continue;
206  if (expHit->GetPosition().z() < 50)
207  continue;
208  if (fMap->GetPadSize(expHit->GetPadNum()) != 0)
209  continue;
210 
211  // This is a good hit so save the experiment charge.
212  exp.push_back(expHit->GetCharge());
213 
214  // Look for the matching simualted hit and save its charge
215  int padNum = expHit->GetPadNum();
216  auto simHit =
217  std::find_if(simHits.begin(), simHits.end(), [padNum](const AtHit *a) { return a->GetPadNum() == padNum; });
218  if (simHit == simHits.end())
219  sim.push_back(0);
220  else
221  sim.push_back((*simHit)->GetCharge());
222  }
223 }
224 
225 void E12014::FillZPos(std::vector<double> &exp, std::vector<double> &sim, const std::vector<AtHit *> &expHits,
226  const std::vector<AtHit *> &simHits, float satThresh)
227 {
228  if (fMap == nullptr)
229  LOG(fatal) << "The map (E12014::fMap) was never set!";
230 
231  exp.clear();
232  sim.clear();
233 
234  for (auto &expHit : expHits) {
235  if (fMap->IsInhibited(expHit->GetPadNum()) != AtMap::InhibitType::kNone)
236  continue;
237  if (expHit->GetCharge() > satThresh)
238  continue;
239 
240  if (fMap->GetPadSize(expHit->GetPadNum()) != 0)
241  continue;
242 
243  // Look for the matching simualted hit and save its charge
244  int padNum = expHit->GetPadNum();
245  auto simHit =
246  std::find_if(simHits.begin(), simHits.end(), [padNum](const AtHit *a) { return a->GetPadNum() == padNum; });
247  if (simHit != simHits.end()) {
248  // This is a good hit so save the experiment charge.
249  exp.push_back(expHit->GetPosition().z());
250  sim.push_back((*simHit)->GetPosition().z());
251  }
252  }
253 }
254 
255 std::set<int>
256 E12014::FillHitSum(std::vector<double> &vec, const std::vector<AtHit *> &hits, int threshold, float satThresh)
257 {
258  vec.clear();
259  vec.resize(512);
260  std::fill_n(vec.begin(), 512, 0);
261  std::set<int> goodPads;
262 
263  if (fMap == nullptr)
264  LOG(fatal) << "The map (E12014::fMap) was never set!";
265 
266  for (auto &hit : hits) {
267  if (fMap->IsInhibited(hit->GetPadNum()) != AtMap::InhibitType::kNone)
268  continue;
269  if (hit->GetCharge() > satThresh)
270  continue;
271 
272  auto func = AtTools::GetHitFunctionTB(*hit);
273  if (func == nullptr)
274  continue;
275 
276  // Add the charge to the array
277  for (int tb = 0; tb < vec.size(); ++tb) {
278  auto val = func->Eval(tb);
279  if (val > threshold)
280  vec[tb] += val;
281  }
282  // Add the pad to the return list
283  goodPads.insert(hit->GetPadNum());
284  }
285 
286  return goodPads;
287 }
288 
289 void E12014::FillSimHitSum(TH1 &hist, const std::vector<AtHit *> &hits, const std::set<int> &goodPads, double amp,
290  int threshold, float satThresh)
291 {
292  std::vector<double> charge;
293  FillSimHitSum(charge, hits, goodPads, amp, threshold, satThresh);
294  ContainerManip::SetHistFromData(hist, charge);
295 }
296 
297 void E12014::FillSimHitSum(std::vector<double> &vec, const std::vector<AtHit *> &hits, const std::set<int> &goodPads,
298  double amp, int threshold, float satThresh)
299 {
300  vec.clear();
301  vec.resize(512);
302  std::fill_n(vec.begin(), 512, 0);
303 
304  for (auto &hit : hits) {
305  if (goodPads.find(hit->GetPadNum()) == goodPads.end()) {
306  LOG(debug) << "Skipping pad " << hit->GetPadNum() << " because not good";
307  continue;
308  }
309  if (hit->GetCharge() > satThresh)
310  continue;
311 
312  auto func = AtTools::GetHitFunctionTB(*hit);
313  if (func == nullptr)
314  continue;
315 
316  // Add the charge to the array
317  LOG(debug) << "Adding pad " << hit->GetPadNum();
318 
319  for (int tb = 0; tb < vec.size(); ++tb) {
320  auto val = func->Eval(tb) * amp;
321  if (val > threshold)
322  vec[tb] += val;
323  }
324  }
325 }
AtMap::InhibitType::kBadPad
@ kBadPad
AtPad.h
CSVRange
Range class for CSVIterator.
Definition: AtCSVReader.h:102
AtRawEvent.h
AtPadReference::cobo
Int_t cobo
Definition: AtPadReference.h:21
AtRawEvent::GetPad
AtPad * GetPad(Int_t padNum)
Definition: AtRawEvent.h:124
AtPadReference.h
E12014::CreateMap
static void CreateMap()
Definition: AtE12014.cxx:29
E12014::FillSimSum
static void FillSimSum(std::vector< double > &sim, const std::vector< AtHit * > &simHits)
Definition: AtE12014.cxx:171
AtRawEvent
Definition: AtRawEvent.h:34
AtE12014.h
E12014::FillZPos
static void FillZPos(std::vector< double > &exp, std::vector< double > &sim, const std::vector< AtHit * > &expHits, const std::vector< AtHit * > &simHits, float satThresh)
Definition: AtE12014.cxx:225
E12014::FillSimHitSum
static void FillSimHitSum(std::vector< double > &vec, const std::vector< AtHit * > &hits, const std::set< int > &goodPads, double amp, int threshold=0, float saturationThreshold=std::numeric_limits< float >::max())
Definition: AtE12014.cxx:297
AtMap::InhibitType::kLowGain
@ kLowGain
AtDataManip.h
E12014::fTBMin
static int fTBMin
Definition: AtE12014.h:25
AtPadArray.h
AtHit.h
AtTools::GetHitFunctionTB
std::unique_ptr< TF1 > GetHitFunctionTB(const AtHit &hit, const AtDigiPar *par=nullptr)
Get charge as a function of TB.
Definition: AtDataManip.cxx:74
E12014::fMap
static std::shared_ptr< AtMap > fMap
Definition: AtE12014.h:24
AtDigiPar
Definition: AtDigiPar.h:14
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
ContainerManip::SetHistFromData
void SetHistFromData(TH1 &hist, const T &data, double xMin=0, double xMax=0)
Use contents of data to set the bin contents of hist.
Definition: AtContainerManip.h:24
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
AtPad::GetADC
const trace & GetADC() const
Definition: AtPad.cxx:97
AtContainerManip.h
AtMap.h
AtPadArray
Holds an addition array of doubles for an AtPad.
Definition: AtPadArray.h:24
E12014::FillHitSums
static int FillHitSums(std::vector< double > &exp, std::vector< double > &sim, const std::vector< AtHit * > &expHits, const std::vector< AtHit * > &simHits, int threshold=0, float saturationThreshold=std::numeric_limits< float >::max(), const AtDigiPar *par=nullptr, std::vector< double > *expADC=nullptr, AtRawEvent *expEvent=nullptr)
Definition: AtE12014.cxx:102
AtPad
Container class for AtPadBase objects.
Definition: AtPad.h:38
AtCSVReader.h
AtPadReference
Definition: AtPadReference.h:20
E12014::FillHits
static void FillHits(std::vector< double > &exp, std::vector< double > &sim, const std::vector< AtHit * > &expHits, const std::vector< AtHit * > &simHits, float satThresh)
Definition: AtE12014.cxx:192
E12014::fThreshold
static int fThreshold
Definition: AtE12014.h:26
E12014::fSatThreshold
static double fSatThreshold
Definition: AtE12014.h:27
AtTpcMap.h
AtHit
Point in space with charge.
Definition: AtHit.h:27