test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
moduleOccupancyPlots.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <sstream>
4 #include <string>
5 
6 #include "TFile.h"
7 #include "TH1D.h"
8 #include "TDirectoryFile.h"
9 #include "TCanvas.h"
10 
11 #define NOISEPREFIX "Profile_NoiseFromCondDB__det__"
12 #define PEDESTALPREFIX "Profile_PedestalFromCondDB__det__"
13 #define OCCUPANCYPREFIX "ClusterDigiPosition__det__"
14 
15 void printPlot(TH1D* hist, char* prefix, char* postfix);
16 int getChannelNumber(int ival);
17 
18 int main(int argc, char *argv[]) {
19 
20  char* rootfilename;
21  char* modulelistname;
22  int pnbits;
23  char* prefix;
24  char* postfix;
25 
26  bool debugPrint = false;
27  bool imagePrint = false;
28 
29  if(argc==6) {
30  rootfilename = argv[1];
31  modulelistname = argv[2];
32  pnbits = atoi(argv[3]);
33  prefix = argv[4];
34  postfix = argv[5];
35  }
36  else {
37  std::cout << "Wrong number of parameters " << argc << std::endl;
38  return 1;
39  }
40 
41  if (debugPrint) std::cout << "ready to go " << rootfilename << ' ' << modulelistname << std::endl;
42 
43  TFile* rootfile = TFile::Open(rootfilename,"READ");
44  if(!rootfile) {
45  std::cout << "Problems with input root file" << std::endl;
46  return 2;
47  }
48  int detid;
49  std::ifstream modulelist(modulelistname);
50 
51  std::stringstream outrootfilename;
52  outrootfilename << prefix << "SummaryFile" << postfix << ".root";
53  TFile* outrootfile = TFile::Open(outrootfilename.str().c_str(),"RECREATE");
54 
55  TH1D* th_summary = nullptr;
56  Double_t TotalEvents = 0.0;
57  Double_t TotalDigis = 0.0;
58 
59  TH1D* TotEvents= new TH1D("TotEvents","TotEvents",1,0,1);
60 
61  if(pnbits & 4) {
62  TDirectoryFile* tdir = (TDirectoryFile*) rootfile->FindObjectAny("AlCaReco");
63  TH1D* hist = (TH1D*)tdir->FindObjectAny("TotalNumberOfCluster__TIB");
64  if (hist) {
65  TotalEvents = hist->GetEntries();
66  TotEvents->SetBinContent(1,TotalEvents);
67  TotEvents->Write();
68  }
69  }
70  while (modulelist >> detid) {
71  if (debugPrint) std::cout << " ready to go with detid " << detid << " " << pnbits << std::endl;
72  // bit 0: noise
73  if(pnbits & 1) {
74  std::stringstream histoname;
75  histoname << NOISEPREFIX << detid;
76  if (debugPrint) std::cout << " ready to go with histogram " << histoname.str() << std::endl;
77  TH1D* hist = (TH1D*)rootfile->FindObjectAny(histoname.str().c_str());
78  if(hist) {
79  if (debugPrint) std::cout << histoname.str() << " found!" << std::endl;
80  if (imagePrint) printPlot(hist,prefix,postfix);
81  hist->Write();
82  } else {
83  std::cout << histoname.str() << " NOT found..." << std::endl;
84  }
85  }
86  // bit 1: pedestal
87  if(pnbits & 2) {
88  std::stringstream histoname;
89  histoname << PEDESTALPREFIX << detid;
90  if (debugPrint) std::cout << " ready to go with histogram " << histoname.str() << std::endl;
91  TH1D* hist = (TH1D*)rootfile->FindObjectAny(histoname.str().c_str());
92  if(hist) {
93  if (debugPrint) std::cout << histoname.str() << " found!" << std::endl;
94  if (imagePrint) printPlot(hist,prefix,postfix);
95  hist->Write();
96  } else {
97  std::cout << histoname.str() << " NOT found..." << std::endl;
98  }
99  }
100  // bit 2: Occupancy
101  if(pnbits & 4) {
102  std::stringstream histoname;
103  histoname << OCCUPANCYPREFIX << detid;
104  std::string SummaryName= "ClusterDigiPosition__det__Summary";
105 
106  if (debugPrint) std::cout << " ready to go with histogram " << histoname.str() << std::endl;
107  if (th_summary == nullptr) th_summary = new TH1D(SummaryName.c_str(), SummaryName.c_str(), 768, 0.5, 768.5);
108  TH1D* hist = (TH1D*)rootfile->FindObjectAny(histoname.str().c_str());
109  if(hist) {
110  if (debugPrint) std::cout << histoname.str() << " found!" << hist->GetEntries() << std::endl;
111  for (int i = 1; i < hist->GetNbinsX()+1; i++) {
112  th_summary->AddBinContent(i, hist->GetBinContent(i));
113  TotalDigis += hist->GetBinContent(i);
114  }
115  if (imagePrint) printPlot(hist,prefix,postfix);
116  hist->Write();
117  } else {
118  std::cout << histoname.str() << " NOT found..." << std::endl;
119  }
120  }
121  // bit 3 : reorder
122  if(pnbits & 8) {
123  std::stringstream histoname;
124  histoname << OCCUPANCYPREFIX << detid;
125  if (debugPrint) std::cout << " ready to go with histogram " << histoname.str() << std::endl;
126  TH1D* hist = (TH1D*)rootfile->FindObjectAny(histoname.str().c_str());
127  if(hist) {
128  if (debugPrint) std::cout << histoname.str() << " found!" << std::endl;
129  std::stringstream histoname_reorder;
130  histoname_reorder << OCCUPANCYPREFIX <<"_reorder_"<< detid;
131  TH1D* hist_reorder = new TH1D(histoname_reorder.str().c_str(), histoname_reorder.str().c_str(),
132  hist->GetXaxis()->GetNbins(), hist->GetXaxis()->GetXmin(), hist->GetXaxis()->GetXmax());
133  for (int i = 0; i < hist_reorder->GetNbinsX(); i++) {
134  int chan = getChannelNumber(i);
135  hist_reorder->SetBinContent(i+1,hist->GetBinContent(chan));
136  }
137  hist->Write();
138  hist_reorder->Write();
139  } else {
140  std::cout << histoname.str() << " NOT found..." << std::endl;
141  }
142  }
143 
144  }
145  if (th_summary) {
146  std::string fname = rootfilename;
147  std::string run = fname.substr(fname.find("R000")+4,6);
148  if (TotalEvents) {
149  Double_t fac = 1.0/TotalEvents;
150  th_summary->Scale(fac);
151  std::cout << " Run Number " << run << " Events " << TotalEvents << " Total # of Digis " << TotalDigis << " Av. Digis " << TotalDigis*fac << std::endl;
152  th_summary->SetEntries(TotalDigis*fac);
153 
154  }
155  th_summary->Write();
156  printPlot(th_summary,prefix,postfix);
157  }
158 
159  outrootfile->Close();
160 
161  return 0;
162 
163 }
164 
165 void printPlot(TH1D* hist, char* prefix, char* postfix) {
166 
167  TCanvas* cc= new TCanvas;
168  hist->Draw();
169  std::stringstream filename;
170  filename << prefix << hist->GetName() << postfix << ".png";
171  cc->Print(filename.str().c_str());
172  delete cc;
173 
174 }
175 int getChannelNumber(int ival) {
176 
177  int chan=int(32*fmod(int(fmod(ival,256.)/2.),4.) +
178  8*int(int(fmod(ival,256.)/2.)/4.) -
179  31*int(int(fmod(ival,256.)/2.)/16.) +
180  fmod(fmod(ival,256.),2.)*128 +
181  int(ival/256)*256);
182  return chan;
183 }
int i
Definition: DBlmapReader.cc:9
void printPlot(TH1D *hist, char *prefix, char *postfix)
int getChannelNumber(int ival)
#define NOISEPREFIX
#define OCCUPANCYPREFIX
tuple argc
Definition: dir2webdir.py:38
string fname
main script
#define PEDESTALPREFIX
tuple filename
Definition: lut2db_cfg.py:20
tuple cout
Definition: gather_cfg.py:145