CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Vx3DHLTAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Vx3DHLTAnalyzer
4 // Class: Vx3DHLTAnalyzer
5 //
11 //
12 // Original Author: Mauro Dinardo, 28 S-012, +41-22-767-8302,
13 // Created: Tue Feb 23 13:15:31 CET 2010
14 
15 
17 
20 
21 #include <Math/Minimizer.h>
22 #include <Math/Factory.h>
23 #include <Math/Functor.h>
24 
25 
27 {
28  debugMode = true;
29  nLumiReset = 1; // Number of integrated lumis to perform the fit
30  dataFromFit = true; // The Beam Spot data can be either taken from the histograms or from the fit results
31  minNentries = 35; // Minimum number of good vertices to perform the fit
32  xRange = 2.; // [cm]
33  xStep = 0.001; // [cm]
34  yRange = 2.; // [cm]
35  yStep = 0.001; // [cm]
36  zRange = 30.; // [cm]
37  zStep = 0.05; // [cm]
38  VxErrCorr = 1.5;
39  fileName = "BeamPixelResults.txt";
40 
41  vertexCollection = consumes<VertexCollection> (iConfig.getUntrackedParameter<InputTag>("vertexCollection", InputTag("pixelVertices")));
42  pixelHitCollection = consumes<SiPixelRecHitCollection>(iConfig.getUntrackedParameter<InputTag>("pixelHitCollection", InputTag("siPixelRecHits")));
43 
44  debugMode = iConfig.getParameter<bool>("debugMode");
45  nLumiReset = iConfig.getParameter<unsigned int>("nLumiReset");
46  dataFromFit = iConfig.getParameter<bool>("dataFromFit");
47  minNentries = iConfig.getParameter<unsigned int>("minNentries");
48  xRange = iConfig.getParameter<double>("xRange");
49  xStep = iConfig.getParameter<double>("xStep");
50  yRange = iConfig.getParameter<double>("yRange");
51  yStep = iConfig.getParameter<double>("yStep");
52  zRange = iConfig.getParameter<double>("zRange");
53  zStep = iConfig.getParameter<double>("zStep");
54  VxErrCorr = iConfig.getParameter<double>("VxErrCorr");
55  fileName = iConfig.getParameter<string>("fileName");
56 }
57 
58 
60 {
61 }
62 
63 
64 void Vx3DHLTAnalyzer::analyze (const Event& iEvent, const EventSetup& iSetup)
65 {
66  Handle<VertexCollection> Vx3DCollection;
67  iEvent.getByToken(vertexCollection, Vx3DCollection);
68 
69  unsigned int i,j;
70  double det;
71  VertexType MyVertex;
72 
73  if (runNumber != iEvent.id().run())
74  {
75  reset("scratch");
76  runNumber = iEvent.id().run();
77 
78  if (debugMode == true)
79  {
80  stringstream debugFile;
81  string tmp(fileName);
82 
83  if (outputDebugFile.is_open() == true) outputDebugFile.close();
84  tmp.erase(strlen(fileName.c_str())-4,4);
85  debugFile << tmp.c_str() << "_Run" << iEvent.id().run() << ".txt";
86  outputDebugFile.open(debugFile.str().c_str(), ios::out);
87  outputDebugFile.close();
88  outputDebugFile.open(debugFile.str().c_str(), ios::app);
89  }
90 
92  }
93  else if (beginTimeOfFit != 0)
94  {
95  totalHits += HitCounter(iEvent);
96 
97  for (vector<Vertex>::const_iterator it3DVx = Vx3DCollection->begin(); it3DVx != Vx3DCollection->end(); it3DVx++)
98  {
99  if ((it3DVx->isValid() == true) &&
100  (it3DVx->isFake() == false) &&
101  (it3DVx->ndof() >= minVxDoF))
102  {
103  for (i = 0; i < DIM; i++)
104  {
105  for (j = 0; j < DIM; j++)
106  {
107  MyVertex.Covariance[i][j] = it3DVx->covariance(i,j);
108  if (isNotFinite(MyVertex.Covariance[i][j]) == true) break;
109  }
110  if (j != DIM) break;
111  }
112  det = std::fabs(MyVertex.Covariance[0][0])*(std::fabs(MyVertex.Covariance[1][1])*std::fabs(MyVertex.Covariance[2][2]) - MyVertex.Covariance[1][2]*MyVertex.Covariance[1][2]) -
113  MyVertex.Covariance[0][1]*(MyVertex.Covariance[0][1]*std::fabs(MyVertex.Covariance[2][2]) - MyVertex.Covariance[0][2]*MyVertex.Covariance[1][2]) +
114  MyVertex.Covariance[0][2]*(MyVertex.Covariance[0][1]*MyVertex.Covariance[1][2] - MyVertex.Covariance[0][2]*std::fabs(MyVertex.Covariance[1][1]));
115  if ((i == DIM) && (det > 0.))
116  {
117  MyVertex.x = it3DVx->x();
118  MyVertex.y = it3DVx->y();
119  MyVertex.z = it3DVx->z();
120  Vertices.push_back(MyVertex);
121  }
122  else if (internalDebug == true)
123  {
124  cout << "Vertex discarded !" << endl;
125  for (i = 0; i < DIM; i++)
126  for (j = 0; j < DIM; j++)
127  cout << "(i,j) --> " << i << "," << j << " --> " << MyVertex.Covariance[i][j] << endl;
128  }
129 
130  Vx_X->Fill(it3DVx->x());
131  Vx_Y->Fill(it3DVx->y());
132  Vx_Z->Fill(it3DVx->z());
133 
134  Vx_ZX->Fill(it3DVx->z(), it3DVx->x());
135  Vx_ZY->Fill(it3DVx->z(), it3DVx->y());
136  Vx_XY->Fill(it3DVx->x(), it3DVx->y());
137  }
138  }
139  }
140 }
141 
142 
144 {
145  Handle<SiPixelRecHitCollection> rechitspixel;
146  iEvent.getByToken(pixelHitCollection, rechitspixel);
147 
148  unsigned int counter = 0;
149 
150  for (SiPixelRecHitCollection::const_iterator j = rechitspixel->begin(); j != rechitspixel->end(); j++)
151  for (edmNew::DetSet<SiPixelRecHit>::const_iterator h = j->begin(); h != j->end(); h++) counter += h->cluster()->size();
152 
153  return counter;
154 }
155 
156 
157 string Vx3DHLTAnalyzer::formatTime (const time_t& t)
158 {
159  char ts[25];
160  strftime(ts, sizeof(ts), "%Y.%m.%d %H:%M:%S %Z", gmtime(&t));
161 
162  string ts_string(ts);
163 
164  return ts_string;
165 }
166 
167 
168 double Gauss3DFunc(const double* par)
169 {
170  double K[DIM][DIM]; // Covariance Matrix
171  double M[DIM][DIM]; // K^-1
172  double det;
173  double sumlog = 0.;
174 
175 // par[0] = K(0,0) --> Var[X]
176 // par[1] = K(1,1) --> Var[Y]
177 // par[2] = K(2,2) --> Var[Z]
178 // par[3] = K(0,1) = K(1,0) --> Cov[X,Y]
179 // par[4] = K(1,2) = K(2,1) --> Cov[Y,Z] --> dy/dz
180 // par[5] = K(0,2) = K(2,0) --> Cov[X,Z] --> dx/dz
181 // par[6] = mean x
182 // par[7] = mean y
183 // par[8] = mean z
184 
185  counterVx = 0;
186  for (unsigned int i = 0; i < Vertices.size(); i++)
187  {
189  (std::fabs(Vertices[i].z-zPos) <= maxLongLength))
190  {
191  if (considerVxCovariance == true)
192  {
193  K[0][0] = std::fabs(par[0]) + VxErrCorr*VxErrCorr * std::fabs(Vertices[i].Covariance[0][0]);
194  K[1][1] = std::fabs(par[1]) + VxErrCorr*VxErrCorr * std::fabs(Vertices[i].Covariance[1][1]);
195  K[2][2] = std::fabs(par[2]) + VxErrCorr*VxErrCorr * std::fabs(Vertices[i].Covariance[2][2]);
196  K[0][1] = K[1][0] = par[3] + VxErrCorr*VxErrCorr * Vertices[i].Covariance[0][1];
197  K[1][2] = K[2][1] = par[4]*(std::fabs(par[2])-std::fabs(par[1])) - par[5]*par[3] + VxErrCorr*VxErrCorr * Vertices[i].Covariance[1][2];
198  K[0][2] = K[2][0] = par[5]*(std::fabs(par[2])-std::fabs(par[0])) - par[4]*par[3] + VxErrCorr*VxErrCorr * Vertices[i].Covariance[0][2];
199  }
200  else
201  {
202  K[0][0] = std::fabs(par[0]);
203  K[1][1] = std::fabs(par[1]);
204  K[2][2] = std::fabs(par[2]);
205  K[0][1] = K[1][0] = par[3];
206  K[1][2] = K[2][1] = par[4]*(std::fabs(par[2])-std::fabs(par[1])) - par[5]*par[3];
207  K[0][2] = K[2][0] = par[5]*(std::fabs(par[2])-std::fabs(par[0])) - par[4]*par[3];
208  }
209 
210  det = K[0][0]*(K[1][1]*K[2][2] - K[1][2]*K[1][2]) -
211  K[0][1]*(K[0][1]*K[2][2] - K[0][2]*K[1][2]) +
212  K[0][2]*(K[0][1]*K[1][2] - K[0][2]*K[1][1]);
213 
214  M[0][0] = (K[1][1]*K[2][2] - K[1][2]*K[1][2]) / det;
215  M[1][1] = (K[0][0]*K[2][2] - K[0][2]*K[0][2]) / det;
216  M[2][2] = (K[0][0]*K[1][1] - K[0][1]*K[0][1]) / det;
217  M[0][1] = M[1][0] = (K[0][2]*K[1][2] - K[0][1]*K[2][2]) / det;
218  M[1][2] = M[2][1] = (K[0][2]*K[0][1] - K[1][2]*K[0][0]) / det;
219  M[0][2] = M[2][0] = (K[0][1]*K[1][2] - K[0][2]*K[1][1]) / det;
220 
221  sumlog += double(DIM)*std::log(2.*pi) + std::log(std::fabs(det)) +
222  (M[0][0]*(Vertices[i].x-par[6])*(Vertices[i].x-par[6]) +
223  M[1][1]*(Vertices[i].y-par[7])*(Vertices[i].y-par[7]) +
224  M[2][2]*(Vertices[i].z-par[8])*(Vertices[i].z-par[8]) +
225  2.*M[0][1]*(Vertices[i].x-par[6])*(Vertices[i].y-par[7]) +
226  2.*M[1][2]*(Vertices[i].y-par[7])*(Vertices[i].z-par[8]) +
227  2.*M[0][2]*(Vertices[i].x-par[6])*(Vertices[i].z-par[8]));
228 
229  counterVx++;
230  }
231  }
232 
233  return sumlog;
234 }
235 
236 
237 int Vx3DHLTAnalyzer::MyFit (vector<double>* vals)
238 {
239  // RETURN CODE:
240  // 0 == OK
241  // -2 == NO OK - not enough "minNentries"
242  // Any other number == NO OK
243  unsigned int nParams = 9;
244 
245  if ((vals != NULL) && (vals->size() == nParams*2))
246  {
247  double nSigmaXY = 100.;
248  double nSigmaZ = 100.;
249  double varFactor = 4./25.; // Take into account the difference between the RMS and sigma (RMS usually greater than sigma)
250  double parDistanceXY = 0.005; // Unit: [cm]
251  double parDistanceZ = 0.5; // Unit: [cm]
252  double parDistanceddZ = 1e-3; // Unit: [rad]
253  double parDistanceCxy = 1e-5; // Unit: [cm^2]
254  double bestEdm = 1e-1;
255 
256  const unsigned int trials = 4;
257  double largerDist[trials] = {0.1, 5., 10., 100.};
258 
259  double covxz,covyz,det;
260  double deltaMean;
261  int bestMovementX = 1;
262  int bestMovementY = 1;
263  int bestMovementZ = 1;
264  int goodData;
265 
266  double edm;
267 
268  vector<double>::const_iterator it = vals->begin();
269 
270  ROOT::Math::Minimizer* Gauss3D = ROOT::Math::Factory::CreateMinimizer("Minuit2","Migrad");
271  Gauss3D->SetMaxFunctionCalls(1e4);
272  Gauss3D->SetTolerance(1e-9); // Tolerance on likelihood
273  if (internalDebug == true) Gauss3D->SetPrintLevel(3);
274  else Gauss3D->SetPrintLevel(0);
275 
276  ROOT::Math::Functor _Gauss3DFunc(&Gauss3DFunc,nParams);
277  Gauss3D->SetFunction(_Gauss3DFunc);
278 
279  if (internalDebug == true) cout << "\n@@@ START FITTING @@@" << endl;
280 
281  // @@@ Fit at X-deltaMean | X | X+deltaMean @@@
282  bestEdm = 1.;
283  for (int i = 0; i < 3; i++)
284  {
285  deltaMean = (double(i)-1.)*std::sqrt((*(it+0))*varFactor);
286  if (internalDebug == true) cout << "deltaMean --> " << deltaMean << endl;
287 
288  Gauss3D->Clear();
289 
290  Gauss3D->SetVariable(0,"var x ", *(it+0)*varFactor, parDistanceXY*parDistanceXY);
291  Gauss3D->SetVariable(1,"var y ", *(it+1)*varFactor, parDistanceXY*parDistanceXY);
292  Gauss3D->SetVariable(2,"var z ", *(it+2), parDistanceZ*parDistanceZ);
293  Gauss3D->SetVariable(3,"cov xy", *(it+3), parDistanceCxy);
294  Gauss3D->SetVariable(4,"dydz ", *(it+4), parDistanceddZ);
295  Gauss3D->SetVariable(5,"dxdz ", *(it+5), parDistanceddZ);
296  Gauss3D->SetVariable(6,"mean x", *(it+6)+deltaMean, parDistanceXY);
297  Gauss3D->SetVariable(7,"mean y", *(it+7), parDistanceXY);
298  Gauss3D->SetVariable(8,"mean z", *(it+8), parDistanceZ);
299 
300  // Set the central positions of the centroid for vertex rejection
301  xPos = Gauss3D->X()[6];
302  yPos = Gauss3D->X()[7];
303  zPos = Gauss3D->X()[8];
304 
305  // Set dimensions of the centroid for vertex rejection
306  maxTransRadius = nSigmaXY * std::sqrt(std::fabs(Gauss3D->X()[0]) + std::fabs(Gauss3D->X()[1])) / 2.;
307  maxLongLength = nSigmaZ * std::sqrt(std::fabs(Gauss3D->X()[2]));
308 
309  Gauss3D->Minimize();
310  goodData = Gauss3D->Status();
311  edm = Gauss3D->Edm();
312 
313  if (counterVx < minNentries) goodData = -2;
314  else if (isNotFinite(edm) == true) goodData = -1;
315  else for (unsigned int j = 0; j < nParams; j++) if (isNotFinite(Gauss3D->Errors()[j]) == true) { goodData = -1; break; }
316  if (goodData == 0)
317  {
318  covyz = Gauss3D->X()[4]*(std::fabs(Gauss3D->X()[2])-std::fabs(Gauss3D->X()[1])) - Gauss3D->X()[5]*Gauss3D->X()[3];
319  covxz = Gauss3D->X()[5]*(std::fabs(Gauss3D->X()[2])-std::fabs(Gauss3D->X()[0])) - Gauss3D->X()[4]*Gauss3D->X()[3];
320 
321  det = std::fabs(Gauss3D->X()[0]) * (std::fabs(Gauss3D->X()[1])*std::fabs(Gauss3D->X()[2]) - covyz*covyz) -
322  Gauss3D->X()[3] * (Gauss3D->X()[3]*std::fabs(Gauss3D->X()[2]) - covxz*covyz) +
323  covxz * (Gauss3D->X()[3]*covyz - covxz*std::fabs(Gauss3D->X()[1]));
324  if (det < 0.) { goodData = -1; if (internalDebug == true) cout << "Negative determinant !" << endl; }
325  }
326 
327  if ((goodData == 0) && (std::fabs(edm) < bestEdm)) { bestEdm = edm; bestMovementX = i; }
328  }
329  if (internalDebug == true) cout << "Found bestMovementX --> " << bestMovementX << endl;
330 
331  // @@@ Fit at Y-deltaMean | Y | Y+deltaMean @@@
332  bestEdm = 1.;
333  for (int i = 0; i < 3; i++)
334  {
335  deltaMean = (double(i)-1.)*std::sqrt((*(it+1))*varFactor);
336  if (internalDebug == true)
337  {
338  cout << "deltaMean --> " << deltaMean << endl;
339  cout << "deltaMean X --> " << (double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor) << endl;
340  }
341 
342  Gauss3D->Clear();
343 
344  Gauss3D->SetVariable(0,"var x ", *(it+0)*varFactor, parDistanceXY*parDistanceXY);
345  Gauss3D->SetVariable(1,"var y ", *(it+1)*varFactor, parDistanceXY*parDistanceXY);
346  Gauss3D->SetVariable(2,"var z ", *(it+2), parDistanceZ*parDistanceZ);
347  Gauss3D->SetVariable(3,"cov xy", *(it+3), parDistanceCxy);
348  Gauss3D->SetVariable(4,"dydz ", *(it+4), parDistanceddZ);
349  Gauss3D->SetVariable(5,"dxdz ", *(it+5), parDistanceddZ);
350  Gauss3D->SetVariable(6,"mean x", *(it+6)+(double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor), parDistanceXY);
351  Gauss3D->SetVariable(7,"mean y", *(it+7)+deltaMean, parDistanceXY);
352  Gauss3D->SetVariable(8,"mean z", *(it+8), parDistanceZ);
353 
354  // Set the central positions of the centroid for vertex rejection
355  xPos = Gauss3D->X()[6];
356  yPos = Gauss3D->X()[7];
357  zPos = Gauss3D->X()[8];
358 
359  // Set dimensions of the centroid for vertex rejection
360  maxTransRadius = nSigmaXY * std::sqrt(std::fabs(Gauss3D->X()[0]) + std::fabs(Gauss3D->X()[1])) / 2.;
361  maxLongLength = nSigmaZ * std::sqrt(std::fabs(Gauss3D->X()[2]));
362 
363  Gauss3D->Minimize();
364  goodData = Gauss3D->Status();
365  edm = Gauss3D->Edm();
366 
367  if (counterVx < minNentries) goodData = -2;
368  else if (isNotFinite(edm) == true) goodData = -1;
369  else for (unsigned int j = 0; j < nParams; j++) if (isNotFinite(Gauss3D->Errors()[j]) == true) { goodData = -1; break; }
370  if (goodData == 0)
371  {
372  covyz = Gauss3D->X()[4]*(std::fabs(Gauss3D->X()[2])-std::fabs(Gauss3D->X()[1])) - Gauss3D->X()[5]*Gauss3D->X()[3];
373  covxz = Gauss3D->X()[5]*(std::fabs(Gauss3D->X()[2])-std::fabs(Gauss3D->X()[0])) - Gauss3D->X()[4]*Gauss3D->X()[3];
374 
375  det = std::fabs(Gauss3D->X()[0]) * (std::fabs(Gauss3D->X()[1])*std::fabs(Gauss3D->X()[2]) - covyz*covyz) -
376  Gauss3D->X()[3] * (Gauss3D->X()[3]*std::fabs(Gauss3D->X()[2]) - covxz*covyz) +
377  covxz * (Gauss3D->X()[3]*covyz - covxz*std::fabs(Gauss3D->X()[1]));
378  if (det < 0.) { goodData = -1; if (internalDebug == true) cout << "Negative determinant !" << endl; }
379  }
380 
381  if ((goodData == 0) && (std::fabs(edm) < bestEdm)) { bestEdm = edm; bestMovementY = i; }
382  }
383  if (internalDebug == true) cout << "Found bestMovementY --> " << bestMovementY << endl;
384 
385  // @@@ Fit at Z-deltaMean | Z | Z+deltaMean @@@
386  bestEdm = 1.;
387  for (int i = 0; i < 3; i++)
388  {
389  deltaMean = (double(i)-1.)*std::sqrt(*(it+2));
390  if (internalDebug == true)
391  {
392  cout << "deltaMean --> " << deltaMean << endl;
393  cout << "deltaMean X --> " << (double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor) << endl;
394  cout << "deltaMean Y --> " << (double(bestMovementY)-1.)*std::sqrt((*(it+1))*varFactor) << endl;
395  }
396 
397  Gauss3D->Clear();
398 
399  Gauss3D->SetVariable(0,"var x ", *(it+0)*varFactor, parDistanceXY*parDistanceXY);
400  Gauss3D->SetVariable(1,"var y ", *(it+1)*varFactor, parDistanceXY*parDistanceXY);
401  Gauss3D->SetVariable(2,"var z ", *(it+2), parDistanceZ*parDistanceZ);
402  Gauss3D->SetVariable(3,"cov xy", *(it+3), parDistanceCxy);
403  Gauss3D->SetVariable(4,"dydz ", *(it+4), parDistanceddZ);
404  Gauss3D->SetVariable(5,"dxdz ", *(it+5), parDistanceddZ);
405  Gauss3D->SetVariable(6,"mean x", *(it+6)+(double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor), parDistanceXY);
406  Gauss3D->SetVariable(7,"mean y", *(it+7)+(double(bestMovementY)-1.)*std::sqrt((*(it+1))*varFactor), parDistanceXY);
407  Gauss3D->SetVariable(8,"mean z", *(it+8)+deltaMean, parDistanceZ);
408 
409  // Set the central positions of the centroid for vertex rejection
410  xPos = Gauss3D->X()[6];
411  yPos = Gauss3D->X()[7];
412  zPos = Gauss3D->X()[8];
413 
414  // Set dimensions of the centroid for vertex rejection
415  maxTransRadius = nSigmaXY * std::sqrt(std::fabs(Gauss3D->X()[0]) + std::fabs(Gauss3D->X()[1])) / 2.;
416  maxLongLength = nSigmaZ * std::sqrt(std::fabs(Gauss3D->X()[2]));
417 
418  Gauss3D->Minimize();
419  goodData = Gauss3D->Status();
420  edm = Gauss3D->Edm();
421 
422  if (counterVx < minNentries) goodData = -2;
423  else if (isNotFinite(edm) == true) goodData = -1;
424  else for (unsigned int j = 0; j < nParams; j++) if (isNotFinite(Gauss3D->Errors()[j]) == true) { goodData = -1; break; }
425  if (goodData == 0)
426  {
427  covyz = Gauss3D->X()[4]*(std::fabs(Gauss3D->X()[2])-std::fabs(Gauss3D->X()[1])) - Gauss3D->X()[5]*Gauss3D->X()[3];
428  covxz = Gauss3D->X()[5]*(std::fabs(Gauss3D->X()[2])-std::fabs(Gauss3D->X()[0])) - Gauss3D->X()[4]*Gauss3D->X()[3];
429 
430  det = std::fabs(Gauss3D->X()[0]) * (std::fabs(Gauss3D->X()[1])*std::fabs(Gauss3D->X()[2]) - covyz*covyz) -
431  Gauss3D->X()[3] * (Gauss3D->X()[3]*std::fabs(Gauss3D->X()[2]) - covxz*covyz) +
432  covxz * (Gauss3D->X()[3]*covyz - covxz*std::fabs(Gauss3D->X()[1]));
433  if (det < 0.) { goodData = -1; if (internalDebug == true) cout << "Negative determinant !" << endl; }
434  }
435 
436  if ((goodData == 0) && (std::fabs(edm) < bestEdm)) { bestEdm = edm; bestMovementZ = i; }
437  }
438  if (internalDebug == true) cout << "Found bestMovementZ --> " << bestMovementZ << endl;
439 
440  Gauss3D->Clear();
441 
442  // @@@ FINAL FIT @@@
443  Gauss3D->SetVariable(0,"var x ", *(it+0)*varFactor, parDistanceXY*parDistanceXY);
444  Gauss3D->SetVariable(1,"var y ", *(it+1)*varFactor, parDistanceXY*parDistanceXY);
445  Gauss3D->SetVariable(2,"var z ", *(it+2), parDistanceZ*parDistanceZ);
446  Gauss3D->SetVariable(3,"cov xy", *(it+3), parDistanceCxy);
447  Gauss3D->SetVariable(4,"dydz ", *(it+4), parDistanceddZ);
448  Gauss3D->SetVariable(5,"dxdz ", *(it+5), parDistanceddZ);
449  Gauss3D->SetVariable(6,"mean x", *(it+6)+(double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor), parDistanceXY);
450  Gauss3D->SetVariable(7,"mean y", *(it+7)+(double(bestMovementY)-1.)*std::sqrt((*(it+1))*varFactor), parDistanceXY);
451  Gauss3D->SetVariable(8,"mean z", *(it+8)+(double(bestMovementZ)-1.)*std::sqrt(*(it+2)), parDistanceZ);
452 
453  // Set the central positions of the centroid for vertex rejection
454  xPos = Gauss3D->X()[6];
455  yPos = Gauss3D->X()[7];
456  zPos = Gauss3D->X()[8];
457 
458  // Set dimensions of the centroid for vertex rejection
459  maxTransRadius = nSigmaXY * std::sqrt(std::fabs(Gauss3D->X()[0]) + std::fabs(Gauss3D->X()[1])) / 2.;
460  maxLongLength = nSigmaZ * std::sqrt(std::fabs(Gauss3D->X()[2]));
461 
462  Gauss3D->Minimize();
463  goodData = Gauss3D->Status();
464  edm = Gauss3D->Edm();
465 
466  if (counterVx < minNentries) goodData = -2;
467  else if (isNotFinite(edm) == true) goodData = -1;
468  else for (unsigned int j = 0; j < nParams; j++) if (isNotFinite(Gauss3D->Errors()[j]) == true) { goodData = -1; break; }
469  if (goodData == 0)
470  {
471  covyz = Gauss3D->X()[4]*(std::fabs(Gauss3D->X()[2])-std::fabs(Gauss3D->X()[1])) - Gauss3D->X()[5]*Gauss3D->X()[3];
472  covxz = Gauss3D->X()[5]*(std::fabs(Gauss3D->X()[2])-std::fabs(Gauss3D->X()[0])) - Gauss3D->X()[4]*Gauss3D->X()[3];
473 
474  det = std::fabs(Gauss3D->X()[0]) * (std::fabs(Gauss3D->X()[1])*std::fabs(Gauss3D->X()[2]) - covyz*covyz) -
475  Gauss3D->X()[3] * (Gauss3D->X()[3]*std::fabs(Gauss3D->X()[2]) - covxz*covyz) +
476  covxz * (Gauss3D->X()[3]*covyz - covxz*std::fabs(Gauss3D->X()[1]));
477  if (det < 0.) { goodData = -1; if (internalDebug == true) cout << "Negative determinant !" << endl; }
478  }
479 
480  // @@@ FIT WITH DIFFERENT PARAMETER DISTANCES @@@
481  for (unsigned int i = 0; i < trials; i++)
482  {
483  if ((goodData != 0) && (goodData != -2))
484  {
485  Gauss3D->Clear();
486 
487  if (internalDebug == true) cout << "FIT WITH DIFFERENT PARAMETER DISTANCES - STEP " << i+1 << endl;
488 
489  Gauss3D->SetVariable(0,"var x ", *(it+0)*varFactor, parDistanceXY*parDistanceXY * largerDist[i]);
490  Gauss3D->SetVariable(1,"var y ", *(it+1)*varFactor, parDistanceXY*parDistanceXY * largerDist[i]);
491  Gauss3D->SetVariable(2,"var z ", *(it+2), parDistanceZ*parDistanceZ * largerDist[i]);
492  Gauss3D->SetVariable(3,"cov xy", *(it+3), parDistanceCxy * largerDist[i]);
493  Gauss3D->SetVariable(4,"dydz ", *(it+4), parDistanceddZ * largerDist[i]);
494  Gauss3D->SetVariable(5,"dxdz ", *(it+5), parDistanceddZ * largerDist[i]);
495  Gauss3D->SetVariable(6,"mean x", *(it+6)+(double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor), parDistanceXY * largerDist[i]);
496  Gauss3D->SetVariable(7,"mean y", *(it+7)+(double(bestMovementY)-1.)*std::sqrt((*(it+1))*varFactor), parDistanceXY * largerDist[i]);
497  Gauss3D->SetVariable(8,"mean z", *(it+8)+(double(bestMovementZ)-1.)*std::sqrt(*(it+2)), parDistanceZ * largerDist[i]);
498 
499  // Set the central positions of the centroid for vertex rejection
500  xPos = Gauss3D->X()[6];
501  yPos = Gauss3D->X()[7];
502  zPos = Gauss3D->X()[8];
503 
504  // Set dimensions of the centroid for vertex rejection
505  maxTransRadius = nSigmaXY * std::sqrt(std::fabs(Gauss3D->X()[0]) + std::fabs(Gauss3D->X()[1])) / 2.;
506  maxLongLength = nSigmaZ * std::sqrt(std::fabs(Gauss3D->X()[2]));
507 
508  Gauss3D->Minimize();
509  goodData = Gauss3D->Status();
510  edm = Gauss3D->Edm();
511 
512  if (counterVx < minNentries) goodData = -2;
513  else if (isNotFinite(edm) == true) goodData = -1;
514  else for (unsigned int j = 0; j < nParams; j++) if (isNotFinite(Gauss3D->Errors()[j]) == true) { goodData = -1; break; }
515  if (goodData == 0)
516  {
517  covyz = Gauss3D->X()[4]*(std::fabs(Gauss3D->X()[2])-std::fabs(Gauss3D->X()[1])) - Gauss3D->X()[5]*Gauss3D->X()[3];
518  covxz = Gauss3D->X()[5]*(std::fabs(Gauss3D->X()[2])-std::fabs(Gauss3D->X()[0])) - Gauss3D->X()[4]*Gauss3D->X()[3];
519 
520  det = std::fabs(Gauss3D->X()[0]) * (std::fabs(Gauss3D->X()[1])*std::fabs(Gauss3D->X()[2]) - covyz*covyz) -
521  Gauss3D->X()[3] * (Gauss3D->X()[3]*std::fabs(Gauss3D->X()[2]) - covxz*covyz) +
522  covxz * (Gauss3D->X()[3]*covyz - covxz*std::fabs(Gauss3D->X()[1]));
523  if (det < 0.) { goodData = -1; if (internalDebug == true) cout << "Negative determinant !" << endl; }
524  }
525  } else break;
526  }
527 
528  if (goodData == 0)
529  for (unsigned int i = 0; i < nParams; i++)
530  {
531  vals->operator[](i) = Gauss3D->X()[i];
532  vals->operator[](i+nParams) = Gauss3D->Errors()[i];
533  }
534 
535  delete Gauss3D;
536  return goodData;
537  }
538 
539  return -1;
540 }
541 
542 
543 void Vx3DHLTAnalyzer::reset (string ResetType)
544 {
545  if (ResetType.compare("scratch") == 0)
546  {
547  runNumber = 0;
548  numberGoodFits = 0;
549  numberFits = 0;
550  lastLumiOfFit = 0;
551 
552  Vx_X->Reset();
553  Vx_Y->Reset();
554  Vx_Z->Reset();
555 
556  Vx_ZX->Reset();
557  Vx_ZY->Reset();
558  Vx_XY->Reset();
559 
560  mXlumi->Reset();
561  mYlumi->Reset();
562  mZlumi->Reset();
563 
564  sXlumi->Reset();
565  sYlumi->Reset();
566  sZlumi->Reset();
567 
568  dxdzlumi->Reset();
569  dydzlumi->Reset();
570 
571  hitCounter->Reset();
573  goodVxCounter->Reset();
575  fitResults->Reset();
576 
577  reportSummary->Fill(0.);
578  reportSummaryMap->Fill(0.5, 0.5, 0.);
579 
580  Vertices.clear();
581 
582  lumiCounter = 0;
583  lumiCounterHisto = 0;
584  totalHits = 0;
585  beginTimeOfFit = 0;
586  endTimeOfFit = 0;
587  beginLumiOfFit = 0;
588  endLumiOfFit = 0;
589  }
590  else if (ResetType.compare("whole") == 0)
591  {
592  Vx_X->Reset();
593  Vx_Y->Reset();
594  Vx_Z->Reset();
595 
596  Vx_ZX->Reset();
597  Vx_ZY->Reset();
598  Vx_XY->Reset();
599 
600  Vertices.clear();
601 
602  lumiCounter = 0;
603  lumiCounterHisto = 0;
604  totalHits = 0;
605  beginTimeOfFit = 0;
606  endTimeOfFit = 0;
607  beginLumiOfFit = 0;
608  endLumiOfFit = 0;
609  }
610  else if (ResetType.compare("partial") == 0)
611  {
612  Vx_X->Reset();
613  Vx_Y->Reset();
614  Vx_Z->Reset();
615 
616  Vertices.clear();
617 
618  lumiCounter = 0;
619  totalHits = 0;
620  beginTimeOfFit = 0;
621  endTimeOfFit = 0;
622  beginLumiOfFit = 0;
623  endLumiOfFit = 0;
624  }
625  else if (ResetType.compare("nohisto") == 0)
626  {
627  Vertices.clear();
628 
629  lumiCounter = 0;
630  lumiCounterHisto = 0;
631  totalHits = 0;
632  beginTimeOfFit = 0;
633  endTimeOfFit = 0;
634  beginLumiOfFit = 0;
635  endLumiOfFit = 0;
636  }
637  else if (ResetType.compare("hitCounter") == 0)
638  totalHits = 0;
639 }
640 
641 
642 void Vx3DHLTAnalyzer::writeToFile (vector<double>* vals,
643  TimeValue_t BeginTimeOfFit,
644  TimeValue_t EndTimeOfFit,
645  unsigned int BeginLumiOfFit,
646  unsigned int EndLumiOfFit,
647  int dataType)
648 {
649  stringstream BufferString;
650  BufferString.precision(5);
651 
652  outputFile.open(fileName.c_str(), ios::out);
653 
654  if ((outputFile.is_open() == true) && (vals != NULL) && (vals->size() == 8*2))
655  {
656  vector<double>::const_iterator it = vals->begin();
657 
658  outputFile << "Runnumber " << runNumber << endl;
659  outputFile << "BeginTimeOfFit " << formatTime(beginTimeOfFit >> 32) << " " << (beginTimeOfFit >> 32) << endl;
660  outputFile << "EndTimeOfFit " << formatTime(endTimeOfFit >> 32) << " " << (endTimeOfFit >> 32) << endl;
661  outputFile << "LumiRange " << beginLumiOfFit << " - " << endLumiOfFit << endl;
662  outputFile << "Type " << dataType << endl;
663  // 3D Vertexing with Pixel Tracks:
664  // Good data = Type 3
665  // Bad data = Type -1
666 
667  BufferString << *(it+0);
668  outputFile << "X0 " << BufferString.str().c_str() << endl;
669  BufferString.str("");
670 
671  BufferString << *(it+1);
672  outputFile << "Y0 " << BufferString.str().c_str() << endl;
673  BufferString.str("");
674 
675  BufferString << *(it+2);
676  outputFile << "Z0 " << BufferString.str().c_str() << endl;
677  BufferString.str("");
678 
679  BufferString << *(it+3);
680  outputFile << "sigmaZ0 " << BufferString.str().c_str() << endl;
681  BufferString.str("");
682 
683  BufferString << *(it+4);
684  outputFile << "dxdz " << BufferString.str().c_str() << endl;
685  BufferString.str("");
686 
687  BufferString << *(it+5);
688  outputFile << "dydz " << BufferString.str().c_str() << endl;
689  BufferString.str("");
690 
691  BufferString << *(it+6);
692  outputFile << "BeamWidthX " << BufferString.str().c_str() << endl;
693  BufferString.str("");
694 
695  BufferString << *(it+7);
696  outputFile << "BeamWidthY " << BufferString.str().c_str() << endl;
697  BufferString.str("");
698 
699  outputFile << "Cov(0,j) " << *(it+8) << " 0.0 0.0 0.0 0.0 0.0 0.0" << endl;
700  outputFile << "Cov(1,j) 0.0 " << *(it+9) << " 0.0 0.0 0.0 0.0 0.0" << endl;
701  outputFile << "Cov(2,j) 0.0 0.0 " << *(it+10) << " 0.0 0.0 0.0 0.0" << endl;
702  outputFile << "Cov(3,j) 0.0 0.0 0.0 " << *(it+11) << " 0.0 0.0 0.0" << endl;
703  outputFile << "Cov(4,j) 0.0 0.0 0.0 0.0 " << *(it+12) << " 0.0 0.0" << endl;
704  outputFile << "Cov(5,j) 0.0 0.0 0.0 0.0 0.0 " << *(it+13) << " 0.0" << endl;
705  outputFile << "Cov(6,j) 0.0 0.0 0.0 0.0 0.0 0.0 " << ((*(it+14)) + (*(it+15)) + 2.*std::sqrt((*(it+14))*(*(it+15)))) / 4. << endl;
706 
707  outputFile << "EmittanceX 0.0" << endl;
708  outputFile << "EmittanceY 0.0" << endl;
709  outputFile << "BetaStar 0.0" << endl;
710  }
711  outputFile.close();
712 
713  if ((debugMode == true) && (outputDebugFile.is_open() == true) && (vals != NULL) && (vals->size() == 8*2))
714  {
715  vector<double>::const_iterator it = vals->begin();
716 
717  outputDebugFile << "Runnumber " << runNumber << endl;
718  outputDebugFile << "BeginTimeOfFit " << formatTime(beginTimeOfFit >> 32) << " " << (beginTimeOfFit >> 32) << endl;
719  outputDebugFile << "EndTimeOfFit " << formatTime(endTimeOfFit >> 32) << " " << (endTimeOfFit >> 32) << endl;
720  outputDebugFile << "LumiRange " << beginLumiOfFit << " - " << endLumiOfFit << endl;
721  outputDebugFile << "Type " << dataType << endl;
722  // 3D Vertexing with Pixel Tracks:
723  // Good data = Type 3
724  // Bad data = Type -1
725 
726  BufferString << *(it+0);
727  outputDebugFile << "X0 " << BufferString.str().c_str() << endl;
728  BufferString.str("");
729 
730  BufferString << *(it+1);
731  outputDebugFile << "Y0 " << BufferString.str().c_str() << endl;
732  BufferString.str("");
733 
734  BufferString << *(it+2);
735  outputDebugFile << "Z0 " << BufferString.str().c_str() << endl;
736  BufferString.str("");
737 
738  BufferString << *(it+3);
739  outputDebugFile << "sigmaZ0 " << BufferString.str().c_str() << endl;
740  BufferString.str("");
741 
742  BufferString << *(it+4);
743  outputDebugFile << "dxdz " << BufferString.str().c_str() << endl;
744  BufferString.str("");
745 
746  BufferString << *(it+5);
747  outputDebugFile << "dydz " << BufferString.str().c_str() << endl;
748  BufferString.str("");
749 
750  BufferString << *(it+6);
751  outputDebugFile << "BeamWidthX " << BufferString.str().c_str() << endl;
752  BufferString.str("");
753 
754  BufferString << *(it+7);
755  outputDebugFile << "BeamWidthY " << BufferString.str().c_str() << endl;
756  BufferString.str("");
757 
758  outputDebugFile << "Cov(0,j) " << *(it+8) << " 0.0 0.0 0.0 0.0 0.0 0.0" << endl;
759  outputDebugFile << "Cov(1,j) 0.0 " << *(it+9) << " 0.0 0.0 0.0 0.0 0.0" << endl;
760  outputDebugFile << "Cov(2,j) 0.0 0.0 " << *(it+10) << " 0.0 0.0 0.0 0.0" << endl;
761  outputDebugFile << "Cov(3,j) 0.0 0.0 0.0 " << *(it+11) << " 0.0 0.0 0.0" << endl;
762  outputDebugFile << "Cov(4,j) 0.0 0.0 0.0 0.0 " << *(it+12) << " 0.0 0.0" << endl;
763  outputDebugFile << "Cov(5,j) 0.0 0.0 0.0 0.0 0.0 " << *(it+13) << " 0.0" << endl;
764  outputDebugFile << "Cov(6,j) 0.0 0.0 0.0 0.0 0.0 0.0 " << ((*(it+14)) + (*(it+15)) + 2.*std::sqrt((*(it+14))*(*(it+15)))) / 4. << endl;
765 
766  outputDebugFile << "EmittanceX 0.0" << endl;
767  outputDebugFile << "EmittanceY 0.0" << endl;
768  outputDebugFile << "BetaStar 0.0" << endl;
769  }
770 }
771 
772 
774 {
775  if ((lumiCounter == 0) && (lumiBlock.luminosityBlock() > lastLumiOfFit))
776  {
777  beginTimeOfFit = lumiBlock.beginTime().value();
778  beginLumiOfFit = lumiBlock.luminosityBlock();
779  lumiCounter++;
781  }
782  else if ((lumiCounter != 0) && (lumiBlock.luminosityBlock() >= (beginLumiOfFit+lumiCounter))) { lumiCounter++; lumiCounterHisto++; }
783 }
784 
785 
786 void Vx3DHLTAnalyzer::endLuminosityBlock (const LuminosityBlock& lumiBlock, const EventSetup& iSetup)
787 {
788  stringstream histTitle;
789  int goodData;
790  unsigned int nParams = 9;
791 
792  if ((lumiCounter%nLumiReset == 0) && (nLumiReset != 0) && (beginTimeOfFit != 0) && (runNumber != 0))
793  {
794  endTimeOfFit = lumiBlock.endTime().value();
795  endLumiOfFit = lumiBlock.luminosityBlock();
797  vector<double> vals;
798 
799  hitCounter->ShiftFillLast((double)totalHits, std::sqrt((double)totalHits), nLumiReset);
800 
801  if (lastLumiOfFit % prescaleHistory == 0)
802  {
803  hitCountHistory->getTH1()->SetBinContent(lastLumiOfFit, (double)totalHits);
804  hitCountHistory->getTH1()->SetBinError(lastLumiOfFit, std::sqrt((double)totalHits));
805  }
806 
807  if (dataFromFit == true)
808  {
809  vector<double> fitResults;
810 
811  fitResults.push_back(Vx_X->getTH1()->GetRMS()*Vx_X->getTH1()->GetRMS());
812  fitResults.push_back(Vx_Y->getTH1()->GetRMS()*Vx_Y->getTH1()->GetRMS());
813  fitResults.push_back(Vx_Z->getTH1()->GetRMS()*Vx_Z->getTH1()->GetRMS());
814  fitResults.push_back(0.0);
815  fitResults.push_back(0.0);
816  fitResults.push_back(0.0);
817  fitResults.push_back(Vx_X->getTH1()->GetMean());
818  fitResults.push_back(Vx_Y->getTH1()->GetMean());
819  fitResults.push_back(Vx_Z->getTH1()->GetMean());
820  for (unsigned int i = 0; i < nParams; i++) fitResults.push_back(0.0);
821 
822  goodData = MyFit(&fitResults);
823 
824  if (internalDebug == true)
825  {
826  cout << "goodData --> " << goodData << endl;
827  cout << "Used vertices --> " << counterVx << endl;
828  cout << "var x --> " << fitResults[0] << " +/- " << fitResults[0+nParams] << endl;
829  cout << "var y --> " << fitResults[1] << " +/- " << fitResults[1+nParams] << endl;
830  cout << "var z --> " << fitResults[2] << " +/- " << fitResults[2+nParams] << endl;
831  cout << "cov xy --> " << fitResults[3] << " +/- " << fitResults[3+nParams] << endl;
832  cout << "dydz --> " << fitResults[4] << " +/- " << fitResults[4+nParams] << endl;
833  cout << "dxdz --> " << fitResults[5] << " +/- " << fitResults[5+nParams] << endl;
834  cout << "mean x --> " << fitResults[6] << " +/- " << fitResults[6+nParams] << endl;
835  cout << "mean y --> " << fitResults[7] << " +/- " << fitResults[7+nParams] << endl;
836  cout << "mean z --> " << fitResults[8] << " +/- " << fitResults[8+nParams] << endl;
837  }
838 
839  if (goodData == 0)
840  {
841  vals.push_back(fitResults[6]);
842  vals.push_back(fitResults[7]);
843  vals.push_back(fitResults[8]);
844  vals.push_back(std::sqrt(std::fabs(fitResults[2])));
845  vals.push_back(fitResults[5]);
846  vals.push_back(fitResults[4]);
847  vals.push_back(std::sqrt(std::fabs(fitResults[0])));
848  vals.push_back(std::sqrt(std::fabs(fitResults[1])));
849 
850  vals.push_back(std::pow(fitResults[6+nParams],2.));
851  vals.push_back(std::pow(fitResults[7+nParams],2.));
852  vals.push_back(std::pow(fitResults[8+nParams],2.));
853  vals.push_back(std::pow(std::fabs(fitResults[2+nParams]) / (2.*std::sqrt(std::fabs(fitResults[2]))),2.));
854  vals.push_back(std::pow(fitResults[5+nParams],2.));
855  vals.push_back(std::pow(fitResults[4+nParams],2.));
856  vals.push_back(std::pow(std::fabs(fitResults[0+nParams]) / (2.*std::sqrt(std::fabs(fitResults[0]))),2.));
857  vals.push_back(std::pow(std::fabs(fitResults[1+nParams]) / (2.*std::sqrt(std::fabs(fitResults[1]))),2.));
858  }
859  else for (unsigned int i = 0; i < 8*2; i++) vals.push_back(0.0);
860 
861  fitResults.clear();
862  }
863  else
864  {
865  counterVx = Vx_X->getTH1F()->GetEntries();
866 
867  if (Vx_X->getTH1F()->GetEntries() >= minNentries)
868  {
869  goodData = 0;
870 
871  vals.push_back(Vx_X->getTH1F()->GetMean());
872  vals.push_back(Vx_Y->getTH1F()->GetMean());
873  vals.push_back(Vx_Z->getTH1F()->GetMean());
874  vals.push_back(Vx_Z->getTH1F()->GetRMS());
875  vals.push_back(0.0);
876  vals.push_back(0.0);
877  vals.push_back(Vx_X->getTH1F()->GetRMS());
878  vals.push_back(Vx_Y->getTH1F()->GetRMS());
879 
880  vals.push_back(std::pow(Vx_X->getTH1F()->GetMeanError(),2.));
881  vals.push_back(std::pow(Vx_Y->getTH1F()->GetMeanError(),2.));
882  vals.push_back(std::pow(Vx_Z->getTH1F()->GetMeanError(),2.));
883  vals.push_back(std::pow(Vx_Z->getTH1F()->GetRMSError(),2.));
884  vals.push_back(0.0);
885  vals.push_back(0.0);
886  vals.push_back(std::pow(Vx_X->getTH1F()->GetRMSError(),2.));
887  vals.push_back(std::pow(Vx_Y->getTH1F()->GetRMSError(),2.));
888  }
889  else
890  {
891  goodData = -2;
892  for (unsigned int i = 0; i < 8*2; i++) vals.push_back(0.0);
893  }
894  }
895 
896  // vals[0] = X0
897  // vals[1] = Y0
898  // vals[2] = Z0
899  // vals[3] = sigmaZ0
900  // vals[4] = dxdz
901  // vals[5] = dydz
902  // vals[6] = BeamWidthX
903  // vals[7] = BeamWidthY
904 
905  // vals[8] = err^2 X0
906  // vals[9] = err^2 Y0
907  // vals[10] = err^2 Z0
908  // vals[11] = err^2 sigmaZ0
909  // vals[12] = err^2 dxdz
910  // vals[13] = err^2 dydz
911  // vals[14] = err^2 BeamWidthX
912  // vals[15] = err^2 BeamWidthY
913 
914  // "goodData" CODE:
915  // 0 == OK --> Reset
916  // -2 == NO OK - not enough "minNentries" --> Wait for more lumisections
917  // Any other number == NO OK --> Reset
918 
919  numberFits++;
920  if (goodData == 0)
921  {
923  if ((internalDebug == true) && (outputDebugFile.is_open() == true)) outputDebugFile << "Used vertices: " << counterVx << endl;
924 
925  numberGoodFits++;
926 
927  histTitle << "Fitted Beam Spot [cm] (Lumi start: " << beginLumiOfFit << " - Lumi end: " << endLumiOfFit << ")";
928  if (lumiCounterHisto >= maxLumiIntegration) reset("whole");
929  else reset("partial");
930  }
931  else
932  {
934  if ((internalDebug == true) && (outputDebugFile.is_open() == true)) outputDebugFile << "Used vertices: " << counterVx << endl;
935 
936  if (goodData == -2)
937  {
938  histTitle << "Fitted Beam Spot [cm] (not enough statistics)";
939  if (lumiCounter >= maxLumiIntegration) reset("whole");
940  else reset("hitCounter");
941  }
942  else
943  {
944  histTitle << "Fitted Beam Spot [cm] (problems)";
945  if (lumiCounterHisto >= maxLumiIntegration) reset("whole");
946  else reset("partial");
947 
948  counterVx = 0;
949  }
950  }
951 
952  reportSummary->Fill(numberFits != 0 ? (double)numberGoodFits/(double)numberFits : 0.0);
953  reportSummaryMap->Fill(0.5, 0.5, numberFits != 0 ? (double)numberGoodFits/(double)numberFits : 0.0);
954 
955  fitResults->setAxisTitle(histTitle.str().c_str(), 1);
956 
957  fitResults->setBinContent(1, 9, vals[0]);
958  fitResults->setBinContent(1, 8, vals[1]);
959  fitResults->setBinContent(1, 7, vals[2]);
960  fitResults->setBinContent(1, 6, vals[3]);
961  fitResults->setBinContent(1, 5, vals[4]);
962  fitResults->setBinContent(1, 4, vals[5]);
963  fitResults->setBinContent(1, 3, vals[6]);
964  fitResults->setBinContent(1, 2, vals[7]);
966 
967  fitResults->setBinContent(2, 9, std::sqrt(vals[8]));
968  fitResults->setBinContent(2, 8, std::sqrt(vals[9]));
969  fitResults->setBinContent(2, 7, std::sqrt(vals[10]));
970  fitResults->setBinContent(2, 6, std::sqrt(vals[11]));
971  fitResults->setBinContent(2, 5, std::sqrt(vals[12]));
972  fitResults->setBinContent(2, 4, std::sqrt(vals[13]));
973  fitResults->setBinContent(2, 3, std::sqrt(vals[14]));
974  fitResults->setBinContent(2, 2, std::sqrt(vals[15]));
976 
977  // Linear fit to the historical plots
978  TF1* myLinFit = new TF1("myLinFit", "[0] + [1]*x", mXlumi->getTH1()->GetXaxis()->GetXmin(), mXlumi->getTH1()->GetXaxis()->GetXmax());
979  myLinFit->SetLineColor(2);
980  myLinFit->SetLineWidth(2);
981  myLinFit->SetParName(0,"Intercept");
982  myLinFit->SetParName(1,"Slope");
983 
984  mXlumi->ShiftFillLast(vals[0], std::sqrt(vals[8]), nLumiReset);
985  myLinFit->SetParameter(0, mXlumi->getTH1()->GetMean(2));
986  myLinFit->SetParameter(1, 0.0);
987  mXlumi->getTH1()->Fit(myLinFit,"QR");
988 
989  mYlumi->ShiftFillLast(vals[1], std::sqrt(vals[9]), nLumiReset);
990  myLinFit->SetParameter(0, mYlumi->getTH1()->GetMean(2));
991  myLinFit->SetParameter(1, 0.0);
992  mYlumi->getTH1()->Fit(myLinFit,"QR");
993 
994  mZlumi->ShiftFillLast(vals[2], std::sqrt(vals[10]), nLumiReset);
995  myLinFit->SetParameter(0, mZlumi->getTH1()->GetMean(2));
996  myLinFit->SetParameter(1, 0.0);
997  mZlumi->getTH1()->Fit(myLinFit,"QR");
998 
999  sXlumi->ShiftFillLast(vals[6], std::sqrt(vals[14]), nLumiReset);
1000  myLinFit->SetParameter(0, sXlumi->getTH1()->GetMean(2));
1001  myLinFit->SetParameter(1, 0.0);
1002  sXlumi->getTH1()->Fit(myLinFit,"QR");
1003 
1004  sYlumi->ShiftFillLast(vals[7], std::sqrt(vals[15]), nLumiReset);
1005  myLinFit->SetParameter(0, sYlumi->getTH1()->GetMean(2));
1006  myLinFit->SetParameter(1, 0.0);
1007  sYlumi->getTH1()->Fit(myLinFit,"QR");
1008 
1009  sZlumi->ShiftFillLast(vals[3], std::sqrt(vals[11]), nLumiReset);
1010  myLinFit->SetParameter(0, sZlumi->getTH1()->GetMean(2));
1011  myLinFit->SetParameter(1, 0.0);
1012  sZlumi->getTH1()->Fit(myLinFit,"QR");
1013 
1014  dxdzlumi->ShiftFillLast(vals[4], std::sqrt(vals[12]), nLumiReset);
1015  myLinFit->SetParameter(0, dxdzlumi->getTH1()->GetMean(2));
1016  myLinFit->SetParameter(1, 0.0);
1017  dxdzlumi->getTH1()->Fit(myLinFit,"QR");
1018 
1019  dydzlumi->ShiftFillLast(vals[5], std::sqrt(vals[13]), nLumiReset);
1020  myLinFit->SetParameter(0, dydzlumi->getTH1()->GetMean(2));
1021  myLinFit->SetParameter(1, 0.0);
1022  dydzlumi->getTH1()->Fit(myLinFit,"QR");
1023 
1024  goodVxCounter->ShiftFillLast((double)counterVx, std::sqrt((double)counterVx), nLumiReset);
1025  myLinFit->SetParameter(0, goodVxCounter->getTH1()->GetMean(2));
1026  myLinFit->SetParameter(1, 0.0);
1027  goodVxCounter->getTH1()->Fit(myLinFit,"QR");
1028 
1029  if (lastLumiOfFit % prescaleHistory == 0)
1030  {
1031  goodVxCountHistory->getTH1()->SetBinContent(lastLumiOfFit, (double)counterVx);
1032  goodVxCountHistory->getTH1()->SetBinError(lastLumiOfFit, std::sqrt((double)counterVx));
1033  }
1034 
1035  delete myLinFit;
1036 
1037  vals.clear();
1038  }
1039  else if (nLumiReset == 0)
1040  {
1041  histTitle << "Fitted Beam Spot [cm] (no ongoing fits)";
1042  fitResults->setAxisTitle(histTitle.str().c_str(), 1);
1043  reportSummaryMap->Fill(0.5, 0.5, 1.0);
1045  reset("nohisto");
1046  }
1047 }
1048 
1049 
1051 {
1052  // ### Set internal variables ###
1053  prescaleHistory = 1; // Set the number of lumis to update historical plot
1054  maxLumiIntegration = 15; // If failing fits, this is the maximum number of integrated lumis after which a reset is issued
1055  minVxDoF = 10.; // Good vertex selection cut
1056  // For vertex fitter without track-weight: d.o.f. = 2*NTracks - 3
1057  // For vertex fitter with track-weight: d.o.f. = sum_NTracks(2*track_weight) - 3
1058  internalDebug = false;
1059  considerVxCovariance = true; // Deconvolute vertex covariance matrix
1060  pi = 3.141592653589793238;
1061  // ##############################
1062 }
1063 
1064 
1065 void Vx3DHLTAnalyzer::endJob () { reset("scratch"); }
1066 
1067 
1068 void Vx3DHLTAnalyzer::beginRun (const Run& iRun, const EventSetup& iSetup)
1069 {
1070  DQMStore* dbe = 0;
1071  dbe = Service<DQMStore>().operator->();
1072 
1073  // ### Set internal variables ###
1074  nBinsHistoricalPlot = 80;
1075  nBinsWholeHistory = 3000; // Correspond to about 20h of data taking: 20h * 60min * 60s / 23s per lumi-block = 3130
1076  // ##############################
1077 
1078  if ( dbe )
1079  {
1080  dbe->setCurrentFolder("BeamPixel");
1081 
1082  Vx_X = dbe->book1D("vertex x", "Primary Vertex X Coordinate Distribution", int(rint(xRange/xStep)), -xRange/2., xRange/2.);
1083  Vx_Y = dbe->book1D("vertex y", "Primary Vertex Y Coordinate Distribution", int(rint(yRange/yStep)), -yRange/2., yRange/2.);
1084  Vx_Z = dbe->book1D("vertex z", "Primary Vertex Z Coordinate Distribution", int(rint(zRange/zStep)), -zRange/2., zRange/2.);
1085  Vx_X->setAxisTitle("Primary Vertices X [cm]",1);
1086  Vx_X->setAxisTitle("Entries [#]",2);
1087  Vx_Y->setAxisTitle("Primary Vertices Y [cm]",1);
1088  Vx_Y->setAxisTitle("Entries [#]",2);
1089  Vx_Z->setAxisTitle("Primary Vertices Z [cm]",1);
1090  Vx_Z->setAxisTitle("Entries [#]",2);
1091 
1092  mXlumi = dbe->book1D("muX vs lumi", "\\mu_{x} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1093  mYlumi = dbe->book1D("muY vs lumi", "\\mu_{y} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1094  mZlumi = dbe->book1D("muZ vs lumi", "\\mu_{z} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1095  mXlumi->setAxisTitle("Lumisection [#]",1);
1096  mXlumi->setAxisTitle("\\mu_{x} [cm]",2);
1097  mXlumi->getTH1()->SetOption("E1");
1098  mYlumi->setAxisTitle("Lumisection [#]",1);
1099  mYlumi->setAxisTitle("\\mu_{y} [cm]",2);
1100  mYlumi->getTH1()->SetOption("E1");
1101  mZlumi->setAxisTitle("Lumisection [#]",1);
1102  mZlumi->setAxisTitle("\\mu_{z} [cm]",2);
1103  mZlumi->getTH1()->SetOption("E1");
1104 
1105  sXlumi = dbe->book1D("sigmaX vs lumi", "\\sigma_{x} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1106  sYlumi = dbe->book1D("sigmaY vs lumi", "\\sigma_{y} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1107  sZlumi = dbe->book1D("sigmaZ vs lumi", "\\sigma_{z} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1108  sXlumi->setAxisTitle("Lumisection [#]",1);
1109  sXlumi->setAxisTitle("\\sigma_{x} [cm]",2);
1110  sXlumi->getTH1()->SetOption("E1");
1111  sYlumi->setAxisTitle("Lumisection [#]",1);
1112  sYlumi->setAxisTitle("\\sigma_{y} [cm]",2);
1113  sYlumi->getTH1()->SetOption("E1");
1114  sZlumi->setAxisTitle("Lumisection [#]",1);
1115  sZlumi->setAxisTitle("\\sigma_{z} [cm]",2);
1116  sZlumi->getTH1()->SetOption("E1");
1117 
1118  dxdzlumi = dbe->book1D("dxdz vs lumi", "dX/dZ vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1119  dydzlumi = dbe->book1D("dydz vs lumi", "dY/dZ vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1120  dxdzlumi->setAxisTitle("Lumisection [#]",1);
1121  dxdzlumi->setAxisTitle("dX/dZ [rad]",2);
1122  dxdzlumi->getTH1()->SetOption("E1");
1123  dydzlumi->setAxisTitle("Lumisection [#]",1);
1124  dydzlumi->setAxisTitle("dY/dZ [rad]",2);
1125  dydzlumi->getTH1()->SetOption("E1");
1126 
1127  Vx_ZX = dbe->book2D("vertex zx", "Primary Vertex ZX Coordinate Distribution", int(rint(zRange/zStep/5.)), -zRange/2., zRange/2., int(rint(xRange/xStep/5.)), -xRange/2., xRange/2.);
1128  Vx_ZY = dbe->book2D("vertex zy", "Primary Vertex ZY Coordinate Distribution", int(rint(zRange/zStep/5.)), -zRange/2., zRange/2., int(rint(yRange/yStep/5.)), -yRange/2., yRange/2.);
1129  Vx_XY = dbe->book2D("vertex xy", "Primary Vertex XY Coordinate Distribution", int(rint(xRange/xStep/5.)), -xRange/2., xRange/2., int(rint(yRange/yStep/5.)), -yRange/2., yRange/2.);
1130  Vx_ZX->setAxisTitle("Primary Vertices Z [cm]",1);
1131  Vx_ZX->setAxisTitle("Primary Vertices X [cm]",2);
1132  Vx_ZX->setAxisTitle("Entries [#]",3);
1133  Vx_ZY->setAxisTitle("Primary Vertices Z [cm]",1);
1134  Vx_ZY->setAxisTitle("Primary Vertices Y [cm]",2);
1135  Vx_ZY->setAxisTitle("Entries [#]",3);
1136  Vx_XY->setAxisTitle("Primary Vertices X [cm]",1);
1137  Vx_XY->setAxisTitle("Primary Vertices Y [cm]",2);
1138  Vx_XY->setAxisTitle("Entries [#]",3);
1139 
1140  hitCounter = dbe->book1D("pixelHits vs lumi", "# Pixel-Hits vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1141  hitCounter->setAxisTitle("Lumisection [#]",1);
1142  hitCounter->setAxisTitle("Pixel-Hits [#]",2);
1143  hitCounter->getTH1()->SetOption("E1");
1144 
1145  hitCountHistory = dbe->book1D("hist pixelHits vs lumi", "History: # Pixel-Hits vs. Lumi", nBinsWholeHistory, 0.5, (double)nBinsWholeHistory+0.5);
1146  hitCountHistory->setAxisTitle("Lumisection [#]",1);
1147  hitCountHistory->setAxisTitle("Pixel-Hits [#]",2);
1148  hitCountHistory->getTH1()->SetOption("E1");
1149 
1150  goodVxCounter = dbe->book1D("good vertices vs lumi", "# Good vertices vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1151  goodVxCounter->setAxisTitle("Lumisection [#]",1);
1152  goodVxCounter->setAxisTitle("Good vertices [#]",2);
1153  goodVxCounter->getTH1()->SetOption("E1");
1154 
1155  goodVxCountHistory = dbe->book1D("hist good vx vs lumi", "History: # Good vx vs. Lumi", nBinsWholeHistory, 0.5, (double)nBinsWholeHistory+0.5);
1156  goodVxCountHistory->setAxisTitle("Lumisection [#]",1);
1157  goodVxCountHistory->setAxisTitle("Good vertices [#]",2);
1158  goodVxCountHistory->getTH1()->SetOption("E1");
1159 
1160  fitResults = dbe->book2D("fit results","Results of Beam Spot Fit", 2, 0., 2., 9, 0., 9.);
1161  fitResults->setAxisTitle("Fitted Beam Spot [cm]", 1);
1162  fitResults->setBinLabel(9, "X", 2);
1163  fitResults->setBinLabel(8, "Y", 2);
1164  fitResults->setBinLabel(7, "Z", 2);
1165  fitResults->setBinLabel(6, "\\sigma_{Z}", 2);
1166  fitResults->setBinLabel(5, "#frac{dX}{dZ}[rad]", 2);
1167  fitResults->setBinLabel(4, "#frac{dY}{dZ}[rad]", 2);
1168  fitResults->setBinLabel(3, "\\sigma_{X}", 2);
1169  fitResults->setBinLabel(2, "\\sigma_{Y}", 2);
1170  fitResults->setBinLabel(1, "Vertices", 2);
1171  fitResults->setBinLabel(1, "Value", 1);
1172  fitResults->setBinLabel(2, "Stat. Error", 1);
1173  fitResults->getTH1()->SetOption("text");
1174 
1175  dbe->setCurrentFolder("BeamPixel/EventInfo");
1176  reportSummary = dbe->bookFloat("reportSummary");
1177  reportSummary->Fill(0.);
1178  reportSummaryMap = dbe->book2D("reportSummaryMap","Pixel-Vertices Beam Spot: % Good Fits", 1, 0., 1., 1, 0., 1.);
1179  reportSummaryMap->Fill(0.5, 0.5, 0.);
1180  dbe->setCurrentFolder("BeamPixel/EventInfo/reportSummaryContents");
1181 
1182  // Convention for reportSummary and reportSummaryMap:
1183  // - 0% at the moment of creation of the histogram
1184  // - n% numberGoodFits / numberFits
1185 
1186  reset("scratch"); // Initialize histograms after creation
1187  }
1188 }
1189 
1190 
1191 // Define this as a plug-in
EDGetTokenT< SiPixelRecHitCollection > pixelHitCollection
RunNumber_t run() const
Definition: EventID.h:39
unsigned int lumiCounter
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
MonitorElement * dxdzlumi
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
MonitorElement * Vx_ZX
void setBinContent(int binx, double content)
set content of bin (1-D)
unsigned int totalHits
double maxLongLength
virtual unsigned int HitCounter(const Event &iEvent)
unsigned int maxLumiIntegration
unsigned int numberFits
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:446
double zPos
vector< VertexType > Vertices
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
unsigned int beginLumiOfFit
MonitorElement * hitCountHistory
unsigned int minNentries
MonitorElement * Vx_X
void setBinLabel(int bin, const std::string &label, int axis=1)
set bin label for x, y or z axis (axis=1, 2, 3 respectively)
virtual void beginRun(const Run &iRun, const EventSetup &iSetup)
Vx3DHLTAnalyzer(const ParameterSet &)
#define NULL
Definition: scimark2.h:8
unsigned int nLumiReset
MonitorElement * mYlumi
data_type const * const_iterator
Definition: DetSetNew.h:30
Timestamp const & beginTime() const
MonitorElement * sYlumi
virtual void beginLuminosityBlock(const LuminosityBlock &lumiBlock, const EventSetup &iSetup)
MonitorElement * reportSummaryMap
virtual void endLuminosityBlock(const LuminosityBlock &lumiBlock, const EventSetup &iSetup)
float float float z
unsigned int nBinsHistoricalPlot
void Fill(long long x)
const Double_t pi
double maxTransRadius
MonitorElement * Vx_XY
MonitorElement * sZlumi
LuminosityBlockNumber_t luminosityBlock() const
double Gauss3DFunc(const double *par)
bool considerVxCovariance
double xPos
void ShiftFillLast(double y, double ye=0., int32_t xscale=1)
double Covariance[3][3]
MonitorElement * fitResults
int iEvent
Definition: GenABIO.cc:230
bool isNotFinite(T x)
Definition: isFinite.h:10
MonitorElement * Vx_ZY
virtual void beginJob()
virtual string formatTime(const time_t &t)
unsigned int endLumiOfFit
unsigned int numberGoodFits
T sqrt(T t)
Definition: SSEVec.h:48
MonitorElement * reportSummary
Timestamp const & endTime() const
MonitorElement * goodVxCounter
virtual int MyFit(vector< double > *vals)
MonitorElement * dydzlumi
int j
Definition: DBlmapReader.cc:9
TH1 * getTH1(void) const
double yPos
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
LuminosityBlock const & getLuminosityBlock() const
Definition: Event.h:77
MonitorElement * Vx_Z
MonitorElement * mXlumi
unsigned long long TimeValue_t
Definition: Timestamp.h:28
MonitorElement * mZlumi
MonitorElement * sXlumi
tuple out
Definition: dbtoconf.py:99
unsigned int lastLumiOfFit
ofstream outputDebugFile
#define DIM
unsigned int nBinsWholeHistory
unsigned int lumiCounterHisto
TimeValue_t beginTimeOfFit
virtual void analyze(const Event &, const EventSetup &)
TH1F * getTH1F(void) const
EDGetTokenT< VertexCollection > vertexCollection
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
edm::EventID id() const
Definition: EventBase.h:56
unsigned int runNumber
static std::atomic< unsigned int > counter
virtual void writeToFile(vector< double > *vals, TimeValue_t BeginTimeOfFit, TimeValue_t EndTimeOfFit, unsigned int BeginLumiOfFit, unsigned int EndLumiOfFit, int dataType)
TimeValue_t endTimeOfFit
virtual void endJob()
unsigned int counterVx
tuple cout
Definition: gather_cfg.py:121
unsigned int prescaleHistory
double VxErrCorr
Definition: DDAxes.h:10
MonitorElement * goodVxCountHistory
MonitorElement * hitCounter
void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)
void Reset(void)
reset ME (ie. contents, errors, etc)
TimeValue_t value() const
Definition: Timestamp.h:56
virtual void reset(string ResetType)
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
Definition: Run.h:41
MonitorElement * Vx_Y