void Avalanche::DrawDrift() { if (dx0->size() < 2) return; // Bounding box Int_t min_t_i = min_element(dt1->begin(), dt1->end()) - dt1->begin(); Int_t max_t_i = max_element(dt1->begin(), dt1->end()) - dt1->begin(); TPolyLine3D* boundingBox = new TPolyLine3D(2); boundingBox->SetPoint(0, dx1->at(max_t_i), dy1->at(max_t_i), dz0->at(max_t_i)); boundingBox->SetPoint(1, dx1->at(min_t_i), dy1->at(min_t_i), dz1->at(min_t_i)); boundingBox->SetLineWidth(2); boundingBox->SetLineColor(kGreen); boundingBox->Draw(); // Drift electrons for (uint e=1; e<dx0->size(); e++) { TPolyLine3D* track = new TPolyLine3D(2); track->SetPoint(0, dx0->at(e), dy0->at(e), dz0->at(e)); track->SetPoint(1, dx1->at(e), dy1->at(e), dz1->at(e)); track->SetLineWidth(1); track->SetLineStyle(3); track->SetLineColor(kBlue); track->Draw(); } }
void Avalanche::DrawPhotoconversion() { TPolyLine3D* track = new TPolyLine3D(2); track->SetPoint(0, pcPosx, pcPosy, pcPosz); track->SetPoint(1, pcPosx + .5*pcPx, pcPosy + .5*pcPy, pcPosz + .5*pcPz); track->SetLineWidth(2); track->SetLineColor(kRed); track->Draw(); }
void DrawRays() { for(int i = 0; i < kNtheta; i++){ TObjArray* focused = gArray[i]->GetFocused(); for(Int_t j = 0; j <= focused->GetLast(); j++){ ARay* ray = (ARay*)(*focused)[j]; TPolyLine3D* pol = ray->MakePolyLine3D(); pol->SetLineColor(i + 2); pol->Draw(); } // j } // i }
void EventDisplay::run(TestBeamEvent *event, Clipboard *clipboard) { TestBeamClusters* clusters=(TestBeamClusters*)clipboard->get("Clusters"); if(clusters==NULL) return; if(clusters->size()<2) return; // plot clusters int graphCounter=-1; for(TestBeamClusters::iterator iter=clusters->begin();iter!=clusters->end();iter++){ TestBeamCluster *cluster = *iter; std::string chip=cluster->element()->detectorId(); graph->SetPoint( graphCounter++ , cluster->globalX(),cluster->globalY(),cluster->globalZ() ); } graph->Set(graphCounter); TH2F* h=(TH2F*)hhh->Clone(); // time_t time(event->getElement(0)->timeStamp()); // std::string timeString(asctime(localtime(&time))); // CommonTools::stringtrim(timeString); h->SetTitle((std::string(h->GetTitle())).c_str()); // h->SetTitle((timeString+std::string(h->GetTitle())).c_str()); graph->SetHistogram(h); graph->GetHistogram()->Draw(); gStyle->SetPalette(1); graph->Draw("pcol"); // plot tracks TestBeamTracks* tracks=(TestBeamTracks*)clipboard->get("Tracks"); if(tracks){ for(TestBeamTracks::iterator iter=tracks->begin();iter!=tracks->end();iter++){ TestBeamTrack *track=(*iter); TPolyLine3D *aline = new TPolyLine3D(2); aline->SetPoint( 0, track->firstState()->X(),track->firstState()->Y(),track->firstState()->Z() ); aline->SetPoint( 1, track->firstState()->X()+track->slopeXZ()*(h->GetMaximum()-5), track->firstState()->Y()+track->slopeYZ()*(h->GetMaximum()-5),h->GetMaximum()-5); aline->SetLineColor(2); aline->Draw(); } } cCC1->Update(); app->Run(true); graph->Clear(); }
Int_t line3Dfit() { gStyle->SetOptStat(0); gStyle->SetOptFit(); //double e = 0.1; Int_t nd = 10000; // double xmin = 0; double ymin = 0; // double xmax = 10; double ymax = 10; TGraph2D * gr = new TGraph2D(); // Fill the 2D graph double p0[4] = {10,20,1,2}; // generate graph with the 3d points for (Int_t N=0; N<nd; N++) { double x,y,z = 0; // Generate a random number double t = gRandom->Uniform(0,10); line(t,p0,x,y,z); double err = 1; // do a gaussian smearing around the points in all coordinates x += gRandom->Gaus(0,err); y += gRandom->Gaus(0,err); z += gRandom->Gaus(0,err); gr->SetPoint(N,x,y,z); //dt->SetPointError(N,0,0,err); } // fit the graph now ROOT::Fit::Fitter fitter; // make the functor objet SumDistance2 sdist(gr); #ifdef __CINT__ ROOT::Math::Functor fcn(&sdist,4,"SumDistance2"); #else ROOT::Math::Functor fcn(sdist,4); #endif // set the function and the initial parameter values double pStart[4] = {1,1,1,1}; fitter.SetFCN(fcn,pStart); // set step sizes different than default ones (0.3 times parameter values) for (int i = 0; i < 4; ++i) fitter.Config().ParSettings(i).SetStepSize(0.01); bool ok = fitter.FitFCN(); if (!ok) { Error("line3Dfit","Line3D Fit failed"); return 1; } const ROOT::Fit::FitResult & result = fitter.Result(); std::cout << "Total final distance square " << result.MinFcnValue() << std::endl; result.Print(std::cout); gr->Draw("p0"); // get fit parameters const double * parFit = result.GetParams(); // draw the fitted line int n = 1000; double t0 = 0; double dt = 10; TPolyLine3D *l = new TPolyLine3D(n); for (int i = 0; i <n;++i) { double t = t0+ dt*i/n; double x,y,z; line(t,parFit,x,y,z); l->SetPoint(i,x,y,z); } l->SetLineColor(kRed); l->Draw("same"); // draw original line TPolyLine3D *l0 = new TPolyLine3D(n); for (int i = 0; i <n;++i) { double t = t0+ dt*i/n; double x,y,z; line(t,p0,x,y,z); l0->SetPoint(i,x,y,z); } l0->SetLineColor(kBlue); l0->Draw("same"); return 0; }
void view_SMEvents_3D_from_Hits() { /*** Displays an 3D occupancy plot for each SM Event. (stop mode event) Can choose which SM event to start at. (find "CHOOSE THIS" in this script) Input file must be a Hits file (_interpreted_Hits.root file). ***/ gROOT->Reset(); // Setting up file, treereader, histogram TFile *f = new TFile("/home/pixel/pybar/tags/2.0.2_new/pyBAR-master/pybar/module_202_new/101_module_202_new_stop_mode_ext_trigger_scan_interpreted_Hits.root"); if (!f) { // if we cannot open the file, print an error message and return immediately cout << "Error: cannot open the root file!\n"; //return; } TTreeReader *reader = new TTreeReader("Table", f); TTreeReaderValue<UInt_t> h5_file_num(*reader, "h5_file_num"); TTreeReaderValue<Long64_t> event_number(*reader, "event_number"); TTreeReaderValue<UChar_t> tot(*reader, "tot"); TTreeReaderValue<UChar_t> relative_BCID(*reader, "relative_BCID"); TTreeReaderValue<Long64_t> SM_event_num(*reader, "SM_event_num"); TTreeReaderValue<Double_t> x(*reader, "x"); TTreeReaderValue<Double_t> y(*reader, "y"); TTreeReaderValue<Double_t> z(*reader, "z"); // Initialize the canvas and graph TCanvas *c1 = new TCanvas("c1","3D Occupancy for Specified SM Event", 1000, 10, 900, 550); c1->SetRightMargin(0.25); TGraph2D *graph = new TGraph2D(); // Variables used to loop the main loop bool endOfReader = false; // if reached end of the reader bool quit = false; // if pressed q int smEventNum = 1; // the current SM-event CHOOSE THIS to start at desired SM event number // Main Loop (loops for every smEventNum) while (!endOfReader && !quit) { // Variables used in this main loop int startEntryNum = 0; int endEntryNum = 0; string histTitle = "3D Occupancy for SM Event "; string inString = ""; bool fitFailed = false; // true if the 3D fit failed bool lastEvent = false; // Declaring some important output values for the current graph and/or line fit int numEntries = 0; double sumSquares = 0; // Get startEntryNum and endEntryNum startEntryNum = getEntryNumWithSMEventNum(reader, smEventNum); endEntryNum = getEntryNumWithSMEventNum(reader, smEventNum + 1); if (startEntryNum == -2) { // can't find the smEventNum cout << "Error: There should not be any SM event numbers that are missing." << "\n"; } else if (startEntryNum == -3) { endOfReader = true; break; } else if (endEntryNum == -3) { // assuming no SM event nums are skipped endEntryNum = reader->GetEntries(false); lastEvent = true; } // Fill TGraph with points and set title and axes graph = new TGraph2D(); // create a new TGraph to refresh reader->SetEntry(startEntryNum); for (int i = 0; i < endEntryNum - startEntryNum; i++) { graph->SetPoint(i, (*x - 0.001), (*y + 0.001), (*z - 0.001)); endOfReader = !(reader->Next()); } histTitle.append(to_string(smEventNum)); graph->SetTitle(histTitle.c_str()); graph->GetXaxis()->SetTitle("x (mm)"); graph->GetYaxis()->SetTitle("y (mm)"); graph->GetZaxis()->SetTitle("z (mm)"); graph->GetXaxis()->SetLimits(0, 20); // ROOT is buggy, x and y use setlimits() graph->GetYaxis()->SetLimits(-16.8, 0); // but z uses setrangeuser() graph->GetZaxis()->SetRangeUser(0, 40.96); c1->SetTitle(histTitle.c_str()); // 3D Fit, display results, draw graph and line fit, only accept "good" events, get input if (!endOfReader || lastEvent) { // Display some results numEntries = graph->GetN(); cout << "Current SM Event Number: " << smEventNum << "\n"; cout << "Number of entries: " << numEntries << "\n"; // Starting the fit. First, get decent starting parameters for the fit - do two 2D fits (one for x vs z, one for y vs z) TGraph *graphZX = new TGraph(); TGraph *graphZY = new TGraph(); reader->SetEntry(startEntryNum); for (int i = 0; i < endEntryNum - startEntryNum; i++) { graphZX->SetPoint(i, (*z - 0.001), (*x + 0.001)); graphZY->SetPoint(i, (*z - 0.001), (*y + 0.001)); reader->Next(); } TFitResultPtr fitZX = graphZX->Fit("pol1", "WQS"); // w for ignore error of each pt, q for quiet (suppress results output), s for return a tfitresultptr TFitResultPtr fitZY = graphZY->Fit("pol1", "WQS"); Double_t param0 = fitZX->GetParams()[0]; Double_t param1 = fitZX->GetParams()[1]; Double_t param2 = fitZY->GetParams()[0]; Double_t param3 = fitZY->GetParams()[1]; // // Draw the lines for the two 2D fits // int n = 2; // TPolyLine3D *lineZX = new TPolyLine3D(n); // TPolyLine3D *lineZY = new TPolyLine3D(n); // lineZX->SetPoint(0, param0, 0, 0); // lineZX->SetPoint(1, param0 + param1 * 40.96, 0, 40.96); // lineZX->SetLineColor(kBlue); // lineZX->Draw("same"); // lineZY->SetPoint(0, 0, param2, 0); // lineZY->SetPoint(1, 0, param2 + param3 * 40.96, 40.96); // lineZY->SetLineColor(kGreen); // lineZY->Draw("same"); // 3D FITTING CODE (based on line3Dfit.C), draw graph and line fit ROOT::Fit::Fitter fitter; SumDistance2 sdist(graph); #ifdef __CINT__ ROOT::Math::Functor fcn(&sdist,4,"SumDistance2"); #else ROOT::Math::Functor fcn(sdist,4); #endif // set the function and the initial parameter values double pStart[4] = {param0,param1,param2,param3}; fitter.SetFCN(fcn,pStart); // set step sizes different than default ones (0.3 times parameter values) for (int i = 0; i < 4; ++i) fitter.Config().ParSettings(i).SetStepSize(0.01); bool ok = fitter.FitFCN(); if (!ok) { Error("line3Dfit","Line3D Fit failed"); fitFailed = true; } else { const ROOT::Fit::FitResult & result = fitter.Result(); const double * fitParams = result.GetParams(); sumSquares = result.MinFcnValue(); std::cout << "Sum of distance squares: " << sumSquares << std::endl; std::cout << "Sum of distance squares divided by numEntries: " << sumSquares/numEntries << std::endl; std::cout << "Theta : " << TMath::ATan(sqrt(pow(fitParams[1], 2) + pow(fitParams[3], 2))) << std::endl; // result.Print(std::cout); // (un)suppress results output // Draw the graph graph->SetMarkerStyle(8); graph->SetMarkerSize(0.5); graph->Draw("pcol"); // Draw the fitted line int n = 1000; double t0 = 0; // t is the z coordinate double dt = 40.96; TPolyLine3D *l = new TPolyLine3D(n); for (int i = 0; i <n;++i) { double t = t0+ dt*i/n; double x,y,z; line(t,fitParams,x,y,z); l->SetPoint(i,x,y,z); } l->SetLineColor(kRed); l->Draw("same"); // Access fit params and minfcnvalue // cout << "FIT1: " << fitParams[1] << "\n"; // cout << "FIT2: " << result.MinFcnValue() << "\n"; } // Criteria to be a good event (if not good entry, then don't show) bool isGoodEvent = false; // the following block of code finds the mean X, Y ans Z values double meanX = 0; double meanY = 0; double meanZ = 0; reader->SetEntry(startEntryNum); for (int i = 0; i < endEntryNum - startEntryNum; i++) { meanX += graph->GetX()[i]; meanY += graph->GetY()[i]; meanZ += graph->GetZ()[i]; reader->Next(); } meanX /= endEntryNum - startEntryNum; meanY /= endEntryNum - startEntryNum; meanZ /= endEntryNum - startEntryNum; // the following code block calculates the fraction of the hits in the smEvent that are inside a sphere, centered at the mean XYZ, of radius 'radius' (larger fraction means the track is less like a long streak and more like a dense blob) double radius = 1; // length in mm double fractionInsideSphere = 0; reader->SetEntry(startEntryNum); for (int i = 0; i < endEntryNum - startEntryNum; i++) { double distanceFromMeanXYZ = sqrt(pow(graph->GetX()[i] - meanX, 2) + pow(graph->GetY()[i] - meanY, 2) + pow(graph->GetZ()[i] - meanZ, 2)); if (distanceFromMeanXYZ <= 2) { fractionInsideSphere += 1; } reader->Next(); } fractionInsideSphere /= endEntryNum - startEntryNum; cout << "fraction inside sphere: " << fractionInsideSphere << "\n"; // if (numEntries >= 50 // && sumSquares/numEntries < 2.0 // && fractionInsideSphere < 0.8) { // isGoodEvent = true; // } isGoodEvent = true; if (isGoodEvent) { // won't show drawings or ask for input unless its a good event c1->Update(); // show all the drawings // handle input bool inStringValid = false; do { cout << "<Enter>: next event; 'b': previous SM event; [number]: specific SM event number; 'q': quit.\n"; getline(cin, inString); // Handles behavior according to input if (inString.empty()) { // <Enter> // leave things be inStringValid = true; } else if (inString.compare("b") == 0) { smEventNum -= 2; // because it gets incremented once at the end of this do while loop inStringValid = true; } else if (inString.compare("q") == 0 || inString.compare(".q") == 0) { quit = true; inStringValid = true; } else if (canConvertStringToPosInt(inString)) { smEventNum = convertStringToPosInt(inString) - 1; // -1 because it gets incremented once at the end of this do while loop inStringValid = true; } // else, leave inStringValid as false, so that it asks for input again } while (!inStringValid); } else { cout << "\n"; } } smEventNum++; } cout << "Exiting program.\n"; }
void view_individual_events() { /*** Displays an 3D occupancy plot for each SM Event (stop mode event). The h5_file_num chosen must have working Hits and EventsCR files (_Hits.root and _EventsCR.root files). Can choose which SM event to start at. (find "CHOOSE THIS" in this script) ***/ gROOT->Reset(); // Setting up files, treereaders, histograms string file_kind = "aggr"; // string that is either "aggr" or "non_aggr" to indicate whether or not its an aggregate file pair or not. int file_num_input = 19; string view_option = "1"; // choose what to view: // "1" or "3d": view the events with their 3d reconstruction and line fit // "2" or "SM_rel_BCID": numHits per SMRelBCID with the 3d reconstruction TFile *fileHits; TFile *fileEventsCR; if (file_kind.compare("non_aggr") == 0) { fileHits = new TFile(("/home/pixel/pybar/tags/2.0.2_new/pyBAR-master/pybar/module_202_new/" + to_string(file_num_input) + "_module_202_new_stop_mode_ext_trigger_scan_interpreted_Hits.root").c_str()); fileEventsCR = new TFile(("/home/pixel/pybar/tags/2.0.2_new/pyBAR-master/pybar/module_202_new/" + to_string(file_num_input) + "_module_202_new_stop_mode_ext_trigger_scan_interpreted_EventsCR.root").c_str()); } else if (file_kind.compare("aggr") == 0) { // fileHits = new TFile("/home/pixel/pybar/tags/2.0.2_new/pyBAR-master/pybar/homemade_scripts/aggregate_data/1_module_202_new_AggrHits.root"); fileHits = new TFile(("/home/pixel/pybar/tags/2.0.2_new/pyBAR-master/pybar/homemade_scripts/aggregate_data/" + to_string(file_num_input) + "_module_202_new_AggrHits.root").c_str()); // fileEventsCR = new TFile("/home/pixel/pybar/tags/2.0.2_new/pyBAR-master/pybar/homemade_scripts/aggregate_data/1_module_202_new_AggrEventsCR.root"); fileEventsCR = new TFile(("/home/pixel/pybar/tags/2.0.2_new/pyBAR-master/pybar/homemade_scripts/aggregate_data/" + to_string(file_num_input) + "_module_202_new_AggrEventsCR.root").c_str()); } else { cout << "Error: Input file_kind is not valid."; } TTreeReader *readerHits = new TTreeReader("Table", fileHits); TTreeReaderValue<UInt_t> h5_file_num_Hits(*readerHits, "h5_file_num"); TTreeReaderValue<Long64_t> event_number(*readerHits, "event_number"); TTreeReaderValue<UChar_t> tot(*readerHits, "tot"); TTreeReaderValue<UChar_t> relative_BCID(*readerHits, "relative_BCID"); TTreeReaderValue<Long64_t> SM_event_num_Hits(*readerHits, "SM_event_num"); TTreeReaderValue<UInt_t> SM_rel_BCID(*readerHits, "SM_rel_BCID"); TTreeReaderValue<Double_t> x(*readerHits, "x"); TTreeReaderValue<Double_t> y(*readerHits, "y"); TTreeReaderValue<Double_t> z(*readerHits, "z"); TTreeReaderValue<Double_t> s(*readerHits, "s"); TTreeReader *readerEventsCR = new TTreeReader("Table", fileEventsCR); TTreeReaderValue<UInt_t> h5_file_num_EventsCR(*readerEventsCR, "h5_file_num"); TTreeReaderValue<Long64_t> SM_event_num_EventsCR(*readerEventsCR, "SM_event_num"); TTreeReaderValue<UInt_t> num_hits(*readerEventsCR, "num_hits"); TTreeReaderValue<UInt_t> sum_tots(*readerEventsCR, "sum_tots"); TTreeReaderValue<Double_t> mean_x(*readerEventsCR, "mean_x"); TTreeReaderValue<Double_t> mean_y(*readerEventsCR, "mean_y"); TTreeReaderValue<Double_t> mean_z(*readerEventsCR, "mean_z"); TTreeReaderValue<Double_t> line_fit_param0(*readerEventsCR, "line_fit_param0"); TTreeReaderValue<Double_t> line_fit_param1(*readerEventsCR, "line_fit_param1"); TTreeReaderValue<Double_t> line_fit_param2(*readerEventsCR, "line_fit_param2"); TTreeReaderValue<Double_t> line_fit_param3(*readerEventsCR, "line_fit_param3"); TTreeReaderValue<Double_t> sum_of_squares(*readerEventsCR, "sum_of_squares"); TTreeReaderValue<UInt_t> event_status(*readerEventsCR, "event_status"); TTreeReaderValue<Double_t> fraction_inside_sphere(*readerEventsCR, "fraction_inside_sphere"); TTreeReaderValue<Double_t> length_track(*readerEventsCR, "length_track"); TTreeReaderValue<Double_t> sum_tots_div_by_length_track(*readerEventsCR, "sum_tots_div_by_length_track"); TTreeReaderValue<Double_t> sum_squares_div_by_DoF(*readerEventsCR, "sum_squares_div_by_DoF"); TTreeReaderValue<Double_t> zenith_angle(*readerEventsCR, "zenith_angle"); TTreeReaderValue<UInt_t> duration(*readerEventsCR, "duration"); // Initialize the canvas and graph_3d TCanvas *c1 = new TCanvas("c1","3D Occupancy for Specified SM Event", 1000, 10, 900, 1000); // c1->SetRightMargin(0.25); TPad *pad1 = new TPad("pad1", "pad1", 0, 0.5, 1, 1.0); // upper pad pad1->SetRightMargin(0.25); TPad *pad2 = new TPad("pad2", "pad2", 0, 0, 1, 0.5); // lower pad // pad2->SetRightMargin(0.35); c1->cd(); TH2F *h_2d_occupancy = new TH2F("h_2d_occupancy", "2D Occupancy", 80, 0, 20, 336, -16.8, 0); h_2d_occupancy->GetXaxis()->SetTitle("x (mm)"); h_2d_occupancy->GetYaxis()->SetTitle("y (mm)"); h_2d_occupancy->GetZaxis()->SetTitle("SM Relative BCID (BCIDs)"); TH1F *h_SM_rel_BCID = new TH1F("h_SM_rel_BCID", "Num Hits per SMRelBCID", 256, 0, 256); h_SM_rel_BCID->GetXaxis()->SetTitle("Stop Mode Relative BCID (BCIDs)"); h_SM_rel_BCID->GetYaxis()->SetTitle("Count (hits)"); bool quit = false; // if pressed q // Main Loop (loops for every entry in readerEventsCR) while (readerEventsCR->Next() && !quit) { if (readerEventsCR->GetCurrentEntry() == 0 && file_kind.compare("non_aggr") == 0) { continue; // skip the entry num 0, because it probably contains no data } // Get startEntryNum_Hits and endEntryNum_Hits (for readerHits) vector<int> entryNumRange_include(2); entryNumRange_include = getEntryNumRangeWithH5FileNumAndSMEventNum(readerHits, *h5_file_num_EventsCR, *SM_event_num_EventsCR); if (entryNumRange_include[0] == -1) { cout << "Error: h5_file_num and SM_event_num should be able to be found in the Hits file.\n"; } // If statement for choosing which graph_3d/h_2d_occupancy to view TGraph2D *graph_3d = new TGraph2D(); // create a new TGraph to refresh; the graph_3d is the 3d plot, the h_2d_occupancy is the 2d plot. h_2d_occupancy->Reset(); // must do reset for histograms, cannot create a new hist to refresh it h_SM_rel_BCID->Reset(); // Fill graph_3d and h_2d_occupancy with points and set title and axes readerHits->SetEntry(entryNumRange_include[0]); for (int i = 0; i < entryNumRange_include[1] - entryNumRange_include[0] + 1; i++) { graph_3d->SetPoint(i, (*x - 0.001), (*y + 0.001), (*z - 0.001)); h_2d_occupancy->Fill(*x, *y, *SM_rel_BCID); h_SM_rel_BCID->Fill(*SM_rel_BCID); readerHits->Next(); } string graphTitle = "3D Reconstruction and Line Fit for h5FileNum " + to_string(*h5_file_num_EventsCR) + ", SMEventNum " + to_string(*SM_event_num_EventsCR); // graphTitle.append(to_string(*SM_event_num_EventsCR)); graph_3d->SetTitle(graphTitle.c_str()); graph_3d->GetXaxis()->SetTitle("x (mm)"); graph_3d->GetYaxis()->SetTitle("y (mm)"); graph_3d->GetZaxis()->SetTitle("z (mm)"); graph_3d->GetXaxis()->SetLimits(0, 20); // ROOT is buggy, x and y use setlimits() graph_3d->GetYaxis()->SetLimits(-16.8, 0); // but z uses setrangeuser() graph_3d->GetZaxis()->SetRangeUser(0, 40.96); c1->SetTitle(graphTitle.c_str()); // Draw the graph_3d on pad1 (upper pad) c1->cd(); pad1->Draw(); pad1->cd(); graph_3d->SetMarkerStyle(8); graph_3d->SetMarkerSize(0.5); graph_3d->Draw("pcol"); // Draw other histogram on pad2 c1->cd(); pad2->Draw(); pad2->cd(); if (view_option.compare("3d") == 0 || view_option.compare("1") == 0) { pad2->SetRightMargin(0.35); h_2d_occupancy->Draw("COLZ"); } else if (view_option.compare("SM_rel_BCID") == 0 || view_option.compare("2") == 0) { pad2->SetRightMargin(0.25); h_SM_rel_BCID->Draw("COLZ"); } else { cout << "Error: Input view_option is not valid.\n"; } pad1->cd(); // Display results, draw graph_3d and line fit if (file_kind.compare("aggr") == 0) { cout << "Aggr EventsCR Entry Num: " << readerEventsCR->GetCurrentEntry(); } cout << " h5 Event Num: " << *h5_file_num_EventsCR << " SM Event Num: " << *SM_event_num_EventsCR << "\n"; // cout << " Number of hits: " << *num_hits << "\n"; // Draw the fitted line only if fit did not fail. if (*event_status != 1) { double fitParams[4]; fitParams[0] = *line_fit_param0; fitParams[1] = *line_fit_param1; fitParams[2] = *line_fit_param2; fitParams[3] = *line_fit_param3; int n = 1000; double t0 = 0; // t is the z coordinate double dt = 40.96; TPolyLine3D *l = new TPolyLine3D(n); for (int i = 0; i <n;++i) { double t = t0+ dt*i/n; double x,y,z; line(t,fitParams,x,y,z); l->SetPoint(i,x,y,z); } l->SetLineColor(kRed); l->Draw("same"); cout << "Sum of squares div by DoF: " << *sum_squares_div_by_DoF; } else { cout << "Sum of squares div by DoF: FIT FAILED"; } cout << " Zenith angle: " << *zenith_angle << "\n"; cout << "Duration: " << *duration << "\n"; // cout << "Fraction inside sphere (1 mm radius): " << *fraction_inside_sphere << "\n"; cout << "Length of track: " << *length_track << "\n"; cout << "SumTots/Length: " << *sum_tots_div_by_length_track << "\n"; // if (view_option.compare("3d") == 0 || view_option.compare("1") == 0) { // } else if (view_option.compare("SM_rel_BCID") == 0 || view_option.compare("2") == 0) { // // // Reset histogram // // h_SM_rel_BCID->Reset(); // // // For every hit, fill in the histogram with the SM_rel_BCID // // readerHits->SetEntry(entryNumRange_include[0]); // // for (int i = 0; i < entryNumRange_include[1] - entryNumRange_include[0] + 1; i++) { // // h_SM_rel_BCID->Fill(*SM_rel_BCID); // // readerHits->Next(); // // } // // // Draw the hist // // c1->cd(); // // pad1->Draw(); // // pad1->cd(); // // h_SM_rel_BCID->Draw(); // // // Print info lines // // if (file_kind.compare("aggr") == 0) { // // cout << "Aggr EventsCR Entry Num: " << readerEventsCR->GetCurrentEntry(); // // } // // cout << " h5 Event Num: " << *h5_file_num_EventsCR << " SM Event Num: " << *SM_event_num_EventsCR << "\n"; // } else { // cout << "Error: Input view_option is not valid.\n"; // } // Ask for input if (true) { // won't show drawings or ask for input unless its a good event // CHOOSE THIS to show all events or only good events c1->Update(); // show all the drawings // handle input string inString = ""; bool inStringValid = false; do { cout << "<Enter>: next; 'b': previous; [number]: the nth SMEvent in the EventsCR file; 'q': quit.\n"; // b is for back getline(cin, inString); // Handles behavior according to input if (inString.empty()) { // <Enter> // leave things be inStringValid = true; } else if (inString.compare("b") == 0) { readerEventsCR->SetEntry(readerEventsCR->GetCurrentEntry() - 2); // smEventNum -= 2; // because it gets incremented once at the end of this do while loop inStringValid = true; } else if (inString.compare("q") == 0 || inString.compare(".q") == 0) { quit = true; inStringValid = true; } else if (canConvertStringToPosInt(inString)) { readerEventsCR->SetEntry(convertStringToPosInt(inString) - 1); // smEventNum = convertStringToPosInt(inString) - 1; // -1 because it gets incremented once at the end of this do while loop inStringValid = true; } // else, leave inStringValid as false, so that it asks for input again } while (!inStringValid); } else { cout << "\n"; } } cout << "Exiting program.\n"; }
void RayTrace(AOpticsManager* manager, TCanvas* can3D) { const int kNdeg = 8; TH2D* h2[kNdeg]; TGraph* graph = new TGraph(); TCanvas* can = new TCanvas("can", "can", 900, 900); TCanvas* can2= new TCanvas("can2", "can2", 900, 900); can->Divide(3, 3, 1e-10, 1e-10); TH2D* hMirror = new TH2D("hMirror", ";X (mm);Y (mm)", 1000, -7, 7, 1000, -7, 7); for(int i = 0; i < kNdeg; i++){ double deg = i*0.5; TGeoTranslation raytr("raytr", -2*kF*TMath::Sin(deg*TMath::DegToRad()), 0, 2*kF*TMath::Cos(deg*TMath::DegToRad())); TVector3 dir; dir.SetMagThetaPhi(1, TMath::Pi() - deg*TMath::DegToRad(), 0); double lambda = 400*nm; // dummy ARayArray* array = ARayShooter::Square(lambda, 14*m, 401, 0, &raytr, &dir); manager->TraceNonSequential(*array); h2[i] = new TH2D("", Form("#it{#theta} = %3.1f#circ;x (mm); y (mm)", deg), 200, -40, 100, 200, -70, 70); TH2D tmp("", "", 100, -1e5, 1e5, 100, -1e5, 1e5); TObjArray* focused = array->GetFocused(); for(Int_t j = 0; j <= focused->GetLast(); j++){ ARay* ray = (ARay*)(*focused)[j]; Double_t p[4]; ray->GetLastPoint(p); tmp.Fill(p[0], p[1]); if (i == 0) { int n = ray->FindNodeNumberStartWith("mirror"); const double* pn = ray->GetPoint(n); hMirror->Fill(pn[0]/m, pn[1]/m); } // if if (i == kNdeg - 1 && gRandom->Uniform() < 0.001) { TPolyLine3D* pol = ray->MakePolyLine3D(); pol->SetLineColor(2); can3D->cd(); pol->Draw(); } // if } // j double meanx = tmp.GetMean(); for(Int_t j = 0; j <= focused->GetLast(); j++){ ARay* ray = (ARay*)(*focused)[j]; Double_t p[4]; ray->GetLastPoint(p); h2[i]->Fill((p[0] - meanx)/mm, p[1]/mm); } // j can->cd(i + 1); h2[i]->Draw("colz"); if(i == 0){ can2->cd(); hMirror->Draw("colz"); } // i delete array; } // i }
Int_t line3dfit_copy() { gStyle->SetOptStat(0); gStyle->SetOptFit(); //double e = 0.1; // Int_t nd = 5; // double xmin = 0; double ymin = 0; // double xmax = 10; double ymax = 10; TGraph2D * gr = new TGraph2D(); // Fill the 2D graph // double p0[4] = {10,20,1,2}; // generate graph with the 3d points // for (Int_t N=0; N<nd; N++) { // double x,y,z = 0; // // Generate a random number // double t = gRandom->Uniform(0,10); // line(t,p0,x,y,z); // double err = 1; // // do a gaussian smearing around the points in all coordinates // x += gRandom->Gaus(0,err); // y += gRandom->Gaus(0,err); // z += gRandom->Gaus(0,err); // gr->SetPoint(N,x,y,z); // //dt->SetPointError(N,0,0,err); // } // gr->SetPoint(0, 3, 3, 5); // gr->SetPoint(1, 3, 3, 3); // gr->SetPoint(2, 4, 3.5, 5); // gr->SetPoint(3, 6.2, 7.3, 5.8); // gr->SetPoint(4, 9, 8, 7); // gr->SetPoint(5, 5, 5, 5); gr->SetPoint(0, 19, -4.25, 1.92); gr->SetPoint(1, 19, -4.30, 1.92); gr->SetPoint(2, 19, -4.35, 1.92); gr->SetPoint(3, 19, -4.40, 1.92); gr->SetPoint(4, 19, -4.45, 1.92); gr->SetPoint(5, 19, -4.50, 1.92); gr->SetPoint(6, 19, -4.55, 1.92); gr->SetPoint(7, 19, -4.60, 1.92); gr->SetPoint(8, 18.75, -4.30, 1.92); gr->SetPoint(9, 18.75, -4.35, 1.92); gr->SetPoint(10, 18.75, -4.40, 1.92); gr->SetPoint(11, 18.75, -4.45, 1.92); gr->SetPoint(12, 18.75, -4.50, 1.92); gr->SetPoint(13, 18.75, -4.55, 1.92); gr->SetPoint(14, 19, -4.20, 2.08); gr->SetPoint(15, 19, -4.65, 2.08); gr->SetPoint(16, 19, -4.70, 2.08); gr->SetPoint(17, 18.75, -4.25, 2.08); gr->SetPoint(18, 18.75, -4.60, 2.08); // gr->SetPoint(19, 19.001, -4.151, 2.241); // gr->SetPoint(20, 18.751, -4.201, 2.241); gr->SetPoint(19, 19., -4.15, 2.24); gr->SetPoint(20, 18.5723, -4.20, 2.24); // gr->SetMarkerStyle(8); // gr->Draw("p"); // gr->Draw("p0"); // TFitResultPtr fit = gr->Fit("pol1", "WS"); // // fit->Print("V"); // Double_t p0 = fit->Value(0); // Double_t p1 = fit->Value(1); // // draw the line // TPolyLine3D *l = new TPolyLine3D(2); // double dz = 8; // l->SetPoint(0,0,0,p0); // l->SetPoint(1,dz,0,dz * p1); // l->SetLineColor(kRed); // l->Draw("same"); // fit the graph now, and make the functor objet ROOT::Fit::Fitter fitter; SumDistance2 sdist(gr); #ifdef __CINT__ ROOT::Math::Functor fcn(&sdist,4,"SumDistance2"); #else ROOT::Math::Functor fcn(sdist,4); #endif // set the function and the initial parameter values double pStart[4] = {1,1,1,1}; fitter.SetFCN(fcn,pStart); // set step sizes different than default ones (0.3 times parameter values) for (int i = 0; i < 4; ++i) fitter.Config().ParSettings(i).SetStepSize(0.01); bool ok = fitter.FitFCN(); if (!ok) { Error("line3Dfit","Line3D Fit failed"); return 1; } const ROOT::Fit::FitResult & result = fitter.Result(); std::cout << "Total final distance square " << result.MinFcnValue() << std::endl; // result.Print(std::cout); // @@@ suppress output // get fit parameters const double * parFit = result.GetParams(); // draw the fitted line int n = 1000; double t0 = 0; double dt = 10; TPolyLine3D *l = new TPolyLine3D(n); for (int i = 0; i <n;++i) { double t = t0+ dt*i/n; double x,y,z; line(t,parFit,x,y,z); l->SetPoint(i,x,y,z); } l->SetLineColor(kRed); l->Draw("same"); // Access to fit params and minfcnvalue cout << parFit[1] << "\n"; cout << result.MinFcnValue() << "\n"; // // draw original line // TPolyLine3D *l0 = new TPolyLine3D(n); // for (int i = 0; i <n;++i) { // double t = t0+ dt*i/n; // double x,y,z; // line(t,p0,x,y,z); // l0->SetPoint(i,x,y,z); // } // l0->SetLineColor(kBlue); // l0->Draw("same"); return 0; }