ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/scripts/Beam/ReadBeamSpot.cpp
Revision: 1.1
Committed: Fri Feb 12 18:03:59 2010 UTC (15 years, 2 months ago) by jengbou
Branch: MAIN
CVS Tags: HEAD
Log Message:
Add scripts for plotting beam positions vs run number (modified from Lorenzo's code)

File Contents

# User Rev Content
1 jengbou 1.1 #include "ReadBeamSpot.h"
2     #include <TFile.h>
3     #include <TH1F.h>
4     #include <TCanvas.h>
5     #include <TROOT.h>
6     #include <iostream>
7     #include <vector>
8     #include <map>
9     #include <string>
10     #include <TStyle.h>
11     #include <TPaveText.h>
12     #include <TLegend.h>
13     #include <TGaxis.h>
14    
15     bool myci_equal(char ch1, char ch2){
16     return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
17     }
18    
19     //------------------------------------------------------------
20     ReadBeamSpot::ReadBeamSpot(){
21     string mthn = "[ReadBeamSpot::ReadBeamSpot()]\t";
22     text_ = "";
23     dataDir_ = "./";
24     }
25    
26     //------------------------------------------------------------
27     ReadBeamSpot::~ReadBeamSpot(){
28     }
29    
30     //------------------------------------------------------------
31     void ReadBeamSpot::openHtmlFile(string fileName){
32     text_ = "";
33     theFile_.open(fileName.c_str());
34     if(!theFile_.is_open()){
35     cout << "[ReadCsvFiles::openCsvFile()]\t" << "Can't open file: " << fileName << endl;
36     }
37     }
38    
39     //------------------------------------------------------------
40     void ReadBeamSpot::closeHtmlFile(void){
41     if(theFile_.is_open()){
42     theFile_.close();
43     }
44     text_ = "";
45     theFile_.clear();
46     }
47    
48     //------------------------------------------------------------
49     void ReadBeamSpot::makePlots(){
50     }
51    
52     void ReadBeamSpot::setStyle(){
53     gStyle->SetFrameBorderMode(0);
54     gStyle->SetCanvasBorderMode(0);
55     gStyle->SetPadBorderMode(0);
56     gStyle->SetFrameFillStyle(0);
57     gStyle->SetLegendBorderSize(0);
58    
59     gStyle->SetPadColor(0);
60     gStyle->SetCanvasColor(0);
61     gStyle->SetTitleColor(1);
62     gStyle->SetStatColor(0);
63    
64     // set the paper & margin sizes
65     gStyle->SetPaperSize(20,26);
66     gStyle->SetPadTopMargin(0.12);
67     //gStyle->SetPadRightMargin(0.05);
68     gStyle->SetPadBottomMargin(0.2);
69     gStyle->SetPadLeftMargin(0.2);
70     gStyle->SetPadTickX(1);
71     gStyle->SetPadTickY(1);
72    
73     //
74     gStyle->SetTextFont(132);
75     gStyle->SetTextSize(0.09);
76     gStyle->SetLabelFont(42,"xyz");
77     gStyle->SetTitleFont(42,"xyz");
78     //gStyle->SetLabelSize(0.032,"xyz");
79     gStyle->SetLabelSize(0.035,"xyz");
80     gStyle->SetTitleSize(0.045,"xyz");
81     gStyle->SetTitleOffset(1.2,"xy");
82    
83     // use bold lines and markers
84     gStyle->SetMarkerStyle(8);
85     gStyle->SetHistLineWidth(2.3);
86     gStyle->SetLineWidth(1.5);
87     //gStyle->SetLineStyleString(2,"[12 12]"); // postscript dashes
88    
89     // do not display any of the standard histogram decorations
90     gStyle->SetOptTitle(0); //0
91     gStyle->SetOptStat(0); //0
92     gStyle->SetOptFit(0); //0
93    
94     gStyle->SetPalette(1,(Int_t*)0);
95     gStyle->SetErrorX(0);
96     // gStyle->SetEndErrorSize(0.5);
97     }
98    
99     //------------------------------------------------------------
100     void ReadBeamSpot::findString(string what){
101     setStyle();
102     if(text_ == ""){
103     char line[65536];
104     while (theFile_.good()){
105     theFile_.getline(line, 65536);
106     text_ += string(line) + string(" ");
107     }
108     }
109     int stringPos = 0;
110     string runNumber;
111     float x0,x0Error;
112     float y0,y0Error;
113     float z0,z0Error;
114     float sigmaZ0,sigmaZ0Error;
115     int valBegin = 0;
116     int valSplit = 0;
117     int valEnd = 0;
118    
119     int numberOfRuns = 0;
120     while(stringPos >= 0){
121     stringPos = ci_find(text_,what,stringPos);
122     if(stringPos < 0) break;
123     ++numberOfRuns;
124     ++stringPos;
125     }
126    
127    
128     map<string,string> runToFillMap;
129     runToFillMap["123815"] = "904";
130     runToFillMap["123908"] = "906";
131     runToFillMap["123970"] = "907";
132     runToFillMap["123976"] = "907";
133     runToFillMap["124023"] = "911";
134     runToFillMap["124024"] = "911";
135     runToFillMap["124025"] = "911";
136     runToFillMap["124027"] = "912";
137     runToFillMap["124030"] = "912";
138     runToFillMap["124120"] = "916 (2.36 TeV)";
139     runToFillMap["124230"] = "919";
140     runToFillMap["124275"] = "923 (2.36 TeV)";
141    
142     TFile rootFile("Output.root","RECREATE");
143     TH1F* hX0VsRun = new TH1F("hX0VsRun", "X0 vs Run number", numberOfRuns,0,numberOfRuns);
144     TH1F* hY0VsRun = new TH1F("hY0VsRun", "Y0 vs Run number", numberOfRuns,0,numberOfRuns);
145     TH1F* hZ0VsRun = new TH1F("hZ0VsRun", "Z0 vs Run number", numberOfRuns,0,numberOfRuns);
146     TH1F* hSigmaZ0VsRun = new TH1F("hSigmaZ0VsRun","SigmaZ0 vs Run number",numberOfRuns,0,numberOfRuns);
147    
148     hX0VsRun->GetXaxis()->SetTitle("Run");
149     hX0VsRun->GetYaxis()->SetTitle("X position of beam spots (cm)");
150    
151     hY0VsRun->GetXaxis()->SetTitle("Run");
152     hY0VsRun->GetYaxis()->SetTitle("Y position of beam spots (cm)");
153    
154     hZ0VsRun->GetXaxis()->SetTitle("Run");
155     hZ0VsRun->GetYaxis()->SetTitle("Z position of beam spots (cm)");
156    
157     hSigmaZ0VsRun->GetXaxis()->SetTitle("Run");
158     hSigmaZ0VsRun->GetYaxis()->SetTitle("#sigma_{z_{0}} of beam spots (cm)");
159    
160     TH1F* hX0VsFill = new TH1F("hX0VsFill", "X0 vs Fill number", numberOfRuns,0,numberOfRuns);
161     TH1F* hY0VsFill = new TH1F("hY0VsFill", "Y0 vs Fill number", numberOfRuns,0,numberOfRuns);
162     TH1F* hZ0VsFill = new TH1F("hZ0VsFill", "Z0 vs Fill number", numberOfRuns,0,numberOfRuns);
163     TH1F* hSigmaZ0VsFill = new TH1F("hSigmaZ0VsFill","SigmaZ0 vs Fill number",numberOfRuns,0,numberOfRuns);
164    
165     hX0VsFill->GetXaxis()->SetTitle("LHC Fill");
166     hX0VsFill->GetYaxis()->SetTitle("X position of beam spots (cm)");
167    
168     hY0VsFill->GetXaxis()->SetTitle("LHC Fill");
169     hY0VsFill->GetYaxis()->SetTitle("Y position of beam spots (cm)");
170    
171     hZ0VsFill->GetXaxis()->SetTitle("LHC Fill");
172     hZ0VsFill->GetYaxis()->SetTitle("Z position of beam spots (cm)");
173    
174     hSigmaZ0VsFill->GetXaxis()->SetTitle("LHC Fill");
175     hSigmaZ0VsFill->GetYaxis()->SetTitle("#sigma_{z_{0}} of beam spots (cm)");
176    
177    
178     stringPos=0;
179     while(stringPos >= 0){
180     // stringPos = text_.find(what,stringPos);
181     // stringPos = ci_find(text_.substr(stringPos,text_.length()-stringPos),what);
182     stringPos = ci_find(text_,what,stringPos);
183     // stringPos = ci_find(text_,what,stringPos);
184     if(stringPos < 0) break;
185     runNumber = text_.substr((text_.substr(0,stringPos)).rfind("<a name=\"Run_")+13,6);
186    
187     valBegin = ci_find(text_,"X0",stringPos)+13;
188     valSplit = ci_find(text_,"+/-",stringPos);
189     valEnd = ci_find(text_,"&#91;cm]",stringPos);
190     x0 = atof(text_.substr(valBegin, valSplit-valBegin-1).c_str());
191     x0Error = atof(text_.substr(valSplit+4, valEnd-valSplit-5).c_str());
192     stringPos = valEnd+8;
193    
194     valBegin = ci_find(text_,"Y0",stringPos)+13;
195     valSplit = ci_find(text_,"+/-",stringPos);
196     valEnd = ci_find(text_,"&#91;cm]",stringPos);
197     y0 = atof(text_.substr(valBegin, valSplit-valBegin-1).c_str());
198     y0Error = atof(text_.substr(valSplit+4, valEnd-valSplit-5).c_str());
199     stringPos = valEnd+8;
200    
201     valBegin = ci_find(text_,"Z0",stringPos)+13;
202     valSplit = ci_find(text_,"+/-",stringPos);
203     valEnd = ci_find(text_,"&#91;cm]",stringPos);
204     z0 = atof(text_.substr(valBegin, valSplit-valBegin-1).c_str());
205     z0Error = atof(text_.substr(valSplit+4, valEnd-valSplit-5).c_str());
206     stringPos = valEnd+8;
207    
208     valBegin = ci_find(text_,"Sigma Z0",stringPos)+19;
209     valSplit = ci_find(text_,"+/-",stringPos);
210     valEnd = ci_find(text_,"&#91;cm]",stringPos);
211     sigmaZ0 = atof(text_.substr(valBegin, valSplit-valBegin-1).c_str());
212     sigmaZ0Error = atof(text_.substr(valSplit+4, valEnd-valSplit-5).c_str());
213     stringPos = valEnd+8;
214    
215     cout << __PRETTY_FUNCTION__
216     << "Pos: " << stringPos
217     << " run: " << runNumber << endl
218     // << "-X0: " << x0
219     // << "-X0error: " << x0Error << "-" << endl
220     // << "-Y0: " << y0
221     // << "-Y0error: " << y0Error << "-" << endl
222     // << "-Z0: " << z0
223     // << "-Z0error: " << z0Error << "-" << endl
224     // << "-sigmaZ0: " << sigmaZ0
225     // << "-sigmaZ0error: " << sigmaZ0Error
226     << "-" << endl;
227     TString runLabel;
228     if (runNumber == "124120" || runNumber == "124275")
229     runLabel = TString(runNumber.c_str())+" (2.36 TeV)";
230     else
231     runLabel = runNumber.c_str();
232    
233     hX0VsRun->SetBinContent(numberOfRuns,x0);
234     hX0VsRun->SetBinError(numberOfRuns,x0Error);
235     hX0VsRun->GetXaxis()->SetBinLabel(numberOfRuns,runLabel);
236    
237     hY0VsRun->SetBinContent(numberOfRuns,y0);
238     hY0VsRun->SetBinError(numberOfRuns,y0Error);
239     hY0VsRun->GetXaxis()->SetBinLabel(numberOfRuns,runLabel);
240    
241     hZ0VsRun->SetBinContent(numberOfRuns,z0);
242     hZ0VsRun->SetBinError(numberOfRuns,z0Error);
243     hZ0VsRun->GetXaxis()->SetBinLabel(numberOfRuns,runLabel);
244    
245     hSigmaZ0VsRun->SetBinContent(numberOfRuns,sigmaZ0);
246     hSigmaZ0VsRun->SetBinError(numberOfRuns,sigmaZ0Error);
247     hSigmaZ0VsRun->GetXaxis()->SetBinLabel(numberOfRuns,runLabel);
248    
249     hX0VsFill->SetBinContent(numberOfRuns,x0);
250     hX0VsFill->SetBinError(numberOfRuns,x0Error);
251     hX0VsFill->GetXaxis()->SetBinLabel(numberOfRuns,runToFillMap[runNumber].c_str());
252    
253     hY0VsFill->SetBinContent(numberOfRuns,y0);
254     hY0VsFill->SetBinError(numberOfRuns,y0Error);
255     hY0VsFill->GetXaxis()->SetBinLabel(numberOfRuns,runToFillMap[runNumber].c_str());
256    
257     hZ0VsFill->SetBinContent(numberOfRuns,z0);
258     hZ0VsFill->SetBinError(numberOfRuns,z0Error);
259     hZ0VsFill->GetXaxis()->SetBinLabel(numberOfRuns,runToFillMap[runNumber].c_str());
260    
261     hSigmaZ0VsFill->SetBinContent(numberOfRuns,sigmaZ0);
262     hSigmaZ0VsFill->SetBinError(numberOfRuns,sigmaZ0Error);
263     hSigmaZ0VsFill->GetXaxis()->SetBinLabel(numberOfRuns,runToFillMap[runNumber].c_str());
264     ++stringPos;
265     --numberOfRuns;
266     }
267     rootFile.Write();
268    
269     TPaveText *ppt= new TPaveText(.36,.72,.76,.78,"NDC");
270     ppt->AddText("CMS Preliminary 2009");
271     ppt->SetTextAlign(12);
272     ppt->SetTextFont(62);
273     ppt->SetTextSize(0.045);
274     ppt->SetBorderSize(0);
275     ppt->SetFillStyle(0);
276    
277     TCanvas cTmp("cTmp","Drawing canvas",800,600);
278     hX0VsRun->GetYaxis()->SetRangeUser(0.085,0.135);
279     hX0VsRun->GetXaxis()->LabelsOption("u");
280     hX0VsRun->Draw("e1");
281     ppt->Draw();
282     cTmp.Print(TString(hX0VsRun->GetTitle())+".png");
283     hZ0VsRun->GetXaxis()->LabelsOption("u");
284     hZ0VsRun->Draw("e1");
285     ppt->Draw();
286     cTmp.Print(TString(hZ0VsRun->GetTitle())+".png");
287     hSigmaZ0VsRun->GetXaxis()->LabelsOption("u");
288     hSigmaZ0VsRun->Draw("e1");
289     ppt->Draw();
290     cTmp.Print(TString(hSigmaZ0VsRun->GetTitle())+".png");
291     hX0VsFill->GetYaxis()->SetRangeUser(0.085,0.135);
292     hX0VsFill->GetXaxis()->LabelsOption("u");
293     hX0VsFill->Draw("e1");
294     ppt->Draw();
295     cTmp.Print(TString(hX0VsFill->GetTitle())+".png");
296     hZ0VsFill->GetXaxis()->LabelsOption("u");
297     hZ0VsFill->Draw("e1");
298     ppt->Draw();
299     cTmp.Print(TString(hZ0VsFill->GetTitle())+".png");
300     hSigmaZ0VsFill->GetXaxis()->LabelsOption("u");
301     hSigmaZ0VsFill->Draw("e1");
302     ppt->Draw();
303     cTmp.Print(TString(hSigmaZ0VsFill->GetTitle())+".png");
304    
305     ppt->SetX1NDC(0.45);
306     ppt->SetY1NDC(0.72);
307     ppt->SetX2NDC(0.85);
308     ppt->SetY2NDC(0.78);
309     hY0VsRun->GetYaxis()->SetRangeUser(0.01,0.06);
310     hY0VsRun->GetXaxis()->LabelsOption("u");
311     hY0VsRun->Draw("e1");
312     ppt->Draw();
313     cTmp.Print(TString(hY0VsRun->GetTitle())+".png");
314     hY0VsFill->GetYaxis()->SetRangeUser(0.01,0.06);
315     hY0VsFill->GetXaxis()->LabelsOption("u");
316     hY0VsFill->Draw("e1");
317     ppt->Draw();
318     cTmp.Print(TString(hY0VsFill->GetTitle())+".png");
319    
320     // Superimpose Transverse
321     Double_t ymin,ymax;
322     ymin=0.085;ymax=0.15;
323    
324     hX0VsRun->GetYaxis()->SetTitle("Transverse position of beam spots (cm)");
325     hX0VsRun->GetXaxis()->LabelsOption("u");
326     hX0VsRun->GetXaxis()->CenterLabels();
327     hX0VsRun->Draw("e1");
328     hX0VsRun->GetYaxis()->SetRangeUser(ymin,ymax);
329     gPad->Update();
330     // hY0VsRun->SetMarkerStyle(21);
331     // hY0VsRun->SetMarkerColor(2);
332     // hY0VsRun->SetLineColor(2);
333     // hY0VsRun->Draw("samee1][");
334     int nbins = hY0VsRun->GetEntries();
335     TH1F*hY0tmp=new TH1F("y0_tmp","y0_tmp",nbins,0,nbins);
336     double level=0.08;
337     for (int i=0;i<nbins;i++){
338     hY0tmp->SetBinContent(i+1,(level+hY0VsRun->GetBinContent(i+1)));
339     hY0tmp->SetBinError(i+1,hY0VsRun->GetBinError(i+1));
340     }
341     hY0tmp->SetMarkerStyle(21);
342     hY0tmp->SetMarkerColor(2);
343     hY0tmp->SetLineColor(2);
344     hY0tmp->Draw("samee1][");
345     // cout << gPad->GetUymin() << endl;
346     TGaxis*axis=new TGaxis(gPad->GetUxmax(),gPad->GetUymin(),gPad->GetUxmax(),gPad->GetUymax(),ymin-level,ymax-level,510,"+L");
347     axis->SetLineColor(2);
348     axis->SetLabelColor(2);
349     axis->SetLabelFont(42);
350     axis->SetLabelSize(0.035);
351     axis->Draw();
352    
353     ppt->SetX1NDC(0.36);
354     ppt->SetY1NDC(0.78);
355     ppt->SetX2NDC(0.76);
356     ppt->SetY2NDC(0.86);
357     ppt->Draw();
358    
359     TLegend*l0=new TLegend(0.55,0.6,0.63,0.7);
360     l0->SetFillStyle(0);
361     l0->AddEntry(hX0VsRun," X_{0}","p");
362     // l0->AddEntry(hY0VsRun," Y_{0}","p");
363     l0->AddEntry(hY0tmp," Y_{0}","p");
364     l0->Draw();
365     // cTmp.Print(TString(hY0VsRun->GetTitle())+".png");
366     cTmp.Print("Transverse Beam Potition vs Run number.png");
367    
368     hX0VsFill->GetYaxis()->SetTitle("Transverse position of beam spots (cm)");
369     hX0VsFill->GetXaxis()->LabelsOption("u");
370     hX0VsFill->Draw("e1");
371     hX0VsFill->GetYaxis()->SetRangeUser(ymin,ymax);
372     // hY0VsFill->SetMarkerStyle(21);
373     // hY0VsFill->SetMarkerColor(2);
374     // hY0VsFill->SetLineColor(2);
375     // hY0VsFill->Draw("samee1][");
376     hY0tmp->Draw("samee1][");
377     axis->Draw();
378     ppt->Draw();
379     // TLegend*l1=new TLegend(0.6,0.5,0.68,0.6);
380     // l1->SetFillStyle(0);
381     // l1->AddEntry(hX0VsFill," X_{0}","p");
382     // l1->AddEntry(hY0VsFill," Y_{0}","p");
383     l0->Draw();
384     cTmp.Print("Transverse Beam Potition vs Fill Number.png");
385    
386     rootFile.Close();
387     }
388    
389     ostream& operator<<(ostream& out, const ReadBeamSpot& reader){
390     return (out << reader.textFound_.str().c_str());
391     }
392    
393     size_t ReadBeamSpot::ci_find(string& str1, string str2, int stringPos){
394     string::iterator pos = search(str1.begin()+stringPos, str1.end(), str2.begin(), str2.end(), myci_equal);
395     if (pos == str1.end())
396     return string::npos;
397     else
398     return pos - str1.begin();
399     }