Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:14:43

0001 
0002 #include <vector>
0003 
0004 #include "TROOT.h"
0005 #include "TFile.h"
0006 #include "TDirectory.h"
0007 #include "TChain.h"
0008 #include "TObject.h"
0009 #include "TCanvas.h"
0010 #include "TMath.h"
0011 #include "TLegend.h"
0012 #include "TGraph.h"
0013 #include "TH1.h"
0014 #include "TH2.h"
0015 #include "TH3.h"
0016 #include "TTree.h"
0017 #include "TF1.h"
0018 #include "TPaveText.h"
0019 #include "TPaveStats.h"
0020 #include "tdrstyle.C"
0021 
0022 int Color [] = {2,4,1,8,6,7,3,9,5};
0023 int Marker[] = {21,22,23,20,29,3,2};
0024 int Style [] = {1,2,5,7,9,10};
0025 
0026 char buffer[2048];
0027 
0028 
0029 char* FileName;
0030 
0031 void Core(char* SavePath);
0032 
0033 
0034 TObject* GetObjectFromPath(TDirectory* File, const char* Path);
0035 void SaveCanvas(TCanvas* c, const char* path, const char* name, bool OnlyPPNG=false);
0036 void DrawStatBox(TObject** Histos, std::vector<char*> legend, bool Mean               , double X=0.15, double Y=0.93, double W=0.15, double H=0.03);
0037 void DrawLegend (TObject** Histos, std::vector<char*> legend, char* Title, char* Style, double X=0.79, double Y=0.93, double W=0.20, double H=0.05);
0038 void DrawSuperposedHistos(TH1D** Histos, std::vector<char*> legend, char* Style,  char* Xlegend, char* Ylegend, double xmin, double xmax, double ymin, double ymax);
0039 void DrawTH2D   (TH2D**    Histos, std::vector<char*> legend, char* Style, char* Xlegend, char* Ylegend, double xmin, double xmax, double ymin, double ymax);
0040 
0041 void makePlot()
0042 {
0043    setTDRStyle();
0044    gStyle->SetPadLeftMargin  (0.18);
0045    gStyle->SetPadTopMargin   (0.05);
0046    gStyle->SetPadBottomMargin(0.10);
0047    gStyle->SetPadRightMargin (0.18);
0048    gStyle->SetTitleSize(0.04, "XYZ");
0049    gStyle->SetTitleXOffset(1.1);
0050    gStyle->SetTitleYOffset(1.35);
0051    gStyle->SetPalette(1);
0052    gStyle->SetCanvasColor(0);
0053    gStyle->SetBarOffset(0);
0054 
0055    TCanvas* c1;
0056    TObject** Histos = new TObject*[10];
0057    std::vector<char*> legend;
0058 
0059    FileName        = "ProbaMap.root";
0060    system("mkdir -p pictures");
0061    Core("pictures/");
0062 }
0063 
0064 
0065 void Core(char* SavePath)
0066 {
0067    TFile* f1               = new TFile(FileName);   
0068    TH3F*  Charge_Vs_Path3D = (TH3F*)GetObjectFromPath(f1,"Charge_Vs_Path");
0069    for(int x=0;x<15;x++){
0070       char xProjName[255];
0071       if(x==0){
0072          sprintf(xProjName,"%s","inc");
0073          Charge_Vs_Path3D->GetXaxis()->SetRange(0,15);
0074       }else{
0075          sprintf(xProjName,"%02i",x);
0076          Charge_Vs_Path3D->GetXaxis()->SetRange(x,x);
0077       }
0078       printf("---------------\n%s------------\n",xProjName);
0079       string xProjNameStr(xProjName);
0080 
0081 
0082       TH2D*  Charge_Vs_Path2D = (TH2D*)Charge_Vs_Path3D->Project3D("zy");
0083       double binMinA = Charge_Vs_Path2D->GetXaxis()->GetBinLowEdge(4);
0084       double binMaxA = Charge_Vs_Path2D->GetXaxis()->GetBinUpEdge(6);
0085 
0086       TH1D*  Charge_Vs_PathA  = (TH1D*)Charge_Vs_Path2D->ProjectionY("projA",4,6);
0087       Charge_Vs_PathA->Rebin(2);
0088       char ALegend[1024];sprintf(ALegend,"[%5.2f,%5.2f]",binMinA,binMaxA);
0089 
0090 
0091       double binMinB = Charge_Vs_Path2D->GetXaxis()->GetBinLowEdge(7);
0092       double binMaxB = Charge_Vs_Path2D->GetXaxis()->GetBinUpEdge(9);
0093       TH1D*  Charge_Vs_PathB  = (TH1D*)Charge_Vs_Path2D->ProjectionY("projB",7,9);
0094       Charge_Vs_PathB->Rebin(2);
0095       char BLegend[1024];sprintf(BLegend,"[%5.2f,%5.2f]",binMinB,binMaxB);
0096 
0097       double binMinC = Charge_Vs_Path2D->GetXaxis()->GetBinLowEdge(10);
0098       double binMaxC = Charge_Vs_Path2D->GetXaxis()->GetBinUpEdge(12);
0099       TH1D*  Charge_Vs_PathC  = (TH1D*)Charge_Vs_Path2D->ProjectionY("projC",10,12);
0100       Charge_Vs_PathC->Rebin(2);
0101       char CLegend[1024];sprintf(CLegend,"[%5.2f,%5.2f]",binMinC,binMaxC);
0102 
0103       double binMinD = Charge_Vs_Path2D->GetXaxis()->GetBinLowEdge(13);
0104       double binMaxD = Charge_Vs_Path2D->GetXaxis()->GetBinUpEdge(15);
0105       TH1D*  Charge_Vs_PathD  = (TH1D*)Charge_Vs_Path2D->ProjectionY("projD",13,15);
0106       Charge_Vs_PathD->Rebin(2);
0107       char DLegend[1024];sprintf(DLegend,"[%5.2f,%5.2f]",binMinD,binMaxD);
0108 
0109       printf("%f to %f\n",binMinA,binMaxA);
0110       printf("%f to %f\n",binMinB,binMaxB);
0111       printf("%f to %f\n",binMinC,binMaxC);
0112       printf("%f to %f\n",binMinD,binMaxD);
0113 
0114 
0115       TCanvas* c0;
0116       TObject** Histos = new TObject*[10]; 
0117       std::vector<char*> legend;
0118 
0119       c0  = new TCanvas("c0", "c0", 600,600);
0120       Charge_Vs_Path2D->SetTitle("");
0121       Charge_Vs_Path2D->SetStats(kFALSE);
0122       Charge_Vs_Path2D->GetXaxis()->SetTitle("pathlength (mm)");
0123       Charge_Vs_Path2D->GetYaxis()->SetTitle("#Delta E/#Delta x (ADC/mm)");
0124       Charge_Vs_Path2D->GetYaxis()->SetTitleOffset(1.80);
0125       Charge_Vs_Path2D->Draw("COLZ");
0126 
0127       c0->SetLogz(true);
0128       SaveCanvas(c0,SavePath,(xProjNameStr+"_TH2").c_str());
0129       delete c0;
0130 
0131 
0132       //Compute Probability Map.
0133       TH2D* Prob_ChargePath  = new TH2D ("Prob_ChargePath"     , "Prob_ChargePath" , Charge_Vs_Path2D->GetXaxis()->GetNbins(), Charge_Vs_Path2D->GetXaxis()->GetXmin(), Charge_Vs_Path2D->GetXaxis()->GetXmax(), Charge_Vs_Path2D->GetYaxis()->GetNbins(), Charge_Vs_Path2D->GetYaxis()->GetXmin(), Charge_Vs_Path2D->GetYaxis()->GetXmax());
0134       for(int j=0;j<=Prob_ChargePath->GetXaxis()->GetNbins()+1;j++){
0135      double Ni = 0;
0136      for(int k=0;k<=Prob_ChargePath->GetYaxis()->GetNbins()+1;k++){ Ni+=Charge_Vs_Path2D->GetBinContent(j,k);} 
0137 
0138      for(int k=0;k<=Prob_ChargePath->GetYaxis()->GetNbins()+1;k++){
0139         double tmp = 1E-10;
0140         for(int l=0;l<=k;l++){ tmp+=Charge_Vs_Path2D->GetBinContent(j,l);}
0141 
0142         if(Ni>0){
0143            Prob_ChargePath->SetBinContent (j, k, tmp/Ni);
0144         }else{
0145            Prob_ChargePath->SetBinContent (j, k, 0);
0146         }
0147      }
0148       }
0149 
0150       c0  = new TCanvas("c0", "c0", 600,600);
0151       Prob_ChargePath->SetTitle("Probability MIP(#DeltaE/#DeltaX) < Obs (#DeltaE/#DeltaX)");
0152       Prob_ChargePath->SetStats(kFALSE);
0153       Prob_ChargePath->GetXaxis()->SetTitle("pathlength (mm)");
0154       Prob_ChargePath->GetYaxis()->SetTitle("Observed #DeltaE/#DeltaX (ADC/mm)");
0155       Prob_ChargePath->GetYaxis()->SetTitleOffset(1.80);
0156       Prob_ChargePath->GetXaxis()->SetRangeUser(0.28,1.2);
0157 //      Prob_ChargePath->GetYaxis()->SetRangeUser(0,1000);
0158       Prob_ChargePath->Draw("COLZ");
0159 
0160       //c0->SetLogz(true);
0161       SaveCanvas(c0,SavePath,(xProjNameStr+"_TH2Proba").c_str());
0162       delete c0;
0163 
0164       c0 = new TCanvas("c1","c1,",600,600);          legend.clear();
0165       Histos[0] = Charge_Vs_PathA;                   legend.push_back(ALegend);
0166       Histos[1] = Charge_Vs_PathB;                   legend.push_back(BLegend);
0167       Histos[2] = Charge_Vs_PathC;                   legend.push_back(CLegend);
0168       Histos[3] = Charge_Vs_PathD;                   legend.push_back(DLegend);
0169       if(((TH1*)Histos[0])->Integral()>=1)((TH1*)Histos[0])->Scale(1/((TH1*)Histos[0])->Integral());
0170       if(((TH1*)Histos[1])->Integral()>=1)((TH1*)Histos[1])->Scale(1/((TH1*)Histos[1])->Integral());
0171       if(((TH1*)Histos[2])->Integral()>=1)((TH1*)Histos[2])->Scale(1/((TH1*)Histos[2])->Integral());
0172       if(((TH1*)Histos[3])->Integral()>=1)((TH1*)Histos[3])->Scale(1/((TH1*)Histos[3])->Integral());
0173    //   DrawSuperposedHistos((TH1D**)Histos, legend, "",  "Normalized Cluster Charge (ADC/mm)", "u.a.", 0,1200, 0,0);
0174       DrawSuperposedHistos((TH1D**)Histos, legend, "",  "Normalized Cluster Charge (ADC/mm)", "u.a.", 0,600, 0,0);
0175       DrawLegend(Histos,legend,"PathLength (mm):","L");
0176       c0->SetGridx(true);
0177       Charge_Vs_PathA->GetXaxis()->SetNdivisions(520);
0178       SaveCanvas(c0,SavePath,(xProjNameStr+"_TH1Linear").c_str());
0179       delete c0;
0180 
0181 
0182       c0 = new TCanvas("c1","c1,",600,600);          legend.clear();
0183       Histos[0] = Charge_Vs_PathA;                   legend.push_back(ALegend);
0184       Histos[1] = Charge_Vs_PathB;                   legend.push_back(BLegend);
0185       Histos[2] = Charge_Vs_PathC;                   legend.push_back(CLegend);
0186       Histos[3] = Charge_Vs_PathD;                   legend.push_back(DLegend);
0187       if(((TH1*)Histos[0])->Integral()>=1)((TH1*)Histos[0])->Scale(1/((TH1*)Histos[0])->Integral());
0188       if(((TH1*)Histos[1])->Integral()>=1)((TH1*)Histos[1])->Scale(1/((TH1*)Histos[1])->Integral());
0189       if(((TH1*)Histos[2])->Integral()>=1)((TH1*)Histos[2])->Scale(1/((TH1*)Histos[2])->Integral());
0190       if(((TH1*)Histos[3])->Integral()>=1)((TH1*)Histos[3])->Scale(1/((TH1*)Histos[3])->Integral());
0191       DrawSuperposedHistos((TH1D**)Histos, legend, "",  "Normalized Cluster Charge (ADC/mm)", "u.a.", 0,3000, 0,0);
0192 //      DrawLegend(Histos,legend,"PathLength (mm):","L");
0193       c0->SetLogy(true);
0194       SaveCanvas(c0,SavePath,(xProjNameStr+"_TH1").c_str());
0195       delete c0;
0196 
0197 
0198 
0199 
0200       delete Charge_Vs_Path2D;
0201       delete Charge_Vs_PathA;
0202       delete Charge_Vs_PathB;
0203       delete Charge_Vs_PathC;
0204       delete Charge_Vs_PathD;
0205       delete Prob_ChargePath;
0206 
0207    }
0208 }
0209 
0210 
0211 
0212 
0213 TObject* GetObjectFromPath(TDirectory* File, const char* Path)
0214 {
0215    string str(Path);
0216    size_t pos = str.find("/");
0217 
0218    if(pos < 256){
0219       string firstPart = str.substr(0,pos);
0220       string endPart   = str.substr(pos+1,str.length());
0221       TDirectory* TMP = (TDirectory*)File->Get(firstPart.c_str());
0222       if(TMP!=NULL)return GetObjectFromPath(TMP,endPart.c_str());
0223 
0224       printf("BUG\n");
0225       return NULL;
0226    }else{
0227       return File->Get(Path);
0228    }
0229 
0230 }
0231 
0232 
0233 
0234 void SaveCanvas(TCanvas* c, const char* path, const char* name, bool OnlyPPNG){
0235    char buff[1024];
0236    sprintf(buff,"%s/%s.png",path,name);  c->SaveAs(buff);   if(OnlyPPNG)return;
0237    sprintf(buff,"%s/%s.eps",path,name);  c->SaveAs(buff);
0238    sprintf(buff,"%s/%s.C"  ,path,name);  c->SaveAs(buff);
0239 }
0240 
0241 
0242 
0243 void DrawLegend(TObject** Histos, std::vector<char*> legend, char* Title, char* Style, double X, double Y, double W, double H)
0244 {
0245    int    N             = legend.size();
0246 
0247    if(strcmp(legend[0],"")!=0){
0248       TLegend* leg;
0249       leg = new TLegend(X,Y,X-W,Y - N*H);
0250       leg->SetFillColor(0);
0251       leg->SetBorderSize(0);
0252       //leg->SetTextAlign(32);
0253       if(strcmp(Title,"")!=0)leg->SetHeader(Title);
0254 
0255       if(strcmp(Style,"DataMC")==0){
0256          for(int i=0;i<N;i++){
0257             TH2D* temp = (TH2D*)Histos[i];//->Clone();
0258             temp->SetMarkerSize(1.3);
0259             if(i==0){
0260                leg->AddEntry(temp, legend[i] ,"P");
0261             }else{
0262                leg->AddEntry(temp, legend[i] ,"L");
0263             }
0264          }
0265       }else{
0266          for(int i=0;i<N;i++){
0267             TH2D* temp = (TH2D*)Histos[i];//->Clone();
0268             temp->SetMarkerSize(1.3);
0269             leg->AddEntry(temp, legend[i] ,Style);
0270          }
0271       }
0272       leg->Draw();
0273    }
0274 }
0275 
0276 
0277 void DrawStatBox(TObject** Histos, std::vector<char*> legend, bool Mean, double X, double Y, double W, double H)
0278 {
0279    int    N             = legend.size();
0280    char   buffer[255];
0281 
0282    if(Mean)H*=3;
0283    for(int i=0;i<N;i++){
0284            TPaveText* stat = new TPaveText(X,Y-(i*H), X+W, Y-(i+1)*H, "NDC");
0285            TH1* Histo = (TH1*)Histos[i];
0286            sprintf(buffer,"Entries : %i\n",(int)Histo->GetEntries());
0287            stat->AddText(buffer);
0288 
0289            if(Mean){
0290            sprintf(buffer,"Mean    : %6.2f\n",Histo->GetMean());
0291            stat->AddText(buffer);
0292 
0293            sprintf(buffer,"RMS     : %6.2f\n",Histo->GetRMS());
0294            stat->AddText(buffer);
0295            }
0296 
0297            stat->SetFillColor(0);
0298            stat->SetLineColor(Color[i]);
0299            stat->SetTextColor(Color[i]);
0300            stat->SetBorderSize(0);
0301            stat->SetMargin(0.05);
0302            stat->SetTextAlign(12);
0303            stat->Draw();
0304    }
0305 }
0306 
0307 
0308 
0309 void DrawTH2D(TH2D** Histos, std::vector<char*> legend, char* Style, char* Xlegend, char* Ylegend, double xmin, double xmax, double ymin, double ymax)
0310 {
0311    int    N             = legend.size();
0312 
0313    for(int i=0;i<N;i++){
0314         if(!Histos[i])continue;
0315         Histos[i]->SetTitle("");
0316         Histos[i]->SetStats(kFALSE);
0317         Histos[i]->GetXaxis()->SetTitle(Xlegend);
0318         Histos[i]->GetYaxis()->SetTitle(Ylegend);
0319         Histos[i]->GetYaxis()->SetTitleOffset(1.60);
0320         if(xmin!=xmax)Histos[i]->SetAxisRange(xmin,xmax,"X");
0321         if(ymin!=ymax)Histos[i]->SetAxisRange(ymin,ymax,"Y");
0322         Histos[i]->SetMarkerStyle(Marker[i]);
0323         Histos[i]->SetMarkerColor(Color[i]);
0324         Histos[i]->SetMarkerSize(0.3);
0325    }
0326 
0327    char Buffer[256];
0328    Histos[0]->Draw(Style);
0329    for(int i=1;i<N;i++){
0330         sprintf(Buffer,"%s same",Style);
0331         Histos[i]->Draw(Buffer);
0332    }
0333 }
0334 
0335 void DrawSuperposedHistos(TH1D** Histos, std::vector<char*> legend, char* Style,  char* Xlegend, char* Ylegend, double xmin, double xmax, double ymin, double ymax)
0336 {
0337    int    N             = legend.size();
0338 
0339    double HistoMax      = -1;
0340    int    HistoHeighest = -1;
0341 
0342    for(int i=0;i<N;i++){
0343         if(!Histos[i])continue;
0344         Histos[i]->SetTitle("");
0345         Histos[i]->SetStats(kFALSE);
0346         Histos[i]->GetXaxis()->SetTitle(Xlegend);
0347         Histos[i]->GetYaxis()->SetTitle(Ylegend);
0348         Histos[i]->GetXaxis()->SetTitleOffset(1.20);
0349         Histos[i]->GetYaxis()->SetTitleOffset(1.70);
0350         if(xmin!=xmax)Histos[i]->SetAxisRange(xmin,xmax,"X");
0351         if(ymin!=ymax)Histos[i]->SetAxisRange(ymin,ymax,"Y");
0352         Histos[i]->SetFillColor(0);
0353         Histos[i]->SetMarkerStyle(Marker[i]);
0354         Histos[i]->SetMarkerColor(Color[i]);
0355         Histos[i]->SetMarkerSize(0.5);
0356         Histos[i]->SetLineColor(Color[i]);
0357         Histos[i]->SetLineWidth(2);
0358        if(strcmp(Style,"DataMC")==0 && i==0){
0359            Histos[i]->SetFillColor(0);
0360            Histos[i]->SetMarkerStyle(20);
0361            Histos[i]->SetMarkerColor(1);
0362            Histos[i]->SetMarkerSize(1);
0363            Histos[i]->SetLineColor(1);
0364            Histos[i]->SetLineWidth(2);
0365        } 
0366 
0367         if(Histos[i]->GetMaximum() >= HistoMax){
0368            HistoMax      = Histos[i]->GetMaximum();
0369            HistoHeighest = i;
0370         }
0371 
0372    }
0373 
0374    char Buffer[256];
0375    if(strcmp(Style,"DataMC")==0){
0376       if(HistoHeighest==0){
0377          Histos[HistoHeighest]->Draw("E1");
0378       }else{
0379          Histos[HistoHeighest]->Draw("HIST");
0380       }
0381       for(int i=0;i<N;i++){        
0382            if(i==0){
0383               Histos[i]->Draw("same E1");
0384            }else{
0385               Histos[i]->Draw("same");
0386            }
0387       }
0388    }else{
0389       Histos[HistoHeighest]->Draw(Style);
0390       for(int i=0;i<N;i++){        
0391            if(strcmp(Style,"")!=0){
0392               sprintf(Buffer,"same %s",Style);
0393            }else{
0394               sprintf(Buffer,"same");
0395            }
0396            Histos[i]->Draw(Buffer);
0397       }
0398    }
0399 }