void dump_required_lvl2(TTree *tree, TLeaf &leaf, CodedOutputStream &o, CodedOutputStream &o2) { const int DL = 1; // definition level multiplier const int RL = 4; // repetition level multiplier std::cout << "Dump vector vector: " << leaf.GetName() << " " << leaf.GetTypeName() << std::endl; auto * branch = leaf.GetBranch(); std::vector<std::vector<T> > * data = NULL; tree->SetBranchAddress(leaf.GetBranch()->GetName(), &data); int entries = tree->GetEntries(); for (int i = 0; i < entries; i++) { branch->GetEntry(i); if (data->size() == 0) { write_out_32(o2, DL*0 + RL*0); } for (int j = 0; j < data->size(); j++) { if (data->at(j).size() == 0) { int dl = 1; int rl = (j > 0 ? 1 : 0); write_out_32(o2, dl*DL + rl*RL); } for (int k = 0; k < data->at(j).size(); k++) { int dl = 2; int rl = (k > 0 ? 2 : (j > 0 ? 1 : 0)); write_out_32(o2, dl*DL + rl*RL); write_out_type(o, data->at(j).at(k)); } } } }
// This piece of code is borrowed from Argo written by Nathaniel Tagg std::vector<std::string> DataFetcher::FindLeavesOfType(std::string pattern) { /// Look in the tree and try to find a leaf element that matches 'pattern'. /// Return the full name of that leaf. std::vector<std::string> leaf_names; // Strip whitespace from pattern. pattern.erase(std::remove_if(pattern.begin(), pattern.end(), ::isspace), pattern.end()); TObjArray * list = tree_->GetListOfLeaves(); for (int i = 0; i < list->GetEntriesFast(); ++i) { TObject * o = list->At(i); TLeaf * leaf = (TLeaf *) o; std::string name = leaf->GetTypeName(); // Strip whitespace from pattern. name.erase(std::remove_if(name.begin(), name.end(), ::isspace), name.end()); size_t found = name.find(pattern); if (found != std::string::npos) { // Return this match. leaf_names.push_back(leaf->GetName()); } } return leaf_names; }
void dump_required_lvl0(TTree *tree, TLeaf &leaf, CodedOutputStream &o) { std::cout << "Dump " << leaf.GetName() << std::endl; auto *branch = leaf.GetBranch(); T data; tree->SetBranchAddress(leaf.GetBranch()->GetName(), &data); int entries = tree->GetEntries(); for (int i = 0; i < entries; i++) { branch->GetEntry(i); write_out_type(o, data); } }
void getlist(ostream& out, TBranch* branch, int depth=0) { TObjArray* array = branch->GetListOfBranches(); if ( ! array ) return; if ( depth > 10 ) return; string name; int nitems = array->GetEntries(); for (int i = 0; i < nitems; i++) { TBranch* b = (TBranch*)((*array)[i]); if ( ! b ) continue; string branchname(b->GetName()); out << SPACE.substr(0,4*depth) << branchname << endl; TObjArray* a = b->GetListOfLeaves(); if ( a ) { int n = a->GetEntries(); { for (int j = 0; j < n; j++) { TLeaf* leaf = (TLeaf*)((*a)[j]); int count = 0; int ndata = 0; TLeaf* leafc = leaf->GetLeafCounter(count); if ( ! leafc) ndata = leaf->GetLen(); else ndata = leafc->GetMaximum(); string leafname(leaf->GetName()); out << SPACE.substr(0,4*(depth+1)) << ndata << " " << leafname << endl; } } // else if ( n == 1 ) // { // TBranch* bc = (TBranch*)((*a)[j]); // string leafname(bc->GetName()); // if ( leafname != branchname ) // out << SPACE.substr(0,4*(depth+1)) << leafname << endl; // } } getlist(out, b, depth+1); } }
void paracoor( TString fin = "TMVA.root", Bool_t useTMVAStyle = kTRUE ) { // set style and remove existing canvas' TMVAGlob::Initialize( useTMVAStyle ); // checks if file with name "fin" is already open, and if not opens one TFile* file = TMVAGlob::OpenFile( fin ); TTree* tree = (TTree*)file->Get("TestTree"); if(!tree) { cout << "--- No TestTree saved in ROOT file. Parallel coordinates will not be plotted" << endl; return; } // first get list of leaves in tree TObjArray* leafList = tree->GetListOfLeaves(); vector<TString> vars; vector<TString> mvas; for (Int_t iar=0; iar<leafList->GetSize(); iar++) { TLeaf* leaf = (TLeaf*)leafList->At(iar); if (leaf != 0) { TString leafName = leaf->GetName(); if (leafName != "type" && leafName != "weight" && leafName != "boostweight" && leafName != "class" && leafName != "className" && leafName != "classID" && !leafName.Contains("prob_")) { // is MVA ? if (TMVAGlob::ExistMethodName( leafName )) { mvas.push_back( leafName ); } else { vars.push_back( leafName ); } } } } cout << "--- Found: " << vars.size() << " variables" << endl; cout << "--- Found: " << mvas.size() << " MVA(s)" << endl; TString type[2] = { "Signal", "Background" }; const Int_t nmva = mvas.size(); TCanvas* csig[nmva]; TCanvas* cbkg[nmva]; for (Int_t imva=0; imva<mvas.size(); imva++) { cout << "--- Plotting parallel coordinates for : " << mvas[imva] << " & input variables" << endl; for (Int_t itype=0; itype<2; itype++) { // create draw option TString varstr = mvas[imva] + ":"; for (Int_t ivar=0; ivar<vars.size(); ivar++) varstr += vars[ivar] + ":"; varstr.Resize( varstr.Last( ':' ) ); // create canvas TString mvashort = mvas[imva]; mvashort.ReplaceAll("MVA_",""); TCanvas* c1 = (itype == 0) ? csig[imva] : cbkg[imva]; c1 = new TCanvas( Form( "c1_%i",itype ), Form( "Parallel coordinate representation for %s and input variables (%s events)", mvashort.Data(), type[itype].Data() ), 50*(itype), 50*(itype), 750, 500 ); tree->Draw( varstr.Data(), Form("classID==%i",1-itype) , "para" ); c1->ToggleEditor(); gStyle->SetOptTitle(0); TParallelCoord* para = (TParallelCoord*)gPad->GetListOfPrimitives()->FindObject( "ParaCoord" ); TParallelCoordVar* mvavar = (TParallelCoordVar*)para->GetVarList()->FindObject( mvas[imva] ); Double_t minrange = tree->GetMinimum( mvavar->GetName() ); Double_t maxrange = tree->GetMaximum( mvavar->GetName() ); Double_t width = 0.2*(maxrange - minrange); Double_t x1 = minrange, x2 = x1 + width; TParallelCoordRange* parrange = new TParallelCoordRange( mvavar, x1, x2 ); parrange->SetLineColor(4); mvavar->AddRange( parrange ); para->AddSelection("-1"); for (Int_t ivar=1; ivar<TMath::Min(Int_t(vars.size()) + 1,3); ivar++) { TParallelCoordVar* var = (TParallelCoordVar*)para->GetVarList()->FindObject( vars[ivar] ); minrange = tree->GetMinimum( var->GetName() ); maxrange = tree->GetMaximum( var->GetName() ); width = 0.2*(maxrange - minrange); switch (ivar) { case 0: { x1 = minrange; x2 = x1 + width; break; } case 1: { x1 = 0.5*(maxrange + minrange - width)*0.02; x2 = x1 + width*0.02; break; } case 2: { x1 = maxrange - width; x2 = x1 + width; break; } } parrange = new TParallelCoordRange( var, x1, x2 ); parrange->SetLineColor( ivar == 0 ? 2 : ivar == 1 ? 5 : 6 ); var->AddRange( parrange ); para->AddSelection( Form("%i",ivar) ); } c1->Update(); TString fname = Form( "plots/paracoor_c%i_%s", imva, itype == 0 ? "S" : "B" ); TMVAGlob::imgconv( c1, fname ); } } }
void dump_tree(TTree *tree, const char *outdir) { GzipOutputStream::Options options; options.compression_level = 1; for(int li = 0; li < tree->GetListOfLeaves()->GetEntries(); li++) { TLeaf *l = (TLeaf*) tree->GetListOfLeaves()->At(li); if (lstat(outdir, NULL) == -1) { mkdir(outdir, 0777); } // Open data file std::string fn = std::string(outdir) + "/" + l->GetName() + ".dit"; auto fd = open(fn.c_str(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); assert(fd != -1); FileOutputStream fstream(fd); GzipOutputStream zstream(&fstream, options); // Open meta file std::string mfn = fn + "m"; auto mfd = open(mfn.c_str(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); assert(mfd != -1); FileOutputStream meta_fstream(mfd); GzipOutputStream meta_zstream(&meta_fstream, options); { // Coded stream block CodedOutputStream o(&zstream); CodedOutputStream o2(&meta_zstream); // determine level and type name int level = 0; std::string tn = l->GetTypeName(); while (tn.substr(0,6) == "vector") { level = level + 1; tn = remove_vector(tn); } if (tn == "double") { dump_required<double>(level, tree, *l, o, o2); } else if (tn == "float") { dump_required<float>(level, tree, *l, o, o2); } else if (tn == "int") { dump_required<int>(level, tree, *l, o, o2); } else if (tn == "short" || tn == "Short_t") { dump_required<short>(level, tree, *l, o, o2); } else if (tn == "unsigned int") { dump_required<unsigned int>(level, tree, *l, o, o2); } else if (tn == "unsigned short") { dump_required<unsigned short>(level, tree, *l, o, o2); } else if (tn == "Float_t") { dump_required<Float_t>(level, tree, *l, o, o2); } else if (tn == "Bool_t") { dump_required<Bool_t>(level, tree, *l, o, o2); } else if (tn == "Char_t") { dump_required<Char_t>(level, tree, *l, o, o2); } else if (tn == "Double_t") { dump_required<Double_t>(level, tree, *l, o, o2); } else if (tn == "Int_t") { dump_required<Int_t>(level, tree, *l, o, o2); } else if (tn == "UInt_t") { dump_required<UInt_t>(level, tree, *l, o, o2); } else if (tn == "string") { dump_required<std::string>(level, tree, *l, o, o2); } else { std::cerr << "Unknown branch type: " << tn << std::endl; assert(false); } } meta_zstream.Close(); zstream.Close(); meta_fstream.Close(); fstream.Close(); } }
bool TreeReader::Initialize(vector <string> br, string opt) { if(!init) { if( !fChain ) { cout << endl; cout << "No tree to initialize" << endl; cout << endl; return false; } TObjArray *fileElements = fChain->GetListOfFiles(); if( !fileElements || ( fileElements->GetEntries() == 0 )) { cout << endl; cout << "No file(s) to initialize" << endl; cout << endl; return false; } } varList.clear(); TObjArray* branches = fChain->GetListOfBranches(); int nBranches = branches->GetEntries(); for (int i = 0; i < nBranches; ++i) { TBranch* branch = (TBranch*)branches->At(i); string brname = branch->GetName(); TLeaf* leaf = branch->GetLeaf(branch->GetName()); if ( leaf == 0 ) // leaf name is different from branch name { TObjArray* leafs = branch->GetListOfLeaves(); leaf = (TLeaf*)leafs->At(0); } string curtype = leaf->GetTypeName(); int id = TypeDB::getType(curtype.c_str()); int arreysize = 1; string title = leaf->GetTitle(); //cout << curtype << " " << title << endl; // Find out whether we have array by inspecting leaf title if ( title.find("[")!=std::string::npos ) { TLeaf * nelem = leaf->GetLeafCounter(arreysize); if(arreysize == 1 && nelem != NULL) arreysize = nelem->GetMaximum() + 1; //search for maximum value of the lenght } if(id >= 0) { bool addVar = true; if(br.size()>0) { addVar = false; for(unsigned b = 0; b < br.size(); b++) { if(opt == "names" || opt == "except") { if(br[b] == brname) { addVar = true; break;} } else if(opt.find("contains")!=string::npos) { if((string(brname)).find(br[b])!=string::npos) { addVar = true; break;} } else if(opt.find("except")==string::npos) cout << "Option " << opt << " not found" << endl; } if(opt.find("except")!=string::npos) addVar = !addVar; } if(addVar) { variable * tmpVar = new variable(id,arreysize); tmpVar->name = leaf->GetName(); tmpVar->bname = branch->GetName(); tmpVar->title = title; varList.push_back(tmpVar); fChain->SetBranchAddress(tmpVar->bname,tmpVar->value.address); } } else { cout << curtype << ": type not found" << endl; exit(1); return false; } } init = true; continueSorting = true; if(pmode=="v") cout << endl << "Set up " << varList.size() << " / " << nBranches << " branches" << endl; return true; }