40 #include "CLHEP/Matrix/SymMatrix.h" 54 #include "TDirectory.h" 126 std::ifstream inputFileStream;
128 inputFileStream.open(inputFileName.c_str());
129 if(inputFileStream.is_open()){
130 inputFile_ =
new TFile(inputFileName.c_str(),
"READ");
133 edm::LogInfo(
"CalculateAPE")<<
"Input file from loop over tracks and corresponding hits sucessfully opened";
137 <<
"...APE calculation stopped. Please check path of input file name in config:\n" 148 TString pluginName(
inputFile_->GetListOfKeys()->At(0)->GetName());
151 TDirectory *sectorDir(
nullptr), *intervalDir(
nullptr);
152 bool sectorBool(
true);
153 unsigned int iSector(1);
154 for(;sectorBool;++iSector){
155 std::stringstream sectorName, fullSectorName;
156 sectorName <<
"Sector_" << iSector <<
"/";
157 fullSectorName << pluginName << sectorName.str();
158 TString fullName(fullSectorName.str().c_str());
160 sectorDir = (TDirectory*)
inputFile_->TDirectory::GetDirectory(fullName);
163 sectorDir->GetObject(
"z_name;1", tkSector.
Name);
164 tkSector.
name = tkSector.
Name->GetTitle();
165 bool intervalBool(
true);
166 for(
unsigned int iInterval(1);intervalBool;++iInterval){
167 std::stringstream intervalName, fullIntervalName;
168 intervalName <<
"Interval_" << iInterval <<
"/";
169 fullIntervalName << fullSectorName.str() << intervalName.str();
170 fullName = fullIntervalName.str().c_str();
171 intervalDir = (TDirectory*)
inputFile_->TDirectory::GetDirectory(fullName);
173 intervalDir->GetObject(
"h_sigmaX;1", tkSector.
m_binnedHists[iInterval][
"sigmaX"]);
174 intervalDir->GetObject(
"h_norResX;1", tkSector.
m_binnedHists[iInterval][
"norResX"]);
175 intervalDir->GetObject(
"h_sigmaY;1", tkSector.
m_binnedHists[iInterval][
"sigmaY"]);
176 intervalDir->GetObject(
"h_norResY;1", tkSector.
m_binnedHists[iInterval][
"norResY"]);
181 intervalBool =
false;
182 if(iSector==1)
edm::LogInfo(
"CalculateAPE")<<
"There are "<<iInterval-1<<
" intervals per sector defined in input file";
185 TDirectory *resultsDir(
nullptr);
186 std::stringstream fullResultName;
187 fullResultName << fullSectorName.str() <<
"Results/";
188 fullName = fullResultName.str().c_str();
189 resultsDir = (TDirectory*)
inputFile_->TDirectory::GetDirectory(fullName);
191 resultsDir->GetObject(
"h_entriesX;1", tkSector.
EntriesX);
192 if(tkSector.
isPixel)resultsDir->GetObject(
"h_entriesY;1", tkSector.
EntriesY);
193 TTree* rawIdTree(
nullptr);
194 resultsDir->GetObject(
"rawIdTree", rawIdTree);
195 unsigned int rawId(0);
196 rawIdTree->SetBranchAddress(
"RawId", &rawId);
198 rawIdTree->GetEntry(
entry);
200 bool alreadyAdded(
false);
201 for(
auto const & i_rawId : tkSector.
v_rawId){
202 if(rawId==i_rawId)alreadyAdded =
true;
204 if(alreadyAdded)
break;
205 tkSector.
v_rawId.push_back(rawId);
211 edm::LogInfo(
"CalculateAPE")<<
"There are "<<iSector-1<<
" sectors defined in input file";
225 i_sector.second.WeightX =
new TH1F(
"h_weightX",
"relative weight w_{x}/w_{tot,x};#sigma_{x} [#mum];w_{x}/w_{tot,x}",v_binX.size()-1,&(v_binX[0]));
226 i_sector.second.MeanX =
new TH1F(
"h_meanX",
"residual mean <r_{x}/#sigma_{r,x}>;#sigma_{x} [#mum];<r_{x}/#sigma_{r,x}>",v_binX.size()-1,&(v_binX[0]));
227 i_sector.second.RmsX =
new TH1F(
"h_rmsX",
"residual rms RMS(r_{x}/#sigma_{r,x});#sigma_{x} [#mum];RMS(r_{x}/#sigma_{r,x})",v_binX.size()-1,&(v_binX[0]));
228 i_sector.second.FitMeanX1 =
new TH1F(
"h_fitMeanX1",
"fitted residual mean #mu_{x};#sigma_{x} [#mum];#mu_{x}",v_binX.size()-1,&(v_binX[0]));
229 i_sector.second.ResidualWidthX1 =
new TH1F(
"h_residualWidthX1",
"residual width #Delta_{x};#sigma_{x} [#mum];#Delta_{x}",v_binX.size()-1,&(v_binX[0]));
230 i_sector.second.CorrectionX1 =
new TH1F(
"h_correctionX1",
"correction to APE_{x};#sigma_{x} [#mum];#Delta#sigma_{align,x} [#mum]",v_binX.size()-1,&(v_binX[0]));
231 i_sector.second.FitMeanX2 =
new TH1F(
"h_fitMeanX2",
"fitted residual mean #mu_{x};#sigma_{x} [#mum];#mu_{x}",v_binX.size()-1,&(v_binX[0]));
232 i_sector.second.ResidualWidthX2 =
new TH1F(
"h_residualWidthX2",
"residual width #Delta_{x};#sigma_{x} [#mum];#Delta_{x}",v_binX.size()-1,&(v_binX[0]));
233 i_sector.second.CorrectionX2 =
new TH1F(
"h_correctionX2",
"correction to APE_{x};#sigma_{x} [#mum];#Delta#sigma_{align,x} [#mum]",v_binX.size()-1,&(v_binX[0]));
235 if(i_sector.second.isPixel){
236 i_sector.second.WeightY =
new TH1F(
"h_weightY",
"relative weight w_{y}/w_{tot,y};#sigma_{y} [#mum];w_{y}/w_{tot,y}",v_binX.size()-1,&(v_binX[0]));
237 i_sector.second.MeanY =
new TH1F(
"h_meanY",
"residual mean <r_{y}/#sigma_{r,y}>;#sigma_{y} [#mum];<r_{y}/#sigma_{r,y}>",v_binX.size()-1,&(v_binX[0]));
238 i_sector.second.RmsY =
new TH1F(
"h_rmsY",
"residual rms RMS(r_{y}/#sigma_{r,y});#sigma_{y} [#mum];RMS(r_{y}/#sigma_{r,y})",v_binX.size()-1,&(v_binX[0]));
239 i_sector.second.FitMeanY1 =
new TH1F(
"h_fitMeanY1",
"fitted residual mean #mu_{y};#sigma_{y} [#mum];#mu_{y}",v_binX.size()-1,&(v_binX[0]));
240 i_sector.second.ResidualWidthY1 =
new TH1F(
"h_residualWidthY1",
"residual width #Delta_{y};#sigma_{y} [#mum];#Delta_{y}",v_binX.size()-1,&(v_binX[0]));
241 i_sector.second.CorrectionY1 =
new TH1F(
"h_correctionY1",
"correction to APE_{y};#sigma_{y} [#mum];#Delta#sigma_{align,y} [#mum]",v_binX.size()-1,&(v_binX[0]));
242 i_sector.second.FitMeanY2 =
new TH1F(
"h_fitMeanY2",
"fitted residual mean #mu_{y};#sigma_{y} [#mum];#mu_{y}",v_binX.size()-1,&(v_binX[0]));
243 i_sector.second.ResidualWidthY2 =
new TH1F(
"h_residualWidthY2",
"residual width #Delta_{y};#sigma_{y} [#mum];#Delta_{y}",v_binX.size()-1,&(v_binX[0]));
244 i_sector.second.CorrectionY2 =
new TH1F(
"h_correctionY2",
"correction to APE_{y};#sigma_{y} [#mum];#Delta#sigma_{align,y} [#mum]",v_binX.size()-1,&(v_binX[0]));
253 std::vector<double> v_binX;
255 for(
int iBin=1; iBin<=EntriesX->GetNbinsX()+1; ++iBin){
256 v_binX.push_back(EntriesX->GetBinLowEdge(iBin));
265 TDirectory* baseDir = resultsFile->mkdir(
"ApeEstimatorSummary");
268 dirName<<
"Sector_" << i_sector.first;
269 TDirectory*
dir = baseDir->mkdir(dirName.str().c_str());
272 i_sector.second.Name->Write();
274 i_sector.second.WeightX->Write();
275 i_sector.second.MeanX->Write();
276 i_sector.second.RmsX->Write();
277 i_sector.second.FitMeanX1->Write();
278 i_sector.second.ResidualWidthX1 ->Write();
279 i_sector.second.CorrectionX1->Write();
280 i_sector.second.FitMeanX2->Write();
281 i_sector.second.ResidualWidthX2->Write();
282 i_sector.second.CorrectionX2->Write();
284 if(i_sector.second.isPixel){
285 i_sector.second.WeightY->Write();
286 i_sector.second.MeanY->Write();
287 i_sector.second.RmsY->Write();
288 i_sector.second.FitMeanY1->Write();
289 i_sector.second.ResidualWidthY1 ->Write();
290 i_sector.second.CorrectionY1->Write();
291 i_sector.second.FitMeanY2->Write();
292 i_sector.second.ResidualWidthY2->Write();
293 i_sector.second.CorrectionY2->Write();
296 resultsFile->Close();
309 TFile* baselineFile(
nullptr);
310 TTree* baselineTreeX(
nullptr);
311 TTree* baselineTreeY(
nullptr);
312 TTree* sectorNameBaselineTree(
nullptr);
314 std::ifstream baselineFileStream;
316 baselineFileStream.open(baselineFileName.c_str());
317 if(baselineFileStream.is_open()){
318 baselineFileStream.close();
319 baselineFile =
new TFile(baselineFileName.c_str(),
"READ");
322 edm::LogInfo(
"CalculateAPE")<<
"Baseline file for APE values sucessfully opened";
323 baselineFile->GetObject(
"iterTreeX;1",baselineTreeX);
324 baselineFile->GetObject(
"iterTreeY;1",baselineTreeY);
325 baselineFile->GetObject(
"nameTree;1",sectorNameBaselineTree);
327 edm::LogWarning(
"CalculateAPE")<<
"There is NO baseline file for APE values, so normalized residual width =1 for ideal conditions is assumed";
334 TFile*
iterationFile =
new TFile(iterationFileName.c_str(),setBaseline ?
"RECREATE" :
"UPDATE");
338 TTree* iterationTreeX(
nullptr);
339 TTree* iterationTreeY(
nullptr);
340 iterationFile->GetObject(
"iterTreeX;1",iterationTreeX);
341 iterationFile->GetObject(
"iterTreeY;1",iterationTreeY);
343 TTree* sectorNameTree(
nullptr);
344 iterationFile->GetObject(
"nameTree;1",sectorNameTree);
346 bool firstIter(
false);
350 iterationTreeX =
new TTree(
"iterTreeX",
"Tree for APE x values of all iterations");
351 iterationTreeY =
new TTree(
"iterTreeY",
"Tree for APE y values of all iterations");
352 edm::LogInfo(
"CalculateAPE")<<
"First APE iteration (number 0.), create iteration file with TTree";
353 sectorNameTree =
new TTree(
"nameTree",
"Tree with names of sectors");
355 iterationTreeX =
new TTree(
"iterTreeX",
"Tree for baseline x values of normalized residual width");
356 iterationTreeY =
new TTree(
"iterTreeY",
"Tree for baseline y values of normalized residual width");
357 edm::LogInfo(
"CalculateAPE")<<
"Set baseline, create baseline file with TTree";
358 sectorNameTree =
new TTree(
"nameTree",
"Tree with names of sectors");
361 const unsigned int iteration(iterationTreeX->GetEntries());
362 edm::LogWarning(
"CalculateAPE")<<
"NOT the first APE iteration (number 0.) but the "<<
iteration<<
". one, is this wanted or forgot to delete old iteration file with TTree?";
375 const unsigned int iSector(i_sector.first);
376 const bool pixelSector(i_sector.second.isPixel);
377 a_apeSectorX[iSector] = 99.;
378 a_apeSectorY[iSector] = 99.;
379 a_baselineSectorX[iSector] = -99.;
380 a_baselineSectorY[iSector] = -99.;
382 a_sectorName[iSector] =
nullptr;
383 a_sectorBaselineName[iSector] =
nullptr;
384 std::stringstream ss_sector, ss_sectorSuffixed;
385 ss_sector <<
"Ape_Sector_" << iSector;
386 if(!setBaseline && baselineTreeX){
387 baselineTreeX->SetBranchAddress(ss_sector.str().c_str(), &a_baselineSectorX[iSector]);
388 baselineTreeX->GetEntry(0);
390 baselineTreeY->SetBranchAddress(ss_sector.str().c_str(), &a_baselineSectorY[iSector]);
391 baselineTreeY->GetEntry(0);
393 sectorNameBaselineTree->SetBranchAddress(ss_sector.str().c_str(), &a_sectorBaselineName[iSector]);
394 sectorNameBaselineTree->GetEntry(0);
397 a_baselineSectorX[iSector] = 1.;
398 a_baselineSectorY[iSector] = 1.;
401 ss_sectorSuffixed << ss_sector.str() <<
"/D";
402 iterationTreeX->Branch(ss_sector.str().c_str(), &a_apeSectorX[iSector], ss_sectorSuffixed.str().c_str());
404 iterationTreeY->Branch(ss_sector.str().c_str(), &a_apeSectorY[iSector], ss_sectorSuffixed.str().c_str());
406 sectorNameTree->Branch(ss_sector.str().c_str(), &a_sectorName[iSector], 32000, 00);
408 iterationTreeX->SetBranchAddress(ss_sector.str().c_str(), &a_apeSectorX[iSector]);
409 iterationTreeX->GetEntry(iterationTreeX->GetEntries()-1);
411 iterationTreeY->SetBranchAddress(ss_sector.str().c_str(), &a_apeSectorY[iSector]);
412 iterationTreeY->GetEntry(iterationTreeY->GetEntries()-1);
414 sectorNameTree->SetBranchAddress(ss_sector.str().c_str(), &a_sectorName[iSector]);
415 sectorNameTree->GetEntry(0);
421 for(
auto & i_sector : m_tkSector_){
424 const std::string& nameLastIter(*a_sectorName[i_sector.first]);
425 if(name!=nameLastIter){
426 edm::LogError(
"CalculateAPE")<<
"Inconsistent sector definition in iterationFile for sector "<<i_sector.first<<
",\n" 427 <<
"Recent iteration has name \""<<name<<
"\", while previous had \""<<nameLastIter<<
"\"\n" 428 <<
"...APE calculation stopped. Please check sector definitions in config!\n";
432 a_sectorName[i_sector.first] =
new std::string(name);
434 if(!setBaseline && baselineFile){
435 const std::string& nameBaseline(*a_sectorBaselineName[i_sector.first]);
436 if(name!=nameBaseline){
437 edm::LogError(
"CalculateAPE")<<
"Inconsistent sector definition in baselineFile for sector "<<i_sector.first<<
",\n" 438 <<
"Recent iteration has name \""<<name<<
"\", while baseline had \""<<nameBaseline<<
"\"\n" 439 <<
"...APE calculation stopped. Please check sector definitions in config!\n";
447 std::ofstream apeOutputFile;
450 apeOutputFile.open(apeOutputFileName.c_str());
451 if(apeOutputFile.is_open()){
452 edm::LogInfo(
"CalculateAPE")<<
"Text file for writing APE values successfully opened";
454 edm::LogError(
"CalculateAPE")<<
"Text file for writing APE values NOT opened,\n" 455 <<
"...APE calculation stopped. Please check path of text file name in config:\n" 456 <<
"\t"<<apeOutputFileName;
464 if(apeWeightName==
"unity") apeWeight =
wUnity;
465 else if(apeWeightName==
"entries")apeWeight =
wEntries;
468 edm::LogError(
"CalculateAPE")<<
"Invalid parameter 'apeWeight' in cfg file: \""<<apeWeightName
469 <<
"\"\nimplemented apeWeights are \"unity\", \"entries\", \"entriesOverSigmaX2\"" 470 <<
"\n...APE calculation stopped.";
478 if(smoothFraction<=0. || smoothFraction>1.){
479 edm::LogError(
"CalculateAPE")<<
"Incorrect parameter in cfg file," 480 <<
"\nsmoothFraction has to be in [0,1], but is "<<smoothFraction
481 <<
"\n...APE calculation stopped.";
484 for(
auto & i_sector : m_tkSector_){
485 typedef std::pair<double,double> Error2AndResidualWidth2PerBin;
486 typedef std::pair<double,Error2AndResidualWidth2PerBin> WeightAndResultsPerBin;
487 std::vector<WeightAndResultsPerBin> v_weightAndResultsPerBinX;
488 std::vector<WeightAndResultsPerBin> v_weightAndResultsPerBinY;
490 double baselineWidthX2(a_baselineSectorX[i_sector.first]);
491 double baselineWidthY2(a_baselineSectorY[i_sector.first]);
494 for(
auto const & i_errBins : i_sector.second.m_binnedHists){
495 std::map<std::string,TH1*> mHists = i_errBins.second;
497 double entriesX = mHists[
"sigmaX"]->GetEntries();
498 double meanSigmaX = mHists[
"sigmaX"]->GetMean();
501 double xMin = mHists[
"norResX"]->GetXaxis()->GetXmin();
502 double xMax = mHists[
"norResX"]->GetXaxis()->GetXmax();
503 double integralX = mHists[
"norResX"]->Integral();
504 double meanX = mHists[
"norResX"]->GetMean();
505 double rmsX = mHists[
"norResX"]->GetRMS();
506 double maximumX = mHists[
"norResX"]->GetBinContent(mHists[
"norResX"]->GetMaximumBin());
509 TF1 funcX_1(
"mygausX",
"gaus", xMin, xMax);
510 funcX_1.SetParameters(maximumX, meanX, rmsX);
511 TString fitOpt(
"ILERQ");
512 if(integralX>minHitsPerInterval){
513 if(mHists[
"norResX"]->
Fit(&funcX_1, fitOpt)){
514 edm::LogWarning(
"CalculateAPE")<<
"Fit1 did not work: "<<mHists[
"norResX"]->Fit(&funcX_1, fitOpt);
517 LogDebug(
"CalculateAPE")<<
"FitResultX1\t"<<mHists[
"norResX"]->Fit(&funcX_1, fitOpt)<<
"\n";
519 double meanX_1 = funcX_1.GetParameter(1);
520 double sigmaX_1 = funcX_1.GetParameter(2);
523 TF1 funcX_2(
"mygausX2",
"gaus",meanX_1 - sigmaFactorFit*
TMath::Abs(sigmaX_1), meanX_1 + sigmaFactorFit*
TMath::Abs(sigmaX_1));
524 funcX_2.SetParameters(funcX_1.GetParameter(0),meanX_1,sigmaX_1);
525 if(integralX>minHitsPerInterval){
526 if(mHists[
"norResX"]->
Fit(&funcX_2, fitOpt)){
527 edm::LogWarning(
"CalculateAPE")<<
"Fit2 did not work for x : "<<mHists[
"norResX"]->Fit(&funcX_2, fitOpt);
530 LogDebug(
"CalculateAPE")<<
"FitResultX2\t"<<mHists[
"norResX"]->Fit(&funcX_2, fitOpt)<<
"\n";
532 double meanX_2 = funcX_2.GetParameter(1);
533 double sigmaX_2 = funcX_2.GetParameter(2);
537 double meanSigmaY(0.);
538 if(i_sector.second.isPixel){
539 entriesY = mHists[
"sigmaY"]->GetEntries();
540 meanSigmaY = mHists[
"sigmaY"]->GetMean();
549 if(i_sector.second.isPixel){
551 double yMin = mHists[
"norResY"]->GetXaxis()->GetXmin();
552 double yMax = mHists[
"norResY"]->GetXaxis()->GetXmax();
553 double integralY = mHists[
"norResY"]->Integral();
554 meanY = mHists[
"norResY"]->GetMean();
555 rmsY = mHists[
"norResY"]->GetRMS();
556 double maximumY = mHists[
"norResY"]->GetBinContent(mHists[
"norResY"]->GetMaximumBin());
559 TF1 funcY_1(
"mygausY",
"gaus", yMin, yMax);
560 funcY_1.SetParameters(maximumY, meanY, rmsY);
561 if(integralY>minHitsPerInterval){
562 if(mHists[
"norResY"]->
Fit(&funcY_1, fitOpt)){
563 edm::LogWarning(
"CalculateAPE")<<
"Fit1 did not work: "<<mHists[
"norResY"]->Fit(&funcY_1, fitOpt);
566 LogDebug(
"CalculateAPE")<<
"FitResultY1\t"<<mHists[
"norResY"]->Fit(&funcY_1, fitOpt)<<
"\n";
568 meanY_1 = funcY_1.GetParameter(1);
569 sigmaY_1 = funcY_1.GetParameter(2);
572 TF1 funcY_2(
"mygausY2",
"gaus",meanY_1 - sigmaFactorFit*
TMath::Abs(sigmaY_1), meanY_1 + sigmaFactorFit*
TMath::Abs(sigmaY_1));
573 funcY_2.SetParameters(funcY_1.GetParameter(0),meanY_1,sigmaY_1);
574 if(integralY>minHitsPerInterval){
575 if(mHists[
"norResY"]->
Fit(&funcY_2, fitOpt)){
576 edm::LogWarning(
"CalculateAPE")<<
"Fit2 did not work for y : "<<mHists[
"norResY"]->Fit(&funcY_2, fitOpt);
579 LogDebug(
"CalculateAPE")<<
"FitResultY2\t"<<mHists[
"norResY"]->Fit(&funcY_2, fitOpt)<<
"\n";
581 meanY_2 = funcY_2.GetParameter(1);
582 sigmaY_2 = funcY_2.GetParameter(2);
586 double fitMeanX_1(meanX_1), fitMeanX_2(meanX_2);
587 double residualWidthX_1(sigmaX_1), residualWidthX_2(sigmaX_2);
588 double fitMeanY_1(meanY_1), fitMeanY_2(meanY_2);
589 double residualWidthY_1(sigmaY_1), residualWidthY_2(sigmaY_2);
591 double correctionX2_1(-0.0010), correctionX2_2(-0.0010);
592 double correctionY2_1(-0.0010), correctionY2_2(-0.0010);
593 correctionX2_1 = meanSigmaX*meanSigmaX*(residualWidthX_1*residualWidthX_1 -baselineWidthX2);
594 correctionX2_2 = meanSigmaX*meanSigmaX*(residualWidthX_2*residualWidthX_2 -baselineWidthX2);
595 if(i_sector.second.isPixel){
596 correctionY2_1 = meanSigmaY*meanSigmaY*(residualWidthY_1*residualWidthY_1 -baselineWidthY2);
597 correctionY2_2 = meanSigmaY*meanSigmaY*(residualWidthY_2*residualWidthY_2 -baselineWidthY2);
600 double correctionX_1 = correctionX2_1>=0. ?
std::sqrt(correctionX2_1) : -
std::sqrt(-correctionX2_1);
601 double correctionX_2 = correctionX2_2>=0. ?
std::sqrt(correctionX2_2) : -
std::sqrt(-correctionX2_2);
602 double correctionY_1 = correctionY2_1>=0. ?
std::sqrt(correctionY2_1) : -
std::sqrt(-correctionY2_1);
603 double correctionY_2 = correctionY2_2>=0. ?
std::sqrt(correctionY2_2) : -
std::sqrt(-correctionY2_2);
605 if(
isnan(correctionX_1))correctionX_1 = -0.0010;
606 if(
isnan(correctionX_2))correctionX_2 = -0.0010;
607 if(
isnan(correctionY_1))correctionY_1 = -0.0010;
608 if(
isnan(correctionY_2))correctionY_2 = -0.0010;
610 if(entriesX<minHitsPerInterval){
611 meanX = 0.; rmsX = -0.0010;
612 fitMeanX_1 = 0.; correctionX_1 = residualWidthX_1 = -0.0010;
613 fitMeanX_2 = 0.; correctionX_2 = residualWidthX_2 = -0.0010;
616 if(entriesY<minHitsPerInterval){
617 meanY = 0.; rmsY = -0.0010;
618 fitMeanY_1 = 0.; correctionY_1 = residualWidthY_1 = -0.0010;
619 fitMeanY_2 = 0.; correctionY_2 = residualWidthY_2 = -0.0010;
622 i_sector.second.MeanX ->SetBinContent(i_errBins.first,meanX);
623 i_sector.second.RmsX ->SetBinContent(i_errBins.first,rmsX);
625 i_sector.second.FitMeanX1 ->SetBinContent(i_errBins.first,fitMeanX_1);
626 i_sector.second.ResidualWidthX1->SetBinContent(i_errBins.first,residualWidthX_1);
627 i_sector.second.CorrectionX1 ->SetBinContent(i_errBins.first,correctionX_1*10000.);
629 i_sector.second.FitMeanX2 ->SetBinContent(i_errBins.first,fitMeanX_2);
630 i_sector.second.ResidualWidthX2->SetBinContent(i_errBins.first,residualWidthX_2);
631 i_sector.second.CorrectionX2 ->SetBinContent(i_errBins.first,correctionX_2*10000.);
633 if(i_sector.second.isPixel){
634 i_sector.second.MeanY ->SetBinContent(i_errBins.first,meanY);
635 i_sector.second.RmsY ->SetBinContent(i_errBins.first,rmsY);
637 i_sector.second.FitMeanY1 ->SetBinContent(i_errBins.first,fitMeanY_1);
638 i_sector.second.ResidualWidthY1->SetBinContent(i_errBins.first,residualWidthY_1);
639 i_sector.second.CorrectionY1 ->SetBinContent(i_errBins.first,correctionY_1*10000.);
641 i_sector.second.FitMeanY2 ->SetBinContent(i_errBins.first,fitMeanY_2);
642 i_sector.second.ResidualWidthY2->SetBinContent(i_errBins.first,residualWidthY_2);
643 i_sector.second.CorrectionY2 ->SetBinContent(i_errBins.first,correctionY_2*10000.);
648 if(entriesX<minHitsPerInterval && entriesY<minHitsPerInterval)
continue;
659 weightX = entriesX/(meanSigmaX*meanSigmaX);
660 weightY = entriesY/(meanSigmaY*meanSigmaY);
663 const Error2AndResidualWidth2PerBin error2AndResidualWidth2PerBinX(meanSigmaX*meanSigmaX, residualWidthX_1*residualWidthX_1);
664 const WeightAndResultsPerBin weightAndResultsPerBinX(weightX, error2AndResidualWidth2PerBinX);
665 if(!(entriesX<minHitsPerInterval)){
667 i_sector.second.WeightX->SetBinContent(i_errBins.first,weightX);
668 v_weightAndResultsPerBinX.push_back(weightAndResultsPerBinX);
671 const Error2AndResidualWidth2PerBin error2AndResidualWidth2PerBinY(meanSigmaY*meanSigmaY, residualWidthY_1*residualWidthY_1);
672 const WeightAndResultsPerBin weightAndResultsPerBinY(weightY, error2AndResidualWidth2PerBinY);
673 if(!(entriesY<minHitsPerInterval)){
675 i_sector.second.WeightY->SetBinContent(i_errBins.first,weightY);
676 v_weightAndResultsPerBinY.push_back(weightAndResultsPerBinY);
683 if(v_weightAndResultsPerBinX.empty()){
684 edm::LogError(
"CalculateAPE")<<
"NO error interval of sector "<<i_sector.first<<
" has a valid x APE calculated,\n...so cannot set APE";
688 if(i_sector.second.isPixel && v_weightAndResultsPerBinY.empty()){
689 edm::LogError(
"CalculateAPE")<<
"NO error interval of sector "<<i_sector.first<<
" has a valid y APE calculated,\n...so cannot set APE";
693 double correctionX2(999.);
694 double correctionY2(999.);
697 double weightSumX(0.);
698 for(
auto const & i_apeBin : v_weightAndResultsPerBinX){
699 weightSumX += i_apeBin.first;
701 i_sector.second.WeightX->Scale(1/weightSumX);
702 double weightSumY(0.);
703 if(i_sector.second.isPixel){
704 for(
auto const & i_apeBin : v_weightAndResultsPerBinY){
705 weightSumY += i_apeBin.first;
707 i_sector.second.WeightY->Scale(1/weightSumY);
712 bool firstIntervalX(
true);
713 for(
auto const & i_apeBin : v_weightAndResultsPerBinX){
715 correctionX2 = i_apeBin.first*i_apeBin.second.first*(i_apeBin.second.second - baselineWidthX2);
716 firstIntervalX =
false;
718 correctionX2 += i_apeBin.first*i_apeBin.second.first*(i_apeBin.second.second - baselineWidthX2);
721 correctionX2 = correctionX2/weightSumX;
723 double numeratorX(0.), denominatorX(0.);
724 for(
auto const & i_apeBin : v_weightAndResultsPerBinX){
725 numeratorX += i_apeBin.first*i_apeBin.second.first*i_apeBin.second.second;
726 denominatorX += i_apeBin.first*i_apeBin.second.first;
728 correctionX2 = numeratorX/denominatorX;
731 if(i_sector.second.isPixel){
734 bool firstIntervalY(
true);
735 for(
auto const & i_apeBin : v_weightAndResultsPerBinY){
737 correctionY2 = i_apeBin.first*i_apeBin.second.first*(i_apeBin.second.second - baselineWidthY2);
738 firstIntervalY =
false;
740 correctionY2 += i_apeBin.first*i_apeBin.second.first*(i_apeBin.second.second - baselineWidthY2);
743 correctionY2 = correctionY2/weightSumY;
745 double numeratorY(0.), denominatorY(0.);
746 for(
auto const & i_apeBin : v_weightAndResultsPerBinY){
747 numeratorY += i_apeBin.first*i_apeBin.second.first*i_apeBin.second.second;
748 denominatorY += i_apeBin.first*i_apeBin.second.first;
750 correctionY2 = numeratorY/denominatorY;
765 apeX2 = a_apeSectorX[i_sector.first];
766 apeY2 = a_apeSectorY[i_sector.first];
768 const double apeX2old = apeX2;
769 const double apeY2old = apeY2;
772 edm::LogInfo(
"CalculateAPE")<<
"Unscaled correction x for sector "<<i_sector.first<<
" is "<<(correctionX2>0. ? +1. : -1.)*
std::sqrt(std::fabs(correctionX2));
773 if(!smoothIteration || firstIter)correctionX2 = correctionX2*correctionScaling*correctionScaling;
774 if(i_sector.second.isPixel){
775 edm::LogInfo(
"CalculateAPE")<<
"Unscaled correction y for sector "<<i_sector.first<<
" is "<<(correctionY2>0. ? +1. : -1.)*
std::sqrt(std::fabs(correctionY2));
776 if(!smoothIteration || firstIter)correctionY2 = correctionY2*correctionScaling*correctionScaling;
781 if(apeX2 + correctionX2 < 0.) correctionX2 = -apeX2;
782 if(apeY2 + correctionY2 < 0.) correctionY2 = -apeY2;
783 const double apeX2new(apeX2old + correctionX2);
784 const double apeY2new(apeY2old + correctionY2);
785 if(!smoothIteration || firstIter){
789 const double apeXtmp = smoothFraction*
std::sqrt(apeX2new) + (1-smoothFraction)*
std::sqrt(apeX2old);
790 const double apeYtmp = smoothFraction*
std::sqrt(apeY2new) + (1-smoothFraction)*
std::sqrt(apeY2old);
791 apeX2 = apeXtmp*apeXtmp;
792 apeY2 = apeYtmp*apeYtmp;
794 if(apeX2<0. || apeY2<0.)
edm::LogError(
"CalculateAPE")<<
"\n\n\tBad APE, but why???\n\n\n";
795 a_apeSectorX[i_sector.first] = apeX2;
796 a_apeSectorY[i_sector.first] = apeY2;
801 const double apeZ(
std::sqrt(0.5*(apeX2+apeY2)));
802 for(
auto const & i_rawId : i_sector.second.v_rawId){
803 if(i_sector.second.isPixel){
804 apeOutputFile<<i_rawId<<
" "<<
std::fixed<<std::setprecision(5)<<apeX<<
" "<<apeY<<
" "<<apeZ<<
"\n";
806 apeOutputFile<<i_rawId<<
" "<<
std::fixed<<std::setprecision(5)<<apeX<<
" "<<apeX<<
" "<<apeX<<
"\n";
810 a_apeSectorX[i_sector.first] = correctionX2;
811 a_apeSectorY[i_sector.first] = correctionY2;
814 if(!setBaseline)apeOutputFile.close();
816 iterationTreeX->Fill();
817 iterationTreeX->Write(
"iterTreeX", TObject::kOverwrite);
818 iterationTreeY->Fill();
819 iterationTreeY->Write(
"iterTreeY", TObject::kOverwrite);
821 sectorNameTree->Fill();
822 sectorNameTree->Write(
"nameTree");
824 iterationFile->Close();
828 baselineFile->Close();
851 TFile* baselineFile(
nullptr);
852 TTree* sectorNameBaselineTree(
nullptr);
854 std::ifstream baselineFileStream;
856 baselineFileStream.open(baselineFileName.c_str());
857 if(baselineFileStream.is_open()){
858 baselineFileStream.close();
859 baselineFile =
new TFile(baselineFileName.c_str(),
"READ");
862 edm::LogInfo(
"CalculateAPE")<<
"Baseline file for APE values sucessfully opened";
863 baselineFile->GetObject(
"nameTree;1",sectorNameBaselineTree);
865 edm::LogWarning(
"CalculateAPE")<<
"There is NO baseline file for APE values, so normalized residual width =1 for ideal conditions is assumed";
871 TFile* defaultFile =
new TFile(defaultFileName.c_str(),
"RECREATE");
874 TTree* defaultTreeX(
nullptr);
875 TTree* defaultTreeY(
nullptr);
876 defaultFile->GetObject(
"iterTreeX;1",defaultTreeX);
877 defaultFile->GetObject(
"iterTreeY;1",defaultTreeY);
879 TTree* sectorNameTree(
nullptr);
880 defaultFile->GetObject(
"nameTree;1",sectorNameTree);
882 bool firstIter(
false);
885 defaultTreeX =
new TTree(
"iterTreeX",
"Tree for default APE x values from GT");
886 defaultTreeY =
new TTree(
"iterTreeY",
"Tree for default APE y values from GT");
887 edm::LogInfo(
"CalculateAPE")<<
"First APE iteration (number 0.), create default file with TTree";
888 sectorNameTree =
new TTree(
"nameTree",
"Tree with names of sectors");
890 edm::LogWarning(
"CalculateAPE")<<
"NOT the first APE iteration (number 0.), is this wanted or forgot to delete old iteration file with TTree?";
900 const unsigned int iSector(i_sector.first);
901 const bool pixelSector(i_sector.second.isPixel);
903 a_defaultSectorX[iSector] = -99.;
904 a_defaultSectorY[iSector] = -99.;
906 a_sectorName[iSector] =
nullptr;
907 a_sectorBaselineName[iSector] =
nullptr;
908 std::stringstream ss_sector, ss_sectorSuffixed;
909 ss_sector <<
"Ape_Sector_" << iSector;
910 if(!setBaseline && sectorNameBaselineTree){
911 sectorNameBaselineTree->SetBranchAddress(ss_sector.str().c_str(), &a_sectorBaselineName[iSector]);
912 sectorNameBaselineTree->GetEntry(0);
916 ss_sectorSuffixed << ss_sector.str() <<
"/D";
917 defaultTreeX->Branch(ss_sector.str().c_str(), &a_defaultSectorX[iSector], ss_sectorSuffixed.str().c_str());
919 defaultTreeY->Branch(ss_sector.str().c_str(), &a_defaultSectorY[iSector], ss_sectorSuffixed.str().c_str());
921 sectorNameTree->Branch(ss_sector.str().c_str(), &a_sectorName[iSector], 32000, 00);
923 defaultTreeX->SetBranchAddress(ss_sector.str().c_str(), &a_defaultSectorX[iSector]);
924 defaultTreeX->GetEntry(0);
926 defaultTreeY->SetBranchAddress(ss_sector.str().c_str(), &a_defaultSectorY[iSector]);
927 defaultTreeY->GetEntry(0);
929 sectorNameTree->SetBranchAddress(ss_sector.str().c_str(), &a_sectorName[iSector]);
930 sectorNameTree->GetEntry(0);
936 for(
auto & i_sector : m_tkSector_){
939 const std::string& nameLastIter(*a_sectorName[i_sector.first]);
940 if(name!=nameLastIter){
941 edm::LogError(
"CalculateAPE")<<
"Inconsistent sector definition in iterationFile for sector "<<i_sector.first<<
",\n" 942 <<
"Recent iteration has name \""<<name<<
"\", while previous had \""<<nameLastIter<<
"\"\n" 943 <<
"...APE calculation stopped. Please check sector definitions in config!\n";
947 else a_sectorName[i_sector.first] =
new std::string(name);
948 if(!setBaseline && baselineFile){
949 const std::string& nameBaseline(*a_sectorBaselineName[i_sector.first]);
950 if(name!=nameBaseline){
951 edm::LogError(
"CalculateAPE")<<
"Inconsistent sector definition in baselineFile for sector "<<i_sector.first<<
",\n" 952 <<
"Recent iteration has name \""<<name<<
"\", while baseline had \""<<nameBaseline<<
"\"\n" 953 <<
"...APE calculation stopped. Please check sector definitions in config!\n";
960 for(
auto & i_sector : m_tkSector_){
961 double defaultApeX(0.);
962 double defaultApeY(0.);
963 unsigned int nModules(0);
964 for(
auto const & i_rawId : i_sector.second.v_rawId){
965 std::vector<AlignTransformErrorExtended> alignErrors = alignmentErrors->
m_alignError;
966 for(std::vector<AlignTransformErrorExtended>::const_iterator i_alignError = alignErrors.begin(); i_alignError != alignErrors.end(); ++i_alignError){
967 if(i_rawId == i_alignError->rawId()){
968 CLHEP::HepSymMatrix errMatrix = i_alignError->matrix();
969 defaultApeX += errMatrix[0][0];
970 defaultApeY += errMatrix[1][1];
975 a_defaultSectorX[i_sector.first] = defaultApeX/nModules;
976 a_defaultSectorY[i_sector.first] = defaultApeY/nModules;
978 sectorNameTree->Fill();
979 sectorNameTree->Write(
"nameTree");
980 defaultTreeX->Fill();
981 defaultTreeX->Write(
"iterTreeX");
982 defaultTreeY->Fill();
983 defaultTreeY->Write(
"iterTreeY");
985 defaultFile->Close();
989 baselineFile->Close();
T getParameter(std::string const &) const
void analyze(const edm::Event &, const edm::EventSetup &) override
std::map< unsigned int, TrackerSectorStruct > m_tkSector_
#define DEFINE_FWK_MODULE(type)
~ApeEstimatorSummary() override
ApeEstimatorSummary(const edm::ParameterSet &)
void getTrackerSectorStructs()
std::vector< AlignTransformErrorExtended > m_alignError
std::map< unsigned int, std::map< std::string, TH1 * > > m_binnedHists
std::vector< double > residualErrorBinning()
const edm::ParameterSet parameterSet_
std::vector< unsigned int > v_rawId