Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <iostream>
0002 #include <fstream>
0003 #include <sstream>
0004 #include <string>
0005 
0006 #include "TFile.h"
0007 #include "TH1D.h"
0008 #include "TDirectoryFile.h"
0009 #include "TCanvas.h"
0010 
0011 #define NOISEPREFIX "Profile_NoiseFromCondDB__det__"
0012 #define PEDESTALPREFIX "Profile_PedestalFromCondDB__det__"
0013 #define OCCUPANCYPREFIX "ClusterDigiPosition__det__"
0014 
0015 void printPlot(TH1D* hist, char* prefix, char* postfix);
0016 int getChannelNumber(int ival);
0017 
0018 int main(int argc, char *argv[]) {
0019 
0020   char* rootfilename;
0021   char* modulelistname;
0022   int pnbits;
0023   char* prefix;
0024   char* postfix;
0025 
0026   bool debugPrint = false;
0027   bool imagePrint = false;
0028 
0029   if(argc==6) {
0030     rootfilename = argv[1];
0031     modulelistname = argv[2];
0032     pnbits = atoi(argv[3]);
0033     prefix = argv[4];
0034     postfix = argv[5];
0035   }
0036   else {
0037     std::cout << "Wrong number of parameters " << argc << std::endl;
0038     return 1;
0039   }
0040 
0041   if (debugPrint) std::cout << "ready to go " << rootfilename << ' ' << modulelistname << std::endl;
0042 
0043   TFile* rootfile = TFile::Open(rootfilename,"READ");
0044   if(!rootfile) {
0045     std::cout << "Problems with input root file" << std::endl;
0046     return 2;
0047   }
0048   int detid;
0049   std::ifstream modulelist(modulelistname);
0050 
0051   std::stringstream outrootfilename;
0052   outrootfilename << prefix << "SummaryFile" << postfix << ".root";
0053   TFile* outrootfile = TFile::Open(outrootfilename.str().c_str(),"RECREATE");
0054   
0055   TH1D* th_summary = nullptr;
0056   Double_t TotalEvents = 0.0;
0057   Double_t TotalDigis = 0.0;
0058 
0059   TH1D* TotEvents= new TH1D("TotEvents","TotEvents",1,0,1);
0060 
0061   if(pnbits & 4) {
0062     TDirectoryFile* tdir = (TDirectoryFile*) rootfile->FindObjectAny("AlCaReco");
0063     TH1D* hist = (TH1D*)tdir->FindObjectAny("TotalNumberOfCluster__TIB");
0064     if (hist) {
0065       TotalEvents = hist->GetEntries();
0066       TotEvents->SetBinContent(1,TotalEvents);
0067       TotEvents->Write();
0068     }
0069   }    
0070   while (modulelist >> detid) {
0071     if (debugPrint) std::cout << " ready to go with detid " << detid << " " << pnbits << std::endl;
0072     // bit 0: noise
0073     if(pnbits & 1) {
0074       std::stringstream histoname;
0075       histoname << NOISEPREFIX << detid;
0076       if (debugPrint) std::cout << " ready to go with histogram " << histoname.str() << std::endl;
0077       TH1D* hist = (TH1D*)rootfile->FindObjectAny(histoname.str().c_str());
0078       if(hist) { 
0079     if (debugPrint) std::cout << histoname.str() << " found!" << std::endl;
0080     if (imagePrint) printPlot(hist,prefix,postfix);
0081     hist->Write();
0082       } else {  
0083     std::cout << histoname.str() << " NOT found..." << std::endl;
0084       }
0085     }
0086     // bit 1: pedestal
0087     if(pnbits & 2) {
0088       std::stringstream histoname;
0089       histoname << PEDESTALPREFIX << detid;
0090     if (debugPrint) std::cout << " ready to go with histogram " << histoname.str() << std::endl;
0091       TH1D* hist = (TH1D*)rootfile->FindObjectAny(histoname.str().c_str());
0092       if(hist) { 
0093     if (debugPrint) std::cout << histoname.str() << " found!" << std::endl;
0094         if (imagePrint) printPlot(hist,prefix,postfix);
0095     hist->Write();
0096       } else {  
0097     std::cout << histoname.str() << " NOT found..." << std::endl;
0098       }
0099     }
0100     // bit 2: Occupancy
0101     if(pnbits & 4) {
0102       std::stringstream histoname;
0103       histoname << OCCUPANCYPREFIX << detid;
0104       std::string SummaryName= "ClusterDigiPosition__det__Summary";
0105 
0106     if (debugPrint) std::cout << " ready to go with histogram " << histoname.str() << std::endl;
0107     if (th_summary == nullptr)  th_summary = new TH1D(SummaryName.c_str(), SummaryName.c_str(), 768, 0.5, 768.5);
0108       TH1D* hist = (TH1D*)rootfile->FindObjectAny(histoname.str().c_str());
0109       if(hist) { 
0110     if (debugPrint) std::cout << histoname.str() << " found!" <<  hist->GetEntries() << std::endl;
0111         for (int i = 1; i < hist->GetNbinsX()+1; i++) {
0112       th_summary->AddBinContent(i, hist->GetBinContent(i));
0113           TotalDigis += hist->GetBinContent(i);
0114     }
0115     hist->SetLineColor(2);
0116     if (imagePrint) printPlot(hist,prefix,postfix);
0117     hist->Write();
0118       } else {  
0119     std::cout << histoname.str() << " NOT found..." << std::endl;
0120       }
0121     }
0122     // bit 3 : reorder
0123     if(pnbits & 8) {
0124       std::stringstream histoname;
0125       histoname << OCCUPANCYPREFIX << detid;
0126     if (debugPrint) std::cout << " ready to go with histogram " << histoname.str() << std::endl;
0127       TH1D* hist = (TH1D*)rootfile->FindObjectAny(histoname.str().c_str());
0128       if(hist) { 
0129     if (debugPrint) std::cout << histoname.str() << " found!" << std::endl;
0130     std::stringstream histoname_reorder;
0131         histoname_reorder << OCCUPANCYPREFIX <<"_reorder_"<< detid;
0132         TH1D* hist_reorder = new TH1D(histoname_reorder.str().c_str(), histoname_reorder.str().c_str(), 
0133                       hist->GetXaxis()->GetNbins(), hist->GetXaxis()->GetXmin(), hist->GetXaxis()->GetXmax());
0134         for (int i = 0; i < hist_reorder->GetNbinsX(); i++) {
0135           int chan = getChannelNumber(i);
0136           hist_reorder->SetBinContent(i+1,hist->GetBinContent(chan));
0137     }
0138     hist->Write();
0139     hist_reorder->Write();
0140       } else {  
0141     std::cout << histoname.str() << " NOT found..." << std::endl;
0142       }
0143     }
0144 
0145   }
0146   if (th_summary) {
0147     std::string fname = rootfilename;
0148     std::string run = fname.substr(fname.find("R000")+4,6);
0149     if (TotalEvents) {
0150       Double_t fac = 1.0/TotalEvents;
0151       th_summary->Scale(fac);
0152       std::cout << " Run Number " << run << " Events " << TotalEvents << " Total # of Digis " << TotalDigis << " Av. Digis " << TotalDigis*fac << std::endl;
0153       th_summary->SetEntries(TotalDigis*fac);
0154     
0155     }
0156     th_summary->Write();
0157     printPlot(th_summary,prefix,postfix);
0158   }
0159 
0160   outrootfile->Close();
0161 
0162   return 0;
0163 
0164 }
0165 
0166 void printPlot(TH1D* hist, char* prefix, char* postfix) {
0167 
0168   TCanvas* cc= new TCanvas;
0169   hist->Draw();
0170   std::stringstream filename;
0171   filename << prefix << hist->GetName() << postfix << ".png";
0172   cc->Print(filename.str().c_str());
0173   delete cc;
0174 
0175 }
0176 int getChannelNumber(int ival) {
0177 
0178   int chan=int(32*fmod(int(fmod(ival,256.)/2.),4.) +
0179           8*int(int(fmod(ival,256.)/2.)/4.) -
0180           31*int(int(fmod(ival,256.)/2.)/16.) +
0181           fmod(fmod(ival,256.),2.)*128 +
0182           int(ival/256)*256);
0183   return chan;
0184 }