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

NuGeneral.cxx

Go to the documentation of this file.
00001 
00002 
00003 // Coded by Jeff Hartnell Jul/2005       
00004 //
00005 // Contact: j.j.hartnell@rl.ac.uk
00007 
00008 #include <cmath>
00009 #include <cassert>
00010 #include <map>
00011 #include <sys/stat.h>
00012 
00013 #include "TDirectory.h"
00014 #include "TGraph.h"
00015 #include "TF1.h"
00016 #include "TH1F.h"
00017 #include "TMath.h"
00018 #include "TProfile2D.h"
00019 #include "TROOT.h"
00020 
00021 #include "MessageService/MsgService.h"
00022 
00023 #include "NtupleUtils/NuGeneral.h"
00024 
00025 using std::endl;
00026 using std::cout;
00027 using std::map;
00028 using std::vector;
00029 using std::string;
00030 
00031 CVSID("$Id: NuGeneral.cxx,v 1.8 2010/02/01 11:39:48 evansj Exp $");
00032 
00033 //......................................................................
00034 
00035 NuGeneral::NuGeneral()
00036 {
00037   MSG("NuGeneral",Msg::kDebug)
00038     <<"Running NuGeneral Constructor..."<<endl;
00039 
00040 
00041   MSG("NuGeneral",Msg::kDebug)
00042     <<"Finished NuGeneral Constructor"<<endl;
00043 }
00044 
00045 //......................................................................
00046 
00047 NuGeneral::~NuGeneral()
00048 {
00049   MSG("NuGeneral",Msg::kDebug)
00050     <<"Running NuGeneral Destructor..."<<endl;
00051   
00052 
00053   MSG("NuGeneral",Msg::kDebug)
00054     <<"Finished NuGeneral Destructor"<<endl;
00055 }
00056 
00057 //......................................................................
00058 
00059 std::string NuGeneral::GetReleaseDirToUse(std::string sPackageDir) const
00060 {
00069 
00070   const string sPub=this->GetBaseReleaseDir();
00071   const string sPriv=this->GetTestReleaseDir();
00072 
00073   //set default return value to sPriv
00074   string sAnaDir=sPriv;
00075   
00076   //try private context first
00077   string sTmp=sPriv+"/"+sPackageDir;
00078   struct stat ss;
00079   MAXMSG("NuGeneral",Msg::kDebug,10)<<"sTmp="<<sTmp<<endl;
00080   if(stat(sTmp.c_str(),&ss)==-1) {
00081     MAXMSG("NuGeneral",Msg::kInfo,10)
00082       <<"Package="<<sPackageDir
00083       <<" not present in SRT_PRIVATE_CONTEXT="<<sPriv
00084       <<", trying SRT_PUBLIC_CONTEXT="<<sPub<<"..."<<endl;
00085     
00086     //now test public context
00087     sTmp=sPub+"/"+sPackageDir;
00088     MAXMSG("NuGeneral",Msg::kDebug,10)<<"sTmp2="<<sTmp<<endl;
00089     if(stat(sTmp.c_str(),&ss)==-1) {
00090       MAXMSG("NuGeneral",Msg::kWarning,10)
00091         <<"Package="<<sPackageDir
00092         <<" not present in SRT_PUBLIC_CONTEXT="<<sPub
00093         <<" or SRT_PRIVATE_CONTEXT="<<sPriv
00094         <<", will abort here..."<<endl;
00095       assert(false);
00096     }
00097     //public context exists so use that
00098     else {
00099       MAXMSG("NuGeneral",Msg::kInfo,10)
00100         <<"Using SRT_PUBLIC_CONTEXT="<<sPub
00101         <<" for package="<<sPackageDir<<endl;
00102       sAnaDir=sPub;
00103     }
00104   }
00105   else {
00106     MAXMSG("NuGeneral",Msg::kInfo,10)
00107       <<"Using SRT_PRIVATE_CONTEXT="<<sPriv
00108       <<" for package="<<sPackageDir<<endl;
00109   }
00110   
00111   return sAnaDir;
00112 }
00113 
00114 //......................................................................
00115 
00116 std::string NuGeneral::GetTestReleaseDir() const
00117 {
00118   //get the environmental variable
00119   char* anaDir=getenv("SRT_PRIVATE_CONTEXT");
00120   
00121   //use a string to hold env instead 
00122   string sAnaDir="";
00123   
00124   if (anaDir!=NULL) {
00125     sAnaDir=anaDir;
00126   }
00127   else {
00128     MSG("NuGeneral",Msg::kError) 
00129       <<"Environmental variable $SRT_PRIVATE_CONTEXT not set"<<endl
00130       <<"Will abort here..."<<endl;
00131     assert(false);
00132   }
00133 
00134   return sAnaDir;
00135 }
00136 
00137 //......................................................................
00138 
00139 std::string NuGeneral::GetBaseReleaseDir() const
00140 {
00141   //get the environmental variable
00142   char* anaDir=getenv("SRT_PUBLIC_CONTEXT");
00143   
00144   //use a string to hold env instead 
00145   string sAnaDir="";
00146   
00147   if (anaDir!=NULL) {
00148     sAnaDir=anaDir;
00149   }
00150   else {
00151     MSG("NuGeneral",Msg::kError) 
00152       <<"Environmental variable $SRT_PUBLIC_CONTEXT not set"<<endl
00153       <<"Will abort here..."<<endl;
00154     assert(false);
00155   }
00156   
00157   return sAnaDir;
00158 }
00159 
00160 //......................................................................
00161 
00162 void NuGeneral::CopyAcrossObjects(TDirectory* dirInput,
00163                                   TDirectory* dirOutput,
00164                                   const std::vector<std::string>& 
00165                                   vObjectNames) const
00166 {
00167   cout<<"NuGeneral: copying across objects"<<endl;
00168 
00169   TDirectory* directoryOrig=gDirectory;
00170   //cout<<"Original directory is:"<<endl;
00171   //directoryOrig->Print();
00172   
00173   //change to the directory where the files are kept
00174   gDirectory=dirInput;
00175   //cout<<"Changed to input directory:"<<endl;
00176   //gDirectory->Print();
00177   
00178   //vector to store the pointers to the histograms to copy across
00179   vector<TObject*> vObjects;
00180   
00181   //get the pointers
00182   for (vector<string>::const_iterator iter=vObjectNames.begin();
00183        iter!=vObjectNames.end();++iter){
00184     TObject* h=gROOT->FindObject(iter->c_str());
00185     vObjects.push_back(h);
00186     if (!h) {
00187       MAXMSG("NuGeneral",Msg::kInfo,10)
00188         <<"CopyAcrossObjects: Can't find object="<<*iter<<endl;
00189     }
00190   }
00191   
00192   //change to the directory where the objects are to be written
00193   gDirectory=dirOutput;
00194   //cout<<"Changed to output directory:"<<endl;
00195   //gDirectory->Print();
00196 
00197   //loop over the objects and write them out
00198   for (vector<TObject*>::const_iterator iter=vObjects.begin();
00199        iter!=vObjects.end();++iter){
00200     if (*iter) {
00201       (*iter)->Write();
00202     }
00203   }
00204 
00205   //reset the global directory to the original directory
00206   gDirectory=directoryOrig;
00207 }
00208 
00209 //......................................................................
00210 
00211 void NuGeneral::SetGraphAxisEtc(TGraph* g,Int_t startTimeSecs,
00212                                 Int_t endTimeSecs) const 
00213 {
00214   MSG("NuGeneral",Msg::kVerbose)
00215     <<endl<<" ** Running the SetGraphAxisEtc method... ** "<<endl;
00216 
00217   TDatime startDatime;
00218   startDatime.Set(startTimeSecs+788918400);
00219   TDatime endDatime;
00220   endDatime.Set(endTimeSecs+788918400);
00221 
00222   //play around with the formatting a little
00223   string sStartHour=Form("%d",startDatime.GetHour());
00224   string sStartMinute=Form("%d",startDatime.GetMinute());
00225   string sStartSecond=Form("%d",startDatime.GetSecond());
00226   string sStartYear=Form("%d",startDatime.GetYear());
00227   string sStartMonth=Form("%d",startDatime.GetMonth());
00228   string sStartDay=Form("%d",startDatime.GetDay());
00229   if (startDatime.GetHour()<10) sStartHour="0"+sStartHour;
00230   if (startDatime.GetMinute()<10) sStartMinute="0"+sStartMinute;
00231   if (startDatime.GetSecond()<10) sStartSecond="0"+sStartSecond;
00232   if (startDatime.GetMonth()<10) sStartMonth="0"+sStartMonth;
00233   if (startDatime.GetDay()<10) sStartDay="0"+sStartDay;
00234 
00235   //play around with the formatting a little
00236   string sEndHour=Form("%d",endDatime.GetHour());
00237   string sEndMinute=Form("%d",endDatime.GetMinute());
00238   string sEndSecond=Form("%d",endDatime.GetSecond());
00239   string sEndYear=Form("%d",endDatime.GetYear());
00240   string sEndMonth=Form("%d",endDatime.GetMonth());
00241   string sEndDay=Form("%d",endDatime.GetDay());
00242   if (endDatime.GetHour()<10) sEndHour="0"+sEndHour;
00243   if (endDatime.GetMinute()<10) sEndMinute="0"+sEndMinute;
00244   if (endDatime.GetSecond()<10) sEndSecond="0"+sEndSecond;
00245   if (endDatime.GetMonth()<10) sEndMonth="0"+sEndMonth;
00246   if (endDatime.GetDay()<10) sEndDay="0"+sEndDay;
00247 
00248   static Bool_t firstTime=true;
00249   if (firstTime){
00250     MSG("NuGeneral",Msg::kInfo) 
00251       <<"Setting graph axis info:"<<endl;
00252     MSG("NuGeneral",Msg::kInfo) 
00253       <<"  Start time "<<sStartHour
00254       <<":"<<sStartMinute
00255       <<":"<<sStartSecond
00256       <<" on "<<startDatime.GetYear()
00257       <<"/"<<sStartMonth
00258       <<"/"<<sStartDay
00259       <<endl;
00260     
00261     MSG("NuGeneral",Msg::kInfo) 
00262       <<"  End time "<<sEndHour
00263       <<":"<<sEndMinute
00264       <<":"<<sEndSecond
00265       <<" on "<<endDatime.GetYear()
00266       <<"/"<<sEndMonth
00267       <<"/"<<sEndDay
00268       <<endl;
00269   }
00270   firstTime=false;
00271 
00272   Int_t timeRange=endTimeSecs-startTimeSecs;
00273 
00274   //check graph pointer is not null
00275   if (g){
00276     if (sStartDay==sEndDay && timeRange<1*24*60*60){
00277       string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay+")";
00278       g->GetXaxis()->SetTitle(title.c_str());
00279       g->GetXaxis()->SetTimeFormat("%H:%M");
00280     }
00281     else if (timeRange>1*24*60*60 && timeRange<2*24*60*60){//1-2 days
00282       string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay
00283         +" - "+sEndYear+"/"+sEndMonth+"/"+sEndDay+")";
00284       g->GetXaxis()->SetTitle(title.c_str());
00285       g->GetXaxis()->SetTimeFormat("%H:%M");
00286     }
00287     else if (timeRange>2*24*60*60 && timeRange<5*24*60*60){//2-5 days
00288       string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay
00289         +" - "+sEndYear+"/"+sEndMonth+"/"+sEndDay+")";
00290       g->GetXaxis()->SetTitle(title.c_str());
00291       g->GetXaxis()->SetTimeFormat("%H:%M-%d/%m");
00292     }
00293     else{
00294       string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay
00295         +" - "+sEndYear+"/"+sEndMonth+"/"+sEndDay+")";
00296       g->GetXaxis()->SetTitle(title.c_str());
00297       g->GetXaxis()->SetTimeFormat("%H:%M-%d/%m");
00298       //stop it cramming loads of days on the axis!
00299       g->GetXaxis()->SetNdivisions(506);
00300       //n = N1 + 100*N2 + 10000*N3
00301       //N1=number of primary divisions.
00302       //N2=number of secondary divisions.
00303       //N3=number of 3rd divisions.
00304     }
00305     
00306     //other x-axis defaults
00307     g->GetXaxis()->SetTimeDisplay(1);
00308     g->GetXaxis()->CenterTitle();
00309     
00310     //other defaults
00311     g->SetMarkerStyle(3);
00312     g->SetMarkerColor(2);
00313     g->SetMarkerSize(0.35);
00314     g->SetLineColor(46);
00315   }
00316   else{
00317     MSG("NuGeneral",Msg::kError)
00318       <<"Input graph pointer is null!"
00319       <<" Will do nothing."<<endl;
00320   }
00321 
00322   MSG("NuGeneral",Msg::kVerbose)
00323     <<endl<<" ** Finished the SetGraphAxisEtc method... **"<<endl;
00324 }
00325 
00326 //......................................................................
00327 
00328 void NuGeneral::SetGraphAxis(TAxis* a,Int_t startTimeSecs,
00329                              Int_t endTimeSecs) const 
00330 {
00331   if (!a){
00332     MSG("NuGeneral",Msg::kWarning)
00333       <<"Input axis pointer is null! Will do nothing"<<endl;
00334     return;
00335   }
00336 
00337   //get datimes
00338   TDatime startDatime;
00339   startDatime.Set(startTimeSecs+788918400);
00340   TDatime endDatime;
00341   endDatime.Set(endTimeSecs+788918400);
00342 
00343   //play around with the formatting a little
00344   string sStartHour=Form("%d",startDatime.GetHour());
00345   string sStartMinute=Form("%d",startDatime.GetMinute());
00346   string sStartSecond=Form("%d",startDatime.GetSecond());
00347   string sStartYear=Form("%d",startDatime.GetYear());
00348   string sStartMonth=Form("%d",startDatime.GetMonth());
00349   string sStartDay=Form("%d",startDatime.GetDay());
00350   if (startDatime.GetHour()<10) sStartHour="0"+sStartHour;
00351   if (startDatime.GetMinute()<10) sStartMinute="0"+sStartMinute;
00352   if (startDatime.GetSecond()<10) sStartSecond="0"+sStartSecond;
00353   if (startDatime.GetMonth()<10) sStartMonth="0"+sStartMonth;
00354   if (startDatime.GetDay()<10) sStartDay="0"+sStartDay;
00355 
00356   //play around with the formatting a little
00357   string sEndHour=Form("%d",endDatime.GetHour());
00358   string sEndMinute=Form("%d",endDatime.GetMinute());
00359   string sEndSecond=Form("%d",endDatime.GetSecond());
00360   string sEndYear=Form("%d",endDatime.GetYear());
00361   string sEndMonth=Form("%d",endDatime.GetMonth());
00362   string sEndDay=Form("%d",endDatime.GetDay());
00363   if (endDatime.GetHour()<10) sEndHour="0"+sEndHour;
00364   if (endDatime.GetMinute()<10) sEndMinute="0"+sEndMinute;
00365   if (endDatime.GetSecond()<10) sEndSecond="0"+sEndSecond;
00366   if (endDatime.GetMonth()<10) sEndMonth="0"+sEndMonth;
00367   if (endDatime.GetDay()<10) sEndDay="0"+sEndDay;
00368 
00369   static Bool_t firstTime=true;
00370   if (firstTime){
00371     MSG("NuGeneral",Msg::kInfo) 
00372       <<"Setting axis info:"<<endl;
00373     MSG("NuGeneral",Msg::kInfo) 
00374       <<"  Start time "<<sStartHour
00375       <<":"<<sStartMinute
00376       <<":"<<sStartSecond
00377       <<" on "<<startDatime.GetYear()
00378       <<"/"<<sStartMonth
00379       <<"/"<<sStartDay
00380       <<endl;
00381 
00382     MSG("NuGeneral",Msg::kInfo) 
00383       <<"  End time   "<<sEndHour
00384       <<":"<<sEndMinute
00385       <<":"<<sEndSecond
00386       <<" on "<<endDatime.GetYear()
00387       <<"/"<<sEndMonth
00388       <<"/"<<sEndDay
00389       <<endl;
00390   }
00391 
00392   Int_t timeRange=endTimeSecs-startTimeSecs;
00393    
00394   if (sStartDay==sEndDay && timeRange<1*24*60*60){
00395     string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay+")";
00396     a->SetTitle(title.c_str());
00397     a->SetTimeFormat("%H:%M");
00398   }
00399   else if (timeRange>=1*24*60*60 && timeRange<2*24*60*60){//1-2 days
00400     string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay
00401       +" - "+sEndYear+"/"+sEndMonth+"/"+sEndDay+")";
00402     a->SetTitle(title.c_str());
00403     a->SetTimeFormat("%H:%M");
00404   }
00405   else if (timeRange>=2*24*60*60 && timeRange<5*24*60*60){//2-5 days
00406     string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay
00407       +" - "+sEndYear+"/"+sEndMonth+"/"+sEndDay+")";
00408     a->SetTitle(title.c_str());
00409     a->SetTimeFormat("%H:%M-%d/%m");
00410   }
00411   else if (timeRange>=5*24*60*60 && timeRange<20*24*60*60){//5-20 days
00412     string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay
00413       +" - "+sEndYear+"/"+sEndMonth+"/"+sEndDay+")";
00414     a->SetTitle(title.c_str());
00415     a->SetTimeFormat("%d/%m");
00416   }
00417   else if (timeRange>=20*24*60*60 && timeRange<90*24*60*60){//20-90 days
00418     string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay
00419       +" - "+sEndYear+"/"+sEndMonth+"/"+sEndDay+")";
00420     a->SetTitle(title.c_str());
00421     a->SetTimeFormat("%Y/%m/%d");
00422   }
00423   else if (timeRange>=90*24*60*60 && 
00424            timeRange<2*365*24*60*60){//50-2*365 days
00425     string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay
00426       +" - "+sEndYear+"/"+sEndMonth+"/"+sEndDay+")";
00427     a->SetTitle(title.c_str());
00428     a->SetTimeFormat("%Y/%m");//Y for year with century
00429   }
00430   else{
00431     string title="Time ("+sStartYear+"/"+sStartMonth+"/"+sStartDay
00432       +" - "+sEndYear+"/"+sEndMonth+"/"+sEndDay+")";
00433     a->SetTitle(title.c_str());
00434     a->SetTimeFormat("%H:%M-%d/%m");
00435     //stop it cramming loads of days on the axis!
00436     a->SetNdivisions(506);
00437     //n = N1 + 100*N2 + 10000*N3
00438     //N1=number of primary divisions.
00439     //N2=number of secondary divisions.
00440     //N3=number of 3rd divisions.
00441   }
00442    
00443   //TDatime da(2003,02,28,12,00,00);
00444   //          gStyle->SetTimeOffset(da.Convert());
00445 
00446   if (firstTime) MSG("NuGeneral",Msg::kInfo) 
00447     <<"Set axis title to be '"<<a->GetTitle()<<"'"<<endl;
00448 
00449   //other x-axis defaults
00450   a->SetTimeDisplay(1);
00451   firstTime=false;
00452 }
00453 
00454 //......................................................................
00455 
00456 void NuGeneral::TH1FFill(TH1F* h,const std::vector<Double_t>& vX) const
00457 {
00458   if (vX.empty()) {
00459     MSG("NuGeneral",Msg::kWarning) 
00460       <<"One or more vectors is empty"<<endl;
00461     return;
00462   }
00463 
00464   if (!h){
00465     MSG("NuGeneral",Msg::kWarning) 
00466       <<"pointer==0"<<endl;
00467     return;
00468   }
00469 
00470   UInt_t vSize=vX.size();
00471   for (UInt_t i=0;i<vSize;i++){
00472     h->Fill(vX[i]);
00473   }
00474 }
00475 
00476 //......................................................................
00477 
00478 void NuGeneral::TH1FFill(TH1F* h,const std::vector<Double_t>& vX,
00479                        const std::vector<Double_t>& vY) const
00480 {
00482 
00483   if (vX.empty() || vY.empty()) {
00484     MSG("NuGeneral",Msg::kWarning) 
00485       <<"One or more vectors is empty"<<endl;
00486     return;
00487   }
00488 
00489   if (!h){
00490     MSG("NuGeneral",Msg::kWarning) 
00491       <<"pointer==0"<<endl;
00492     return;
00493   }
00494 
00495   UInt_t vSize=vX.size();
00496   for (UInt_t i=0;i<vSize;i++){
00497     h->Fill(vX[i],vY[i]);
00498   }
00499 }
00500 
00501 //......................................................................
00502 
00503 void NuGeneral::TProfileFill(TProfile* p,
00504                              std::vector<Double_t>& vX,
00505                              std::vector<Double_t>& vY) const
00506 {
00507   MSG("NuGeneral",Msg::kDebug) 
00508     <<" ** Running FillTProfile method... **"<<endl;
00509 
00510   if (vX.empty()) {
00511     MSG("NuGeneral",Msg::kWarning) 
00512       <<"FillTProfile: One or more vectors is empty"<<endl;
00513     return;
00514   }
00515 
00516   if (!p){
00517     MSG("NuGeneral",Msg::kWarning) 
00518       <<"FillTProfile: TProfile pointer==0"<<endl;
00519     return;
00520   }
00521 
00522   if (vX.size()!=vY.size()) {
00523     MSG("NuGeneral",Msg::kWarning) 
00524       <<"FillTProfile: vectors different sizes; returning..."<<endl;
00525     return;
00526   }
00527 
00528   UInt_t vSize=vX.size();
00529   for (UInt_t i=0;i<vSize;i++){
00530     p->Fill(vX[i],vY[i]);
00531   }
00532 
00533   MSG("NuGeneral",Msg::kDebug) 
00534     <<" ** Finished FillTProfile method **"<<endl;
00535 }
00536 
00537 //......................................................................
00538 
00539 void NuGeneral::TProfile2DFill(TProfile2D* p,
00540                                std::vector<Double_t>& vX,
00541                                std::vector<Double_t>& vY,
00542                                std::vector<Double_t>& vZ) const
00543 {
00544   if (vX.empty()) {
00545     MSG("NuGeneral",Msg::kWarning) 
00546       <<"FillTProfile2D: One or more vectors is empty"<<endl;
00547     return;
00548   }
00549 
00550   if (!p){
00551     MSG("NuGeneral",Msg::kWarning) 
00552       <<"FillTProfile2D: TProfile2D pointer==0"<<endl;
00553     return;
00554   }
00555 
00556   if (vX.size()!=vY.size() || vX.size()!=vZ.size()) {
00557     MSG("NuGeneral",Msg::kWarning) 
00558       <<"FillTProfile2D: vectors different sizes; returning..."<<endl;
00559     return;
00560   }
00561 
00562   UInt_t vSize=vX.size();
00563   for (UInt_t i=0;i<vSize;i++){
00564     p->Fill(vX[i],vY[i],vZ[i]);
00565   }
00566 }
00567 
00568 //......................................................................
00569 
00570 void NuGeneral::EpochTo1995(std::vector<Double_t>& t) const
00571 {
00572   if (t.empty()) {
00573     MSG("NuGeneral",Msg::kWarning) 
00574       <<"EpochTo1995: One or more vectors is empty"<<endl;
00575     return;
00576   }
00577 
00578   UInt_t vSize=t.size();
00579   for (UInt_t i=0;i<vSize;i++){
00580     t[i]-=788918400;// seconds between 1970 and 1995
00581   }
00582 }
00583 
00584 //......................................................................
00585 
00586 void NuGeneral::EpochTo1995(Int_t& t) const
00587 {
00588   t-=788918400;// seconds between 1970 and 1995
00589 }
00590 
00591 //......................................................................
00592 
00593 TGraph* NuGeneral::TGraphVect(std::vector<Double_t>& vX,
00594                              std::vector<Double_t>& vY) const
00595 {
00596   MSG("NuGeneral",Msg::kDebug) 
00597     <<" ** Running TGraphVect method... **"<<endl;
00598 
00599   if (vX.empty()) {
00600     MSG("NuGeneral",Msg::kWarning) 
00601       <<"TGraphVect: One or more vectors is empty"<<endl;
00602     return 0;
00603   }
00604 
00605   if (vX.size()!=vY.size()) {
00606     MSG("NuGeneral",Msg::kWarning) 
00607       <<"TGraphVect: vectors different sizes; returning..."<<endl;
00608     return 0;
00609   }
00610 
00611   TGraph* g=new TGraph(vX.size());
00612 
00613   UInt_t vSize=vX.size();
00614   for (UInt_t i=0;i<vSize;i++){
00615     g->SetPoint(i,vX[i],vY[i]);
00616   }
00617 
00618   MSG("NuGeneral",Msg::kDebug) 
00619     <<" ** Finished TGraphVect method **"<<endl;
00620   return g;
00621 }
00622 
00623 //......................................................................
00624 
00625 void NuGeneral::TGraphOffset(std::vector<TGraph*>& v,
00626                              Double_t factor) const
00627 {
00628   Double_t max=-1e200;
00629   Double_t min=+1e200;
00630 
00631   //find the min and max
00632   for (vector<TGraph*>::iterator vIt=v.begin();vIt!=v.end();++vIt){
00633     for (Int_t i=0;i<(*vIt)->GetN();i++){
00634       Double_t x=0;
00635       Double_t y=0;
00636       (*vIt)->GetPoint(i,x,y);
00637       MSG("NuGeneral",Msg::kVerbose)
00638         <<"i="<<i<<", x="<<x<<", y="<<y<<endl;
00639 
00640       //find the min and max
00641       if (y>max) max=y;
00642       if (y<min) min=y;
00643     }
00644   }
00645 
00646   //change the scale
00647   for (vector<TGraph*>::iterator vIt=v.begin();vIt!=v.end();++vIt){
00648     for (Int_t i=0;i<(*vIt)->GetN();i++){
00649       Double_t x=0;
00650       Double_t y=0;
00651       (*vIt)->GetPoint(i,x,y);
00652       MSG("NuGeneral",Msg::kVerbose)
00653         <<"i="<<i<<", x="<<x<<", y="<<y<<endl;
00654 
00655       //scale the y value
00656       y-=min;
00657       y*=factor;//scale by factor, e.g. convert to nanosec
00658       (*vIt)->SetPoint(i,x,y);
00659     }
00660   }
00661 
00662   MSG("NuGeneral",Msg::kDebug)
00663     <<"Found min="<<min<<", max="<<max<<endl;
00664 
00665   //convert min and max
00666   max=(max-min)*factor;  
00667   min=(min-min)*factor;
00668   MSG("NuGeneral",Msg::kDebug)
00669     <<"Converted range to min="<<min<<", max="<<max<<endl;
00670   
00671   //now set the min and max for 10% above and below size of range
00672   for (vector<TGraph*>::iterator vIt=v.begin();vIt!=v.end();++vIt){
00673     (*vIt)->SetMinimum(min-0.1*(max-min));
00674     (*vIt)->SetMaximum(max+0.1*(max-min));
00675   }
00676 }
00677 
00678 //......................................................................
00679 
00680 void NuGeneral::TGraphMinMax(vector<TGraph*>& v) const
00681 {
00682   Float_t max=-1e200;
00683   Float_t min=+1e200;
00684 
00685   for (vector<TGraph*>::iterator vIt=v.begin();vIt!=v.end();++vIt){
00686     for (Int_t i=0;i<(*vIt)->GetN();i++){
00687       Double_t x=0;
00688       Double_t y=0;
00689       (*vIt)->GetPoint(i,x,y);
00690       MSG("NuGeneral",Msg::kVerbose)
00691         <<"i="<<i<<", x="<<x<<", y="<<y<<endl;
00692   
00693       //find the min and max
00694       if (y>max) max=y;
00695       if (y<min) min=y;
00696     }
00697   }
00698 
00699   MSG("NuGeneral",Msg::kDebug)
00700     <<"Found min="<<min<<", max="<<max<<endl;
00701 
00702   //now set the min and max for 10% above and below size of range
00703   for (vector<TGraph*>::iterator vIt=v.begin();vIt!=v.end();++vIt){
00704     (*vIt)->SetMinimum(min-0.1*(max-min));
00705     (*vIt)->SetMaximum(max+0.1*(max-min));
00706   }
00707 }
00708 
00709 //......................................................................
00710 
00711 TGraph* NuGeneral::TGraphMap(const std::map<Int_t,Float_t>& m) const
00712 {
00713   TGraph* g=new TGraph(m.size());
00714 
00715   map<Int_t,Float_t>::const_iterator mIter=m.begin();
00716 
00717   Int_t i=0;
00718 
00719   while (mIter!=m.end()){
00720     g->SetPoint(i,mIter->first,mIter->second);
00721 
00722     i++;
00723     mIter++;
00724   }
00725   return g;
00726 }
00727 
00728 //......................................................................
00729 
00730 void NuGeneral::NormaliseVector(std::vector<Double_t>& v,Int_t mode) const
00731 {
00732   if (v.size()>0){
00733 
00734     if (mode==1){//simple average
00735       //find average
00736       Double_t average=0;
00737       for (UInt_t i=0;i<v.size();i++) average+=v[i];
00738       average/=v.size();
00739       
00740       MSG("NuGeneral",Msg::kVerbose)<<"Average="<<average<<endl; 
00741       
00742       //normalise
00743       for (UInt_t i=0;i<v.size();i++) v[i]/=average;
00744     }
00745     else if (mode==2){//fit the function to remove effect of outliers
00746       
00747       Double_t vMax=-9e50;
00748       Double_t vMin=9e50;
00749 
00750       //work out mins and maxs
00751       for (UInt_t i=0;i<v.size();i++){
00752         if (v[i]>vMax) vMax=v[i];
00753         if (v[i]<vMin) vMin=v[i];
00754       }
00755       
00756       //limit the number of bins
00757       Int_t numBins=300;
00758       if (4*v.size()<10000) numBins=4*v.size();
00759 
00760       MSG("NuGeneral",Msg::kInfo)
00761         <<"vMax="<<vMax <<", vMin="<<vMin<<", numBins="<<numBins<<endl;
00762 
00763       //create the histo
00764       TH1F* h=new TH1F("h","h",numBins,vMin-fabs(0.1*vMin),
00765                        vMax+fabs(0.1*vMin));
00766       h->SetBit(TH1::kCanRebin);
00767 
00768       Double_t average=0;
00769       for (UInt_t i=0;i<v.size();i++){
00770         //find average
00771         average+=v[i];
00772 
00773         //fill the histo
00774         h->Fill(v[i]);
00775       }
00776       average/=v.size();
00777 
00778       //fit but use 0 (zero!) otherwise it stamps on the current canvas!
00779       h->Fit("gaus","q0");
00780       TF1* func=h->GetFunction("gaus");
00781       Double_t p1=func->GetParameter(1);
00782       Double_t p2=func->GetParameter(2);
00783 
00784       //don't use the fit if it is screwed up
00785       Double_t avToNormWith=p1;
00786       if (p1<vMin || p1>vMax) avToNormWith=average;
00787 
00788       MSG("NuGeneral",Msg::kInfo)
00789         <<"Fitted mean="<<p1
00790         <<" (average="<<average
00791         <<") fitted rms="<<p2
00792         <<", using avToNormWith="<<avToNormWith<<endl;
00793 
00794       //normalise
00795       for (UInt_t i=0;i<v.size();i++) v[i]/=avToNormWith;
00796 
00797       delete h;
00798     }
00799   }
00800 }
00801 
00802 //......................................................................
00803 
00804 void NuGeneral::ScaleVector(std::vector<Double_t>& v,
00805                             Double_t scaleFactor) const
00806 {
00807   for (UInt_t i=0;i<v.size();i++) v[i]*=scaleFactor;
00808 }
00809 
00810 //......................................................................
00811 
00812 void NuGeneral::FlipVector(std::vector<Double_t>& v) const
00813 {
00814   vector<Double_t> tempV=v;
00815   for (UInt_t i=0;i<v.size();i++) v[i]=tempV[v.size()-1-i];
00816 }
00817 
00818 //......................................................................
00819 
00820 Int_t NuGeneral::Rnd(Double_t x) const
00821 {
00822   Double_t xi=0;
00823 
00824   Double_t floatPart=modf(x,&xi); 
00825 
00826   //round up if necessary
00827   if (floatPart>0.5) xi++;
00828 
00829   return static_cast<Int_t>(xi);
00830 }
00831 
00832 //......................................................................
00833 
00834 void NuGeneral::ScaleMap(std::map<Int_t,Float_t>& m,
00835                          const std::map<Int_t,Float_t>& scaleFactor) const
00836 {
00837   if (m.size()==scaleFactor.size()){
00838     
00839     map<Int_t,Float_t>::iterator mIter=m.begin();
00840     map<Int_t,Float_t>::const_iterator scale=scaleFactor.begin();
00841 
00842     //loop over map and scale it
00843     while (mIter!=m.end()){
00844 
00845       //divide by the scale factor
00846       if (scale->second!=0){
00847         MSG("NuGeneral",Msg::kVerbose)
00848           <<"mIter="<<mIter->second<<", scale="<<scale->second<<endl;
00849         mIter->second/=scale->second;
00850       }
00851       else MSG("NuGeneral",Msg::kWarning)<<"Scale factor zero!"<<endl;
00852 
00853       mIter++;
00854       scale++;
00855     }
00856   }
00857   else{
00858     MSG("NuGeneral",Msg::kWarning)
00859       <<"Not scaling map, vectors are different sizes!"<<endl;
00860   }
00861 }
00862 
00863 //......................................................................
00864 
00865 // TODO CJB - this helper function is also in NuUtilities
00866 Double_t NuGeneral::OscWeight(Double_t DeltaM2, Double_t Sin22Theta,
00867                               Double_t Enu) const
00868 {
00869   //calculate oscillation probability
00870   const Double_t Distance = 735;
00871   Double_t weight=1;
00872   Double_t sinm=0;
00873   
00874   sinm=sin((1.27*DeltaM2*Distance)/Enu);
00875   weight=1.- Sin22Theta * sinm * sinm;
00876 
00877   return weight;
00878 }
00879 
00880 //......................................................................
00881 
00882 std::string NuGeneral::IdHEPAsString(Int_t IdHEP) const
00883 {
00884   string sIdHEP="";
00885   
00886   Bool_t shortName=true;
00887   if (shortName) {
00888     if (IdHEP==22) sIdHEP="photon";
00889     
00890     else if (IdHEP==11) sIdHEP="e-";
00891     else if (IdHEP==-11) sIdHEP="e+";
00892     else if (IdHEP==13) sIdHEP="mu-";
00893     else if (IdHEP==-13) sIdHEP="mu+";
00894     else if (IdHEP==15) sIdHEP="tau-";
00895     else if (IdHEP==-15) sIdHEP="tau+";
00896 
00897     else if (IdHEP==12) sIdHEP="nue";
00898     else if (IdHEP==-12) sIdHEP="nuebar";
00899     else if (IdHEP==14) sIdHEP="numu";
00900     else if (IdHEP==-14) sIdHEP="numubar";
00901     else if (IdHEP==16) sIdHEP="nutaubar";
00902     else if (IdHEP==-16) sIdHEP="nutaubar";
00903 
00904     else if (IdHEP==211) sIdHEP="pi+";
00905     else if (IdHEP==-211) sIdHEP="pi-";
00906     else if (IdHEP==221) sIdHEP="eta+";
00907     else if (IdHEP==-221) sIdHEP="eta-";
00908     else if (IdHEP==111) sIdHEP="pi0";
00909     
00910     else if (IdHEP==213) sIdHEP="rho+";
00911     else if (IdHEP==-213) sIdHEP="rho-";
00912     else if (IdHEP==223) sIdHEP="omega+";
00913     else if (IdHEP==-223) sIdHEP="omega-";
00914     else if (IdHEP==113) sIdHEP="rho0";
00915     
00916     else if (IdHEP==321) sIdHEP="k+";
00917     else if (IdHEP==-321) sIdHEP="k-";
00918     else if (IdHEP==310) sIdHEP="k-short";
00919     else if (IdHEP==130) sIdHEP="k-long";
00920     else if (IdHEP==311) sIdHEP="k0";
00921 
00922     else if (IdHEP==411) sIdHEP="D+";
00923     else if (IdHEP==421) sIdHEP="D0";
00924     else if (IdHEP==431) sIdHEP="D??";
00925     else if (IdHEP==4122) sIdHEP="Lambda_c+";
00926 
00927     else if (IdHEP==2212) sIdHEP="p+";
00928     else if (IdHEP==-2212) sIdHEP="p-";
00929     else if (IdHEP==2112) sIdHEP="n";
00930 
00931     else if (IdHEP==1114) sIdHEP="delta-";
00932     else if (IdHEP==-2114) sIdHEP="delta0";
00933     else if (IdHEP==2214) sIdHEP="delta+";
00934     else if (IdHEP==2224) sIdHEP="delta++";
00935     
00936     else if (IdHEP==28) sIdHEP="geantino";
00937   }
00938   else {
00939 
00940   }
00941   
00942   if (sIdHEP==""){
00943     string sForm=Form("%d",IdHEP);
00944     sIdHEP="IdHEP="+sForm+"=?";
00945   }
00946 
00947   return sIdHEP;
00948 }
00949 
00950 //......................................................................
00951 
00952 
00953 
00954 
00955 

Generated on Mon Feb 15 11:07:13 2010 for loon by  doxygen 1.3.9.1