/* * CMHbookLib.h * * Created on: Jul 13, 2012 * Author: rryan */ #ifndef CMHBOOKTOROOTLIB_H_ #define CMHBOOKTOROOTLIB_H_ using namespace std; // ROOT 1D histogram header #include "TH1.h" // ROOT 2D histogram header #include "TH2.h" // CMHbook histogram structs #include "uthbook.h" #include #include #include class CMHbookToROOTLib { private: /* **** ROOT 2D over and underflow bins may not be understood **** * * 2D ROOT bins filled as follows: * Example: nbinx = 3; nbiny = 3; * xUnderYUnder = 1; xUnderflow = 2; xUnderYOver = 3; * yUnderflow = 4; yOverflow = 5; xOverYUnder = 6; * xOverflow = 7; xOverYOver = 8; * All bin contents = * * _______________________________________ * |xUyU |xU |xU |xU |xUyO | * | 6 | 2 | 2 | 2 | 3 | * |_______|_______|_______|_______|_______| * |yU |bin1,1 |bin1,2 |bin1,3 |yO | * | 4 | * | * | * | 5 | * |_______|_______|_______|_______|_______| * |yU |bin2,1 |bin2,2 |bin2,3 |yO | * | 4 | * | * | * | 5 | * |_______|_______|_______|_______|_______| * |yU |bin3,1 |bin3,2 |bin3,3 |yO | * | 4 | * | * | * | 5 | * |_______|_______|_______|_______|_______| * |xOyU |xO |xO |xO |xO+yO | * | 6 | 7 | 7 | 7 | 8 | * |_______|_______|_______|_______|_______| * */ template R* histToRoot(string rootTitle, string histTitle, int entries, int nbinx, T xmin, T xmax, int nbiny, T ymin, T ymax, T xUnderYUnder, T xUnderflow, T xUnderYOver, T yUnderflow, T yOverflow, T xOverYUnder, T xOverflow, T xOverYOver, vector > data); public: template H* fromCSVToROOTHist(string CSV); }; template R* histToRoot(string rootTitle, string histTitle, int entries, int nbinx, T xmin, T xmax, int nbiny, T ymin, T ymax, T xUnderYUnder, T xUnderflow, T xUnderYOver, T yUnderflow, T yOverflow, T xOverYUnder, T xOverflow, T xOverYOver, vector > data) { cout << "error in converting histToRoot unknown template type" << endl; } template<> TH1I* histToRoot(string rootTitle, string histTitle, int entries, int nbinx, int xmin, int xmax, int nbiny, int ymin, int ymax, int xUnderYUnder, int xUnderflow, int xUnderYOver, int yUnderflow, int yOverflow, int xOverYUnder, int xOverflow, int xOverYOver, vector > data) { TH1I *returnHist = new TH1I(rootTitle.c_str(), histTitle.c_str(), (Int_t) nbinx, (Double_t) xmin, (Double_t) xmax); returnHist->SetBinContent(0, xUnderflow); for (int i = 0; i < data.size(); i++) { returnHist->SetBinContent(i + 1, data[i][0]); } returnHist->SetBinContent(data.size() + 1, xOverflow); returnHist->SetEntries(entries); return returnHist; } template<> TH2I* histToRoot(string rootTitle, string histTitle, int entries, int nbinx, int xmin, int xmax, int nbiny, int ymin, int ymax, int xUnderYUnder, int xUnderflow, int xUnderYOver, int yUnderflow, int yOverflow, int xOverYUnder, int xOverflow, int xOverYOver, vector > data) { TH2I* returnHist = new TH2I(rootTitle.c_str(), histTitle.c_str(), (Int_t) nbinx, (Double_t) xmin, (Double_t) xmax, (Int_t) nbiny, (Double_t) ymin, (Double_t) ymax); // ROOT underflow set to 0. Underflow data is lost. int i, k; for (i = 0; i < nbinx + 2; i++) { for (k = 0; k < nbiny + 2; k++) { // enters the x under y underflow into (0,0) if (i == 0 && k == 0) { returnHist->SetBinContent(i, k, xUnderYOver); } // enters the x underflow into (0,k) else if (i == 0 && k > 0 && k < (nbiny + 1)) { returnHist->SetBinContent(i, k, yUnderflow); } // enters the x under y overflow into (i,k+1) else if (i == 0 && k == (nbiny + 1)) { returnHist->SetBinContent(i, k + 1, xUnderYOver); } // enters the y underflow into (i,0) else if (i > 0 && i < (nbinx + 1) && k == 0) { returnHist->SetBinContent(i, k, yUnderflow); } // fills inner values with bin contents else if (i > 0 && i < (nbinx + 1) && k > 0 && k < (nbiny + 1)) { returnHist->SetBinContent(i, k, data[i - 1][k - 1]); } // enters the y overflow into (i,k+1) else if (i > 0 && i < (nbinx + 1) && k == (nbiny + 1)) { returnHist->SetBinContent(i, k, yOverflow); } // enters the x over y underflow into (i+1,0) else if (i == (nbinx + 1) && k == 0) { returnHist->SetBinContent(i, k, xOverYUnder); } // enters the x overflow into (i+1,k) else if (i == (nbinx + 1) && k > 0 && k < (nbiny + 1)) { returnHist->SetBinContent(i, k, xOverflow); } // enters the x over y overflow into (i+1,k+1) else if (i == (nbinx + 1) && k == (nbiny + 1)) { returnHist->SetBinContent(i, k, xOverYOver); } } } returnHist->SetEntries(entries); return returnHist; } template<> TH1F* histToRoot(string rootTitle, string histTitle, int entries, int nbinx, float xmin, float xmax, int nbiny, float ymin, float ymax, float xUnderYUnder, float xUnderflow, float xUnderYOver, float yUnderflow, float yOverflow, float xOverYUnder, float xOverflow, float xOverYOver, vector > data) { TH1F *returnHist = new TH1F(rootTitle.c_str(), histTitle.c_str(), (Int_t) nbinx, (Double_t) xmin, (Double_t) xmax); returnHist->SetBinContent(0, xUnderflow); for (int i = 0; i < data.size(); i++) { returnHist->SetBinContent(i + 1, data[i][0]); } returnHist->SetBinContent(data.size() + 1, xOverflow); returnHist->SetEntries(entries); return returnHist; } template<> TH2F* histToRoot(string rootTitle, string histTitle, int entries, int nbinx, float xmin, float xmax, int nbiny, float ymin, float ymax, float xUnderYUnder, float xUnderflow, float xUnderYOver, float yUnderflow, float yOverflow, float xOverYUnder, float xOverflow, float xOverYOver, vector > data) { TH2F *returnHist = new TH2F(rootTitle.c_str(), histTitle.c_str(), (Int_t) nbinx, (Double_t) xmin, (Double_t) xmax, (Int_t) nbiny, (Double_t) ymin, (Double_t) ymax); // ROOT underflow set to 0. Underflow data is lost. int i, k; for (i = 0; i < nbinx + 2; i++) { for (k = 0; k < nbiny + 2; k++) { // enters the x under y underflow into (0,0) if (i == 0 && k == 0) { returnHist->SetBinContent(i, k, xUnderYOver); } // enters the x underflow into (0,k) else if (i == 0 && k > 0 && k < (nbiny + 1)) { returnHist->SetBinContent(i, k, yUnderflow); } // enters the x under y overflow into (i,k+1) else if (i == 0 && k == (nbiny + 1)) { returnHist->SetBinContent(i, k + 1, xUnderYOver); } // enters the y underflow into (i,0) else if (i > 0 && i < (nbinx + 1) && k == 0) { returnHist->SetBinContent(i, k, yUnderflow); } // fills inner values with bin contents else if (i > 0 && i < (nbinx + 1) && k > 0 && k < (nbiny + 1)) { returnHist->SetBinContent(i, k, data[i - 1][k - 1]); } // enters the y overflow into (i,k+1) else if (i > 0 && i < (nbinx + 1) && k == (nbiny + 1)) { returnHist->SetBinContent(i, k, yOverflow); } // enters the x over y underflow into (i+1,0) else if (i == (nbinx + 1) && k == 0) { returnHist->SetBinContent(i, k, xOverYUnder); } // enters the x overflow into (i+1,k) else if (i == (nbinx + 1) && k > 0 && k < (nbiny + 1)) { returnHist->SetBinContent(i, k, xOverflow); } // enters the x over y overflow into (i+1,k+1) else if (i == (nbinx + 1) && k == (nbiny + 1)) { returnHist->SetBinContent(i, k, xOverYOver); } } } returnHist->SetEntries(entries); return returnHist; } template H* fromCSVToROOTHist(string CSV) { stringstream ss(CSV); stringstream sp; string stringPiece; char type; string title; int xbin, ybin; T xmin, xmax, ymin, ymax; int entries; T xUnderYUnder, xUnderflow, xUnderYOver, yUnderflow; T yOverflow, xOverYUnder, xOverflow, xOverYOver; /* * Incoming format: * * ss << type << "," << title << "," << entries << "," << nbinx << "," << xmin << "," << xmax * << "," << nbiny << "," << ymin << "," << ymax << "," << xUnderYUnder * << "," << xUnderflow << ","<< xUnderYOver << "," << yUnderflow * << ","<< yOverflow << "," << xOverYUnder << ","<< xOverflow * << "," << xOverYOver << ","; * * Then data. */ getline(ss, stringPiece, ','); sp.str(stringPiece); sp >> type; getline(ss, stringPiece, ','); sp.clear(); sp.str(stringPiece); sp >> title; getline(ss, stringPiece, ','); sp.clear(); sp.str(stringPiece); sp >> entries; getline(ss, stringPiece, ','); sp.clear(); sp.str(stringPiece); sp >> xbin; getline(ss, stringPiece, ','); sp.clear(); sp.str(stringPiece); sp >> xmin; getline(ss, stringPiece, ','); sp.clear(); sp.str(stringPiece); sp >> xmax; getline(ss, stringPiece, ','); sp.clear(); sp.str(stringPiece); sp >> ybin; getline(ss, stringPiece, ','); sp.clear(); sp.str(stringPiece); sp >> ymin; getline(ss, stringPiece, ','); sp.clear(); sp.str(stringPiece); sp >> ymax; T underOver[8]; for (int i = 0; i < 8; i++) { getline(ss, stringPiece, ','); sp.clear(); sp.str(stringPiece); sp >> underOver[i]; } string field; string rootTitle = "ROOT_" + title; vector > data; if (ybin < 1) { for (int i = 0; i < xbin; i++) { getline(ss, field, ','); sp.clear(); sp.str(field); T point; sp >> point; vector copy(1, point); data.push_back(copy); } } else { for (int i = 0; i < xbin; i++) { vector copy; for (int k = 0; k < ybin; k++) { getline(ss, field, ','); sp.clear(); sp.str(field); T point; sp >> point; copy.push_back(point); } data.push_back(copy); } } H *returnHist; returnHist = histToRoot(rootTitle, title, entries, xbin, xmin, xmax, ybin, ymin, ymax, underOver[0], underOver[1], underOver[2], underOver[3], underOver[4], underOver[5], underOver[6], underOver[7], data); return returnHist; } #endif /* CMHBOOKTOROOTLIB_H_ */