Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-08 05:11:29

0001 #include <iostream>
0002 #include <fstream>
0003 #include <sstream>
0004 #include <string>
0005 
0006 #include <TROOT.h>
0007 #include <TStyle.h>
0008 #include "TFile.h"
0009 #include "THStack.h"
0010 #include "TH1D.h"
0011 #include "TDirectoryFile.h"
0012 #include "TCanvas.h"
0013 #include "TLine.h"
0014 #include "TLegend.h"
0015 #include "TRint.h"
0016 
0017 int main(int argc, char* argv[]) {
0018   gROOT->SetStyle("Plain");
0019 
0020   char* filelist;
0021   char* modulelist;
0022 
0023   filelist = argv[1];
0024   modulelist = argv[2];
0025 
0026   int stripmin = 999;
0027   int stripmax = 999;
0028 
0029   if (argv[3] && argv[4]) {
0030     stripmin = atoi(argv[3]);
0031     stripmax = atoi(argv[4]);
0032   }
0033 
0034   std::string detid;
0035   std::string hn;
0036 
0037   TLegend leg(0.1, 0.7, 0.2, 0.9);
0038   leg.SetFillStyle(0);
0039 
0040   std::ifstream inmodules(modulelist);
0041 
0042   while (true) {
0043     inmodules >> detid;
0044     if (!inmodules.good())
0045       break;
0046 
0047     hn = "ClusterDigiPosition__det__" + detid;  //std::to_string(detid);
0048 
0049     TCanvas c1("c1", "c1", 1600, 900);
0050 
0051     c1.SetBatch(kTRUE);
0052     c1.SetLogy(1);
0053     c1.SetGridy(1);
0054 
0055     //cout << detid << endl;
0056     std::ifstream fileToCountLines(filelist);
0057     std::size_t lines_count = 0;
0058     std::string line;
0059 
0060     while (std::getline(fileToCountLines, line))
0061       ++lines_count;
0062 
0063     const float dim = lines_count;
0064 
0065     std::ifstream filesin(filelist);
0066 
0067     std::string filename;
0068     int k = 0;
0069 
0070     TH1D* trend = new TH1D("trend", "trend", int(dim), 0.5, dim + 0.5);
0071     trend->SetMarkerSize(3);
0072     trend->SetMarkerStyle(8);
0073     trend->SetMarkerColor(4);
0074 
0075     std::string ttitle = hn + "_trend";
0076     trend->SetTitle(ttitle.c_str());
0077 
0078     double max = -1;
0079     double min = 1000000;
0080 
0081     leg.Clear();
0082     TFile* fin;
0083 
0084     THStack* hs = new THStack("hs", "");
0085 
0086     while (true) {
0087       filesin >> filename;
0088       if (!filesin.good())
0089         break;
0090 
0091       std::string runNum = filename.substr(filename.find("run_") + 4, 6);
0092       //std::cout << runNum << std::endl;
0093 
0094       fin = TFile::Open(filename.c_str());
0095 
0096       if (!fin) {
0097         std::cout << "Cannot open file " << filename.c_str() << std::endl;
0098         return 0;
0099       }
0100 
0101       TH1D* Events = (TH1D*)fin->Get("TotEvents");
0102 
0103       double EvtNum = Events->GetBinContent(1);
0104 
0105       TH1D* histo = (TH1D*)fin->Get(hn.c_str());
0106 
0107       if (!histo) {
0108         std::cout << "Cannot open histo " << hn.c_str() << std::endl;
0109         return 0;
0110       }
0111 
0112       histo->SetDirectory(nullptr);
0113       histo->SetStats(kFALSE);
0114 
0115       if (hn.find("Summary") == std::string::npos)
0116         histo->Scale(1 / EvtNum);
0117 
0118       double numberPerEvent;
0119       if (stripmin == 999 && stripmax == 999)
0120         numberPerEvent = histo->Integral();
0121       else
0122         numberPerEvent = histo->Integral(stripmin, stripmax);
0123 
0124       if (max <= histo->GetBinContent(histo->GetMaximumBin()))
0125         max = histo->GetBinContent(histo->GetMaximumBin());
0126       if (min > histo->GetBinContent(histo->GetMinimumBin()))
0127         min = histo->GetBinContent(histo->GetMinimumBin());
0128 
0129       histo->SetLineColor(k + 1);
0130       histo->SetMarkerStyle(9);
0131       histo->SetMarkerColor(k + 1);
0132 
0133       trend->SetBinContent(k + 1, numberPerEvent);
0134       trend->GetXaxis()->SetBinLabel(k + 1, runNum.c_str());
0135 
0136       leg.AddEntry(histo, runNum.c_str(), "L");
0137       hs->Add(histo);
0138       k++;
0139 
0140       fin->Close();
0141     }
0142 
0143     if (min == 0)
0144       min = 1.e-6;
0145 
0146     max = max * 10;  // in a way one can read the legend
0147 
0148     hs->SetMaximum(max);
0149     hs->SetMinimum(min);
0150     hs->SetTitle(hn.c_str());
0151 
0152     hs->Draw("nostack");
0153 
0154     TLine l;
0155 
0156     l.SetLineColor(4);
0157     l.SetLineStyle(2);
0158     l.SetLineWidth(3);
0159 
0160     l.DrawLine(128, min, 128, max);
0161     l.DrawLine(256, min, 256, max);
0162     l.DrawLine(384, min, 384, max);
0163     l.DrawLine(384, min, 384, max);
0164     l.DrawLine(512, min, 512, max);
0165     l.DrawLine(640, min, 640, max);
0166 
0167     leg.Draw();
0168     std::string outname = hn + "_Super.png";
0169     c1.SaveAs(outname.c_str());
0170 
0171     c1.SetGridx(1);
0172 
0173     double mintrend = 0;
0174     if (trend->GetMinimum() == 0)
0175       mintrend = 1.e-4;
0176     else
0177       mintrend = trend->GetMinimum();
0178 
0179     trend->SetStats(kFALSE);
0180 
0181     trend->GetYaxis()->SetRangeUser(mintrend * 0.5, trend->GetMaximum() * 2);
0182     trend->Draw("P");
0183     outname = hn + "_Trend.png";
0184     c1.SaveAs(outname.c_str());
0185     c1.Clear();
0186 
0187     delete trend;
0188   }
0189   return 0;
0190 }