// http://www.cppreference.com/ // http://cplusplus.com/ // http://root.cern.ch/root/html/ClassIndex.html // http://root.cern.ch/root/html/tutorials/ #include #include #include "TString.h" #include "TFile.h" #include "TH1F.h" #include "TH2F.h" #include "TCanvas.h" #include "TF1.h" #include "TH1D.h" #include "TProfile.h" #include "TStyle.h" #include "TRandom3.h" double sqr(double in) { return in * in; } void cplusplusandrootintro() { // some C/C++ variable types int integer_number = 42; printf("integer_number = %d\n", integer_number); size_t size_number = 10; printf("size_number = %lu\n", size_number); float float_number = 3.1415926535; printf("float_number = %f\n", float_number); double double_number = 2.718281828459045235; printf("double_number = %e\n", double_number); std::string some_text = "I'm a string!"; printf("some_text = %s\n", some_text.c_str()); // create a vector of length 10, with all values 42 std::vector< int > vec_of_ints(10, 42); printf("vec_of_ints: length = %lu, first element = %d\n", vec_of_ints.size(), vec_of_ints.at(0)); // create a vector of length 10, with all values roughly pi std::vector< float > vec_of_floats(5, 3.1415926535); printf("vec_of_floats: length = %lu, last element = %g\n", vec_of_floats.size(), vec_of_floats.at(4)); printf("- - - - - - -\n"); std::vector< std::vector< double > > twodim_vec; twodim_vec.push_back(std::vector< double >(4, 2.718281828459045235)); twodim_vec.push_back(std::vector< double >(3, 3.1415926535)); printf("twodim_vec: length = %lu\n", twodim_vec.size()); printf("twodim_vec: length of first entry = %lu\n", twodim_vec.at(0).size()); printf("twodim_vec: length of second entry = %lu\n", twodim_vec.at(1).size()); printf("twodim_vec: element 0,1 = %f\n", twodim_vec.at(0).at(1)); printf("- - - - - - -\n"); // short excure into pointers (we will need it in ROOT at some point) int num = 42; int *p_num = # printf("num = %d, address of num = %d\n", num, &num); printf("p_num = %d, value of pnum = %d\n", p_num, *p_num); *p_num -= 1; printf("num = %d, address of num = %d\n", num, &num); printf("p_num = %d, value of pnum = %d\n", p_num, *p_num); printf("- - - - - - -\n"); // some ROOT equivalents Int_t ROOT_integer_number = integer_number; Float_t ROOT_float_number = float_number; Double_t ROOT_double_number = double_number; TString ROOT_string = some_text; // they can be mixed, e.g. std::vector< TString > vec_of_tstrings(11, "test"); // structure your code if (ROOT_integer_number == 42) { printf("ROOT_integer_number = %d\n", ROOT_integer_number); } if (ROOT_double_number == 0. || ROOT_float_number == 0.) { printf("this will probably not be written to the screen"); } else if (1 == 2) { } else { printf("ROOT_double_number = %f\n", ROOT_double_number); } printf("- - - - - - -\n"); for (size_t i = 0; i < vec_of_ints.size(); ++i) { printf("vec_of_ints.at(%lu) = %d\n", i, vec_of_ints.at(i)); vec_of_ints.at(i) += sqr(i); } printf("- - - - - - -\n"); int j = vec_of_ints.size() - 1; while (j >= 0) { printf("vec_of_ints.at(%d) = %d\n", j, vec_of_ints.at(j)); --j; } printf("- - - - - - -\n"); TCanvas *canvas = new TCanvas("canvas", "some title", 0, 0, 1000, 800); canvas->Divide(4, 3); TH1F *hist1 = new TH1F("hist1", "title;x-axis;y-axis", 100, -.5, 99.5); for (size_t i = 0; i < vec_of_ints.size(); ++i) { hist1->Fill(vec_of_ints.at(i) - 42); } canvas->cd(1); hist1->Draw(); TFile *file1 = new TFile("cplusplusandrootintro.root", "recreate"); TH1F *hist2 = new TH1F("hist2", "title;x-axis;y-axis", 10, -.5, 9.5); for (int k = 0; k < 10; ++k) { hist2->Fill(k, k); } file1->Write(); canvas->cd(2); hist2->Draw(); file1->Close(); TFile *file2 = new TFile("cplusplusandrootintro.root", "update"); file2->ls(); hist1->Write(); file2->Close(); TFile file3("cplusplusandrootintro.root", "read"); file3.ls(); hist2 = (TH1F*)file3.Get("hist2"); canvas->cd(3); hist1->Draw(); canvas->cd(4); hist2->Draw(); canvas->Update(); canvas->SaveAs("cplusplusandrootintro.png"); std::getchar(); file3.Close(); gStyle->SetOptStat(1111); gStyle->SetOptFit(1111); gRandom = new TRandom3(); TF1 *gauss = new TF1("gauss", "gaus"); gauss->SetParameters(10, 1, 2); TH1F *hist3 = new TH1F("hist3", "title;x-axis;y-axis", 100, -10., 10.); hist3->FillRandom("gauss", 10000); canvas->cd(4); hist3->Draw(); hist3->Fit(gauss); TH2F *hist4 = new TH2F("hist4", "title;x-axis;y-axis;z-axis", 20, -10., 10., 20, 0., 1.); hist4->FillRandom("gauss", 10000); canvas->cd(5); hist4->Draw(); canvas->cd(6); hist4->Draw("box"); canvas->cd(7); hist4->Draw("colz"); canvas->cd(8); hist4->Draw("lego"); canvas->cd(9); TProfile *profile = hist4->ProfileX("_pfx"); profile->Draw(); canvas->cd(10); TH1D *projection = hist4->ProjectionX("_pjx", 1 ,1); projection->Draw(); TH1F *hist5 = new TH1F("hist5", "title;x-axis;y-axis", 100, -10., 10.); for (int k = 0; k < 10000; ++k) { hist5->Fill(projection->GetRandom()); } canvas->cd(11); hist5->Draw(); }