/* * The Ferromagnet_Scan function reads in * a data file of magentic field scans * from within the ferromagnet and returns * a graph of the magnetic permeability * of the ferromagnet vs the external field * provided by the Helmholtz coil. */ TGraphErrors* plot_uvB( const TString scan_file, TF1* calib_fit, double R, double R_sig ) { /*Read in Data File to ROOT Tree*/ cout<< "processing file " << scan_file <<endl; TTree *TData = new TTree(); TData->ReadFile(scan_file, "t/D:I:B"); /*Use TTree Draw command to write branches to usable arrays*/ int n = TData->Draw("I:TMath::Abs(B):0.0:0.005", "", "goff"); vector<double> B_ext, B_in, Bratio, u; for(int i = 0; i < n; i++) { B_ext.push_back( calib_fit->Eval(TData->GetV1()[i]) ); B_in.push_back( TData->GetV2()[i] ); Bratio.push_back( B_in[i] / B_ext[i]); u.push_back( (Bratio[i]*(R**2) + Bratio[i] - 2 - 2*sqrt((Bratio[i]**2)*(R**2) - Bratio[i]*(R**2) - Bratio[i] + 1 ) ) / (Bratio[i]*(R**2) - Bratio[i]) ); } TGraphErrors *g_uvB = new TGraphErrors(n, &B_ext[0], &u[0], TData->GetV3(), TData->GetV4()); /* g_uvB->Fit("pol1", "", "", 10, 60); cout << "Permeability at 50: " << g_uvB->GetFunction("pol1")->Eval(50) << endl; cout << "Permeability at 40: " << g_uvB->GetFunction("pol1")->Eval(40) << endl; cout << "Permeability at 30: " << g_uvB->GetFunction("pol1")->Eval(30) << endl; */ return g_uvB; }
makePlot_check_all() { gStyle->SetOptStat(0); vector< TString > v_infile; // v_infile.push_back( TString("output/fitResults_run001_1layer_sheath.root") ); // v_infile.push_back( TString("output/fitResults_run002_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run003_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run004_2layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run005_2layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run006_3layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run007_4layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run008_2layer_cylinder.root") ); v_infile.push_back( TString("output/fitResults_run009_2layer_cylinder.root") ); // v_infile.push_back( TString("output/fitResults_run010_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run011_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run012_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run013_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run014_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run015_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run016_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run017_2layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run018_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run019_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run020_1layer_sheath.root") ); v_infile.push_back( TString("output/fitResults_run021_1layer.root") ); for ( unsigned f = 0; f < v_infile.size(); f++ ) { cout << "Plot " << v_infile.at(f) << endl; TCanvas *c0 = new TCanvas(); TFile *fin = new TFile( v_infile.at(f) ); TTree *tin = (TTree*)fin->Get("fitResults"); tin->Draw("bshield:bext:bshield_err:bext_err"); TGraphErrors *gshield = new TGraphErrors( tin->GetEntries(), tin->GetV2(), tin->GetV1(), tin->GetV4(), tin->GetV3() ); gshield->SetTitle(v_infile.at(f)); gshield->GetXaxis()->SetTitle("B_{ext} [mT]"); gshield->GetYaxis()->SetTitle("B_{shield} [mT]"); gshield->Draw("AP"); fin->Close(); if ( f == 0 ) c0->Print("plot_check_all.ps("); else if ( f == v_infile.size() - 1 ) c0->Print("plot_check_all.ps)"); else c0->Print("plot_check_all.ps"); } }
int plot_Fun4All_All_DeltaEta_DeltaPhi() { const std::string inFile = "LeptoAna_1000events_All.root"; const std::string inDirectory = "/gpfs/mnt/gpfs02/phenix/scratch/jlab/Leptoquark/"; std::string inputFile = inDirectory+inFile; TFile *f = TFile::Open(inputFile.c_str()); TTree *t = (TTree*)f->Get("ntp_leptoquark"); // const int Nevent = t->GetMaximum("event"); const int Nevent = 100; cout << "Running " << Nevent << " events" << endl; const int Nentries1 = t->Draw("isMaxEnergyJet","(isMaxEnergyJet<10)*(calorimeterid<10)","goff"); Double_t *arr_jet = t->GetV1(); vector<int> v_jet(Nentries1); for(int i = 0; i < Nentries1; i++) { v_jet[i] = (int)arr_jet[i]; } const int Nentries = t->Draw("towereta:towerphi:towerenergy:event","(isMaxEnergyJet<10)*(calorimeterid<10)","goff"); Double_t *arr_eta = t->GetV1(); Double_t *arr_phi = t->GetV2(); Double_t *arr_e = t->GetV3(); Double_t *arr_event = t->GetV4(); if(Nentries1 != Nentries) { cerr << "ERROR: Draw commands to not return the same dimensions. Check that your logical expressions are the same." << endl; return -1; } vector<double> v_DeltaEta, v_DeltaPhi, v_DeltaTheta, v_Energy; vector<double> v_DeltaEta_j2, v_DeltaPhi_j2, v_DeltaTheta_j2, v_Energy_j2; for(int i = 0; i < Nevent; i++) { double Emax = 0; int Emax_i = 0; double Emax_j2 = 0; int Emax_i_j2 = 0; for(int j = 0; j < Nentries; j++) { if((t->GetV4()[j]-1 == i) && (v_jet[j] == 1)) { if(t->GetV3()[j] > Emax) { Emax = t->GetV3()[j]; Emax_i = j; } } if((t->GetV4()[j]-1 == i) && (v_jet[j] == 2)) { if(t->GetV3()[j] > Emax_j2) { Emax_j2 = t->GetV3()[j]; Emax_i_j2 = j; } } } for(int j = 0; j < Nentries; j++) { if((t->GetV4()[j]-1 == i) && (v_jet[j] == 1)) { v_DeltaEta.push_back(t->GetV1()[j] - t->GetV1()[Emax_i]); v_DeltaTheta.push_back(2*TMath::ATan(TMath::Power(TMath::E(),-1*t->GetV1()[j])) - 2*TMath::ATan(TMath::Power(TMath::E(),-1*t->GetV1()[Emax_i]))); v_DeltaPhi.push_back(t->GetV2()[j] - t->GetV2()[Emax_i]); v_Energy.push_back(t->GetV3()[j]); } if((t->GetV4()[j]-1 == i) && (v_jet[j] == 2)) { v_DeltaEta_j2.push_back(t->GetV1()[j] - t->GetV1()[Emax_i_j2]); v_DeltaTheta_j2.push_back(2*TMath::ATan(TMath::Power(TMath::E(),-1*t->GetV1()[j])) - 2*TMath::ATan(TMath::Power(TMath::E(),-1*t->GetV1()[Emax_i_j2]))); v_DeltaPhi_j2.push_back(t->GetV2()[j] - t->GetV2()[Emax_i_j2]); v_Energy_j2.push_back(t->GetV3()[j]); } } } //----------------------------------------------------------------------------------------------------------- gStyle->SetOptStat(0); double xmin = -1; double xmax = 1; double ymin = -0.5; double ymax = 0.5; std::string title = "isMaxJetEnergy = 1"; TCanvas *c1 = new TCanvas(); TH2D *h1 = new TH2D("h1",title.c_str(),40,xmin,xmax,40,ymin,ymax); for(int i = 0; (unsigned)i < v_DeltaEta.size(); i++) { h1->Fill(v_DeltaEta[i],v_DeltaPhi[i], v_Energy[i]/Nevent); } c1->SetLogz(); h1->Draw("colz"); // h1->SetMinimum(1); // h1->SetMaximum(1000); h1->GetXaxis()->SetTitle("#Delta#eta"); h1->GetYaxis()->SetTitle("#Delta#phi"); c1->Update(); //----------------------------------------------------------------------------------------------------------- std::string title2 = "isMaxJetEnergy = 2"; TCanvas *c2 = new TCanvas(); TH2D *h2 = new TH2D("h2",title2.c_str(),40,xmin,xmax,40,ymin,ymax); for(int i = 0; (unsigned)i < v_DeltaEta_j2.size(); i++) { h2->Fill(v_DeltaEta_j2[i],v_DeltaPhi_j2[i], v_Energy_j2[i]/Nevent); } c2->SetLogz(); h2->Draw("colz"); // h2->SetMinimum(1); // h2->SetMaximum(1000); h2->GetXaxis()->SetTitle("#Delta#eta"); h2->GetYaxis()->SetTitle("#Delta#phi"); c2->Update(); //----------------------------------------------------------------------------------------------------------- TCanvas *c3 = new TCanvas(); TH1D *h3 = h1->ProjectionY(); h3->SetLineColor(kGreen+3); TH1D *h4 = h2->ProjectionY(); h4->SetLineColor(kBlue); TF1 *f3 = new TF1("f3", "gaus", ymin, ymax); f3->SetLineColor(kGreen+3); h3->Fit("f3","Q R"); TF1 *f4 = new TF1("f4", "gaus", ymin, ymax); f4->SetLineColor(kBlue+2); h4->Fit("f4","Q R"); h3->Draw(); h4->Draw("SAME"); TLegend *leg = new TLegend(0.2,0.9,0.7,0.75); leg->SetBorderSize(1); leg->AddEntry(h3,title.c_str(),"l"); leg->AddEntry(h4,title2.c_str(),"l"); leg->Draw("SAME"); c3->Update(); cout << endl; cout << "*******************************************************" << endl; cout << "Ratio of Gaussian Width to Max Energy:" << endl; cout << " for: " << title << " -> " << f3->GetParameter(2) << " / " << h3->GetBinContent(h3->GetMaximumBin()) << " = " << f3->GetParameter(2) / h3->GetBinContent(h3->GetMaximumBin()) << endl; cout << " for: " << title2 << " -> " << f4->GetParameter(2) << " / " << h4->GetBinContent(h4->GetMaximumBin()) << " = " << f4->GetParameter(2) / h4->GetBinContent(h4->GetMaximumBin()) << endl; cout << " Ratio of ratios: " << (f3->GetParameter(2) / h3->GetBinContent(h3->GetMaximumBin())) / (f4->GetParameter(2) / h4->GetBinContent(h4->GetMaximumBin())) << endl; cout << "*******************************************************" << endl; cout << endl; c1->Close(); c2->Close(); c3->Close(); //----------------------------------------------------------------------------------------------------------- vector<double> v_Ratios; for(int i = 0; i < Nevent; i++) { TH1D *h5 = new TH1D("h5","",20,ymin*2,ymax*2); TH1D *h6 = new TH1D("h6","",20,ymin*2,ymax*2); for(int j = 0; j < Nentries/100; j++) { if((t->GetV4()[j]-1 == i) && (v_jet[j] == 1)) { h5->Fill(v_DeltaPhi[j], v_Energy[j]); } if((t->GetV4()[j]-1 == i) && (v_jet[j] == 2)) { h6->Fill(v_DeltaPhi_j2[j], v_Energy_j2[j]); } } TF1 *f5 = new TF1("f5", "gaus", ymin, ymax); f3->SetLineColor(kGreen+3); h5->Fit("f5","Q"); TF1 *f6 = new TF1("f6", "gaus", ymin, ymax); f6->SetLineColor(kBlue+2); h6->Fit("f6","Q"); TCanvas *c5 = new TCanvas(); h5->Draw(); TCanvas *c6 = new TCanvas(); h6->Draw(); // cout << endl; // cout << "Ratio of Gaussian Width to Max Energy:" << endl; // cout << " for: " << title << " -> " << f5->GetParameter(2) << " / " << h5->GetBinContent(h5->GetMaximumBin()) // << " = " << f5->GetParameter(2) / h5->GetBinContent(h5->GetMaximumBin()) << endl; // cout << " for: " << title2 << " -> " << f6->GetParameter(2) << " / " << h6->GetBinContent(h6->GetMaximumBin()) // << " = " << f6->GetParameter(2) / h6->GetBinContent(h6->GetMaximumBin()) << endl; cout << " Ratio of ratios for event " << i+1 << " : " << (f5->GetParameter(2) / h5->GetBinContent(h5->GetMaximumBin())) / (f6->GetParameter(2) / h6->GetBinContent(h6->GetMaximumBin())) << endl; if((f5->GetParameter(2) / h5->GetBinContent(h5->GetMaximumBin())) / (f6->GetParameter(2) / h6->GetBinContent(h6->GetMaximumBin())) != NAN) v_Ratios.push_back((f5->GetParameter(2) / h5->GetBinContent(h5->GetMaximumBin())) / (f6->GetParameter(2) / h6->GetBinContent(h6->GetMaximumBin()))); delete h5; delete h6; c5->Close(); c6->Close(); } TCanvas *c7 = new TCanvas(); TH1D *h7 = new TH1D("h7","",400,-200,200); for(int i = 0; (unsigned)i < v_Ratios.size(); i++) { h7->Fill(v_Ratios[i]); } h7->Draw(); return 0; }
int TrackParametrization( TString csvfile="fitslices_out.csv" ) { /* Read data from input file */ TTree *tres = new TTree(); tres->ReadFile( csvfile, "ptrue:etatrue:psig:psig_err:pmean:pmean_err:norm", ',' ); /* Print read-in tree */ tres->Print(); /* colors array */ unsigned colors[8] = {1,2,3,4,6,7,14,16}; /* Create vector of theta values to include for visualization*/ vector< double > etas_vis; etas_vis.push_back(-2.75); etas_vis.push_back(-2.25); etas_vis.push_back(-1.75); etas_vis.push_back(-0.25); etas_vis.push_back( 0.25); etas_vis.push_back( 1.75); etas_vis.push_back( 2.25); // etas_vis.push_back(-3.25); // etas_vis.push_back(-2.25); // etas_vis.push_back(-1.25); // etas_vis.push_back(-0.25); // etas_vis.push_back( 0.25); // etas_vis.push_back( 1.25); // etas_vis.push_back( 2.25); // etas_vis.push_back( 3.25); /* Create vector of theta values to include for fitting*/ vector< double > etas_fit; for ( double eta = -4.45; eta < 4.5; eta += 0.1 ) etas_fit.push_back( eta ); /* Create fit function */ TF1* f_momres = new TF1("f_momres", "sqrt( [0]*[0] + [1]*[1]*x*x )" ); cout << "\nFit function: " << f_momres->GetTitle() << "\n" << endl; /* Create scratch canvas */ TCanvas *cscratch = new TCanvas("cscratch"); /* Create framehistogram */ TH1F* hframe = new TH1F("hframe","",100,0,40); hframe->GetYaxis()->SetRangeUser(0,0.15); hframe->GetYaxis()->SetNdivisions(505); hframe->GetXaxis()->SetTitle("Momentum (GeV/c)"); hframe->GetYaxis()->SetTitle("#sigma_{p}/p"); /* create combined canvas plot */ TCanvas *c1 = new TCanvas(); hframe->Draw(); /* Create legend */ TLegend* leg_eta = new TLegend( 0.2, 0.6, 0.5, 0.9); leg_eta->SetNColumns(2); /* Create ofstream to write fit parameter results */ ofstream ofsfit("track_momres_new.csv"); ofsfit<<"eta,par1,par1err,par2,par2err"<<endl; /* Create resolution-vs-momentum plot with fits for each selected theta value */ for ( int i = 0; i < etas_fit.size(); i++ ) { /* Switch to scratch canvas */ cscratch->cd(); double eta = etas_fit.at(i); /* No tracking outside -4 < eta < 4 */ if ( eta < -4 || eta > 4 ) continue; cout << "\n***Eta = " << eta << endl; /* Define range of theta because float comparison with fixed value doesn't work too well for cuts in ROOT trees */ double eta_min = eta * 0.999; double eta_max = eta * 1.001; /* Cut for tree */ TCut cutx( Form("ptrue > 1 && ( (etatrue > 0 && (etatrue > %f && etatrue < %f)) || (etatrue < 0 && (etatrue < %f && etatrue > %f)) )", eta_min, eta_max, eta_min, eta_max) ); /* "Draw" tree on scratch canvas to fill V1...V4 arrays */ tres->Draw("psig:ptrue:psig_err:0", cutx ); /* Create TGraphErrors with selected data from tree */ TGraphErrors *gres = new TGraphErrors( tres->GetEntries(cutx), &(tres->GetV2())[0], &(tres->GetV1())[0], &(tres->GetV4())[0], &(tres->GetV3())[0] ); /* reset function parameters before fit */ f_momres->SetParameter(0,0.1); f_momres->SetParameter(1,0.1); /* Only plot pseudorapidities listed on etas_vis; if not plotting, still do the fit */ bool vis = false; int vi = 0; for ( vi = 0; vi < etas_vis.size(); vi++ ) { if ( abs( etas_vis.at(vi) - eta ) < 0.001 ) { vis = true; break; } } if ( vis ) { /* Add graph to legend */ leg_eta->AddEntry(gres, Form("#eta = %.1f", eta), "P"); /* Add graph to plot */ c1->cd(); gres->SetMarkerColor(colors[vi]); gres->Draw("Psame"); f_momres->SetLineColor(colors[vi]); gres->Fit(f_momres); } else { gres->Fit(f_momres); } /* Write fir results to file */ double par1 = f_momres->GetParameter(0); double par1err = f_momres->GetParError(0); double par2 = f_momres->GetParameter(1); double par2err = f_momres->GetParError(1); ofsfit << eta << "," << par1 << "," << par1err << "," << par2 << "," << par2err << endl; } /* Draw legend */ c1->cd(); //TCanvas *c2 = new TCanvas(); //hframe->Draw(); leg_eta->Draw(); /* Print plots */ c1->Print("track_momres_vareta.eps"); //c2->Print("track_momres_vareta_legend.eps"); /* Close output stream */ ofsfit.close(); return 0; }