40 #include "CLHEP/Matrix/SymMatrix.h" 54 #include "TDirectory.h" 125 std::ifstream inputFileStream;
127 inputFileStream.open(inputFileName.c_str());
128 if(inputFileStream.is_open()){
129 inputFile_ =
new TFile(inputFileName.c_str(),
"READ");
132 edm::LogInfo(
"CalculateAPE")<<
"Input file from loop over tracks and corresponding hits sucessfully opened";
136 <<
"...APE calculation stopped. Please check path of input file name in config:\n" 139 "Bad input file name");
148 TString pluginName(
inputFile_->GetListOfKeys()->At(0)->GetName());
151 TDirectory *sectorDir(
nullptr), *intervalDir(
nullptr);
152 bool sectorBool(
true);
153 for(
unsigned int iSector(1);sectorBool;++iSector){
154 std::stringstream sectorName, fullSectorName;
155 sectorName <<
"Sector_" << iSector <<
"/";
156 fullSectorName << pluginName << sectorName.str();
157 TString fullName(fullSectorName.str().c_str());
159 sectorDir = (TDirectory*)
inputFile_->TDirectory::GetDirectory(fullName);
162 sectorDir->GetObject(
"z_name;1", tkSector.
Name);
163 tkSector.
name = tkSector.
Name->GetTitle();
164 bool intervalBool(
true);
165 for(
unsigned int iInterval(1);intervalBool;++iInterval){
166 std::stringstream intervalName, fullIntervalName;
167 intervalName <<
"Interval_" << iInterval <<
"/";
168 fullIntervalName << fullSectorName.str() << intervalName.str();
169 fullName = fullIntervalName.str().c_str();
170 intervalDir = (TDirectory*)
inputFile_->TDirectory::GetDirectory(fullName);
172 intervalDir->GetObject(
"h_sigmaX;1", tkSector.
m_binnedHists[iInterval][
"sigmaX"]);
173 intervalDir->GetObject(
"h_norResX;1", tkSector.
m_binnedHists[iInterval][
"norResX"]);
174 intervalDir->GetObject(
"h_sigmaY;1", tkSector.
m_binnedHists[iInterval][
"sigmaY"]);
175 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(std::vector<unsigned int>::const_iterator i_rawId = tkSector.
v_rawId.begin(); i_rawId != tkSector.
v_rawId.end(); ++i_rawId){
202 if(rawId==*i_rawId)alreadyAdded =
true;
204 if(alreadyAdded)
break;
205 tkSector.
v_rawId.push_back(rawId);
212 edm::LogInfo(
"CalculateAPE")<<
"There are "<<iSector-1<<
" sectors defined in input file";
222 for(std::map<unsigned int,TrackerSectorStruct>::iterator i_sector=
m_tkSector_.begin(); i_sector!=
m_tkSector_.end(); ++i_sector){
223 (*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]));
224 (*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]));
225 (*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]));
226 (*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]));
227 (*i_sector).second.ResidualWidthX1 =
new TH1F(
"h_residualWidthX1",
"residual width #Delta_{x};#sigma_{x} [#mum];#Delta_{x}",v_binX.size()-1,&(v_binX[0]));
228 (*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]));
229 (*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]));
230 (*i_sector).second.ResidualWidthX2 =
new TH1F(
"h_residualWidthX2",
"residual width #Delta_{x};#sigma_{x} [#mum];#Delta_{x}",v_binX.size()-1,&(v_binX[0]));
231 (*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]));
233 if((*i_sector).second.isPixel){
234 (*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]));
235 (*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]));
236 (*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]));
237 (*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]));
238 (*i_sector).second.ResidualWidthY1 =
new TH1F(
"h_residualWidthY1",
"residual width #Delta_{y};#sigma_{y} [#mum];#Delta_{y}",v_binX.size()-1,&(v_binX[0]));
239 (*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]));
240 (*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]));
241 (*i_sector).second.ResidualWidthY2 =
new TH1F(
"h_residualWidthY2",
"residual width #Delta_{y};#sigma_{y} [#mum];#Delta_{y}",v_binX.size()-1,&(v_binX[0]));
242 (*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]));
251 std::vector<double> v_binX;
253 for(
int iBin=1; iBin<=EntriesX->GetNbinsX()+1; ++iBin){
254 v_binX.push_back(EntriesX->GetBinLowEdge(iBin));
263 TDirectory* baseDir = resultsFile->mkdir(
"ApeEstimatorSummary");
264 for(std::map<unsigned int,TrackerSectorStruct>::const_iterator i_sector=
m_tkSector_.begin(); i_sector!=
m_tkSector_.end(); ++i_sector){
266 dirName<<
"Sector_" << (*i_sector).first;
267 TDirectory*
dir = baseDir->mkdir(dirName.str().c_str());
270 (*i_sector).second.Name->Write();
272 (*i_sector).second.WeightX->Write();
273 (*i_sector).second.MeanX->Write();
274 (*i_sector).second.RmsX->Write();
275 (*i_sector).second.FitMeanX1->Write();
276 (*i_sector).second.ResidualWidthX1 ->Write();
277 (*i_sector).second.CorrectionX1->Write();
278 (*i_sector).second.FitMeanX2->Write();
279 (*i_sector).second.ResidualWidthX2->Write();
280 (*i_sector).second.CorrectionX2->Write();
282 if((*i_sector).second.isPixel){
283 (*i_sector).second.WeightY->Write();
284 (*i_sector).second.MeanY->Write();
285 (*i_sector).second.RmsY->Write();
286 (*i_sector).second.FitMeanY1->Write();
287 (*i_sector).second.ResidualWidthY1 ->Write();
288 (*i_sector).second.CorrectionY1->Write();
289 (*i_sector).second.FitMeanY2->Write();
290 (*i_sector).second.ResidualWidthY2->Write();
291 (*i_sector).second.CorrectionY2->Write();
294 resultsFile->Close();
307 TFile* baselineFile(
nullptr);
308 TTree* baselineTreeX(
nullptr);
309 TTree* baselineTreeY(
nullptr);
310 TTree* sectorNameBaselineTree(
nullptr);
312 std::ifstream baselineFileStream;
314 baselineFileStream.open(baselineFileName.c_str());
315 if(baselineFileStream.is_open()){
316 baselineFileStream.close();
317 baselineFile =
new TFile(baselineFileName.c_str(),
"READ");
320 edm::LogInfo(
"CalculateAPE")<<
"Baseline file for APE values sucessfully opened";
321 baselineFile->GetObject(
"iterTreeX;1",baselineTreeX);
322 baselineFile->GetObject(
"iterTreeY;1",baselineTreeY);
323 baselineFile->GetObject(
"nameTree;1",sectorNameBaselineTree);
326 edm::LogWarning(
"CalculateAPE")<<
"There is NO baseline file for APE values, so normalized residual width =1 for ideal conditions is assumed";
333 TFile*
iterationFile =
new TFile(iterationFileName.c_str(),setBaseline ?
"RECREATE" :
"UPDATE");
337 TTree* iterationTreeX(
nullptr);
338 TTree* iterationTreeY(
nullptr);
339 iterationFile->GetObject(
"iterTreeX;1",iterationTreeX);
340 iterationFile->GetObject(
"iterTreeY;1",iterationTreeY);
342 TTree* sectorNameTree(
nullptr);
343 iterationFile->GetObject(
"nameTree;1",sectorNameTree);
345 bool firstIter(
false);
349 iterationTreeX =
new TTree(
"iterTreeX",
"Tree for APE x values of all iterations");
350 iterationTreeY =
new TTree(
"iterTreeY",
"Tree for APE y values of all iterations");
351 edm::LogInfo(
"CalculateAPE")<<
"First APE iteration (number 0.), create iteration file with TTree";
352 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");
362 const unsigned int iteration(iterationTreeX->GetEntries());
363 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?";
368 double a_apeSectorX[16589];
369 double a_apeSectorY[16589];
370 double a_baselineSectorX[16589];
371 double a_baselineSectorY[16589];
375 std::map<unsigned int, TrackerSectorStruct>::const_iterator i_sector;
377 const unsigned int iSector(i_sector->first);
378 const bool pixelSector(i_sector->second.isPixel);
379 a_apeSectorX[iSector] = 99.;
380 a_apeSectorY[iSector] = 99.;
381 a_baselineSectorX[iSector] = -99.;
382 a_baselineSectorY[iSector] = -99.;
384 a_sectorName[iSector] =
nullptr;
385 a_sectorBaselineName[iSector] =
nullptr;
386 std::stringstream ss_sector, ss_sectorSuffixed;
387 ss_sector <<
"Ape_Sector_" << iSector;
388 if(!setBaseline && baselineTreeX){
389 baselineTreeX->SetBranchAddress(ss_sector.str().c_str(), &a_baselineSectorX[iSector]);
390 baselineTreeX->GetEntry(0);
392 baselineTreeY->SetBranchAddress(ss_sector.str().c_str(), &a_baselineSectorY[iSector]);
393 baselineTreeY->GetEntry(0);
395 sectorNameBaselineTree->SetBranchAddress(ss_sector.str().c_str(), &a_sectorBaselineName[iSector]);
396 sectorNameBaselineTree->GetEntry(0);
400 a_baselineSectorX[iSector] = 1.;
401 a_baselineSectorY[iSector] = 1.;
404 ss_sectorSuffixed << ss_sector.str() <<
"/D";
405 iterationTreeX->Branch(ss_sector.str().c_str(), &a_apeSectorX[iSector], ss_sectorSuffixed.str().c_str());
407 iterationTreeY->Branch(ss_sector.str().c_str(), &a_apeSectorY[iSector], ss_sectorSuffixed.str().c_str());
409 sectorNameTree->Branch(ss_sector.str().c_str(), &a_sectorName[iSector], 32000, 00);
412 iterationTreeX->SetBranchAddress(ss_sector.str().c_str(), &a_apeSectorX[iSector]);
413 iterationTreeX->GetEntry(iterationTreeX->GetEntries()-1);
415 iterationTreeY->SetBranchAddress(ss_sector.str().c_str(), &a_apeSectorY[iSector]);
416 iterationTreeY->GetEntry(iterationTreeY->GetEntries()-1);
418 sectorNameTree->SetBranchAddress(ss_sector.str().c_str(), &a_sectorName[iSector]);
419 sectorNameTree->GetEntry(0);
425 for(std::map<unsigned int,TrackerSectorStruct>::iterator i_sector =
m_tkSector_.begin(); i_sector !=
m_tkSector_.end(); ++i_sector){
428 const std::string& nameLastIter(*a_sectorName[(*i_sector).first]);
429 if(name!=nameLastIter){
430 edm::LogError(
"CalculateAPE")<<
"Inconsistent sector definition in iterationFile for sector "<<i_sector->first<<
",\n" 431 <<
"Recent iteration has name \""<<name<<
"\", while previous had \""<<nameLastIter<<
"\"\n" 432 <<
"...APE calculation stopped. Please check sector definitions in config!\n";
436 else a_sectorName[(*i_sector).first] =
new std::string(name);
437 if(!setBaseline && baselineFile){
438 const std::string& nameBaseline(*a_sectorBaselineName[(*i_sector).first]);
439 if(name!=nameBaseline){
440 edm::LogError(
"CalculateAPE")<<
"Inconsistent sector definition in baselineFile for sector "<<i_sector->first<<
",\n" 441 <<
"Recent iteration has name \""<<name<<
"\", while baseline had \""<<nameBaseline<<
"\"\n" 442 <<
"...APE calculation stopped. Please check sector definitions in config!\n";
450 std::ofstream apeOutputFile;
453 apeOutputFile.open(apeOutputFileName.c_str());
454 if(apeOutputFile.is_open()){
455 edm::LogInfo(
"CalculateAPE")<<
"Text file for writing APE values successfully opened";
457 edm::LogError(
"CalculateAPE")<<
"Text file for writing APE values NOT opened,\n" 458 <<
"...APE calculation stopped. Please check path of text file name in config:\n" 459 <<
"\t"<<apeOutputFileName;
470 if(apeWeightName==
"unity") apeWeight =
wUnity;
471 else if(apeWeightName==
"entries")apeWeight =
wEntries;
474 edm::LogError(
"CalculateAPE")<<
"Invalid parameter 'apeWeight' in cfg file: \""<<apeWeightName
475 <<
"\"\nimplemented apeWeights are \"unity\", \"entries\", \"entriesOverSigmaX2\"" 476 <<
"\n...APE calculation stopped.";
484 if(smoothFraction<=0. || smoothFraction>1.){
485 edm::LogError(
"CalculateAPE")<<
"Incorrect parameter in cfg file," 486 <<
"\nsmoothFraction has to be in [0,1], but is "<<smoothFraction
487 <<
"\n...APE calculation stopped.";
490 for(std::map<unsigned int,TrackerSectorStruct>::iterator i_sector =
m_tkSector_.begin(); i_sector !=
m_tkSector_.end(); ++i_sector){
492 typedef std::pair<double,double> Error2AndResidualWidth2PerBin;
493 typedef std::pair<double,Error2AndResidualWidth2PerBin> WeightAndResultsPerBin;
494 std::vector<WeightAndResultsPerBin> v_weightAndResultsPerBinX;
495 std::vector<WeightAndResultsPerBin> v_weightAndResultsPerBinY;
498 double baselineWidthX2(a_baselineSectorX[(*i_sector).first]);
499 double baselineWidthY2(a_baselineSectorY[(*i_sector).first]);
503 for(
std::map<
unsigned int, std::map<std::string,TH1*> >::const_iterator i_errBins = (*i_sector).second.m_binnedHists.begin();
504 i_errBins != (*i_sector).second.m_binnedHists.end(); ++i_errBins){
505 std::map<std::string,TH1*> mHists = (*i_errBins).second;
507 double entriesX = mHists[
"sigmaX"]->GetEntries();
508 double meanSigmaX = mHists[
"sigmaX"]->GetMean();
511 double xMin = mHists[
"norResX"]->GetXaxis()->GetXmin();
512 double xMax = mHists[
"norResX"]->GetXaxis()->GetXmax();
513 double integralX = mHists[
"norResX"]->Integral();
514 double meanX = mHists[
"norResX"]->GetMean();
515 double rmsX = mHists[
"norResX"]->GetRMS();
516 double maximumX = mHists[
"norResX"]->GetBinContent(mHists[
"norResX"]->GetMaximumBin());
520 TF1 funcX_1(
"mygausX",
"gaus", xMin, xMax);
521 funcX_1.SetParameters(maximumX, meanX, rmsX);
522 TString fitOpt(
"ILERQ");
523 if(integralX>minHitsPerInterval){
524 if(mHists[
"norResX"]->
Fit(&funcX_1, fitOpt)){
525 edm::LogWarning(
"CalculateAPE")<<
"Fit1 did not work: "<<mHists[
"norResX"]->Fit(&funcX_1, fitOpt);
528 LogDebug(
"CalculateAPE")<<
"FitResultX1\t"<<mHists[
"norResX"]->Fit(&funcX_1, fitOpt)<<
"\n";
530 Double_t meanX_1 = funcX_1.GetParameter(1);
531 Double_t sigmaX_1 = funcX_1.GetParameter(2);
535 TF1 funcX_2(
"mygausX2",
"gaus",meanX_1 - sigmaFactorFit*
TMath::Abs(sigmaX_1), meanX_1 + sigmaFactorFit*
TMath::Abs(sigmaX_1));
536 funcX_2.SetParameters(funcX_1.GetParameter(0),meanX_1,sigmaX_1);
537 if(integralX>minHitsPerInterval){
538 if(mHists[
"norResX"]->
Fit(&funcX_2, fitOpt)){
539 edm::LogWarning(
"CalculateAPE")<<
"Fit2 did not work for x : "<<mHists[
"norResX"]->Fit(&funcX_2, fitOpt);
542 LogDebug(
"CalculateAPE")<<
"FitResultX2\t"<<mHists[
"norResX"]->Fit(&funcX_2, fitOpt)<<
"\n";
544 Double_t meanX_2 = funcX_2.GetParameter(1);
545 Double_t sigmaX_2 = funcX_2.GetParameter(2);
550 double meanSigmaY(0.);
551 if((*i_sector).second.isPixel){
552 entriesY = mHists[
"sigmaY"]->GetEntries();
553 meanSigmaY = mHists[
"sigmaY"]->GetMean();
556 Double_t meanY_1(0.);
557 Double_t sigmaY_1(0.);
558 Double_t meanY_2(0.);
559 Double_t sigmaY_2(0.);
562 if((*i_sector).second.isPixel){
564 double yMin = mHists[
"norResY"]->GetXaxis()->GetXmin();
565 double yMax = mHists[
"norResY"]->GetXaxis()->GetXmax();
566 double integralY = mHists[
"norResY"]->Integral();
567 meanY = mHists[
"norResY"]->GetMean();
568 rmsY = mHists[
"norResY"]->GetRMS();
569 double maximumY = mHists[
"norResY"]->GetBinContent(mHists[
"norResY"]->GetMaximumBin());
572 TF1 funcY_1(
"mygausY",
"gaus", yMin, yMax);
573 funcY_1.SetParameters(maximumY, meanY, rmsY);
574 if(integralY>minHitsPerInterval){
575 if(mHists[
"norResY"]->
Fit(&funcY_1, fitOpt)){
576 edm::LogWarning(
"CalculateAPE")<<
"Fit1 did not work: "<<mHists[
"norResY"]->Fit(&funcY_1, fitOpt);
579 LogDebug(
"CalculateAPE")<<
"FitResultY1\t"<<mHists[
"norResY"]->Fit(&funcY_1, fitOpt)<<
"\n";
581 meanY_1 = funcY_1.GetParameter(1);
582 sigmaY_1 = funcY_1.GetParameter(2);
585 TF1 funcY_2(
"mygausY2",
"gaus",meanY_1 - sigmaFactorFit*
TMath::Abs(sigmaY_1), meanY_1 + sigmaFactorFit*
TMath::Abs(sigmaY_1));
586 funcY_2.SetParameters(funcY_1.GetParameter(0),meanY_1,sigmaY_1);
587 if(integralY>minHitsPerInterval){
588 if(mHists[
"norResY"]->
Fit(&funcY_2, fitOpt)){
589 edm::LogWarning(
"CalculateAPE")<<
"Fit2 did not work for y : "<<mHists[
"norResY"]->Fit(&funcY_2, fitOpt);
592 LogDebug(
"CalculateAPE")<<
"FitResultY2\t"<<mHists[
"norResY"]->Fit(&funcY_2, fitOpt)<<
"\n";
594 meanY_2 = funcY_2.GetParameter(1);
595 sigmaY_2 = funcY_2.GetParameter(2);
601 double fitMeanX_1(meanX_1), fitMeanX_2(meanX_2);
602 double residualWidthX_1(sigmaX_1), residualWidthX_2(sigmaX_2);
603 double fitMeanY_1(meanY_1), fitMeanY_2(meanY_2);
604 double residualWidthY_1(sigmaY_1), residualWidthY_2(sigmaY_2);
606 double correctionX2_1(-0.0010), correctionX2_2(-0.0010);
607 double correctionY2_1(-0.0010), correctionY2_2(-0.0010);
608 correctionX2_1 = meanSigmaX*meanSigmaX*(residualWidthX_1*residualWidthX_1 -baselineWidthX2);
609 correctionX2_2 = meanSigmaX*meanSigmaX*(residualWidthX_2*residualWidthX_2 -baselineWidthX2);
610 if((*i_sector).second.isPixel){
611 correctionY2_1 = meanSigmaY*meanSigmaY*(residualWidthY_1*residualWidthY_1 -baselineWidthY2);
612 correctionY2_2 = meanSigmaY*meanSigmaY*(residualWidthY_2*residualWidthY_2 -baselineWidthY2);
615 double correctionX_1 = correctionX2_1>=0. ?
std::sqrt(correctionX2_1) : -
std::sqrt(-correctionX2_1);
616 double correctionX_2 = correctionX2_2>=0. ?
std::sqrt(correctionX2_2) : -
std::sqrt(-correctionX2_2);
617 double correctionY_1 = correctionY2_1>=0. ?
std::sqrt(correctionY2_1) : -
std::sqrt(-correctionY2_1);
618 double correctionY_2 = correctionY2_2>=0. ?
std::sqrt(correctionY2_2) : -
std::sqrt(-correctionY2_2);
620 if(
isnan(correctionX_1))correctionX_1 = -0.0010;
621 if(
isnan(correctionX_2))correctionX_2 = -0.0010;
622 if(
isnan(correctionY_1))correctionY_1 = -0.0010;
623 if(
isnan(correctionY_2))correctionY_2 = -0.0010;
625 if(entriesX<minHitsPerInterval){
626 meanX = 0.; rmsX = -0.0010;
627 fitMeanX_1 = 0.; correctionX_1 = residualWidthX_1 = -0.0010;
628 fitMeanX_2 = 0.; correctionX_2 = residualWidthX_2 = -0.0010;
631 if(entriesY<minHitsPerInterval){
632 meanY = 0.; rmsY = -0.0010;
633 fitMeanY_1 = 0.; correctionY_1 = residualWidthY_1 = -0.0010;
634 fitMeanY_2 = 0.; correctionY_2 = residualWidthY_2 = -0.0010;
637 (*i_sector).second.MeanX ->SetBinContent((*i_errBins).first,meanX);
638 (*i_sector).second.RmsX ->SetBinContent((*i_errBins).first,rmsX);
640 (*i_sector).second.FitMeanX1 ->SetBinContent((*i_errBins).first,fitMeanX_1);
641 (*i_sector).second.ResidualWidthX1->SetBinContent((*i_errBins).first,residualWidthX_1);
642 (*i_sector).second.CorrectionX1 ->SetBinContent((*i_errBins).first,correctionX_1*10000.);
644 (*i_sector).second.FitMeanX2 ->SetBinContent((*i_errBins).first,fitMeanX_2);
645 (*i_sector).second.ResidualWidthX2->SetBinContent((*i_errBins).first,residualWidthX_2);
646 (*i_sector).second.CorrectionX2 ->SetBinContent((*i_errBins).first,correctionX_2*10000.);
648 if((*i_sector).second.isPixel){
649 (*i_sector).second.MeanY ->SetBinContent((*i_errBins).first,meanY);
650 (*i_sector).second.RmsY ->SetBinContent((*i_errBins).first,rmsY);
652 (*i_sector).second.FitMeanY1 ->SetBinContent((*i_errBins).first,fitMeanY_1);
653 (*i_sector).second.ResidualWidthY1->SetBinContent((*i_errBins).first,residualWidthY_1);
654 (*i_sector).second.CorrectionY1 ->SetBinContent((*i_errBins).first,correctionY_1*10000.);
656 (*i_sector).second.FitMeanY2 ->SetBinContent((*i_errBins).first,fitMeanY_2);
657 (*i_sector).second.ResidualWidthY2->SetBinContent((*i_errBins).first,residualWidthY_2);
658 (*i_sector).second.CorrectionY2 ->SetBinContent((*i_errBins).first,correctionY_2*10000.);
663 if(entriesX<minHitsPerInterval && entriesY<minHitsPerInterval)
continue;
676 weightX = entriesX/(meanSigmaX*meanSigmaX);
677 weightY = entriesY/(meanSigmaY*meanSigmaY);
680 const Error2AndResidualWidth2PerBin error2AndResidualWidth2PerBinX(meanSigmaX*meanSigmaX, residualWidthX_1*residualWidthX_1);
681 const WeightAndResultsPerBin weightAndResultsPerBinX(weightX, error2AndResidualWidth2PerBinX);
682 if(!(entriesX<minHitsPerInterval)){
684 (*i_sector).second.WeightX->SetBinContent((*i_errBins).first,weightX);
685 v_weightAndResultsPerBinX.push_back(weightAndResultsPerBinX);
688 const Error2AndResidualWidth2PerBin error2AndResidualWidth2PerBinY(meanSigmaY*meanSigmaY, residualWidthY_1*residualWidthY_1);
689 const WeightAndResultsPerBin weightAndResultsPerBinY(weightY, error2AndResidualWidth2PerBinY);
690 if(!(entriesY<minHitsPerInterval)){
692 (*i_sector).second.WeightY->SetBinContent((*i_errBins).first,weightY);
693 v_weightAndResultsPerBinY.push_back(weightAndResultsPerBinY);
700 if(v_weightAndResultsPerBinX.empty()){
701 edm::LogError(
"CalculateAPE")<<
"NO error interval of sector "<<(*i_sector).first<<
" has a valid x APE calculated,\n...so cannot set APE";
705 if((*i_sector).second.isPixel && v_weightAndResultsPerBinY.empty()){
706 edm::LogError(
"CalculateAPE")<<
"NO error interval of sector "<<(*i_sector).first<<
" has a valid y APE calculated,\n...so cannot set APE";
710 double correctionX2(999.);
711 double correctionY2(999.);
714 double weightSumX(0.);
715 std::vector<WeightAndResultsPerBin>::const_iterator i_apeBin;
716 for(i_apeBin=v_weightAndResultsPerBinX.begin(); i_apeBin!=v_weightAndResultsPerBinX.end(); ++i_apeBin){
717 weightSumX += i_apeBin->first;
719 (*i_sector).second.WeightX->Scale(1/weightSumX);
720 double weightSumY(0.);
721 if((*i_sector).second.isPixel){
722 std::vector<WeightAndResultsPerBin>::const_iterator i_apeBin;
723 for(i_apeBin=v_weightAndResultsPerBinY.begin(); i_apeBin!=v_weightAndResultsPerBinY.end(); ++i_apeBin){
724 weightSumY += i_apeBin->first;
726 (*i_sector).second.WeightY->Scale(1/weightSumY);
732 bool firstIntervalX(
true);
733 for(i_apeBin=v_weightAndResultsPerBinX.begin(); i_apeBin!=v_weightAndResultsPerBinX.end(); ++i_apeBin){
735 correctionX2 = i_apeBin->first*i_apeBin->second.first*(i_apeBin->second.second - baselineWidthX2);
736 firstIntervalX =
false;
739 correctionX2 += i_apeBin->first*i_apeBin->second.first*(i_apeBin->second.second - baselineWidthX2);
742 correctionX2 = correctionX2/weightSumX;
745 double numeratorX(0.), denominatorX(0.);
746 std::vector<WeightAndResultsPerBin>::const_iterator i_apeBin;
747 for(i_apeBin=v_weightAndResultsPerBinX.begin(); i_apeBin!=v_weightAndResultsPerBinX.end(); ++i_apeBin){
748 numeratorX += i_apeBin->first*i_apeBin->second.first*i_apeBin->second.second;
749 denominatorX += i_apeBin->first*i_apeBin->second.first;
751 correctionX2 = numeratorX/denominatorX;
754 if((*i_sector).second.isPixel){
757 bool firstIntervalY(
true);
758 for(i_apeBin=v_weightAndResultsPerBinY.begin(); i_apeBin!=v_weightAndResultsPerBinY.end(); ++i_apeBin){
760 correctionY2 = i_apeBin->first*i_apeBin->second.first*(i_apeBin->second.second - baselineWidthY2);
761 firstIntervalY =
false;
764 correctionY2 += i_apeBin->first*i_apeBin->second.first*(i_apeBin->second.second - baselineWidthY2);
767 correctionY2 = correctionY2/weightSumY;
770 double numeratorY(0.), denominatorY(0.);
771 std::vector<WeightAndResultsPerBin>::const_iterator i_apeBin;
772 for(i_apeBin=v_weightAndResultsPerBinY.begin(); i_apeBin!=v_weightAndResultsPerBinY.end(); ++i_apeBin){
773 numeratorY += i_apeBin->first*i_apeBin->second.first*i_apeBin->second.second;
774 denominatorY += i_apeBin->first*i_apeBin->second.first;
776 correctionY2 = numeratorY/denominatorY;
793 apeX2 = a_apeSectorX[(*i_sector).first];
794 apeY2 = a_apeSectorY[(*i_sector).first];
796 const double apeX2old = apeX2;
797 const double apeY2old = apeY2;
800 edm::LogInfo(
"CalculateAPE")<<
"Unscaled correction x for sector "<<(*i_sector).first<<
" is "<<(correctionX2>0. ? +1. : -1.)*
std::sqrt(std::fabs(correctionX2));
801 if(!smoothIteration || firstIter)correctionX2 = correctionX2*correctionScaling*correctionScaling;
802 if((*i_sector).second.isPixel){
803 edm::LogInfo(
"CalculateAPE")<<
"Unscaled correction y for sector "<<(*i_sector).first<<
" is "<<(correctionY2>0. ? +1. : -1.)*
std::sqrt(std::fabs(correctionY2));
804 if(!smoothIteration || firstIter)correctionY2 = correctionY2*correctionScaling*correctionScaling;
809 if(apeX2 + correctionX2 < 0.) correctionX2 = -apeX2;
810 if(apeY2 + correctionY2 < 0.) correctionY2 = -apeY2;
811 const double apeX2new(apeX2old + correctionX2);
812 const double apeY2new(apeY2old + correctionY2);
813 if(!smoothIteration || firstIter){
821 if(apeX2<0. || apeY2<0.)
edm::LogError(
"CalculateAPE")<<
"\n\n\tBad APE, but why???\n\n\n";
822 a_apeSectorX[(*i_sector).first] = apeX2;
823 a_apeSectorY[(*i_sector).first] = apeY2;
828 const double apeZ(
std::sqrt(0.5*(apeX2+apeY2)));
829 std::vector<unsigned int>::const_iterator i_rawId;
830 for(i_rawId = (*i_sector).second.v_rawId.begin(); i_rawId != (*i_sector).second.v_rawId.end(); ++i_rawId){
831 if((*i_sector).second.isPixel){
832 apeOutputFile<<*i_rawId<<
" "<<
std::fixed<<std::setprecision(5)<<apeX<<
" "<<apeY<<
" "<<apeZ<<
"\n";
835 apeOutputFile<<*i_rawId<<
" "<<
std::fixed<<std::setprecision(5)<<apeX<<
" "<<apeX<<
" "<<apeX<<
"\n";
841 a_apeSectorX[(*i_sector).first] = correctionX2;
842 a_apeSectorY[(*i_sector).first] = correctionY2;
845 if(!setBaseline)apeOutputFile.close();
847 iterationTreeX->Fill();
848 iterationTreeX->Write(
"iterTreeX", TObject::kOverwrite);
849 iterationTreeY->Fill();
850 iterationTreeY->Write(
"iterTreeY", TObject::kOverwrite);
852 sectorNameTree->Fill();
853 sectorNameTree->Write(
"nameTree");
855 iterationFile->Close();
857 if(baselineFile)baselineFile->Close();
878 TFile* baselineFile(
nullptr);
879 TTree* sectorNameBaselineTree(
nullptr);
881 std::ifstream baselineFileStream;
883 baselineFileStream.open(baselineFileName.c_str());
884 if(baselineFileStream.is_open()){
885 baselineFileStream.close();
886 baselineFile =
new TFile(baselineFileName.c_str(),
"READ");
889 edm::LogInfo(
"CalculateAPE")<<
"Baseline file for APE values sucessfully opened";
890 baselineFile->GetObject(
"nameTree;1",sectorNameBaselineTree);
893 edm::LogWarning(
"CalculateAPE")<<
"There is NO baseline file for APE values, so normalized residual width =1 for ideal conditions is assumed";
899 TFile* defaultFile =
new TFile(defaultFileName.c_str(),
"RECREATE");
902 TTree* defaultTreeX(
nullptr);
903 TTree* defaultTreeY(
nullptr);
904 defaultFile->GetObject(
"iterTreeX;1",defaultTreeX);
905 defaultFile->GetObject(
"iterTreeY;1",defaultTreeY);
907 TTree* sectorNameTree(
nullptr);
908 defaultFile->GetObject(
"nameTree;1",sectorNameTree);
910 bool firstIter(
false);
913 defaultTreeX =
new TTree(
"iterTreeX",
"Tree for default APE x values from GT");
914 defaultTreeY =
new TTree(
"iterTreeY",
"Tree for default APE y values from GT");
915 edm::LogInfo(
"CalculateAPE")<<
"First APE iteration (number 0.), create default file with TTree";
916 sectorNameTree =
new TTree(
"nameTree",
"Tree with names of sectors");
919 edm::LogWarning(
"CalculateAPE")<<
"NOT the first APE iteration (number 0.), is this wanted or forgot to delete old iteration file with TTree?";
923 double a_defaultSectorX[16589];
924 double a_defaultSectorY[16589];
928 std::map<unsigned int, TrackerSectorStruct>::const_iterator i_sector;
930 const unsigned int iSector(i_sector->first);
931 const bool pixelSector(i_sector->second.isPixel);
933 a_defaultSectorX[iSector] = -99.;
934 a_defaultSectorY[iSector] = -99.;
936 a_sectorName[iSector] =
nullptr;
937 a_sectorBaselineName[iSector] =
nullptr;
938 std::stringstream ss_sector, ss_sectorSuffixed;
939 ss_sector <<
"Ape_Sector_" << iSector;
940 if(!setBaseline && sectorNameBaselineTree){
941 sectorNameBaselineTree->SetBranchAddress(ss_sector.str().c_str(), &a_sectorBaselineName[iSector]);
942 sectorNameBaselineTree->GetEntry(0);
946 ss_sectorSuffixed << ss_sector.str() <<
"/D";
947 defaultTreeX->Branch(ss_sector.str().c_str(), &a_defaultSectorX[iSector], ss_sectorSuffixed.str().c_str());
949 defaultTreeY->Branch(ss_sector.str().c_str(), &a_defaultSectorY[iSector], ss_sectorSuffixed.str().c_str());
951 sectorNameTree->Branch(ss_sector.str().c_str(), &a_sectorName[iSector], 32000, 00);
954 defaultTreeX->SetBranchAddress(ss_sector.str().c_str(), &a_defaultSectorX[iSector]);
955 defaultTreeX->GetEntry(0);
957 defaultTreeY->SetBranchAddress(ss_sector.str().c_str(), &a_defaultSectorY[iSector]);
958 defaultTreeY->GetEntry(0);
960 sectorNameTree->SetBranchAddress(ss_sector.str().c_str(), &a_sectorName[iSector]);
961 sectorNameTree->GetEntry(0);
967 for(std::map<unsigned int,TrackerSectorStruct>::iterator i_sector =
m_tkSector_.begin(); i_sector !=
m_tkSector_.end(); ++i_sector){
970 const std::string& nameLastIter(*a_sectorName[(*i_sector).first]);
971 if(name!=nameLastIter){
972 edm::LogError(
"CalculateAPE")<<
"Inconsistent sector definition in iterationFile for sector "<<i_sector->first<<
",\n" 973 <<
"Recent iteration has name \""<<name<<
"\", while previous had \""<<nameLastIter<<
"\"\n" 974 <<
"...APE calculation stopped. Please check sector definitions in config!\n";
978 else a_sectorName[(*i_sector).first] =
new std::string(name);
979 if(!setBaseline && baselineFile){
980 const std::string& nameBaseline(*a_sectorBaselineName[(*i_sector).first]);
981 if(name!=nameBaseline){
982 edm::LogError(
"CalculateAPE")<<
"Inconsistent sector definition in baselineFile for sector "<<i_sector->first<<
",\n" 983 <<
"Recent iteration has name \""<<name<<
"\", while baseline had \""<<nameBaseline<<
"\"\n" 984 <<
"...APE calculation stopped. Please check sector definitions in config!\n";
993 for(std::map<unsigned int,TrackerSectorStruct>::iterator i_sector =
m_tkSector_.begin(); i_sector !=
m_tkSector_.end(); ++i_sector){
995 double defaultApeX(0.);
996 double defaultApeY(0.);
997 unsigned int nModules(0);
998 std::vector<unsigned int>::const_iterator i_rawId;
999 for(i_rawId = (*i_sector).second.v_rawId.begin(); i_rawId != (*i_sector).second.v_rawId.end(); ++i_rawId){
1000 std::vector<AlignTransformErrorExtended> alignErrors = alignmentErrors->
m_alignError;
1001 for(std::vector<AlignTransformErrorExtended>::const_iterator i_alignError = alignErrors.begin(); i_alignError != alignErrors.end(); ++i_alignError){
1002 if(*i_rawId == i_alignError->rawId()){
1003 CLHEP::HepSymMatrix errMatrix = i_alignError->matrix();
1004 defaultApeX += errMatrix[0][0];
1005 defaultApeY += errMatrix[1][1];
1010 a_defaultSectorX[(*i_sector).first] = defaultApeX/nModules;
1011 a_defaultSectorY[(*i_sector).first] = defaultApeY/nModules;
1014 sectorNameTree->Fill();
1015 sectorNameTree->Write(
"nameTree");
1016 defaultTreeX->Fill();
1017 defaultTreeX->Write(
"iterTreeX");
1018 defaultTreeY->Fill();
1019 defaultTreeY->Write(
"iterTreeY");
1021 defaultFile->Close();
1022 if(baselineFile)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_
Power< A, B >::type pow(const A &a, const B &b)
std::vector< unsigned int > v_rawId