CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HBHETimingShapedFlag.cc
Go to the documentation of this file.
1 #include <iostream>
4 
5 /*
6 v1.0 by Jeff Temple
7 29 August 2009
8 
9 This takes the timing envelope algorithms developed by
10 Phil Dudero and uses them to apply a RecHit Flag.
11 */
12 
14 {
15 }
16 
17 HBHETimingShapedFlagSetter::HBHETimingShapedFlagSetter(const std::vector<double>& v_userEnvelope)
18 {
19  makeTfilterEnvelope(v_userEnvelope);
20 
21  ignorelowest_=false;
22  ignorehighest_=false;
23  win_offset_=0.;
24  win_gain_=1.;
25 }
26 
27 HBHETimingShapedFlagSetter::HBHETimingShapedFlagSetter(const std::vector<double>& v_userEnvelope,
28  bool ignorelowest, bool ignorehighest,
29  double win_offset, double win_gain)
30 {
31  makeTfilterEnvelope(v_userEnvelope);
32 
33  ignorelowest_ = ignorelowest; // can ignore flagging hits below lowest energy threshold
34  ignorehighest_ = ignorehighest; // can ignore flagging hits above highest energy threshold
35  win_offset_ = win_offset; // timing offset
36  win_gain_ = win_gain; // time gain
37 }
38 
39 void
40 HBHETimingShapedFlagSetter::makeTfilterEnvelope(const std::vector<double>& v_userEnvelope)
41 {
42  // Transform vector of doubles into a map of <energy,lowtime,hitime> triplets
43  // Add extra protection in case vector of doubles is not a multiple of 3
44  if (v_userEnvelope.size()%3)
45  throw cms::Exception("Invalid tfilterEnvelope definition") <<
46  "Must be one energy and two times per point";
47 
48  for (unsigned int i=0;i<v_userEnvelope.size();i+=3) {
49  int intGeV = (int)(v_userEnvelope[i]+0.5);
50  std::pair<double,double> pairOfTimes = std::make_pair(v_userEnvelope[i+1],
51  v_userEnvelope[i+2]);
52  if ((pairOfTimes.first > 0) ||
53  (pairOfTimes.second < 0) )
54  throw cms::Exception("Invalid tfilterEnvelope definition") <<
55  "Min and max time values must straddle t=0; use win_offset to shift";
56 
57  tfilterEnvelope_[intGeV] = pairOfTimes;
58  }
59 }
60 
61 void
63 {
64  std::cout <<"Timing Energy/Time parameters are:"<<std::endl;
65  TfilterEnvelope_t::const_iterator it;
66  for (it=tfilterEnvelope_.begin();it!=tfilterEnvelope_.end();++it)
67  std::cout <<"\t"<<it->first<<" GeV\t"<<it->second.first<<" ns\t"<<it->second.second<<" ns"<<std::endl;
68 
69  std::cout <<"ignorelowest = "<<ignorelowest_<<std::endl;
70  std::cout <<"ignorehighest = "<<ignorehighest_<<std::endl;
71  std::cout <<"win_offset = "<<win_offset_<<std::endl;
72  std::cout <<"win_gain = "<<win_gain_<<std::endl;
73 }
74 
75 int
77 {
78  int status=0; // 3 bits reserved;status can range from 0-7
79 
80  // tfilterEnvelope stores triples of energy and high/low time limits;
81  // make sure we're checking over an even number of values
82  // energies are guaranteed by std::map to appear in increasing order
83 
84  // need at least two values to make comparison, and must
85  // always have energy, time pair; otherwise, assume "in time" and don't set bits
86  if (tfilterEnvelope_.size()==0)
87  return 0;
88 
89  double twinmin, twinmax; // min, max 'good' time; values outside this range have flag set
90  double rhtime=hbhe.time();
91  double energy=hbhe.energy();
92 
93  if (energy< (double)tfilterEnvelope_.begin()->first) // less than lowest energy threshold
94  {
95  // Can skip timing test on cells below lowest threshold if so desired
96  if (ignorelowest_)
97  return 0;
98  else {
99  twinmin=tfilterEnvelope_.begin()->second.first;
100  twinmax=tfilterEnvelope_.begin()->second.second;
101  }
102  }
103  else
104  {
105  // Loop over energies in tfilterEnvelope
106  TfilterEnvelope_t::const_iterator it;
107  for (it=tfilterEnvelope_.begin();it!=tfilterEnvelope_.end();++it)
108  {
109  // Identify tfilterEnvelope index for this rechit energy
110  if (energy < (double)it->first)
111  break;
112  }
113 
114  if (it==tfilterEnvelope_.end())
115  {
116  // Skip timing test on cells above highest threshold if so desired
117  if (ignorehighest_)
118  return 0;
119  else {
120  twinmin=tfilterEnvelope_.rbegin()->second.first;
121  twinmax=tfilterEnvelope_.rbegin()->second.second;
122  }
123  }
124  else
125  {
126  // Perform linear interpolation between energy boundaries
127 
128  std::map<int,std::pair<double,double> >::const_iterator prev = it; prev--;
129 
130  // twinmax interpolation
131  double energy1 = prev->first;
132  double lim1 = prev->second.second;
133  double energy2 = it->first;
134  double lim2 = it->second.second;
135 
136  twinmax=lim1+((lim2-lim1)*(energy-energy1)/(energy2-energy1));
137 
138  // twinmin interpolation
139  lim1 = prev->second.first;
140  lim2 = it->second.first;
141 
142  twinmin=lim1+((lim2-lim1)*(energy-energy1)/(energy2-energy1));
143  }
144  }
145 
146  // Apply offset, gain
147  twinmin=win_offset_+twinmin*win_gain_;
148  twinmax=win_offset_+twinmax*win_gain_;
149 
150  // Set status high if time outside expected range
151  if (rhtime<=twinmin || rhtime >= twinmax)
152  status=1; // set status to 1
153 
154  return status;
155 }
156 
158 {
159  int status = timingStatus(hbhe);
160 
161  // Though we're only using a single bit right now, 3 bits are reserved for these cuts
163 
164  return;
165 }
int i
Definition: DBlmapReader.cc:9
void setFlagField(uint32_t value, int base, int width=1)
Definition: CaloRecHit.cc:20
float time() const
Definition: CaloRecHit.h:21
float energy() const
Definition: CaloRecHit.h:19
int timingStatus(const HBHERecHit &hbhe)
TfilterEnvelope_t tfilterEnvelope_
void SetTimingShapedFlags(HBHERecHit &hbhe)
void makeTfilterEnvelope(const std::vector< double > &v_userEnvelope)
tuple cout
Definition: gather_cfg.py:121
tuple status
Definition: ntuplemaker.py:245