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