Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:57

0001 #include "TTree.h"
0002 #include "TText.h"
0003 #include "TGraph.h"
0004 #include "TFile.h"
0005 #include "TH1F.h"
0006 #include "TCanvas.h"
0007 #include "TLegend.h"
0008 #include "TPaveStats.h"
0009 
0010 TGraph* makegr(char* infileName, Double_t startPos = -300, Double_t step = 20, const Int_t n = 32, char* Det = "BPIX Half Barrels", char* ScanDirection = "delta z", char* plotVar = "Chi2", Double_t offset = 0, int lineColor = 1)
0011 {
0012 
0013     gStyle->SetPadLeftMargin(0.15);
0014     gStyle->SetPadRightMargin(0.15);
0015     gStyle->SetOptFit();
0016     gStyle->SetOptStat(1111);
0017     gStyle->SetTitleFontSize(0.05);
0018     gStyle->SetTitleOffset(2, "Y");
0019 
0020     TFile* inFile = TFile::Open(infileName);
0021 
0022     char mytreeN[64];
0023     Double_t zPos[n - 1];
0024     Int_t zPosI[n-1];
0025     Double_t chi2 = 0;
0026     Int_t nhit = 0;
0027     Double_t chi2Vec[n - 1];
0028     Int_t nhitVec[n - 1];
0029     Double_t chi2ph[n - 1];
0030 
0031     for (int iter = 2; iter <= n; iter++) {
0032         snprintf(mytreeN, sizeof(mytreeN), "T9_%d", iter);
0033         TTree* mytree = (TTree*)inFile->Get(mytreeN);
0034         mytree->SetBranchAddress("AlignableChi2", &chi2);
0035         mytree->SetBranchAddress("Nhit", &nhit);
0036         mytree->GetEntry(0);
0037         //T9_1 has Chi2 at iter0, T9_2 has Chi2 at iter1
0038         zPos[iter - 2] = startPos + step * (iter - 2);
0039         zPosI[iter - 2] = startPos + step * (iter - 2);
0040         chi2Vec[iter - 2] = chi2 - offset;
0041         nhitVec[iter - 2] = nhit;
0042         if (nhit != 0)
0043             chi2ph[iter - 2] = chi2 / nhit;
0044         else
0045             chi2ph[iter - 2] = 0;
0046         cout << ScanDirection << " position=" << zPos[iter - 2] << ", chi2=" << chi2Vec[iter - 2] << ", hit=" << nhitVec[iter - 2] << ", chi2 per hit=" << chi2ph[iter - 2] << endl;
0047     }
0048 
0049     TGraph* gr;
0050 
0051     if (strcmp(plotVar, "Chi2") == 0)
0052         gr = new TGraph(n - 1, zPos, chi2Vec);
0053     else if (strcmp(plotVar, "Chi2PerHit") == 0)
0054         gr = new TGraph(n - 1, zPos, chi2ph);
0055     else if (strcmp(plotVar,"nhits")==0)
0056         gr = new TGraph (n-1,zPosI,nhitVec);
0057 
0058     gr->SetLineColor(lineColor);
0059 
0060     char title[50];
0061     sprintf(title, "%s scan of %s in %s", plotVar, ScanDirection, Det);
0062 
0063     gr->SetTitle(title);
0064     gr->GetXaxis()->SetTitle(ScanDirection);
0065     gr->GetYaxis()->SetTitle(plotVar);
0066     return gr;
0067 }
0068 void plotchi2()
0069 {
0070 
0071     /************* START DEFINED BY USER *****************/
0072 
0073     char* infile = "/afs/cern.ch/cms/CAF/CMSALCA/ALCA_TRACKERALIGN2/HIP/public/AlignmentCamp2016/CMSSW_8_0_3/src/Alignment/HIPAlignmentAlgorithm/hp1609_scanBPIXz/main/IOUserVariables.root";
0074     Double_t start = -300, end = 300, step = 20; //unit is um, the number of iterations must be bigger than (end-start)/step+2
0075     char* detName = "BPIX_Half_Barrels"; //scanned detector
0076     char* scanDir = "delta_z"; //scan direction
0077 //    char* scanVar = "Chi2"; // choose from: Chi2, Chi2PerHit,nhits
0078     char* scanVar = "nhits"; // choose from: Chi2, Chi2PerHit,nhits
0079 
0080     /************** END DEFINED BY USER ******************/
0081 
0082     int n_iter = (end - start) / step + 2;
0083     TGraph* gr1 = makegr(infile, start, step, n_iter, detName, scanDir, scanVar);
0084 
0085     TCanvas* c = new TCanvas("c", "c", 1000, 10, 800, 800);
0086 
0087     c->cd();
0088     gr1->Draw("AC*");
0089 
0090     c->Update();
0091     TLine* l = new TLine(0, c->GetUymin(), 0, c->GetUymax());
0092     l->SetLineStyle(2);
0093     l->Draw();
0094 
0095     char plotName[50];
0096     sprintf(plotName, "%sscan_%s_%s.png", scanVar, scanDir, detName);
0097 
0098     c->SaveAs(plotName);
0099 
0100 }