void getHistosFromRE(const string& mhid, const string& filepath, const string& sre, vector<std::pair<string,wTH1*> >& v_wth1) { if (gl_verbose) cout<<"Searching for regexp "<<sre<<" in "<<filepath; // allow for multiple regexes in OR combination // vector<string> v_regexes; Tokenize(sre,v_regexes,"|"); if (!v_regexes.size()) v_regexes.push_back(sre); // Build validated TRegexp arguments in preparation for directory recursion // TObjArray *Args = new TObjArray(); for (size_t i=0; i<v_regexes.size(); i++) { TRegexp re(v_regexes[i].c_str(),kTRUE); if (re.Status() != TRegexp::kOK) { cerr << "The regexp " << v_regexes[i] << " is invalid, Status() = "; cerr << re.Status() << endl; exit(-1); } else { Args->AddLast(new TObjString(v_regexes[i].c_str())); } } // Get the root file // TFile *rootfile = openRootFile(filepath); if (!rootfile) { cerr << "File failed to open, " << filepath << endl; Args->Delete(); delete Args; return; } // Do the recursion, collect matches // TObjArray *Matches = new TObjArray(); recurseDirs(rootfile, ®exMatchHisto, Args, Matches); Args->Delete(); delete Args; // Returns two objects per match: // 1. the (string) path that was matched and // 2. the object whose path matched // int nx2matches = Matches->GetEntriesFast(); if (gl_verbose) cout << "... " << nx2matches/2 << " match(es) found."; // Add the matches to the global map of histos // int istart = v_wth1.size(); for (int i=0; i<nx2matches; i+=2) { TString fullspec = ((TObjString *)(*Matches)[i])->GetString(); wTH1 *wth1 = new wTH1((TH1 *)((*Matches)[i+1])); wth1->histo()->UseCurrentStyle(); wth1->histo()->SetLineColor(((i/2)%9)+1); wth1->histo()->SetLineStyle((i/18)+1); wth1->histo()->SetLineWidth(2); wth1->SetLegendEntry(wth1->histo()->GetName()); string hidi= mhid+"_"+int2str(istart+(i/2)); v_wth1.push_back(std::pair<string,wTH1 *>(hidi,wth1)); //glmap_objpath2id.insert(pair<string,string>(fullspec,hidi)); glmap_id2histo.insert(pair<string,wTH1 *>(hidi,wth1)); glmap_id2objpath.insert(pair<string,string>(hidi,string(fullspec.Data()))); } //Matches->Delete(); // need the histos! delete Matches; if (gl_verbose) cout << endl; } // getHistosFromRE
TTree *getTreeFromSpec(const string& tid, const string& spec) { TTree *tree = NULL; TFile *rootfile = NULL; vector<string> v_tokens; string fullspec; if( gl_verbose) cout << "processing " << spec << endl; string tspec; string rootfn; fullspec = spec; Tokenize(fullspec,v_tokens,":"); int ntok = (int)v_tokens.size(); if( ((ntok != 2) && (ntok != 3) ) || //ntok==3 means file specifier is a URL (!v_tokens[0].size()) || (!v_tokens[1].size()) ) { cerr << "malformed root tree path file:folder/subfolder/.../histo " << fullspec << endl; return NULL; } else { rootfn = (ntok==2) ? v_tokens[0] : v_tokens[0]+":"+v_tokens[1]; tspec = (ntok==2) ? v_tokens[1] : v_tokens[2]; } #if 0 map<string,string>::const_iterator it = glmap_objpath2id.find(fullspec); if( it != glmap_objpath2id.end() ) { // Allow the possibility to run the script a second time in root if( gl_verbose) cout << "Object " << fullspec << " already read in, here it is" << endl; map<string,tree *>::const_iterator hit = glmap_id2histo.find(it->second); if( hit == glmap_id2histo.end() ) { if( gl_verbose) cout << "oops, sorry, I lied." << endl; return NULL; } tree = hit->second; } else { #endif rootfile = openRootFile(rootfn); if (rootfile) { tree = (TTree *)rootfile->Get(tspec.c_str()); if( !tree) { cerr << "couldn't find " << tspec << " in " << rootfn << endl; } else { // success, record that you read it in. if( gl_verbose) cout << "Found " << fullspec << endl; glmap_objpath2id.insert(pair<string,string>(fullspec,tid)); glmap_id2objpath.insert(pair<string,string>(tid,fullspec)); glmap_id2tree.insert(pair<string,TTree *>(tid,tree)); } } //} return tree; } // getTreeFromSpec //====================================================================== bool // returns true if success processTreeSection(FILE *fp, string& theline, bool& new_section) { string *tid = NULL; TTree *t1 = NULL; if (gl_verbose) cout << "Processing Tree section" << endl; new_section=false; while (getLine(fp,theline,"Tree")) { if (!theline.size()) continue; if (theline[0] == '#') continue; // comments are welcome if (theline[0] == '[') { new_section=true; break; } string key, value; if (!getKeyValue(theline,key,value)) continue; //-------------------- if (key == "id") { //-------------------- if (tid != NULL) { cerr << "no more than one id per F1 section allowed " << value << endl; break; } tid = new string(value); map<string, TTree *>::const_iterator tit = glmap_id2tree.find(*tid); if (tit != glmap_id2tree.end()) { cerr << "Tree id " << *tid << " already defined" << endl; break; } //------------------------------ } else if( key == "path" ) { //------------------------------ if( !tid ) { cerr << "id key must be defined first in the section" << endl; continue; } if( t1 ) { cerr << "histo already defined" << endl; continue; } t1 = getTreeFromSpec(*tid,value); if( !t1 ) continue; } else { cerr << "unknown key " << key << endl; } } if (tid) delete tid; return (t1 != NULL); } // processTreesection
bool // returns true if success processTreeSection(FILE *fp, string& theline, bool& new_section) { string *tid = NULL; TChain *t1 = NULL; vector<string> v_tokens; string treename; if (gl_verbose) cout << "Processing Tree section" << endl; new_section=false; while (getLine(fp,theline,"Tree")) { if (!theline.size()) continue; if (theline[0] == '#') continue; // comments are welcome if (theline[0] == '[') { new_section=true; break; } string key, value; if (!getKeyValue(theline,key,value)) continue; //-------------------- if (key == "id") { //-------------------- if (tid != NULL) { cerr << "no more than one id per F1 section allowed " << value << endl; break; } tid = new string(value); map<string, TChain *>::const_iterator tit = glmap_id2chain.find(*tid); if (tit != glmap_id2chain.end()) { cerr << "Tree id " << *tid << " already defined" << endl; break; } //------------------------------ } else if( key == "treename" ) { //------------------------------ if( !tid ) { cerr << "id key must be defined first in the section" << endl; continue; } if( t1 ) { cerr << "Tree already defined" << endl; continue; } treename = value; //------------------------------ } else if( key == "globslist" ) { //------------------------------ t1 = getChainFromGlobslist(*tid,treename,value); if( !t1 ) exit(-1); //------------------------------ } else if( key == "copytree" ) { //------------------------------ if( !tid ) { cerr << "id key must be defined first in the section" << endl; continue; } Tokenize(value,v_tokens,","); if (v_tokens.size() != 2) { cerr << "copytree syntax expected: copytree=treeid,cutstring: " << value << endl; continue; } TChain *t2 = findChain(v_tokens[0]); if (!t2) { cerr << "tree " << v_tokens[0] << " must be defined previously" << endl; continue; } if (gl_verbose) cout<<"Begin CopyTree of "<<v_tokens[0]<<" with selection "<<v_tokens[1]<<flush; t1 = (TChain *)(t2->CopyTree(v_tokens[1].c_str())); if( !t1 ) { cerr << "CopyTree failed" << endl; exit(-1); } if (gl_verbose) cout<<"...Done."<<endl; //------------------------------ } else if( key == "save2file" ) { //------------------------------ if( !t1 ) { cerr << "save2file: must define tree first" << endl; continue; } TFile *rootfile = openRootFile(value,"RECREATE"); t1->SetDirectory(rootfile); t1->Write(); rootfile->Flush(); if (gl_verbose) cout << "Tree written to file " << value << endl; rootfile->Close(); //------------------------------ } else if( key == "unbinnedfit" ) { //------------------------------ if( !tid ) { cerr << "id key must be defined first in the section" << endl; continue; } if( !t1 ) { cerr << "Tree must already be defined using 'globslist'" << endl; continue; } int fitresult=-99; Tokenize(value,v_tokens,","); switch(v_tokens.size()) { case 2: fitresult = t1->UnbinnedFit(v_tokens[0].c_str(), v_tokens[1].c_str()); break; case 3: fitresult = t1->UnbinnedFit(v_tokens[0].c_str(), v_tokens[1].c_str(), v_tokens[2].c_str()); break; case 4: fitresult = t1->UnbinnedFit(v_tokens[0].c_str(), // funcname v_tokens[1].c_str(), // varexp v_tokens[2].c_str(), // selection v_tokens[3].c_str()); // option break; default: cerr << "unbinnedfit: expect 2-4 arguments separated by commas: " << value <<endl; exit(-1); } cout << "fit result returned = " << fitresult << endl; cout << "Number of selected entries in the fit = " << t1->GetSelectedRows() << endl; } else { cerr << "unknown key " << key << endl; } } if (tid) delete tid; return (t1 != NULL); } // processTreesection