Changeset 262 in Idarraga
- Timestamp:
- Nov 30, 2011, 11:53:01 AM (12 years ago)
- Location:
- root_tools/multipleplots
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
root_tools/multipleplots/LinkDef_plots_treeTools.h
r154 r262 17 17 #pragma link C++ function GetHistoKey; 18 18 #pragma link C++ function GetHistoPtr; 19 #pragma link C++ function GetHistosMap; 19 20 #endif -
root_tools/multipleplots/plots_treeTools.cpp
r154 r262 9 9 #include <TCut.h> 10 10 11 #include <stdlib.h> 12 #include <map> 13 14 using namespace std; 15 11 16 // globals 12 17 map<TString, TTree *> g_myTreeMap; 13 18 map<TString, double> g_myTreeMapScale; 19 map<int, TString> g_myTreeOriginalOrdering; 20 int g_OriginalOrdering = 0; 14 21 15 22 map<TString, TObject *> g_myHistosMap; … … 18 25 vector<TString> g_histosOrder; 19 26 27 map<TString, TObject *> GetHistosMap(){return g_myHistosMap;} 28 20 29 int GetNHistos(TString idString){ 21 22 int cntr = 0; 23 24 25 26 27 if(tempS.Contains(idString, TString::kExact)) 28 29 30 31 30 map<TString, TObject *>::iterator itr = g_myHistosMap.begin(); 31 int cntr = 0; 32 TString tempS; 33 for( ; itr != g_myHistosMap.end() ; itr++){ 34 //cout << (*itr).second->GetName() << endl; 35 tempS = (*itr).second->GetName(); 36 if(tempS.Contains(idString, TString::kExact)) 37 cntr++; 38 } 39 40 return cntr; 32 41 } 33 42 34 43 TString GetHistoKey(int i, TString idString){ 35 36 37 38 39 40 41 42 44 map<TString, TObject *>::iterator itr = g_myHistosMap.begin(); 45 int cntr = 0; 46 TString tempS; 47 for( ; itr != g_myHistosMap.end() ; itr++){ 48 tempS = (*itr).second->GetName(); 49 if(cntr++ == i && tempS.Contains(idString, TString::kExact)) return (*itr).first; 50 } 51 return TString(""); 43 52 } 44 53 45 54 TObject * GetHistoPtr(int i, TString idString){ 46 47 48 49 50 51 52 53 55 map<TString, TObject *>::iterator itr = g_myHistosMap.begin(); 56 int cntr = 0; 57 TString tempS; 58 for( ; itr != g_myHistosMap.end() ; itr++){ 59 tempS = (*itr).second->GetName(); 60 if(cntr++ == i && tempS.Contains(idString, TString::kExact)) return (*itr).second; 61 } 62 return 0x0; 54 63 } 55 64 56 65 57 66 void TreeFillMap(TString n, TTree * p){ 58 67 g_myTreeMap[n] = p; 59 68 } 60 69 61 70 TTree * GetTreePtr(TString n){ 62 71 return g_myTreeMap[n]; 63 72 } 64 73 65 74 int NOfColons(const char * sc){ 66 75 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 76 std::string s = sc; 77 size_t pos = std::string::npos; 78 int dims = 1; 79 80 do{ 81 82 pos = s.find(':'); 83 if(pos == std::string::npos) continue; 84 85 s = s.substr(pos+1); 86 dims++; 87 88 } while(pos != std::string::npos); 89 90 return dims; 82 91 } 83 92 84 93 TObject * DrawAll(TString baseDraw, TCut cutsDraw, OnCanvasOptions & ocOpts, 85 ActionFlags afl){//, TVirtualPad * vPad){ 86 87 // Check the dimension 88 int nDim = NOfColons(baseDraw.Data()); 89 cout << endl; 90 cout << "[INFO] attemp to draw " << nDim << "D histogram with cut:" << endl; 91 cout << " "; cutsDraw.Print(); 92 93 TString tempDraw = ""; 94 TString tempHisto = ""; 95 96 map<TString, TTree *>::iterator itr = g_myTreeMap.begin(); 97 98 for( ; itr != g_myTreeMap.end() ; itr++){ 99 100 tempDraw = baseDraw; 101 tempDraw += ">>"; 102 103 // histo name /////////////////////////////// 104 tempHisto = "h_"; 105 // get rid of ":" in the name 106 TString baseReplacement = baseDraw; 107 baseReplacement.ReplaceAll(':', "_"); 108 baseReplacement.ReplaceAll('[', ""); 109 baseReplacement.ReplaceAll(']', ""); 110 baseReplacement.ReplaceAll('$', ""); 111 baseReplacement.ReplaceAll('(', ""); 112 baseReplacement.ReplaceAll(')', ""); 113 tempHisto += baseReplacement; 114 tempHisto += "_"; 115 tempHisto += (*itr).first; 116 ///////////////////////////////////////////// 117 118 tempDraw += tempHisto; 119 // Add constructor parameters here 120 tempDraw += ocOpts.constructorStr; 121 122 // Draw 123 (*itr).second->Draw(tempDraw, cutsDraw); 124 // save histos pointers 125 // do not use gPad ! 126 //g_myHistosMap[(*itr).first] = gPad->GetPrimitive(tempHisto); 127 g_myHistosMap[(*itr).first] = gDirectory->Get(tempHisto); 128 // Scale to the factor in GetTree(...) 129 130 ((TH1 *)(g_myHistosMap[(*itr).first]))->Scale(g_myTreeMapScale[(*itr).first]); 131 132 133 // histogram integrals to calculate if requested 134 g_myHistosCalc[(*itr).first] = 0.; 135 136 if(afl.doIntegral){ 137 // do nothing for now 138 } 139 140 cout << "[DRAW] " 141 << tempDraw << " --> " 142 << tempHisto << " (" 143 << g_myHistosMap[(*itr).first] 144 << ") " << (*itr).second 145 << endl; 146 147 //g_myHistosMap[(*itr).first]->Dump(); 148 } 149 150 151 152 TH1 * tempHistoPtr = 0x0; 153 TH2 * tempHisto2DPtr = 0x0; 154 if(nDim == 1) tempHistoPtr = (TH1 *) (*(g_myHistosMap.begin())).second; 155 if(nDim == 2) tempHisto2DPtr = (TH2 *) (*(g_myHistosMap.begin())).second; 156 if(!tempHistoPtr && !tempHisto2DPtr) { 157 cout << "[ERROR] no histo at all ?!" << endl; 158 exit(1); 159 } 160 161 if(nDim == 1 && g_histosOrder.empty()) { // if 1D histos and if order is empty 162 163 map<TString, TObject *>::iterator itr2; 164 165 // decide the order, highest first 166 for(itr2 = g_myHistosMap.begin() ; itr2 != g_myHistosMap.end() ; itr2++) 167 g_histosOrder.push_back( (*itr2).first ); 168 169 cout << "[INFO] ordering ..." << endl; 170 171 double maxh = 0.; 172 maxh = tempHistoPtr->GetMaximum(); 173 174 TString temph = ""; 175 // start comparing from the second histo 176 177 int cntr = 1; 178 itr2 = g_myHistosMap.begin(); 179 itr2++; 180 for( ; itr2 != g_myHistosMap.end() ; itr2++) 181 { 182 tempHistoPtr = dynamic_cast<TH1 *>((*itr2).second); // works for TH2 as well 183 184 if(tempHistoPtr->GetMaximum() > maxh){ 185 186 maxh = tempHistoPtr->GetMaximum(); 187 188 temph = g_histosOrder[cntr]; 189 g_histosOrder[cntr] = g_histosOrder[cntr-1]; 190 g_histosOrder[cntr-1] = temph; 191 } 192 cntr++; 193 } 194 } 195 196 // print order and draw ! 197 vector<TString>::iterator itr3 = g_histosOrder.begin(); 198 cout << " Order : "; 199 int linecolor = 1; 200 TString drawOpt = ocOpts.drawOpt; // initialize to user-defined 201 202 //vPad->cd(); 203 204 pair<TH1 *, double> storeMax; 205 storeMax.first = 0x0; 206 storeMax.second = 0; 207 double minims = -1; 208 for( ; itr3 != g_histosOrder.end() ; itr3++){ 209 cout << *itr3; 210 if(itr3+1 != g_histosOrder.end() ) cout << " , "; 211 else cout << endl; 212 // draw 213 if(nDim == 1) { 214 tempHistoPtr = dynamic_cast<TH1 *>(g_myHistosMap[*itr3]); 215 tempHistoPtr->SetLineColor(linecolor++); 216 217 if(!ocOpts.drawNormalized) tempHistoPtr->Draw(drawOpt); 218 else tempHistoPtr = tempHistoPtr->DrawNormalized(drawOpt); 219 220 //tempHistoPtr->Fit("gaus","","same",0.05,1.2); 221 222 //tempHistoPtr->GetXaxis()->SetRangeUser(0,10); 223 // the order has been decided in the first plot 224 // but in a second iteration we may need to 225 // RangeUser 226 if(tempHistoPtr->GetMaximum() > storeMax.second) { 227 228 if(storeMax.first == 0x0) storeMax.first = tempHistoPtr; // store the first pointer only --> contains the right axis 229 storeMax.second = tempHistoPtr->GetMaximum(); 230 231 if(minims < 0) minims = tempHistoPtr->GetMinimum(); 232 233 } 234 235 if(tempHistoPtr->GetMinimum() < minims) 236 minims = tempHistoPtr->GetMinimum(); 237 238 }else if(nDim == 2){ 239 tempHisto2DPtr = dynamic_cast<TH2 *>(g_myHistosMap[*itr3]); 240 tempHisto2DPtr->SetMarkerColor(linecolor++); 241 tempHisto2DPtr->SetMarkerStyle(ocOpts.markerStyle); 242 tempHisto2DPtr->Draw(drawOpt); 243 } 244 245 // the order has been decided in the first plot 246 // but in a second iteration we may need to 247 // RangeUser 248 249 250 drawOpt = "same"; 251 } 252 253 // Set Range user 254 if(nDim == 1) { 255 if(ocOpts.setLogY){ 256 if(ocOpts.drawNormalized && minims == 0) minims = 1E-6; 257 storeMax.first->GetYaxis()->SetRangeUser(minims, storeMax.second*10.); 258 }else{ 259 storeMax.first->GetYaxis()->SetRangeUser(minims, storeMax.second*1.1); 260 } 261 cout << "RangeUser - -> " << storeMax.first << " to " << storeMax.second << endl; 262 } 263 264 //if(ocOpts.setLogY) gPad->SetLogy(); 265 266 // do legend 267 if(ocOpts.doLegend){ 268 TLegend * t1 = new TLegend(0.6, 0.5, 0.9, 0.9); 269 t1->SetBorderSize(1); 270 t1->SetFillColor(kWhite); 271 for(itr3 = g_histosOrder.begin() ; 272 itr3 != g_histosOrder.end() ; 273 itr3++){ 274 if(nDim == 1) t1->AddEntry(dynamic_cast<TH1 *>(g_myHistosMap[*itr3]), *itr3, "L"); 275 if(nDim == 2) t1->AddEntry(dynamic_cast<TH2 *>(g_myHistosMap[*itr3]), *itr3, "M"); 276 } 277 t1->Draw(); 278 } 279 280 // return the first drawn 281 return g_myHistosMap[g_histosOrder[0]]; 94 ActionFlags afl){//, TVirtualPad * vPad){ 95 96 // Check the dimension 97 int nDim = NOfColons(baseDraw.Data()); 98 cout << endl; 99 cout << "[INFO] attemp to draw " << nDim << "D histogram with cut:" << endl; 100 cout << " "; cutsDraw.Print(); 101 102 TString tempDraw = ""; 103 TString tempHisto = ""; 104 105 /* 106 map<int, TString>::iterator itrO = g_myTreeOriginalOrdering.begin(); 107 for( ; itrO != g_myTreeOriginalOrdering.end() ; itrO++) { 108 cout << (*itrO).first << " --> " << (*itrO).second << endl; 109 } 110 */ 111 112 map<TString, TTree *>::iterator itr = g_myTreeMap.begin(); 113 for(itr = g_myTreeMap.begin() ; itr != g_myTreeMap.end() ; itr++){ 114 115 tempDraw = baseDraw; 116 tempDraw += ">>"; 117 118 // histo name /////////////////////////////// 119 tempHisto = "h_"; 120 // get rid of ":" in the name 121 TString baseReplacement = baseDraw; 122 baseReplacement.ReplaceAll(':', "_"); 123 baseReplacement.ReplaceAll('[', ""); 124 baseReplacement.ReplaceAll(']', ""); 125 baseReplacement.ReplaceAll('$', ""); 126 baseReplacement.ReplaceAll('(', ""); 127 baseReplacement.ReplaceAll(')', ""); 128 tempHisto += baseReplacement; 129 tempHisto += "_"; 130 tempHisto += (*itr).first; 131 ///////////////////////////////////////////// 132 133 tempDraw += tempHisto; 134 // Add constructor parameters here 135 tempDraw += ocOpts.constructorStr; 136 137 // Draw 138 (*itr).second->Draw(tempDraw, cutsDraw); 139 // save histos pointers 140 // do not use gPad ! 141 //g_myHistosMap[(*itr).first] = gPad->GetPrimitive(tempHisto); 142 g_myHistosMap[(*itr).first] = gDirectory->Get(tempHisto); 143 // Scale to the factor in GetTree(...) 144 145 ((TH1 *)(g_myHistosMap[(*itr).first]))->Scale(g_myTreeMapScale[(*itr).first]); 146 147 148 // histogram integrals to calculate if requested 149 g_myHistosCalc[(*itr).first] = 0.; 150 151 if(afl.doIntegral){ 152 // do nothing for now 153 } 154 155 cout << "[DRAW] " 156 << tempDraw << " --> " 157 << tempHisto << " (" 158 << g_myHistosMap[(*itr).first] 159 << ") " << (*itr).second 160 << endl; 161 162 //g_myHistosMap[(*itr).first]->Dump(); 163 } 164 165 TH1 * tempHistoPtr = 0x0; 166 TH2 * tempHisto2DPtr = 0x0; 167 if(nDim == 1) tempHistoPtr = (TH1 *) (*(g_myHistosMap.begin())).second; 168 if(nDim == 2) tempHisto2DPtr = (TH2 *) (*(g_myHistosMap.begin())).second; 169 if(!tempHistoPtr && !tempHisto2DPtr) { 170 cout << "[ERROR] no histo at all ?!" << endl; 171 exit(1); 172 } 173 174 if(nDim == 1 && g_histosOrder.empty() && ocOpts.orderHistos) { // if 1D histos and if order is empty 175 176 map<TString, TObject *>::iterator itr2; 177 178 // decide the order, highest first 179 for(itr2 = g_myHistosMap.begin() ; itr2 != g_myHistosMap.end() ; itr2++) 180 g_histosOrder.push_back( (*itr2).first ); 181 182 cout << "[INFO] ordering ..." << endl; 183 184 double maxh = 0.; 185 maxh = tempHistoPtr->GetMaximum(); 186 187 TString temph = ""; 188 // start comparing from the second histo 189 190 int cntr = 1; 191 itr2 = g_myHistosMap.begin(); 192 itr2++; 193 for( ; itr2 != g_myHistosMap.end() ; itr2++) 194 { 195 tempHistoPtr = static_cast<TH1 *>((*itr2).second); // works for TH2 as well 196 197 if(tempHistoPtr->GetMaximum() > maxh){ 198 199 maxh = tempHistoPtr->GetMaximum(); 200 201 temph = g_histosOrder[cntr]; 202 g_histosOrder[cntr] = g_histosOrder[cntr-1]; 203 g_histosOrder[cntr-1] = temph; 204 } 205 cntr++; 206 } 207 } 208 209 // if no order required, use the original ordering 210 211 if( nDim == 1 && g_histosOrder.empty() && !ocOpts.orderHistos ) { 212 213 for(int itr2 = 0 ; itr2 < (int)g_myTreeOriginalOrdering.size() ; itr2++) { 214 //cout << " --> " << g_myTreeOriginalOrdering[itr2] << endl; 215 g_histosOrder.push_back( g_myTreeOriginalOrdering[itr2] ); 216 } 217 218 } 219 220 // print order and draw ! 221 vector<TString>::iterator itr3 = g_histosOrder.begin(); 222 cout << " Order : "; 223 int linecolor = 1; 224 TString drawOpt = ocOpts.drawOpt; // initialize to user-defined 225 226 //vPad->cd(); 227 228 pair<TH1 *, double> storeMax; 229 storeMax.first = 0x0; 230 storeMax.second = 0; 231 double minims = -1; 232 for( ; itr3 != g_histosOrder.end() ; itr3++){ 233 cout << *itr3; 234 if(itr3+1 != g_histosOrder.end() ) cout << " , "; 235 else cout << endl; 236 // draw 237 if(nDim == 1) { 238 tempHistoPtr = dynamic_cast<TH1 *>(g_myHistosMap[*itr3]); 239 tempHistoPtr->SetLineColor(linecolor++); 240 241 if(!ocOpts.drawNormalized) tempHistoPtr->Draw(drawOpt); 242 else tempHistoPtr = tempHistoPtr->DrawNormalized(drawOpt); 243 244 //tempHistoPtr->Fit("gaus","","same",0.05,1.2); 245 246 //tempHistoPtr->GetXaxis()->SetRangeUser(0,10); 247 // the order has been decided in the first plot 248 // but in a second iteration we may need to 249 // RangeUser 250 if(tempHistoPtr->GetMaximum() > storeMax.second) { 251 252 if(storeMax.first == 0x0) storeMax.first = tempHistoPtr; // store the first pointer only --> contains the right axis 253 storeMax.second = tempHistoPtr->GetMaximum(); 254 255 if(minims < 0) minims = tempHistoPtr->GetMinimum(); 256 257 } 258 259 if(tempHistoPtr->GetMinimum() < minims) 260 minims = tempHistoPtr->GetMinimum(); 261 262 }else if(nDim == 2){ 263 tempHisto2DPtr = dynamic_cast<TH2 *>(g_myHistosMap[*itr3]); 264 tempHisto2DPtr->SetMarkerColor(linecolor++); 265 tempHisto2DPtr->SetMarkerStyle(ocOpts.markerStyle); 266 tempHisto2DPtr->Draw(drawOpt); 267 } 268 269 // the order has been decided in the first plot 270 // but in a second iteration we may need to 271 // RangeUser 272 273 274 drawOpt = "same"; 275 } 276 277 // Set Range user 278 if(nDim == 1) { 279 if(ocOpts.setLogY){ 280 if(ocOpts.drawNormalized && minims == 0) minims = 1E-6; 281 storeMax.first->GetYaxis()->SetRangeUser(minims, storeMax.second*10.); 282 }else{ 283 storeMax.first->GetYaxis()->SetRangeUser(minims, storeMax.second*1.1); 284 } 285 cout << "RangeUser - -> " << storeMax.first << " to " << storeMax.second << endl; 286 } 287 288 //if(ocOpts.setLogY) gPad->SetLogy(); 289 290 // do legend 291 if(ocOpts.doLegend){ 292 TLegend * t1 = new TLegend(0.6, 0.5, 0.9, 0.9); 293 t1->SetBorderSize(1); 294 t1->SetFillColor(kWhite); 295 for(itr3 = g_histosOrder.begin() ; 296 itr3 != g_histosOrder.end() ; 297 itr3++){ 298 if(nDim == 1) t1->AddEntry(dynamic_cast<TH1 *>(g_myHistosMap[*itr3]), *itr3, "L"); 299 if(nDim == 2) t1->AddEntry(dynamic_cast<TH2 *>(g_myHistosMap[*itr3]), *itr3, "M"); 300 } 301 t1->Draw(); 302 } 303 304 // return the first drawn 305 return g_myHistosMap[g_histosOrder[0]]; 282 306 } 283 307 284 308 void GetTree(TTree * T, TString nickName, double scale){ 285 309 286 287 310 g_myTreeMap[nickName] = T; 311 g_myTreeMapScale[nickName] = scale; 288 312 289 313 } 290 314 291 315 void GetTree(TString filename, TString treeName, TString nickName, double scale){ 292 293 TFile * f = new TFile(filename); 294 if(!f->IsOpen()){ 295 cout << "[ERROR] couldn't open file " << filename << endl; 296 exit(1); 297 } 298 299 TTree * T = (TTree *) f->Get(treeName); 300 301 if(!T){ 302 cout << "[ERROR] couldn't find tree " << treeName << endl; 303 exit(1); 304 } 305 306 g_myTreeMap[nickName] = T; 307 g_myTreeMapScale[nickName] = scale; 308 //return T; 316 317 TFile * f = new TFile(filename); 318 if(!f->IsOpen()){ 319 cout << "[ERROR] couldn't open file " << filename << endl; 320 exit(1); 321 } 322 323 TTree * T = (TTree *) f->Get(treeName); 324 325 if(!T){ 326 cout << "[ERROR] couldn't find tree " << treeName << endl; 327 exit(1); 328 } 329 cout << "Pushing back : " << nickName << endl; 330 g_myTreeMap[nickName] = T; 331 g_myTreeMapScale[nickName] = scale; 332 g_myTreeOriginalOrdering[g_OriginalOrdering++] = nickName; 333 //return T; 309 334 } 310 335 … … 317 342 318 343 } 319 */344 */ -
root_tools/multipleplots/plots_treeTools.h
r154 r262 28 28 Bool_t setLogY; 29 29 Bool_t drawNormalized; 30 Bool_t orderHistos; 30 31 ClassDef(OnCanvasOptions, 1) 31 32 }; … … 42 43 void GetTree(TTree *, TString, double); 43 44 TTree * GetTreePtr(TString); 45 map<TString, TObject *> GetHistosMap(); 44 46 TObject * DrawAll(TString, TCut, OnCanvasOptions &, ActionFlags); 45 47 void TreeFillMap(TString, TTree *);
Note: See TracChangeset
for help on using the changeset viewer.