void ExpManager::GetExp2DGraph(TString NameTitle, double xmin, double xmax, double ymin, double ymax, double zmin, double zmax) { TGraph2DErrors *fGraph = new TGraph2DErrors(); //= new TGraph2DErrors(np, x_array, y_array, bz_array, ex, ey, ez); fGraph->SetTitle("Exp Data;Y (mm);X (mm);Magnetic Field (mT)"); fGraph->SetMarkerSize(1.2); fGraph->SetMarkerStyle(20); fGraph->SetMarkerColor(kBlue); fGraph->SetLineColor(kBlue); fGraph->SetLineWidth(2); int graph_counter = 0 ; for (unsigned i=0; i< fExpY.size(); i++) { if( (fExpX.at(i) >= xmin && fExpX.at(i) <= xmax) && (fExpY.at(i) >= ymin && fExpY.at(i) <= ymax) && (fExpZ.at(i) >= zmin && fExpZ.at(i) <= zmax) ) { fGraph->SetPoint(graph_counter,fExpY.at(i),fExpX.at(i),fExpB.at(i)); fGraph->SetPointError(graph_counter,fExpYErr.at(i),fExpXErr.at(i),fExpBErr.at(i)); graph_counter++; } } fGraph->SetTitle(NameTitle+Form(" Experimental Data : %.2f < X < %.2f mm __ %.2f < Y < %.2f mm;Y (mm);X (mm);Magnetic Field (mT)",xmin,xmax,ymin,ymax)); fGraph->SetName(NameTitle+Form("_Exp_X_%.2f_%.2fmm_Y_%.2f_%.2fmm",xmin,xmax,ymin,ymax)); fGraph->Write(); }
void misalignmentDependence(TCanvas *c1old, Int_t nFiles,TString *names,TString misalignment,Double_t *values,Double_t *phases,TString xvar,TString yvar, TF1 *function,Int_t parameter,TString parametername = "",TString functionname = "", Bool_t resolution = false, TString saveas = "") { cout << saveas << endl; if (c1old == 0) return; c1old = (TCanvas*)c1old->Clone("c1old"); if (misalignment == "" || yvar == "") return; Bool_t drawfits = (parameter < 0); if (parameter < 0) parameter = -parameter - 1; //-1 --> 0, -2 --> 1, -3 --> 2, ... TString yaxislabel = nPart(1,parametername); TString parameterunits = nPart(2,parametername); if (parameterunits != "") yaxislabel.Append(" (").Append(parameterunits).Append(")"); TList *list = c1old->GetListOfPrimitives(); int n = list->GetEntries() - 2 - (xvar == ""); setTDRStyle(); gStyle->SetOptStat(0); gStyle->SetOptFit(0); gStyle->SetFitFormat("5.4g"); gStyle->SetFuncColor(2); gStyle->SetFuncStyle(1); gStyle->SetFuncWidth(1); if (!drawfits) { gStyle->SetCanvasDefW(678); gStyle->SetPadRightMargin(0.115); } TH1 **p = new TH1*[n]; TF1 **f = new TF1*[n]; Bool_t used[n]; for (Int_t i = 0; i < n; i++) { stringstream s0; s0 << "p" << i; TString pname = s0.str(); p[i] = (TH1*)list->/*At(i+1+(xvar == ""))*/FindObject(pname); used[i] = (p[i] != 0); if (used[i]) p[i]->SetDirectory(0); if (xvar == "" && function == 0) continue; stringstream s; s << function->GetName() << i; TString newname = s.str(); f[i] = (TF1*)function->Clone(newname); stufftodelete->Add(f[i]); } Double_t *result = new Double_t[nFiles]; Double_t *error = new Double_t[nFiles]; if (xvar == "" && function == 0) { yaxislabel = axislabel(yvar,'y',resolution); for (Int_t i = 0; i < nFiles; i++) { if (!used[i]) continue; if (!resolution) { result[i] = p[i]->GetMean(); error[i] = p[i]->GetMeanError(); } else { result[i] = p[i]->GetRMS(); error[i] = p[i]->GetRMSError(); } cout << result[i] << " +/- " << error[i] << endl; } } else { for (int i = 0; i < n; i++) { if (!used[i]) continue; f[i]->SetLineColor(colors[i]); f[i]->SetLineStyle(styles[i]); f[i]->SetLineWidth(1); p[i]->SetMarkerColor(colors[i]); p[i]->SetMarkerStyle(20+i); p[i]->SetLineColor(colors[i]); p[i]->SetLineStyle(styles[i]); p[i]->Fit(f[i],"IM"); error[i] = f[i]->GetParError (parameter); if (function->GetName() == TString("sine")) { if (f[i]->GetParameter(0) < 0) { f[i]->SetParameter(0,-f[i]->GetParameter(0)); f[i]->SetParameter(2,f[i]->GetParameter(2)+pi); } while(f[i]->GetParameter(2) >= 2*pi) f[i]->SetParameter(2,f[i]->GetParameter(2)-2*pi); while(f[i]->GetParameter(2) < 0) f[i]->SetParameter(2,f[i]->GetParameter(2)+2*pi); } result[i] = f[i]->GetParameter(parameter); } } TCanvas *c1 = TCanvas::MakeDefCanvas(); if (drawfits && !(xvar == "" && function == 0) && yvar != "") { TString legendtitle = "["; legendtitle.Append(functionname); legendtitle.Append("]"); TLegend *legend = new TLegend(.7,.7,.9,.9,legendtitle,"br"); stufftodelete->Add(legend); TString drawoption = ""; TH1 *maxp = (TH1*)list->FindObject("maxp"); if (maxp != 0) { maxp->Draw(); drawoption = "same"; } for (int i = 0; i < n; i++) { if (!used[i]) continue; p[i]->Draw(drawoption); f[i]->Draw("same"); drawoption = "same"; stringstream s; s.precision(3); s << nPart(1,parametername) << " = " << result[i] << " #pm " << error[i]; if (parameterunits != "") s << " " << parameterunits; TString str = s.str(); legend->AddEntry(p[i],names[i],"pl"); legend->AddEntry(f[i],str,"l"); } c1->Update(); Double_t x1min = .98*gPad->GetUxmin() + .02*gPad->GetUxmax(); Double_t x2max = .02*gPad->GetUxmin() + .98*gPad->GetUxmax(); Double_t y1min = .98*gPad->GetUymin() + .02*gPad->GetUymax(); Double_t y2max = .02*gPad->GetUymin() + .98*gPad->GetUymax(); Double_t width = .4*(x2max-x1min); Double_t height = (1./20)*legend->GetListOfPrimitives()->GetEntries()*(y2max-y1min); width *= 2; height /= 2; legend->SetNColumns(2); Double_t newy2max = placeLegend(legend,width,height,x1min,y1min,x2max,y2max); p[0]->GetYaxis()->SetRangeUser(gPad->GetUymin(),(newy2max-.02*gPad->GetUymin())/.98); if (maxp != 0) maxp->GetYaxis()->SetRangeUser(gPad->GetUymin(),(newy2max-.02*gPad->GetUymin())/.98); legend->SetFillStyle(0); legend->Draw(); } else { if (values == 0) return; Bool_t phasesmatter = false; if (misalignment == "elliptical" || misalignment == "sagitta" || misalignment == "skew") { if (phases == 0) { cout << "This misalignment has a phase, but you didn't supply the phases!" << endl << "Can't produce plots depending on the misalignment value." << endl; return; } int firstnonzero = -1; for (Int_t i = 0; i < nFiles; i++) { if (values[i] == 0) continue; //if the amplitude is 0 the phase is arbitrary if (firstnonzero == -1) firstnonzero = i; if (phases[i] != phases[firstnonzero]) phasesmatter = true; } } if (!phasesmatter) { TGraphErrors *g = new TGraphErrors(nFiles,values,result,(Double_t*)0,error); g->SetName(""); stufftodelete->Add(g); TString xaxislabel = "#epsilon_{"; xaxislabel.Append(misalignment); xaxislabel.Append("}"); g->GetXaxis()->SetTitle(xaxislabel); if (xvar != "") { yaxislabel.Append(" ["); yaxislabel.Append(functionname); yaxislabel.Append("]"); } g->GetYaxis()->SetTitle(yaxislabel); g->SetMarkerColor(colors[0]); g->SetMarkerStyle(20); g->Draw("AP"); Double_t yaxismax = g->GetYaxis()->GetXmax(); Double_t yaxismin = g->GetYaxis()->GetXmin(); if (yaxismin > 0) { yaxismax += yaxismin; yaxismin = 0; } g->GetYaxis()->SetRangeUser(yaxismin,yaxismax); g->Draw("AP"); } else { double *xvalues = new double[nFiles]; double *yvalues = new double[nFiles]; //these are not physically x and y (except in the case of skew) for (int i = 0; i < nFiles; i++) { xvalues[i] = values[i] * cos(phases[i]); yvalues[i] = values[i] * sin(phases[i]); } TGraph2DErrors *g = new TGraph2DErrors(nFiles,xvalues,yvalues,result,(Double_t*)0,(Double_t*)0,error); g->SetName(""); stufftodelete->Add(g); delete[] xvalues; //A TGraph2DErrors has its own copy of xvalues and yvalues, so it's ok to delete these copies. delete[] yvalues; TString xaxislabel = "#epsilon_{"; xaxislabel.Append(misalignment); xaxislabel.Append("}cos(#delta)"); TString realyaxislabel = xaxislabel; realyaxislabel.ReplaceAll("cos(#delta)","sin(#delta)"); g->GetXaxis()->SetTitle(xaxislabel); g->GetYaxis()->SetTitle(realyaxislabel); TString zaxislabel = /*"fake"*/yaxislabel; //yaxislabel is defined earlier if (xvar != "") { zaxislabel.Append(" ["); zaxislabel.Append(functionname); zaxislabel.Append("]"); } g->GetZaxis()->SetTitle(zaxislabel); g->SetMarkerStyle(20); g->Draw("pcolerr"); } } if (saveas != "") { saveplot(c1,saveas); delete[] p; delete[] f; delete[] result; delete[] error; delete c1old; } }