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";
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();
T getParameter(std::string const &) const
std::map< unsigned int, TrackerSectorStruct > m_tkSector_
const edm::ParameterSet parameterSet_