Changeset 262 in Idarraga


Ignore:
Timestamp:
Nov 30, 2011, 11:53:01 AM (12 years ago)
Author:
idarraga
Message:
 
Location:
root_tools/multipleplots
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • root_tools/multipleplots/LinkDef_plots_treeTools.h

    r154 r262  
    1717#pragma link C++ function GetHistoKey;
    1818#pragma link C++ function GetHistoPtr;
     19#pragma link C++ function GetHistosMap;
    1920#endif
  • root_tools/multipleplots/plots_treeTools.cpp

    r154 r262  
    99#include <TCut.h>
    1010
     11#include <stdlib.h>
     12#include <map>
     13
     14using namespace std;
     15
    1116// globals
    1217map<TString, TTree *> g_myTreeMap;
    1318map<TString, double> g_myTreeMapScale;
     19map<int, TString> g_myTreeOriginalOrdering;
     20int g_OriginalOrdering = 0;
    1421
    1522map<TString, TObject *> g_myHistosMap;
     
    1825vector<TString> g_histosOrder;
    1926
     27map<TString, TObject *> GetHistosMap(){return g_myHistosMap;}
     28
    2029int GetNHistos(TString idString){
    21   map<TString, TObject *>::iterator itr = g_myHistosMap.begin();
    22   int cntr = 0;
    23   TString tempS;
    24   for( ; itr != g_myHistosMap.end() ; itr++){
    25     //cout << (*itr).second->GetName() << endl;
    26     tempS = (*itr).second->GetName();
    27     if(tempS.Contains(idString, TString::kExact))
    28       cntr++;
    29   }
    30 
    31   return cntr;
     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;
    3241}
    3342
    3443TString GetHistoKey(int i, TString idString){
    35   map<TString, TObject *>::iterator itr = g_myHistosMap.begin();
    36   int cntr = 0;
    37   TString tempS;
    38   for( ; itr != g_myHistosMap.end() ; itr++){
    39     tempS = (*itr).second->GetName();
    40     if(cntr++ == i && tempS.Contains(idString, TString::kExact)) return (*itr).first;
    41   }
    42   return TString("");
     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("");
    4352}
    4453
    4554TObject * GetHistoPtr(int i, TString idString){
    46   map<TString, TObject *>::iterator itr = g_myHistosMap.begin();
    47   int cntr = 0;
    48   TString tempS;
    49   for( ; itr != g_myHistosMap.end() ; itr++){
    50     tempS = (*itr).second->GetName();
    51     if(cntr++ == i && tempS.Contains(idString, TString::kExact)) return (*itr).second;
    52   }
    53   return 0x0;
     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;
    5463}
    5564
    5665
    5766void TreeFillMap(TString n, TTree * p){
    58   g_myTreeMap[n] = p;
     67        g_myTreeMap[n] = p;
    5968}
    6069
    6170TTree * GetTreePtr(TString n){
    62   return  g_myTreeMap[n];
     71        return  g_myTreeMap[n];
    6372}
    6473
    6574int NOfColons(const char * sc){
    6675
    67   std::string s = sc;
    68   size_t pos = std::string::npos;
    69   int dims = 1;
    70 
    71   do{
    72 
    73     pos = s.find(':');
    74     if(pos == std::string::npos) continue;
    75 
    76     s = s.substr(pos+1);
    77     dims++;
    78 
    79   } while(pos != std::string::npos);
    80 
    81   return dims;
     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;
    8291}
    8392
    8493TObject * 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]];
    282306}
    283307
    284308void GetTree(TTree * T, TString nickName, double scale){
    285309
    286   g_myTreeMap[nickName] = T;
    287   g_myTreeMapScale[nickName] = scale;
     310        g_myTreeMap[nickName] = T;
     311        g_myTreeMapScale[nickName] = scale;
    288312
    289313}
    290314
    291315void 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;
    309334}
    310335
     
    317342
    318343}
    319 */
     344 */
  • root_tools/multipleplots/plots_treeTools.h

    r154 r262  
    2828  Bool_t setLogY;
    2929  Bool_t drawNormalized;
     30  Bool_t orderHistos;
    3031  ClassDef(OnCanvasOptions, 1)
    3132};
     
    4243void GetTree(TTree *, TString, double);
    4344TTree * GetTreePtr(TString);
     45map<TString, TObject *> GetHistosMap();
    4446TObject * DrawAll(TString, TCut, OnCanvasOptions &, ActionFlags);
    4547void TreeFillMap(TString, TTree *);
Note: See TracChangeset for help on using the changeset viewer.