Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4MicromegasDigitizer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4MicromegasDigitizer.cc
1 
7 
8 // Move to new storage containers
9 #include <trackbase/TrkrDefs.h>
10 #include <trackbase/TrkrHit.h>
11 #include <trackbase/TrkrHitSet.h>
14 
15 #include <phparameter/PHParameterInterface.h> // for PHParameterInterface
16 
18 #include <fun4all/SubsysReco.h>
19 
20 #include <phool/PHRandomSeed.h>
21 #include <phool/getClass.h>
22 
23 #include <gsl/gsl_randist.h>
24 #include <gsl/gsl_rng.h> // for gsl_rng_alloc, gsl_rng...
25 
26 #include <cassert>
27 #include <iostream> // for operator<<, basic_ostream
28 #include <set>
29 #include <utility> // for pair
30 
31 namespace
32 {
33 
34  // local version of std::clamp, which is only available for c++17
35  template <class T>
36  constexpr const T& clamp(const T& v, const T& lo, const T& hi)
37  {
38  return (v < lo) ? lo : (hi < v) ? hi
39  : v;
40  }
41 
42 } // namespace
43 //____________________________________________________________________________
45  : SubsysReco(name)
46  , PHParameterInterface(name)
47 {
48  // initialize rng
49  const unsigned int seed = PHRandomSeed();
50  m_rng.reset(gsl_rng_alloc(gsl_rng_mt19937));
51  gsl_rng_set(m_rng.get(), seed);
52 
54 }
55 
56 //____________________________________________________________________________
58 {
60 
61  // load parameters
62  m_adc_threshold = get_double_param("micromegas_adc_threshold");
63  m_enc = get_double_param("micromegas_enc");
64  m_pedestal = get_double_param("micromegas_pedestal");
65  m_volts_per_charge = get_double_param("micromegas_volts_per_charge");
66 
67  // printout
68  std::cout
69  << "PHG4MicromegasDigitizer::InitRun\n"
70  << " m_adc_threshold: " << m_adc_threshold << " electrons\n"
71  << " m_enc: " << m_enc << " electrons\n"
72  << " m_pedestal: " << m_pedestal << " electrons\n"
73  << " m_volts_per_charge: " << m_volts_per_charge << " mV/fC\n"
74  << std::endl;
75 
76  // threshold is effectively applied on top of pedestal
78 
79  /*
80  * Factor that convertes charge in a voltage in each z bin
81  * the scale up factor of 2.4 is meant to account for shaping time (80ns)
82  * it only applies to the signal
83  * see: simulations/g4simulations/g4tpc/PHG4TpcDigitizer::InitRun
84  */
87 
89 }
90 
91 //____________________________________________________________________________
93 {
94  // Get Nodes
95  // Get the TrkrHitSetContainer node
96  auto trkrhitsetcontainer = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
97  assert(trkrhitsetcontainer);
98 
99  // Get the TrkrHitTruthAssoc node
100  auto hittruthassoc = findNode::getClass<TrkrHitTruthAssoc>(topNode, "TRKR_HITTRUTHASSOC");
101 
102  // get all micromegas hitsets
103  const auto hitset_range = trkrhitsetcontainer->getHitSets(TrkrDefs::TrkrId::micromegasId);
104  for (auto hitset_it = hitset_range.first; hitset_it != hitset_range.second; ++hitset_it)
105  {
106  // get key
107  const TrkrDefs::hitsetkey hitsetkey = hitset_it->first;
108 
109  // get all of the hits from this hitset
110  TrkrHitSet* hitset = hitset_it->second;
111  TrkrHitSet::ConstRange hit_range = hitset->getHits();
112 
113  // keep track of hits to be removed
114  std::set<TrkrDefs::hitkey> removed_keys;
115 
116  // loop over hits
117  for (auto hit_it = hit_range.first; hit_it != hit_range.second; ++hit_it)
118  {
119  // store key and hit
120  const TrkrDefs::hitkey& key = hit_it->first;
121  TrkrHit* hit = hit_it->second;
122 
123  // get energy (electrons)
124  const double signal = hit->getEnergy();
125  const double noise = add_noise();
126 
127  // convert to mV
128  const double voltage = (m_pedestal + noise) * m_volt_per_electron_noise + signal * m_volt_per_electron_signal;
129 
130  // compare to threshold
132  {
133  // keep hit, update adc
134  hit->setAdc(clamp<uint>(voltage * m_adc_per_volt, 0, 1023));
135  }
136  else
137  {
138  // mark hit as removable
139  removed_keys.insert(key);
140  }
141  }
142 
143  // remove hits
144  for (const auto& key : removed_keys)
145  {
146  hitset->removeHit(key);
147  if (hittruthassoc)
148  {
149  hittruthassoc->removeAssoc(hitsetkey, key);
150  }
151  }
152  }
154 }
155 
156 //___________________________________________________________________________
158 {
159  // all values taken from TPC sampa chips (simulations/g4simulations/g4tpc/PHG4TpcDigitizer)
160  set_default_double_param("micromegas_enc", 670);
161  set_default_double_param("micromegas_adc_threshold", 2680);
162  set_default_double_param("micromegas_pedestal", 50000);
163  set_default_double_param("micromegas_volts_per_charge", 20);
164 }
165 
166 //___________________________________________________________________________
168 {
169  return gsl_ran_gaussian(m_rng.get(), m_enc);
170 }