Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:45

0001 #include "HistoFinder.cc"
0002 #include "TLegend.h"
0003 #include "THStack.h"
0004 #include <vector>
0005 
0006 
0007 /**
0008  * Base class used by Normalized and Merged. It sets up the canvas and the legend.
0009  */
0010 class BaseNormalized {
0011  public:
0012   BaseNormalized() {}
0013   ~BaseNormalized() {}
0014   BaseNormalized( const TString & name, const TString & title ) :
0015     name_(name),
0016     title_(title),
0017     canvas_(new TCanvas(name+"_canvas",title+" canvas",1000,800)),
0018     legend_(new TLegend(0.779429,0.715556,0.979429,0.955556))
0019   {
0020     // legend_->SetHeader("");
0021     legend_->SetTextSize(0.04);
0022     // legend_->SetMarkerSize(1.4); // increase markersize so that they are visible
0023     legend_->SetFillColor(0); // Have a white background
0024   }
0025  protected:
0026   TString name_;
0027   TString title_;
0028   TCanvas * canvas_;  
0029   TLegend * legend_;
0030   std::vector<TH1*> histoList_;
0031   static Color_t colors_[4];
0032 };
0033 
0034 Color_t BaseNormalized::colors_[] = {kBlack, kRed, kBlue, kGreen};
0035 
0036 /**
0037  * This class draws hitograms so that they are normalized and with different colors.
0038  * It also adds a legend.
0039  */
0040 class Normalized : BaseNormalized {
0041  public:
0042   // Normalized() {}
0043   Normalized( const TString & name, const TString & title ) : BaseNormalized( name, title ) {}
0044   // virtual ~Normalized() {}
0045   void Add( TH1 * histo, const TString & name ) {
0046     histoList_.push_back(histo);
0047     legend_->AddEntry(histo, name);
0048   }
0049   // Do the loop here, so that we can use options like "errors"
0050   void Draw( const TString & xTitle = "", const TString & yTitle = "", const bool errors = false ) {
0051 
0052     // Create a new THStack so that it handle tha maximum
0053     // THStack stack(name_, title_);
0054     THStack * stack = new THStack(name_, title_);
0055 
0056     int colorIndex = 0;
0057     if( !(histoList_.empty()) ) {
0058       std::vector<TH1*>::iterator histoIter = histoList_.begin();
0059       for( ; histoIter != histoList_.end(); ++histoIter, ++colorIndex ) {
0060         TH1 * histo = *histoIter;
0061         if(errors) histo->Sumw2();
0062         // histo->SetNormFactor(1);
0063         if( colorIndex < 4 ) histo->SetLineColor(colors_[colorIndex]);
0064         else histo->SetLineColor(colorIndex);
0065         // Draw and get the maximum value
0066         TString normalizedHistoName(histo->GetName());
0067         TH1 * normalizedHisto = (TH1*)histo->Clone(normalizedHistoName+"clone");
0068         normalizedHisto->Scale(1/normalizedHisto->Integral());
0069         stack->Add(normalizedHisto);
0070       }
0071       // Take the maximum of all the drawed histograms
0072       // First we need to draw the histogram, or getAxis() will return 0... (see root code...)
0073       canvas_->Draw();
0074       canvas_->cd();
0075       stack->Draw("nostack");
0076       stack->GetYaxis()->SetTitleOffset(1.2);
0077       stack->GetYaxis()->SetTitle(yTitle);
0078       stack->GetXaxis()->SetTitle(xTitle);
0079       stack->GetXaxis()->SetTitleColor(kBlack);
0080       stack->Draw("nostack");
0081       legend_->Draw("same");
0082 
0083       canvas_->Update();
0084       canvas_->Draw();
0085       canvas_->ForceUpdate();
0086       //canvas_->Print("test.pdf");
0087       canvas_->Write();
0088 
0089     }
0090   }
0091 };
0092 
0093 /**
0094  * This class adds histograms into a merged one and draws it.
0095  * If the scaleFactors are not passed, the histograms are added
0096  * without rescaling. If the scaled factors are passed, the
0097  * histograms are scaled with those factors.
0098  */
0099 class Merged : public BaseNormalized {
0100  public:
0101   Merged( const TString & name, const TString & title ) : BaseNormalized(name, title) {}
0102   // virtual ~Merged() {}
0103   void Add( TH1 * histo, const TString & name, const double & scaleFactorValue = 0 ) {
0104     histoList_.push_back(histo);
0105     legend_->AddEntry(histo, name);
0106     scaleFactor.push_back(scaleFactorValue);
0107   }
0108   void Draw( const TString & xTitle = "", const TString & yTitle = "", const bool errors = false ) {
0109     canvas_->cd();
0110     if( !(histoList_.empty()) ) {
0111       TString mergedName = histoList_[0]->GetName();
0112       mergedName+="_Merged";
0113       TH1 * histo_Merged = (TH1*)histoList_[0]->Clone(mergedName);
0114       histo_Merged->Reset();
0115       std::vector<TH1*>::iterator histoIter = histoList_.begin();
0116       int scaleFactorIndex = 0;
0117       for( ; histoIter != histoList_.end(); ++histoIter, ++scaleFactorIndex ) {
0118         TH1 * histo = *histoIter;
0119         if( scaleFactor[scaleFactorIndex] != 0 ) histo_Merged->Add(histo, scaleFactor[scaleFactorIndex]/histo->Integral());
0120         else histo_Merged->Add(histo);
0121       }
0122       if(errors) histo_Merged->Sumw2();
0123       histo_Merged->GetXaxis()->SetTitle(xTitle);
0124       histo_Merged->GetYaxis()->SetTitleOffset(1.2);
0125       histo_Merged->GetYaxis()->SetTitle(yTitle);
0126       histo_Merged->Draw();
0127       canvas_->Draw();
0128       canvas_->Write();
0129     }
0130   }
0131  protected:
0132   std::vector<double> scaleFactor;
0133 };
0134 
0135 /**
0136  * Small function used to draw J/Psi, Y and Z histograms, both superimposed and merged. Runs on ResolutionAnalyzer files.
0137  */
0138 void drawKinematics( TFile ** inputFileList, int * inputScaleList, const TString & name, const TString & title, const TString & xTitle = "", const TString & yTitle = "", const TString & yTitleMerged = "", const TString & legPreText = "" ) {
0139   std::cout << "Drawing: " << name << std::endl;
0140   HistoFinder finder;
0141 
0142   TH1F * histoPt_JPsi = (TH1F*)finder(name, inputFileList[0]);
0143   TH1F * histoPt_Y = (TH1F*)finder(name, inputFileList[1]);
0144   TH1F * histoPt_Z = (TH1F*)finder(name, inputFileList[2]);
0145 
0146   Normalized resonancePt( name, title );
0147   resonancePt.Add(histoPt_JPsi, legPreText+"J/#Psi");
0148   resonancePt.Add(histoPt_Y, legPreText+"Y");
0149   resonancePt.Add(histoPt_Z, legPreText+"Z");
0150   resonancePt.Draw(xTitle, yTitle);
0151 
0152   if( inputScaleList != 0 ) {
0153     Merged resonancePtMerged( name+"Merged", title+" merged" );
0154     resonancePtMerged.Add(histoPt_JPsi, "J/#Psi", inputScaleList[0]);
0155     resonancePtMerged.Add(histoPt_Y, "Y", inputScaleList[1]);
0156     resonancePtMerged.Add(histoPt_Z, "Z", inputScaleList[2]);
0157     resonancePtMerged.Draw(xTitle, yTitleMerged);
0158   }
0159 }
0160 
0161 /**
0162  * This function draws histograms of kinematic characteristics of the Z, Y and J/Psi samples
0163  */
0164 void KinematicsComparison() {
0165   gROOT->SetBatch(true);
0166   gROOT->SetStyle("Plain");
0167 
0168   TFile * inputFileList[3] = {
0169     new TFile("ResolutionAnalyzer_JPsi.root","READ"),
0170     new TFile("ResolutionAnalyzer_Y.root","READ"),
0171     new TFile("ResolutionAnalyzer_Z.root","READ")
0172   };
0173 
0174   TFile * outputFile = new TFile("KinematicsComparison.root", "RECREATE");
0175 
0176   int inputScaleList[3] = { 126629, 48122, 1667 };
0177 
0178   outputFile->cd();
0179 
0180   // Resonance comparison histograms
0181   // -------------------------------
0182   TDirectory * resonancesDir = outputFile->mkdir("Resonances");
0183   resonancesDir->cd();
0184   // drawKinematics( inputFileList, 0, "RecoResonance_Pt", "resonance Pt", "Pt(GeV)", "arbitrary units", "expected number of events in 3.3/pb" );
0185   // Pt
0186   drawKinematics( inputFileList, inputScaleList, "RecoResonance_Pt", "resonance Pt", "pt(GeV)", "arbitrary units", "expected number of events in 3.3/pb" );
0187   // Eta
0188   drawKinematics( inputFileList, inputScaleList, "RecoResonance_Eta", "resonance #eta", "#eta", "arbitrary units", "expected number of events in 3.3/pb" );
0189   // Phi
0190   drawKinematics( inputFileList, inputScaleList, "RecoResonance_Phi", "resonance #phi", "#phi", "arbitrary units", "expected number of events in 3.3/pb" );
0191 
0192   // Histograms of muons from the resonances
0193   // ---------------------------------------
0194   TDirectory * resonancesMuonsDir = outputFile->mkdir("ResonanceMuons");
0195   resonancesMuonsDir->cd();
0196   // DeltaPt
0197   drawKinematics( inputFileList, 0, "RecoResonanceMuons_Pt", "pt of muons from resonance", "pt(GeV)", "arbitrary units", "", "muons from " );
0198   // Eta
0199   drawKinematics( inputFileList, 0, "RecoResonanceMuons_Eta", "#eta of muons from resonance", "#eta", "arbitrary units", "", "muons from " );
0200   // Phi
0201   drawKinematics( inputFileList, 0, "RecoResonanceMuons_Phi", "#phi of muons from resonance", "#phi", "arbitrary units", "", "muons from " );
0202 
0203   // Histograms of Deltas of muons from the resonances
0204   // -------------------------------------------------
0205   TDirectory * resonancesMuonsDeltasDir = outputFile->mkdir("ResonanceMuonsDeltas");
0206   resonancesMuonsDeltasDir->cd();
0207   // DeltaCotgTheta
0208   drawKinematics( inputFileList, 0, "DeltaRecoResonanceMuons_DeltaCotgTheta", "#Delta Cotg(#theta) of muons from resonance", "Cotg(#theta)", "arbitrary units", "", "muons from " );
0209   // DeltaTheta
0210   drawKinematics( inputFileList, 0, "DeltaRecoResonanceMuons_DeltaTheta", "#Delta#theta of muons from resonance", "#Delta#theta", "arbitrary units", "", "muons from " );
0211   // DeltaEta
0212   drawKinematics( inputFileList, 0, "DeltaRecoResonanceMuons_DeltaEta", "|#Delta#eta| of muons from resonance", "|#Delta#eta|", "arbitrary units", "", "muons from " );
0213   // DeltaEtaSign
0214   drawKinematics( inputFileList, 0, "DeltaRecoResonanceMuons_DeltaEtaSign", "#Delta#eta with sign of muons from resonance", "#Delta#eta", "arbitrary units", "", "muons from " );
0215   // DeltaPhi
0216   drawKinematics( inputFileList, 0, "DeltaRecoResonanceMuons_DeltaPhi", "#Delta#phi of muons from resonance", "#Delta#phi", "arbitrary units", "", "muons from " );
0217   // DeltaR
0218   drawKinematics( inputFileList, 0, "DeltaRecoResonanceMuons_DeltaR", "#Delta R of muons from resonance", "#Delta R", "arbitrary units", "", "muons from " );
0219 }