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  :
22  myGaussianTailGenerator_(nullptr)
23 {
24  edm::ParameterSet RecHitsParameters
25  = p.getParameter<edm::ParameterSet>("ECALPreshower");
26  noise_ = RecHitsParameters.getParameter<double>("Noise");
27  threshold_ = RecHitsParameters.getParameter<double>("Threshold");
28  inputCol_=RecHitsParameters.getParameter<edm::InputTag>("MixedSimHits");
29 
30  initialized_=false;
31 
32  Genfun::Erf myErf;
33  if( noise_>0. ) {
34  preshowerHotFraction_ = 0.5-0.5*myErf(threshold_/noise_/sqrt(2.));
35  myGaussianTailGenerator_ = new GaussianTail(noise_, threshold_);
36  } else {
38  }
39 }
40 
42 {
43  initialized_=false;
45 }
46 
48 {
49 
50  ecalsRecHits_.clear();
51 }
52 
53 
56 {
57  // if no preshower, do nothing
58 
59  if(ncells_==0) return;
60  loadPCaloHits(iEvent, random);
61  if( myGaussianTailGenerator_ ) noisify(random);
62 
63  std::map<uint32_t,std::pair<float,bool> >::const_iterator
64  it=ecalsRecHits_.begin();
65  std::map<uint32_t,std::pair<float,bool> >::const_iterator
66  itend=ecalsRecHits_.end();
67 
68  for(;it!=itend;++it)
69  {
70  // check if the hit has been killed
71  if(it->second.second) continue;
72  // check if it is above the threshold
73  if(it->second.first<threshold_) continue;
74  ESDetId detid(it->first);
75  // std::cout << detid << " " << it->second.first << std::endl;
76  ecalHits.push_back(EcalRecHit(detid,it->second.first,0.));
77  }
78 }
79 
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_, random, 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,
141 {
142  // noise won't be injected in cells that contain signal
143  double mean = (double)(ncells-theMap.size())*preshowerHotFraction_;
144  unsigned nps = random->poissonShoot(mean);
145 
146  unsigned ncell=0;
147  unsigned cellindex=0;
148  uint32_t cellnumber=0;
149  std::map<uint32_t,std::pair<float,bool> >::const_iterator itcheck;
150 
151  while(ncell < nps)
152  {
153  cellindex = (unsigned)(random->flatShoot()*ncells);
154  cellnumber = thecells[cellindex];
155  itcheck=theMap.find(cellnumber);
156  if(itcheck==theMap.end()) // inject only in empty cells
157  {
158  std::pair <float,bool> noisehit(myGaussianTailGenerator_->shoot(random),
159  false);
160  theMap.insert(std::pair<uint32_t,std::pair<float,bool> >
161  (cellnumber,noisehit));
162  ++ncell;
163  }
164  }
165  // edm::LogInfo("CaloRecHitsProducer") << "CaloRecHitsProducer : added noise in "<< ncell << " HCAL cells " << std::endl;
166 }
167 
168 
169 // Takes a hit (from a PSimHit) and fills a map
170 void EcalPreshowerRecHitsMaker::Fill(uint32_t id,float energy, std::map<uint32_t,std::pair<float,bool> >& myHits,
171  RandomEngineAndDistribution const* random, bool signal)
172 {
173  // The signal hits are singletons (no need to look into a map)
174  if(signal)
175  {
176  // a new hit is created
177  // we can give a hint for the insert
178  // Add the noise at this point. We are sure that it won't be added several times
179  energy += random->gaussShoot(0.,noise_);
180  std::pair<float,bool> hit(energy,false);
181  // if it is signal, it is already ordered, so we can give a hint for the
182  // insert
183  if(signal)
184  myHits.insert(myHits.end(),std::pair<uint32_t,std::pair<float,bool> >(id,hit));
185  }
186  else // In this case,there is a risk of duplication. Need to look into the map
187  {
188  std::map<uint32_t,std::pair<float,bool> >::iterator itcheck=myHits.find(id);
189  if(itcheck==myHits.end())
190  {
191  energy += random->gaussShoot(0.,noise_);
192  std::pair<float,bool> hit(energy,false);
193  myHits.insert(std::pair<uint32_t,std::pair<float,bool> >(id,hit));
194  }
195  else
196  {
197  itcheck->second.first += energy;
198  }
199  }
200 }
201 /*
202 void EcalPreshowerRecHitsMaker::noisifySignal(std::map<uint32_t,std::pair<float,bool> >& theMap,
203  RandomEngineAndDistribution const* random)
204 {
205  std::map<uint32_t,std::pair<float,bool> >::iterator it=theMap.begin();
206  std::map<uint32_t,std::pair<float,bool> >::iterator itend=theMap.end();
207  for(;it!=itend;++it)
208  {
209  it->second.first+= random->gaussShoot(0.,noise_);
210  if(it->second.first < threshold_)
211  {
212  it->second.second=true;
213  it->second.first = 0.;
214  }
215  }
216 }
217 */
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_
double flatShoot(double xmin=0.0, double xmax=1.0) const
void push_back(T const &t)
TRandom random
Definition: MVATrainer.cc:138
#define nullptr
void loadEcalPreshowerRecHits(edm::Event &iEvent, ESRecHitCollection &esRecHits, RandomEngineAndDistribution const *)
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)
double shoot(RandomEngineAndDistribution const *) const
Definition: GaussianTail.cc:18
unsigned createVectorsOfCells(const edm::EventSetup &es)
int iEvent
Definition: GenABIO.cc:230
const GaussianTail * myGaussianTailGenerator_
T sqrt(T t)
Definition: SSEVec.h:48
void loadPCaloHits(const edm::Event &iEvent, RandomEngineAndDistribution const *)
void noisifySubdet(std::map< uint32_t, std::pair< float, bool > > &theMap, const std::vector< uint32_t > &thecells, unsigned ncells, RandomEngineAndDistribution const *)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:405
void Fill(uint32_t id, float energy, std::map< uint32_t, std::pair< float, bool > > &myHits, RandomEngineAndDistribution const *, bool signal=true)
T const * product() const
Definition: Handle.h:81
void noisify(RandomEngineAndDistribution const *)
const T & get() const
Definition: EventSetup.h:55
void init(const edm::EventSetup &es)
double gaussShoot(double mean=0.0, double sigma=1.0) const
unsigned int poissonShoot(double mean) const
EcalPreshowerRecHitsMaker(edm::ParameterSet const &p)