CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalPreshowerRecHitsMaker.cc
Go to the documentation of this file.
16 
17 #include "CLHEP/GenericFunctions/Erf.hh"
18 
20  edm::ParameterSet const & p,
21  const RandomEngine * myrandom)
22  :
23  random_(myrandom),
24  myGaussianTailGenerator_(0)
25 {
26  edm::ParameterSet RecHitsParameters
27  = p.getParameter<edm::ParameterSet>("ECALPreshower");
28  noise_ = RecHitsParameters.getParameter<double>("Noise");
29  threshold_ = RecHitsParameters.getParameter<double>("Threshold");
30  inputCol_=RecHitsParameters.getParameter<edm::InputTag>("MixedSimHits");
31 
32  initialized_=false;
33 
34  Genfun::Erf myErf;
35  if( noise_>0. ) {
36  preshowerHotFraction_ = 0.5-0.5*myErf(threshold_/noise_/sqrt(2.));
37  myGaussianTailGenerator_ = new GaussianTail(random_, noise_, threshold_);
38  } else {
40  }
41 }
42 
44 {
45  initialized_=false;
47 }
48 
50 {
51 
52  ecalsRecHits_.clear();
53 }
54 
55 
57 {
58  // if no preshower, do nothing
59 
60  if(ncells_==0) return;
61  loadPCaloHits(iEvent);
63 
64  std::map<uint32_t,std::pair<float,bool> >::const_iterator
65  it=ecalsRecHits_.begin();
66  std::map<uint32_t,std::pair<float,bool> >::const_iterator
67  itend=ecalsRecHits_.end();
68 
69  for(;it!=itend;++it)
70  {
71  // check if the hit has been killed
72  if(it->second.second) continue;
73  // check if it is above the threshold
74  if(it->second.first<threshold_) continue;
75  ESDetId detid(it->first);
76  // std::cout << detid << " " << it->second.first << std::endl;
77  ecalHits.push_back(EcalRecHit(detid,it->second.first,0.));
78  }
79 }
80 
82 {
83 
84  clean();
85 
87  iEvent.getByLabel(inputCol_,cf);
88  std::auto_ptr<MixCollection<PCaloHit> > colcalo(new MixCollection<PCaloHit>(cf.product(),std::pair<int,int>(0,0) ));
89 
90  MixCollection<PCaloHit>::iterator it=colcalo->begin();
91  MixCollection<PCaloHit>::iterator itend=colcalo->end();
92  for(;it!=itend;++it)
93  {
94  Fill(it->id(),it->energy(),ecalsRecHits_,it.getTrigger());
95  // Fill(it->id(),it->energy(),ecalsRecHits_);
96  }
97 }
98 
100 {
101  if(initialized_) return;
103  edm::LogInfo("CaloRecHitsProducer") << "Total number of cells in Preshower " << ncells_ << std::endl;
104  initialized_=true;
105 }
106 
108 {
110  es.get<CaloGeometryRecord>().get(pG);
111  unsigned total=0;
112 
113  escells_.reserve(137728);
114 
115  const CaloSubdetectorGeometry* geom=pG->getSubdetectorGeometry(DetId::Ecal,EcalPreshower);
116  if(geom==0) return 0;
117  const std::vector<DetId>& ids(geom->getValidDetIds(DetId::Ecal,EcalPreshower));
118  for (std::vector<DetId>::const_iterator i=ids.begin(); i!=ids.end(); i++)
119  {
120  escells_.push_back(i->rawId());
121  ++total;
122  }
123  return escells_.size();
124 }
125 
127 {
128  if(ecalsRecHits_.size()<ncells_)
129  {
130  // Not needed anymore, the noise is added when loading the PCaloHits
131  // noisifySignal(ecalsRecHits_);
133  }
134  else
135  edm::LogWarning("CaloRecHitsProducer") << "All Preshower cells on ! " << std::endl;
136 }
137 
138 
139 void EcalPreshowerRecHitsMaker::noisifySubdet(std::map<uint32_t,std::pair<float,bool> >& theMap, const std::vector<uint32_t>& thecells, unsigned ncells)
140 {
141  // noise won't be injected in cells that contain signal
142  double mean = (double)(ncells-theMap.size())*preshowerHotFraction_;
143  unsigned nps = random_->poissonShoot(mean);
144 
145  unsigned ncell=0;
146  unsigned cellindex=0;
147  uint32_t cellnumber=0;
148  std::map<uint32_t,std::pair<float,bool> >::const_iterator itcheck;
149 
150  while(ncell < nps)
151  {
152  cellindex = (unsigned)(random_->flatShoot()*ncells);
153  cellnumber = thecells[cellindex];
154  itcheck=theMap.find(cellnumber);
155  if(itcheck==theMap.end()) // inject only in empty cells
156  {
157  std::pair <float,bool> noisehit(myGaussianTailGenerator_->shoot(),
158  false);
159  theMap.insert(std::pair<uint32_t,std::pair<float,bool> >
160  (cellnumber,noisehit));
161  ++ncell;
162  }
163  }
164  // edm::LogInfo("CaloRecHitsProducer") << "CaloRecHitsProducer : added noise in "<< ncell << " HCAL cells " << std::endl;
165 }
166 
167 
168 // Takes a hit (from a PSimHit) and fills a map
169 void EcalPreshowerRecHitsMaker::Fill(uint32_t id,float energy, std::map<uint32_t,std::pair<float,bool> >& myHits,bool signal)
170 {
171  // The signal hits are singletons (no need to look into a map)
172  if(signal)
173  {
174  // a new hit is created
175  // we can give a hint for the insert
176  // Add the noise at this point. We are sure that it won't be added several times
177  energy += random_->gaussShoot(0.,noise_);
178  std::pair<float,bool> hit(energy,false);
179  // if it is signal, it is already ordered, so we can give a hint for the
180  // insert
181  if(signal)
182  myHits.insert(myHits.end(),std::pair<uint32_t,std::pair<float,bool> >(id,hit));
183  }
184  else // In this case,there is a risk of duplication. Need to look into the map
185  {
186  std::map<uint32_t,std::pair<float,bool> >::iterator itcheck=myHits.find(id);
187  if(itcheck==myHits.end())
188  {
189  energy += random_->gaussShoot(0.,noise_);
190  std::pair<float,bool> hit(energy,false);
191  myHits.insert(std::pair<uint32_t,std::pair<float,bool> >(id,hit));
192  }
193  else
194  {
195  itcheck->second.first += energy;
196  }
197  }
198 }
199 /*
200 void EcalPreshowerRecHitsMaker::noisifySignal(std::map<uint32_t,std::pair<float,bool> >& theMap)
201 {
202  std::map<uint32_t,std::pair<float,bool> >::iterator it=theMap.begin();
203  std::map<uint32_t,std::pair<float,bool> >::iterator itend=theMap.end();
204  for(;it!=itend;++it)
205  {
206  it->second.first+= random_->gaussShoot(0.,noise_);
207  if(it->second.first < threshold_)
208  {
209  it->second.second=true;
210  it->second.first = 0.;
211  }
212  }
213 }
214 */
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
std::vector< uint32_t > escells_
std::map< uint32_t, std::pair< float, bool > > ecalsRecHits_
void push_back(T const &t)
void noisifySubdet(std::map< uint32_t, std::pair< float, bool > > &theMap, const std::vector< uint32_t > &thecells, unsigned ncells)
double gaussShoot(double mean=0.0, double sigma=1.0) const
Definition: RandomEngine.h:37
virtual const std::vector< DetId > & getValidDetIds(DetId::Detector det=DetId::Detector(0), int subdet=0) const
Get a list of valid detector ids (for the given subdetector)
unsigned createVectorsOfCells(const edm::EventSetup &es)
int iEvent
Definition: GenABIO.cc:243
EcalPreshowerRecHitsMaker(edm::ParameterSet const &p, const RandomEngine *random)
const GaussianTail * myGaussianTailGenerator_
void Fill(uint32_t id, float energy, std::map< uint32_t, std::pair< float, bool > > &myHits, bool signal=true)
T sqrt(T t)
Definition: SSEVec.h:46
unsigned int poissonShoot(double mean) const
Definition: RandomEngine.h:44
void loadPCaloHits(const edm::Event &iEvent)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: Handle.h:74
double flatShoot(double xmin=0.0, double xmax=1.0) const
Definition: RandomEngine.h:30
double shoot() const
Definition: GaussianTail.cc:20
void loadEcalPreshowerRecHits(edm::Event &iEvent, ESRecHitCollection &esRecHits)
void init(const edm::EventSetup &es)