CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AlcaBeamSpotManager.cc
Go to the documentation of this file.
1 
13 #include <vector>
14 #include <math.h>
15 #include <limits.h>
16 
17 using namespace edm;
18 using namespace reco;
19 using namespace std;
20 
21 //--------------------------------------------------------------------------------------------------
23 }
24 
25 //--------------------------------------------------------------------------------------------------
27  beamSpotOutputBase_(iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters").getUntrackedParameter<std::string>("BeamSpotOutputBase")),
28  beamSpotModuleName_(iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters").getUntrackedParameter<std::string>("BeamSpotModuleName")),
29  beamSpotLabel_ (iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters").getUntrackedParameter<std::string>("BeamSpotLabel"))
30 {
31  LogInfo("AlcaBeamSpotManager")
32  << "Output base: " << beamSpotOutputBase_
33  << std::endl;
34  reset();
35 }
36 
37 //--------------------------------------------------------------------------------------------------
39 }
40 
41 //--------------------------------------------------------------------------------------------------
43  beamSpotMap_.clear();
44 }
45 //--------------------------------------------------------------------------------------------------
47 
48  Handle<BeamSpot> beamSpotHandle;
49  iLumi.getByLabel(beamSpotModuleName_,beamSpotLabel_, beamSpotHandle);
50  //iLumi.getByLabel("beamspot","alcaBeamSpot", beamSpotHandle);
51 
52  if(beamSpotHandle.isValid()) { // check the product
53  beamSpotMap_[iLumi.luminosityBlock()] = *beamSpotHandle;
54  const BeamSpot* aBeamSpot = &beamSpotMap_[iLumi.luminosityBlock()];
55  aBeamSpot = beamSpotHandle.product();
56  LogInfo("AlcaBeamSpotManager")
57  << "Lumi: " << iLumi.luminosityBlock() << std::endl;
58  LogInfo("AlcaBeamSpotManager")
59  << *aBeamSpot << std::endl;
60  }
61  else {
62  LogInfo("AlcaBeamSpotManager")
63  << "Lumi: " << iLumi.luminosityBlock() << std::endl;
64  LogInfo("AlcaBeamSpotManager")
65  << " BS is not valid!" << std::endl;
66  }
67 
68 }
69 
70 //--------------------------------------------------------------------------------------------------
72  vector<bsMap_iterator> listToErase;
73  for(bsMap_iterator it=beamSpotMap_.begin(); it!=beamSpotMap_.end();it++){
74  if(it->second.type() != BeamSpot::Tracker){
75  listToErase.push_back(it);
76  }
77  }
78  for(vector<bsMap_iterator>::iterator it=listToErase.begin(); it !=listToErase.end(); it++){
79  beamSpotMap_.erase(*it);
80  }
81  if(beamSpotMap_.size() <= 1){
82  return;
83  }
84  //Return only if lumibased since the collapsing alghorithm requires the next and next to next lumi sections
85  else if(beamSpotMap_.size() == 2 && beamSpotOutputBase_ == "lumibased"){
86  return;
87  }
88  if(beamSpotOutputBase_ == "lumibased"){
89 // bsMap_iterator referenceBS = beamSpotMap_.begin();
90  bsMap_iterator firstBS = beamSpotMap_.begin();
91 // bsMap_iterator lastBS = beamSpotMap_.begin();
92  bsMap_iterator currentBS = beamSpotMap_.begin();
93  bsMap_iterator nextBS = ++beamSpotMap_.begin();
94  bsMap_iterator nextNextBS = ++(++(beamSpotMap_.begin()));
95 
96  map<LuminosityBlockNumber_t,BeamSpot> tmpBeamSpotMap_;
97  bool docreate = true;
98  bool endOfRun = false;//Added
99  bool docheck = true;//Added
100  bool foundShift = false;
101  long countlumi = 0;//Added
102  string tmprun = "";//Added
103  long maxNlumis = 60;//Added
104 // if weighted:
105 // maxNlumis = 999999999
106 
107 
108  unsigned int iteration = 0;
109  //while(nextNextBS!=beamSpotMap_.end()){
110  while(nextBS!=beamSpotMap_.end()){
111  LogInfo("AlcaBeamSpotManager")
112  << "Iteration: " << iteration << " size: " << beamSpotMap_.size() << "\n"
113  << "Lumi: " << currentBS->first << "\n"
114  << currentBS->second
115  << "\n" << nextBS->first << "\n" << nextBS->second
116  << endl;
117  if (nextNextBS!=beamSpotMap_.end())
118  LogInfo("AlcaBeamSpotManager")
119  << nextNextBS->first << "\n" << nextNextBS->second
120  << endl;
121 
122 
123  if(docreate){
124  firstBS = currentBS;
125  docreate = false;//Added
126  }
127  //if(iteration >= beamSpotMap_.size()-3){
128  if(iteration >= beamSpotMap_.size()-2){
129  LogInfo("AlcaBeamSpotManager")
130  << "Reached lumi " << currentBS->first
131  << " now close payload because end of data has been reached.";
132  docreate = true;
133  endOfRun = true;
134  }
135  // check we run over the same run
136 // if (ibeam->first.Run() != inextbeam->first.Run()){
137 // LogInfo("AlcaBeamSpotManager")
138 // << "close payload because end of run.";
139 // docreate = true;
140 // }
141  // check maximum lumi counts
142  if (countlumi == maxNlumis -1){
143  LogInfo("AlcaBeamSpotManager")
144  << "close payload because maximum lumi sections accumulated within run ";
145  docreate = true;
146  countlumi = 0;
147  }
148 // if weighted:
149 // docheck = False
150  // check offsets
151  if(docheck){
152  foundShift = false;
153  LogInfo("AlcaBeamSpotManager")
154  << "Checking checking!" << endl;
155  float limit = 0;
156  pair<float,float> adelta1;
157  pair<float,float> adelta2;
158  pair<float,float> adelta;
159  pair<float,float> adelta1dxdz;
160  pair<float,float> adelta2dxdz;
161  pair<float,float> adelta1dydz;
162  pair<float,float> adelta2dydz;
163  pair<float,float> adelta1widthX;
164  pair<float,float> adelta2widthX;
165  pair<float,float> adelta1widthY;
166  pair<float,float> adelta2widthY;
167  pair<float,float> adelta1z0;
168  pair<float,float> adelta1sigmaZ;
169 
170  // define minimum limit
171  float min_limit = 0.0025;
172 
173  // limit for x and y
174  limit = currentBS->second.BeamWidthX()/2.;
175  if(limit < min_limit){
176  limit = min_limit;
177  }
178 
179  //check movements in X
180  adelta1 = delta(currentBS->second.x0(), currentBS->second.x0Error(), nextBS->second.x0(), nextBS->second.x0Error());
181  adelta2 = pair<float,float>(0.,1.e9);
182  if (nextNextBS->second.type() != -1){
183  adelta2 = delta(nextBS->second.x0(), nextBS->second.x0Error(), nextNextBS->second.x0(), nextNextBS->second.x0Error());
184  }
185  bool deltaX = (deltaSig(adelta1.first,adelta1.second) > 3.5 && adelta1.first >= limit)?true:false;
186  if(iteration < beamSpotMap_.size()-2){
187  if( !deltaX && adelta1.first*adelta2.first > 0. && fabs(adelta1.first+adelta2.first) >= limit){
188  LogInfo("AlcaBeamSpotManager")
189  << " positive, " << (adelta1.first+adelta2.first) << " limit=" << limit << endl;
190  deltaX = true;
191  }
192  else if( deltaX && adelta1.first*adelta2.first < 0 && adelta2.first != 0 && fabs(adelta1.first/adelta2.first) > 0.33 && fabs(adelta1.first/adelta2.first) < 3){
193  LogInfo("AlcaBeamSpotManager")
194  << " negative, " << adelta1.first/adelta2.first << endl;
195  deltaX = false;
196  }
197  }
198 
199  //calculating all deltas
200  adelta1dxdz = delta(currentBS->second.dxdz(), currentBS->second.dxdzError(), nextBS->second.dxdz(), nextBS->second.dxdzError());
201  adelta2dxdz = pair<float,float>(0.,1.e9);
202  adelta1dydz = delta(currentBS->second.dydz(), currentBS->second.dydzError(), nextBS->second.dydz(), nextBS->second.dydzError());
203  adelta2dydz = pair<float,float>(0.,1.e9);
204  adelta1widthX = delta(currentBS->second.BeamWidthX(), currentBS->second.BeamWidthXError(), nextBS->second.BeamWidthX(), nextBS->second.BeamWidthXError());
205  adelta2widthX = pair<float,float>(0.,1.e9);
206  adelta1widthY = delta(currentBS->second.BeamWidthY(), currentBS->second.BeamWidthYError(), nextBS->second.BeamWidthY(), nextBS->second.BeamWidthYError());
207  adelta2widthY = pair<float,float>(0.,1.e9);
208  adelta1z0 = delta(currentBS->second.z0(), currentBS->second.z0Error(), nextBS->second.z0(), nextBS->second.z0Error());
209  adelta1sigmaZ = delta(currentBS->second.sigmaZ(), currentBS->second.sigmaZ0Error(), nextBS->second.sigmaZ(), nextBS->second.sigmaZ0Error());
210 
211  //check movements in Y
212  adelta1 = delta(currentBS->second.y0(), currentBS->second.y0Error(), nextBS->second.y0(), nextBS->second.y0Error());
213  adelta2 = pair<float,float>(0.,1.e9);
214  if( nextNextBS->second.type() != BeamSpot::Unknown){
215  adelta2 = delta(nextBS->second.y0(), nextBS->second.y0Error(), nextNextBS->second.y0(), nextNextBS->second.y0Error());
216  adelta2dxdz = delta(nextBS->second.dxdz(), nextBS->second.dxdzError(), nextNextBS->second.dxdz(), nextNextBS->second.dxdzError());
217  adelta2dydz = delta(nextBS->second.dydz(), nextBS->second.dydzError(), nextNextBS->second.dydz(), nextNextBS->second.dydzError());
218  adelta2widthX = delta(nextBS->second.BeamWidthX(), nextBS->second.BeamWidthXError(), nextNextBS->second.BeamWidthX(), nextNextBS->second.BeamWidthXError());
219  adelta2widthY = delta(nextBS->second.BeamWidthY(), nextBS->second.BeamWidthYError(), nextNextBS->second.BeamWidthY(), nextNextBS->second.BeamWidthYError());
220 
221  }
222  bool deltaY = (deltaSig(adelta1.first,adelta1.second) > 3.5 && adelta1.first >= limit)?true:false;
223  if(iteration < beamSpotMap_.size()-2){
224  if( !deltaY && adelta1.first*adelta2.first > 0. && fabs(adelta1.first+adelta2.first) >= limit){
225  LogInfo("AlcaBeamSpotManager")
226  << " positive, " << (adelta1.first+adelta2.first) << " limit=" << limit << endl;
227  deltaY = true;
228  }
229  else if( deltaY && adelta1.first*adelta2.first < 0 && adelta2.first != 0 && fabs(adelta1.first/adelta2.first) > 0.33 && fabs(adelta1.first/adelta2.first) < 3){
230  LogInfo("AlcaBeamSpotManager")
231  << " negative, " << adelta1.first/adelta2.first << endl;
232  deltaY = false;
233  }
234  }
235 
236  limit = currentBS->second.sigmaZ()/2.;
237  bool deltaZ = (deltaSig(adelta1z0.first,adelta1z0.second) > 3.5 && fabs(adelta1z0.first) >= limit)?true:false;
238  adelta = delta(currentBS->second.sigmaZ(), currentBS->second.sigmaZ0Error(), nextBS->second.sigmaZ(), nextBS->second.sigmaZ0Error());
239  bool deltasigmaZ = (deltaSig(adelta.first,adelta.second) > 5.0)?true:false;
240  bool deltadxdz = false;
241  bool deltadydz = false;
242  bool deltawidthX = false;
243  bool deltawidthY = false;
244 
245  if(iteration < beamSpotMap_.size()-2){
246 
247  adelta = delta(currentBS->second.dxdz(), currentBS->second.dxdzError(), nextBS->second.dxdz(), nextBS->second.dxdzError());
248  deltadxdz = (deltaSig(adelta.first,adelta.second) > 5.0)?true:false;
249  if(deltadxdz && (adelta1dxdz.first*adelta2dxdz.first) < 0 && adelta2dxdz.first != 0 && fabs(adelta1dxdz.first/adelta2dxdz.first) > 0.33 && fabs(adelta1dxdz.first/adelta2dxdz.first) < 3){
250  deltadxdz = false;
251  }
252 
253  adelta = delta(currentBS->second.dydz(), currentBS->second.dydzError(), nextBS->second.dydz(), nextBS->second.dydzError());
254  deltadydz = (deltaSig(adelta.first,adelta.second) > 5.0)?true:false;
255  if(deltadydz && (adelta1dydz.first*adelta2dydz.first) < 0 && adelta2dydz.first != 0 && fabs(adelta1dydz.first/adelta2dydz.first) > 0.33 && fabs(adelta1dydz.first/adelta2dydz.first) < 3){
256  deltadydz = false;
257  }
258 
259  adelta = delta(currentBS->second.BeamWidthX(), currentBS->second.BeamWidthXError(), nextBS->second.BeamWidthX(), nextBS->second.BeamWidthXError());
260  deltawidthX = (deltaSig(adelta.first,adelta.second) > 5.0)?true:false;
261  if(deltawidthX && (adelta1widthX.first*adelta2widthX.first) < 0 && adelta2widthX.first != 0 && fabs(adelta1widthX.first/adelta2widthX.first) > 0.33 && fabs(adelta1widthX.first/adelta2widthX.first) < 3){
262  deltawidthX = false;
263  }
264 
265  adelta = delta(currentBS->second.BeamWidthY(), currentBS->second.BeamWidthYError(), nextBS->second.BeamWidthY(), nextBS->second.BeamWidthYError());
266  deltawidthY = (deltaSig(adelta.first,adelta.second) > 5.0)?true:false;
267  if(deltawidthY && (adelta1widthY.first*adelta2widthY.first) < 0 && adelta2widthY.first != 0 && fabs(adelta1widthY.first/adelta2widthY.first) > 0.33 && fabs(adelta1widthY.first/adelta2widthY.first) < 3){
268  deltawidthY = false;
269  }
270 
271  }
272  if (deltaX || deltaY || deltaZ || deltasigmaZ || deltadxdz || deltadydz || deltawidthX || deltawidthY){
273  docreate = true;
274  foundShift = true;
275  LogInfo("AlcaBeamSpotManager")
276  << "close payload because of movement in"
277  << " X=" << deltaX
278  << ", Y=" << deltaY
279  << ", Z=" << deltaZ
280  << ", sigmaZ=" << deltasigmaZ
281  << ", dxdz=" << deltadxdz
282  << ", dydz=" << deltadydz
283  << ", widthX=" << deltawidthX
284  << ", widthY=" << deltawidthY
285  << endl;
286  }
287 
288  }
289  if(docreate){
290  if(foundShift){
291  tmpBeamSpotMap_[firstBS->first] = weight(firstBS,nextBS);
292  if (endOfRun){
293  //if we're here, then we need to found a shift in the last LS
294  //We already created a new IOV, now create one just for the last LS
295  tmpBeamSpotMap_[nextBS->first] = nextBS->second;
296  }
297  }
298  else if(!foundShift && !endOfRun){ //maxLS reached
299  tmpBeamSpotMap_[firstBS->first] = weight(firstBS,nextBS);
300  }
301  else { // end of run with no shift detectred in last LS
302  tmpBeamSpotMap_[firstBS->first] = weight(firstBS,beamSpotMap_.end());
303  }
304  firstBS = nextBS;
305  countlumi = 0;
306 
307  }
308  //tmprun = currentBS->second.Run
309  ++countlumi;
310 
311  currentBS = nextBS;
312  nextBS = nextNextBS;
313  nextNextBS++;
314  ++iteration;
315  }
316  beamSpotMap_.clear();
317  beamSpotMap_ = tmpBeamSpotMap_;
318  }
319  else if(beamSpotOutputBase_ == "runbased"){
320  BeamSpot aBeamSpot = weight(beamSpotMap_.begin(),beamSpotMap_.end());
321  LuminosityBlockNumber_t firstLumi = beamSpotMap_.begin()->first;
322  beamSpotMap_.clear();
323  beamSpotMap_[firstLumi] = aBeamSpot;
324  }
325  else{
326  LogInfo("AlcaBeamSpotManager")
327  << "Unrecognized BeamSpotOutputBase parameter: " << beamSpotOutputBase_
328  << endl;
329  }
330 }
331 
332 //--------------------------------------------------------------------------------------------------
334  const bsMap_iterator& end){
335  double x,xError = 0;
336  double y,yError = 0;
337  double z,zError = 0;
338  double sigmaZ,sigmaZError = 0;
339  double dxdz,dxdzError = 0;
340  double dydz,dydzError = 0;
341  double widthX,widthXError = 0;
342  double widthY,widthYError = 0;
343  LogInfo("AlcaBeamSpotManager")
344  << "Weighted BeamSpot will span lumi "
345  << begin->first << " to " << end->first
346  << endl;
347 
349  for(bsMap_iterator it=begin; it!=end; it++){
350  weight(x , xError , it->second.x0() , it->second.x0Error());
351  weight(y , yError , it->second.y0() , it->second.y0Error());
352  weight(z , zError , it->second.z0() , it->second.z0Error());
353  weight(sigmaZ, sigmaZError, it->second.sigmaZ() , it->second.sigmaZ0Error());
354  weight(dxdz , dxdzError , it->second.dxdz() , it->second.dxdzError());
355  weight(dydz , dydzError , it->second.dydz() , it->second.dydzError());
356  weight(widthX, widthXError, it->second.BeamWidthX(), it->second.BeamWidthXError());
357  weight(widthY, widthYError, it->second.BeamWidthY(), it->second.BeamWidthYError());
358  if(it->second.type() == BeamSpot::Tracker){
359  type = BeamSpot::Tracker;
360  }
361  }
362  BeamSpot::Point bsPosition(x,y,z);
364  error(0,0) = xError*xError;
365  error(1,1) = yError*yError;
366  error(2,2) = zError*zError;
367  error(3,3) = sigmaZError*sigmaZError;
368  error(4,4) = dxdzError*dxdzError;
369  error(5,5) = dydzError*dydzError;
370  error(6,6) = widthXError*widthXError;
371  BeamSpot weightedBeamSpot(bsPosition,sigmaZ,dxdz,dydz,widthX,error,type);
372  weightedBeamSpot.setBeamWidthY(widthY);
373  LogInfo("AlcaBeamSpotManager")
374  << "Weighted BeamSpot will be:" <<'\n'
375  << weightedBeamSpot
376  << endl;
377  return weightedBeamSpot;
378 }
379 
380 //--------------------------------------------------------------------------------------------------
381 void AlcaBeamSpotManager::weight(double& mean,double& meanError,const double& val,const double& valError){
382  double tmpError = 0;
383  if (meanError < 1e-8){
384  tmpError = 1/(valError*valError);
385  mean = val*tmpError;
386  }
387  else{
388  tmpError = 1/(meanError*meanError) + 1/(valError*valError);
389  mean = mean/(meanError*meanError) + val/(valError*valError);
390  }
391  mean = mean/tmpError;
392  meanError = sqrt(1/tmpError);
393 }
394 
395 //--------------------------------------------------------------------------------------------------
396 pair<float,float> AlcaBeamSpotManager::delta(const float& x, const float& xError, const float& nextX, const float& nextXError){
397  return pair<float,float>(x - nextX, sqrt(pow(xError,2) + pow(nextXError,2)) );
398 }
399 
400 //--------------------------------------------------------------------------------------------------
401 float AlcaBeamSpotManager::deltaSig(const float& num, const float& den){
402  if(den != 0){
403  return fabs(num/den);
404  }
405  else{
406  return float(LONG_MAX);
407  }
408 }
409 
type
Definition: HCALResponse.h:22
math::Error< dimension >::type CovarianceMatrix
Definition: BeamSpot.h:32
BeamType
beam spot flags
Definition: BeamSpot.h:27
std::string beamSpotOutputBase_
std::map< edm::LuminosityBlockNumber_t, reco::BeamSpot >::iterator bsMap_iterator
math::XYZPoint Point
point in the space
Definition: BeamSpot.h:30
bool getByLabel(std::string const &label, Handle< PROD > &result) const
unsigned int LuminosityBlockNumber_t
Definition: EventID.h:31
std::string beamSpotModuleName_
LuminosityBlockNumber_t luminosityBlock() const
tuple iteration
Definition: align_cfg.py:5
void setBeamWidthY(double v)
Definition: BeamSpot.h:100
Definition: DDAxes.h:10
T sqrt(T t)
Definition: SSEVec.h:28
reco::BeamSpot weight(const bsMap_iterator &begin, const bsMap_iterator &end)
virtual ~AlcaBeamSpotManager(void)
#define end
Definition: vmac.h:38
bool isValid() const
Definition: HandleBase.h:76
tuple tmprun
Definition: ntuplemaker.py:242
void readLumi(const edm::LuminosityBlock &)
long long int num
Definition: procUtils.cc:71
T const * product() const
Definition: Handle.h:74
#define begin
Definition: vmac.h:31
std::pair< float, float > delta(const float &x, const float &xError, const float &nextX, const float &nextXError)
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
std::map< edm::LuminosityBlockNumber_t, reco::BeamSpot > beamSpotMap_
float deltaSig(const float &num, const float &den)