Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Data.cxx

Go to the documentation of this file.
00001 // $Id: Data.cxx,v 1.11 2009/10/09 20:26:51 jyuko Exp $
00002 
00003 // C/C++
00004 #include <cmath>
00005 #include <iostream>
00006 #include <sstream>
00007 
00008 // ROOT
00009 #include "TDirectory.h"
00010 #include "TH1D.h"
00011 #include "TH2F.h"
00012 
00013 // Local
00014 #include "PhysicsNtuple/Default.h"
00015 #include "PhysicsNtuple/Hist/HistMan.h"
00016 
00017 // Local
00018 #include "Data.h"
00019 
00020 using namespace std;
00021 
00022 //-------------------------------------------------------------------------------------
00023 Anp::Plot::Data::Data()
00024    :fPlot1d(true),
00025     fPlot2d(false),
00026     fPlotAll(true),
00027     fCorrSize(0),
00028     fFillFrac(0),
00029     fFillNbin(0),
00030     fFillSize(0),
00031     fDir1d(0),
00032     fDir2d(0),
00033     fDirCorr(0),
00034     fMap1d(),
00035     fMap2d(),
00036     fMapCorr(),
00037     fCorr()
00038 {
00039 }
00040 
00041 //-------------------------------------------------------------------------------------
00042 Anp::Plot::Data::~Data()
00043 {
00044 }
00045 
00046 //-------------------------------------------------------------------------------------
00047 void Anp::Plot::Data::Fill(DataIter ibeg, DataIter iend, const double weight)
00048 {
00049    for(DataIter xit = ibeg; xit != iend; ++xit)
00050    {
00051       const short xkey  = xit -> Key();
00052       const float xdata = xit -> Data();
00053  
00054       if(fDir1d)
00055       {
00056          Fill(xkey, xdata, weight);
00057       }
00058 
00059       if(!fDir2d)
00060       {
00061          continue;
00062       }
00063 
00064       for(DataIter yit = xit + 1; yit != iend; ++yit)
00065       {
00066          const short ykey  = yit -> Key();
00067          const float ydata = yit -> Data();
00068          
00069          TH2 *h2 = Get(fMap2d, fDir2d, xkey, ykey);
00070          if(h2)
00071          {
00072             h2 -> Fill(xdata, ydata, weight);
00073          }
00074       }
00075    }
00076 
00077    if(fDirCorr && fCorrSize > 1)
00078    {      
00079       if(fCorr.Fill(ibeg, iend) >= fCorrSize)
00080       {
00081          const Corr::CorrMap cmap = fCorr.GetCorrCoef();
00082          
00083          for(Corr::CorrMap::const_iterator cit = cmap.begin(); cit != cmap.end(); ++cit)
00084          {
00085             const Corr::Key &key = cit -> first;
00086             const double corr = cit -> second;
00087             
00088             TH1 *hc = Get(fMapCorr, fDirCorr, key.Key1(), key.Key2());
00089             if(hc)
00090             {
00091                hc -> Fill(corr);
00092             }
00093          }
00094 
00095          fCorr.Reset();
00096       }
00097    }
00098 }
00099 
00100 //-------------------------------------------------------------------------------------
00101 void Anp::Plot::Data::Set(const string &key, const unsigned int value)
00102 {
00103    //
00104    // Configure internal parameters
00105    //
00106 
00107    if(key == "Plot1d")
00108    {
00109       fPlot1d = static_cast<bool>(value);
00110    }
00111    else if(key == "Plot2d")
00112    {
00113       fPlot2d = static_cast<bool>(value);
00114    }
00115    else if(key == "PlotAll")
00116    {
00117       fPlotAll = static_cast<bool>(value);
00118    }
00119    else if(key == "CorrSize")
00120    {
00121       fCorrSize = value;
00122    }
00123    else if(key == "FillFrac")
00124    {
00125       fFillFrac = value;
00126    }
00127    else if(key == "FillNbin")
00128    {
00129       fFillNbin = value;
00130    }
00131    else if(key == "FillSize")
00132    {
00133       fFillSize = value;
00134    }
00135    else
00136    {
00137       cerr << "Data::Set - unknown key: " << key << endl;
00138    }
00139 }
00140 
00141 //-------------------------------------------------------------------------------------
00142 void Anp::Plot::Data::Set(TDirectory *dir)
00143 {
00144    //
00145    // Create TDirectory objects for internal use
00146    //
00147 
00148    if(!dir)
00149    {
00150       return;
00151    }
00152 
00153    if(fPlot1d)
00154    {
00155       fDir1d = dir;
00156    }
00157    if(fPlot2d)
00158    {
00159       fDir2d = Anp::GetDir("plot2d", dir);
00160    }      
00161    if(fCorrSize)
00162    {
00163       fDirCorr = Anp::GetDir("corr", dir);
00164    }
00165 }
00166 
00167 //-------------------------------------------------------------------------------------
00168 TH1* Anp::Plot::Data::Fill(const short key, const float data, const double weight)
00169 {
00170    //
00171    // Find (create for a first time), fill and return TH1 histogram
00172    //
00173 
00174    TH1 *h = 0;
00175    
00176    Short1Map::iterator sit = fMap1d.find(key);
00177    if(sit == fMap1d.end())
00178    {
00179       //
00180       // Attempt to read histogram from histman, if it fails then make one here
00181       //
00182       
00183       h = HistMan::Instance().CreateTH1(key, ".");
00184       if(h)
00185       {
00186          Anp::SetDir(h, fDir1d);
00187          sit = fMap1d.insert(Short1Map::value_type(key, h)).first;
00188       }
00189       else if(fPlotAll && fFillSize > 9 && fFillNbin > 1)
00190       {
00191          //
00192          // Create TH1 histogram, store first fFillSize events 
00193          // and use them to determine histogram range.
00194          //      
00195          vector<DataItem<float, double> > &fvec = fFloat[key];
00196 
00197          if(fvec.size() < fFillSize)
00198          {
00199             fvec.push_back(Anp::DataItem<float, double>(data, weight));
00200          }
00201          else if(!fvec.empty())
00202          {
00203             stringstream hname;
00204             hname << "put_" << std::setw(5) << std::setfill('0') << key;
00205             
00206             //
00207             // Sort vector, then exclude lowest and highest 5% of events
00208             //
00209             unsigned int lpos =  (1*fvec.size())/100;
00210             unsigned int hpos = (99*fvec.size())/100;
00211             if(fFillFrac < 100)
00212             {
00213                lpos = (fFillFrac*fvec.size())/100;
00214                hpos = (static_cast<unsigned int>(100 - int(fFillFrac))*fvec.size())/100;
00215             }
00216 
00217             assert(lpos < fvec.size() && hpos < fvec.size());
00218 
00219             std::sort(fvec.begin(), fvec.end());
00220             const double xmin = fvec[lpos].Key();
00221             const double xmax = fvec[hpos].Key();
00222 
00223             //
00224             // Create TH1
00225             //
00226             if(!hname.str().empty())
00227             {
00228                Anp::Lock<Anp::Mutex> lock(Anp::GetROOTMutex());
00229                h = new TH1D(hname.str().c_str(), hname.str().c_str(), fFillNbin, xmin, xmax);
00230             }
00231             Anp::SetDir(h, fDir1d);
00232 
00233             //
00234             // Insert TH1 into the map, now we are done with this key
00235             //
00236             sit = fMap1d.insert(Short1Map::value_type(key, h)).first;
00237             
00238             for(vector<DataItem<float, double> >::iterator fit = fvec.begin(); fit != fvec.end(); ++fit)
00239             {
00240                h -> Fill(fit -> Key(), fit -> Data());
00241             }
00242             
00243             fvec.clear();
00244          }
00245       }
00246       else
00247       {
00248          sit = fMap1d.insert(Short1Map::value_type(key, 0)).first;
00249       }
00250    }
00251    else
00252    {
00253       //
00254       // Histogram might already exists, get TH1 pointer 
00255       //
00256       h = sit -> second;
00257    }
00258 
00259    //
00260    // For valid TH1 pointer fill histogram
00261    //
00262    if(h)
00263    {
00264       h -> Fill(data, weight);
00265    }
00266 
00267    return h;
00268 }
00269 
00270 //-------------------------------------------------------------------------------------
00271 TH2* Anp::Plot::Data::Get(Short2Map &smap, TDirectory *dir, const short xkey, const short ykey)
00272 {
00273    //
00274    // Find (create for a first time) and return TH2 histogram
00275    //
00276    const HistKey key(xkey, ykey);
00277 
00278    Short2Map::iterator sit = smap.find(key);
00279    if(sit == smap.end())
00280    {
00281       TH2 *h = HistMan::Instance().GetTH2(xkey, ykey);
00282       if(h)
00283       {
00284          Anp::SetDir(h, dir);
00285       }
00286       
00287       sit = smap.insert(Short2Map::value_type(key, h)).first;
00288    }      
00289 
00290    return sit -> second;
00291 }
00292 
00293 //-------------------------------------------------------------------------------------
00294 TH1* Anp::Plot::Data::Get(CorrMap &cmap, TDirectory *dir, const short xkey, const short ykey)
00295 {
00296    const HistKey key(xkey, ykey);
00297 
00298    CorrMap::iterator sit = cmap.find(key);
00299    if(sit == cmap.end())
00300    {
00301       stringstream hname, aname;
00302       hname << "key_" << xkey << "_and_key_" << ykey;
00303       aname << xkey << " and " << ykey << " correlation coefficient";
00304 
00305       TH1 *h = 0;
00306       if(!hname.str().empty())
00307       {
00308          Anp::Lock<Anp::Mutex> lock(Anp::GetROOTMutex());
00309          TH1 *h = new TH1D(hname.str().c_str(), hname.str().c_str(), 
00310                            100, -1.00001, 1.00001);      
00311          h -> GetXaxis() -> SetTitle(aname.str().c_str());
00312          h -> GetXaxis() -> CenterTitle();
00313       }
00314       Anp::SetDir(h, dir);
00315       
00316       sit = cmap.insert(CorrMap::value_type(key, h)).first;
00317    }      
00318 
00319    return sit -> second;
00320 }

Generated on Mon Feb 15 11:06:33 2010 for loon by  doxygen 1.3.9.1