Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 13:02:46

0001 #include <TROOT.h>
0002 #include <TFile.h>
0003 #include <TH2D.h>
0004 #include <TStyle.h>
0005 #include <TCanvas.h>
0006 #include <TLegend.h>
0007 #include <TPaveStats.h>
0008 #include <TPaveText.h>
0009 #include <algorithm>
0010 #include <map>
0011 #include <vector>
0012 #include <string>
0013 #include <iomanip>
0014 #include <iostream>
0015 #include <fstream>
0016 #include <sstream>
0017 
0018 struct record {
0019   record(int ser = 0, int id = 0, double x = 0, double y = 0, double z = 0)
0020       : serial_(ser), id_(id), x_(x), y_(y), z_(z) {}
0021 
0022   int serial_, id_;
0023   double x_, y_, z_;
0024 };
0025 
0026 struct recordLess {
0027   bool operator()(const record& a, const record& b) {
0028     return ((a.z_ < b.z_) || ((a.z_ == b.z_) && (a.y_ < b.y_)) || ((a.z_ == b.z_) && (a.y_ == b.y_) && (a.x_ < b.x_)));
0029   }
0030 };
0031 
0032 void checkDuplicate(std::string fname, std::string outFile) {
0033   std::vector<record> records;
0034   ifstream infile(fname.c_str());
0035   if (!infile.is_open()) {
0036     std::cout << "Cannot open " << fname << std::endl;
0037   } else {
0038     while (1) {
0039       int ser, id;
0040       double x, y, z;
0041       infile >> ser >> id >> x >> y >> z;
0042       if (!infile.good())
0043         break;
0044       record rec(ser, id, x, y, z);
0045       records.push_back(rec);
0046     }
0047     infile.close();
0048     std::cout << "Reads " << records.size() << " records from " << fname << std::endl;
0049 
0050     std::ofstream fileout;
0051     fileout.open(outFile.c_str(), std::ofstream::out);
0052     std::cout << "Opens " << outFile << " in output mode" << std::endl;
0053 
0054     // Use std::sort
0055     std::sort(records.begin(), records.end(), recordLess());
0056     unsigned int bad1(0), bad2(0);
0057     for (unsigned k = 0; k < records.size(); ++k) {
0058       unsigned int dup(0);
0059       for (unsigned k1 = k + 1; k1 < records.size(); ++k1) {
0060         if ((fabs(records[k].x_ - records[k1].x_) < 0.0001) && (fabs(records[k].y_ - records[k1].y_) < 0.0001) &&
0061             (fabs(records[k].z_ - records[k1].z_) < 0.0001)) {
0062           ++dup;
0063         } else {
0064           break;
0065         }
0066       }
0067       fileout << "[" << records[k].serial_ << "] ID " << std::hex << records[k].id_ << std::dec << std::setprecision(6)
0068               << " (" << records[k].x_ << ", " << records[k].y_ << ", " << records[k].z_ << ") with " << dup
0069               << " Duplicates:";
0070       if (dup > 0) {
0071         for (unsigned k1 = 0; k1 < dup; ++k1)
0072           fileout << std::hex << " " << records[k + k1 + 1].id_ << std::dec;
0073         fileout << " ****** Check *****" << std::endl;
0074         k += dup;
0075         ++bad1;
0076         bad2 += (dup + 1);
0077       } else {
0078         fileout << std::endl;
0079       }
0080     }
0081     fileout.close();
0082     std::cout << "Bad records " << bad1 << ":" << bad2 << std::endl;
0083   }
0084 }
0085 
0086 void plotHist(const char* fname, bool save = false) {
0087   std::string name[4] = {"hist01", "hist02", "hist11", "hist12"};
0088   std::string titl[4] = {"z = -1, layer = 1", "z = -1, layer = 2", "z = 1, layer = 1", "z = 1, layer = 2"};
0089   gStyle->SetCanvasBorderMode(0);
0090   TFile* file = new TFile(fname);
0091   if (file != nullptr) {
0092     gStyle->SetCanvasColor(kWhite);
0093     gStyle->SetPadColor(kWhite);
0094     gStyle->SetFillColor(kWhite);
0095     gStyle->SetOptStat(1110);
0096     gStyle->SetOptTitle(0);
0097     for (int k = 0; k < 4; ++k) {
0098       TH2D* hist = (TH2D*)file->FindObjectAny(name[k].c_str());
0099       if (hist != nullptr) {
0100         char namep[20];
0101         sprintf(namep, "c_%s", name[k].c_str());
0102         TCanvas* pad = new TCanvas(namep, namep, 700, 700);
0103         pad->SetRightMargin(0.10);
0104         pad->SetTopMargin(0.10);
0105         hist->GetXaxis()->SetTitleSize(0.04);
0106         hist->GetXaxis()->SetLabelSize(0.035);
0107         hist->GetXaxis()->SetTitle("x (cm)");
0108         hist->GetYaxis()->SetTitle("y (cm)");
0109         hist->GetYaxis()->SetLabelOffset(0.005);
0110         hist->GetYaxis()->SetTitleSize(0.04);
0111         hist->GetYaxis()->SetLabelSize(0.035);
0112         hist->GetYaxis()->SetTitleOffset(1.10);
0113         hist->SetMarkerColor(2);
0114         hist->Draw();
0115         pad->Update();
0116         TPaveStats* st1 = (TPaveStats*)hist->GetListOfFunctions()->FindObject("stats");
0117         if (st1 != nullptr) {
0118           st1->SetY1NDC(0.75);
0119           st1->SetY2NDC(0.90);
0120           st1->SetX1NDC(0.65);
0121           st1->SetX2NDC(0.90);
0122         }
0123         TPaveText* txt1 = new TPaveText(0.25, 0.91, 0.75, 0.96, "blNDC");
0124         txt1->SetFillColor(0);
0125         txt1->AddText(titl[k].c_str());
0126         txt1->Draw("same");
0127         pad->Modified();
0128         pad->Update();
0129         if (save) {
0130           sprintf(namep, "%s.pdf", pad->GetName());
0131           pad->Print(namep);
0132         }
0133       }
0134     }
0135   }
0136 }