#include #include #include #include "TFile.h" #include "TString.h" #include "TDirectory.h" #include "TKey.h" #include "TAxis.h" #include "TObject.h" #include "TH1.h" typedef std::map DiscInd; typedef DiscInd::const_iterator DiscInd_iter; typedef std::map IndDisc; typedef IndDisc::const_iterator IndDisc_iter; void GetInputVarNumberingFromTemplate(std::string file, std::string dirname="UtuplePlotter", std::string searchstring="InputVarHiSB_",bool debug=true){ std::string method("GetInputVarNumberingFromTemplate: "); TFile *f=new TFile(file.c_str()); TDirectory* dir=dynamic_cast(f->Get(dirname.c_str())); if(!dir){ std::cout << method<< "Unable to locate" << dirname<GetListOfKeys()); TKey *key; while ((key = dynamic_cast(next()))) { std::string KeyName(key->GetName()); // Want to find and dissect InputVarHiSB_25_3_3 (for the default searc hstring) // We get the index number from the histogram name by recognizing // InputVarHiSB_ and pulling the string between that and the next _. // After that, we need to get the title of this to get the // name of the discrimnant // In this example the x title is lep2_E - N_{j}=3 // We want to find the space and dash then pull the name of the // discrimnant. // Then we insert this into a map and at the end // print the map. We also print the result in a format suitable // for putting the list of variables into a steering file. // So.. // See if we have the searchstring int prepos=KeyName.find(searchstring,0); if(debug)std::cout << "For string " << KeyName << " prepos= " << prepos << std::endl; if(prepos>=0){ // // Ok, we have it, os now we look for the next _ to get the index if(debug)std::cout <<"Checking "<< KeyName<0){ // Now we found the underscore. We store the index int indlen=postpos-endpos-1; if(debug)std::cout << "endpos=" << endpos << " postpos= " << postpos << " indlen= " << indlen << std::endl; std::string indexno=KeyName.substr(endpos+1,indlen); if(debug)std::cout << " Index is " << indexno << std::endl; // Now get the object, cast to a TH1, Get the Xaxis and get the title TObject* o=dynamic_cast(dir->Get(KeyName.c_str())); if(!o){ std::cout << method<< "Unable to get object for " << KeyName << std::endl; return; } TH1* h=dynamic_cast(o); // Get TH1 if(!o){ std::cout << method<<"Unable to get histogram for " << KeyName << std::endl; return; } TAxis *xa=h->GetXaxis(); // Get Xaxis if(!xa){ std::cout << method<< "Unable to get xaxis for " << KeyName << std::endl; return; } std::string title=xa->GetTitle();// Get the title // if(debug)std::cout << "Title is : " << title << std::endl; // Dissect the title: find the space and dash int dashpos=title.find(" "); // Ok, got the pieces, so extract the name std::string discriminator=title.substr(0, dashpos); if(debug)std::cout << "Got Discriminator " << discriminator << std::endl; // Put it into the map if(discrmap->find(discriminator)!=discrmap->end()){ if(debug)std::cout <<"Already have "<< std::endl; }else{ if(debug)std::cout << "Inserted" << std::endl; (*discrmap)[discriminator]=atoi(indexno.c_str()); } } } } // Print the map ordered by names and create an index-ordered map IndDisc* indmap=new IndDisc(); for (DiscInd_iter i=discrmap->begin();i!=discrmap->end();i++){ std::cout << i->first << " : " << i->second << std::endl; (*indmap)[i->second]=i->first; } // Print the index-ordered map for (IndDisc_iter i=indmap->begin();i!=indmap->end();i++){ std::cout << i->first << " : " << i->second << std::endl; } // Print the syntax needed for steering files std::cout << "ColumnParameter ExtraPlotVariables 0 "; for (DiscInd_iter i=discrmap->begin();i!=discrmap->end();i++){ if(i!=discrmap->begin())std::cout << ":"; std::cout << i->first << "="<second; } std::cout << endl; }