Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:42

0001 
0002 int Color [] = {4,2,1,3,9,6,7,8,5};
0003 int Marker[] = {20,22,21,23,29,3,2};
0004 int Style [] = {1,2,5,7,9,10};
0005 
0006 
0007 TObject* GetObjectFromPath(TDirectory* File, string Path)
0008 {
0009    size_t pos = Path.find("/");
0010    if(pos < 256){
0011       string firstPart = Path.substr(0,pos);
0012       string endPart   = Path.substr(pos+1,Path.length());
0013       TDirectory* TMP = (TDirectory*)File->Get(firstPart.c_str());
0014       if(TMP!=NULL)return GetObjectFromPath(TMP,endPart);
0015 
0016       printf("BUG: %s\n",Path.c_str());
0017       return NULL;
0018    }else{
0019       return File->Get(Path.c_str());
0020    }
0021    
0022 }
0023 
0024 void SaveCanvas(TCanvas* c, string path, string name, bool OnlyPPNG=false){
0025    string filepath;
0026    filepath = path + "_" + name + ".png"; c->SaveAs(filepath.c_str()); if(OnlyPPNG)return;
0027    filepath = path + "_" + name + ".eps"; c->SaveAs(filepath.c_str());
0028    filepath = path + "_" + name + ".C"  ; c->SaveAs(filepath.c_str());
0029 }
0030 
0031 void DrawPreliminary(string TextToPrint, double X=0.28, double Y=0.98, double W=0.85, double H=0.95){
0032    TPaveText* T = new TPaveText(X,Y,W,H, "NDC");
0033    T->SetFillColor(0);
0034    T->SetTextAlign(11);
0035    T->AddText(TextToPrint.c_str());
0036 //   if(Type==0)T->AddText("CMS Preliminary 2010 :   #sqrt{s} = 7 TeV");
0037 //   if(Type==1)T->AddText("CMS Preliminary 2010 : MC with   #sqrt{s} = 7 TeV");
0038 //   if(Type==2)T->AddText("CMS Preliminary 2010 : Data with   #sqrt{s} = 7 TeV");
0039    T->Draw("same");
0040 }
0041 
0042 void DrawLegend (TObject** Histos, std::vector<string> legend, string Title, string StyleCase, double X=0.79, double Y=0.93, double W=0.20, double H=0.05)
0043 {
0044    int    N             = legend.size();
0045    
0046    if(legend[0]!=""){
0047       TLegend* leg;
0048       leg = new TLegend(X,Y,X-W,Y - N*H);
0049       leg->SetFillColor(0);
0050       leg->SetBorderSize(0);
0051       //leg->SetTextAlign(32);
0052       if(Title!="")leg->SetHeader(Title.c_str());
0053 
0054       if(StyleCase=="DataMC"){
0055          for(int i=0;i<N;i++){
0056             TH2D* temp = (TH2D*)Histos[i]->Clone();
0057             temp->SetMarkerSize(1.3);
0058             if(i==0){
0059                leg->AddEntry(temp, legend[i].c_str() ,"P");
0060             }else{
0061                leg->AddEntry(temp, legend[i].c_str() ,"L");
0062             }
0063          }
0064       }else{
0065          for(int i=0;i<N;i++){
0066             TH2D* temp = (TH2D*)Histos[i]->Clone();
0067             temp->SetMarkerSize(1.3);
0068             leg->AddEntry(temp, legend[i].c_str() ,StyleCase.c_str());
0069          }
0070       }
0071       leg->Draw();
0072    }
0073 } 
0074 
0075 
0076 void DrawStatBox(TObject** Histos, std::vector<string> legend, bool Mean, double X=0.15, double Y=0.93, double W=0.15, double H=0.03)
0077 {  
0078    int    N             = legend.size();
0079    char   buffer[255];
0080 
0081    if(Mean)H*=3;
0082    for(int i=0;i<N;i++){
0083            TPaveText* stat = new TPaveText(X,Y-(i*H), X+W, Y-(i+1)*H, "NDC");
0084            TH1* Histo = (TH1*)Histos[i];
0085            sprintf(buffer,"Entries : %i\n",(int)Histo->GetEntries());
0086            stat->AddText(buffer);
0087 
0088            if(Mean){
0089            sprintf(buffer,"Mean    : %6.2f\n",Histo->GetMean());
0090            stat->AddText(buffer);
0091 
0092            sprintf(buffer,"RMS     : %6.2f\n",Histo->GetRMS());
0093            stat->AddText(buffer);
0094            }
0095 
0096            stat->SetFillColor(0);
0097            stat->SetLineColor(Color[i]);
0098            stat->SetTextColor(Color[i]);
0099            stat->SetBorderSize(0);
0100            stat->SetMargin(0.05);
0101            stat->SetTextAlign(12);
0102            stat->Draw();
0103    }
0104 }
0105 
0106 
0107 
0108 void DrawTH2D(TH2D** Histos, std::vector<string> legend, string StyleCase, string Xlegend, string Ylegend, double xmin, double xmax, double ymin, double ymax)
0109 {
0110    int    N             = legend.size();
0111    
0112    for(int i=0;i<N;i++){
0113         if(!Histos[i])continue;
0114         Histos[i]->SetTitle("");
0115         Histos[i]->SetStats(kFALSE);
0116         Histos[i]->GetXaxis()->SetTitle(Xlegend.c_str());
0117         Histos[i]->GetYaxis()->SetTitle(Ylegend.c_str());
0118         Histos[i]->GetYaxis()->SetTitleOffset(1.60);
0119         if(xmin!=xmax)Histos[i]->SetAxisRange(xmin,xmax,"X");
0120         if(ymin!=ymax)Histos[i]->SetAxisRange(ymin,ymax,"Y");
0121         Histos[i]->SetMarkerStyle(Marker[i]);
0122         Histos[i]->SetMarkerColor(Color[i]);
0123         Histos[i]->SetMarkerSize(0.3);
0124    }
0125 
0126    char Buffer[256];
0127    Histos[0]->Draw(StyleCase.c_str());
0128    for(int i=1;i<N;i++){
0129         sprintf(Buffer,"%s same",StyleCase.c_str());
0130         Histos[i]->Draw(Buffer);
0131    }
0132 }
0133 
0134 
0135 void DrawSuperposedHistos(TH1** Histos, std::vector<string> legend, string StyleCase,  string Xlegend, string Ylegend, double xmin, double xmax, double ymin, double ymax, bool Normalize=false)
0136 {
0137    int    N             = legend.size();
0138 
0139    double HistoMax      = -1;
0140    int    HistoHeighest = -1;
0141 
0142    for(int i=0;i<N;i++){
0143         if(!Histos[i])continue;
0144         if(Normalize && Histos[i]->Integral()!=0)Histos[i]->Scale(1.0/Histos[i]->Integral());
0145         Histos[i]->SetTitle("");
0146         Histos[i]->SetStats(kFALSE);
0147         Histos[i]->GetXaxis()->SetTitle(Xlegend.c_str());
0148         Histos[i]->GetYaxis()->SetTitle(Ylegend.c_str());
0149         Histos[i]->GetYaxis()->SetTitleOffset(1.70);
0150         if(xmin!=xmax)Histos[i]->SetAxisRange(xmin,xmax,"X");
0151         if(ymin!=ymax)Histos[i]->SetAxisRange(ymin,ymax,"Y");
0152         Histos[i]->SetFillColor(0);
0153         Histos[i]->SetMarkerStyle(Marker[i]);
0154         Histos[i]->SetMarkerColor(Color[i]);
0155         Histos[i]->SetMarkerSize(1.0);
0156         Histos[i]->SetLineColor(Color[i]);
0157         Histos[i]->SetLineWidth(2);
0158        if(StyleCase=="DataMC" && i==0){
0159            Histos[i]->SetFillColor(0);
0160            Histos[i]->SetMarkerStyle(20);
0161            Histos[i]->SetMarkerColor(1);
0162            Histos[i]->SetMarkerSize(1);
0163            Histos[i]->SetLineColor(1);
0164            Histos[i]->SetLineWidth(2);
0165        }
0166 
0167         if(Histos[i]->GetMaximum() >= HistoMax){
0168            HistoMax      = Histos[i]->GetMaximum();
0169            HistoHeighest = i;
0170         }
0171 
0172    }
0173 
0174    char Buffer[256];
0175    if(StyleCase=="DataMC"){
0176       if(HistoHeighest==0){
0177          Histos[HistoHeighest]->Draw("E1");
0178       }else{
0179          Histos[HistoHeighest]->Draw("HIST");
0180       }
0181       for(int i=0;i<N;i++){
0182            if(i==0){
0183               Histos[i]->Draw("same E1");
0184            }else{
0185               Histos[i]->Draw("same");
0186            }
0187       }
0188    }else{
0189       Histos[HistoHeighest]->Draw(StyleCase.c_str());
0190       for(int i=0;i<N;i++){
0191            if(StyleCase!=""){
0192               sprintf(Buffer,"same %s",StyleCase.c_str());
0193            }else{
0194               sprintf(Buffer,"same");
0195            }
0196            Histos[i]->Draw(Buffer);
0197       }
0198    }
0199 }
0200 
0201 
0202 
0203