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

LIRawNtAna.cxx

Go to the documentation of this file.
00001 
00002 
00003 // Program name: LIRawNtAna.cxx                
00004 //                                                    
00005 // Package: LISummary
00006 //                                                                    
00007 // Coded by Jeff Hartnell Sep/2002-Oct/2003
00008 //                                                                    
00009 // Purpose: To analyse li_tree in LIData*.root files 
00010 //   
00011 // Contact: jeffrey.hartnell@physics.ox.ac.uk                         
00013 //long one
00014 #include <cstdlib>
00015 #include <fstream>
00016 #include <cmath>
00017 
00018 //#include "TStyle.h"
00019 #include "TROOT.h"
00020 #include "TChain.h"
00021 #include "TFile.h"
00022 #include "TTree.h"
00023 //#include "TH1.h"
00024 #include "TH2.h"
00025 //#include "TF1.h"
00026 //#include "TCanvas.h"
00027 //#include "TAxis.h"
00028 //#include "TDatime.h" 
00029 //#include "TGraph.h"
00030 //#include "TGraphAsymmErrors.h"
00031 //#include "TGraphErrors.h"
00032 //#include "TLegend.h"
00033 //#include "TPad.h"
00034 //#include "TPaveText.h"
00035 //#//include "TText.h"
00036 //#include "TError.h"
00037 
00038 //#include "Plex/PlexHandle.h"
00039 #include "MessageService/MsgService.h"
00040 #include "RawData/RawChannelId.h"
00041 
00042 #include "LISummary/LIRawNtAna.h"
00043 //#include "LISummary/LIChannel.h"
00044 //#include "LISummary/LIPlane.h"
00045 //#include "LISummary/LIRun.h"
00046 //#include "LISummary/LITuning.h"
00047 //#include "Calibrator/Calibrator.h"
00048 
00049 ClassImp(LIRawNtAna)
00050 
00051 CVSID("$Id: LIRawNtAna.cxx,v 1.7 2009/03/11 14:56:30 djauty Exp $");
00052 
00053 //......................................................................
00054 
00055 LIRawNtAna::LIRawNtAna()
00056 {
00057   MSG("LIRawNtAna", Msg::kDebug) 
00058     <<"Running LIRawNtAna constructor..."<<endl;
00059 
00060   //data members
00061   nt_run=-1;
00062   nt_snarl=-1;
00063   nt_timesec=-1;
00064   nt_timenanosec=-1;
00065   nt_errcode=-1;
00066   nt_crate=-1;
00067   nt_varc=-1;
00068   nt_vmm=-1;
00069   nt_vaadc=-1;
00070   nt_vachip=-1;
00071   nt_vachannel=-1;
00072   nt_pixel=-1;
00073   nt_adc=-1;
00074   nt_tdc=-1;
00075   nt_readouttype=-1;
00076   nt_pbox=-1;
00077   nt_pboxopp=-1;
00078   for (UInt_t i=0;i<nStripPerPixel;++i){
00079     nt_led[i]=-1;
00080     nt_ledopp[i]=-1;
00081   }
00082 
00083   //data members not in tree
00084   fOutFile=0;
00085   chain=0;
00086   numEvents=-1;
00087   
00088   //set up the chain and perform some checks
00089   this->MakeChain();
00090   this->SetChainBranches();
00091   numEvents=static_cast<Int_t>(chain->GetEntries());
00092   MSG("LIRawNtAna",Msg::kInfo) 
00093     <<"Number of events in chain="<<numEvents<<endl; 
00094 
00095   MSG("LIRawNtAna", Msg::kInfo) 
00096     <<"Finished LIRawNtAna constructor"<<endl;
00097 }
00098 
00099 //......................................................................
00100 
00101 LIRawNtAna::~LIRawNtAna()
00102 {
00103   MSG("LIRawNtAna", Msg::kDebug) 
00104     <<"Running LIRawNtAna destructor..."<<endl;
00105 
00106   if (fOutFile){
00107     //this makes histos disappear from the canvases 
00108     //so do it at the very end
00109     fOutFile->Close();
00110   }
00111 
00112   MSG("LIRawNtAna", Msg::kDebug) 
00113     <<"Finished LIRawNtAna destructor"<<endl;
00114 }
00115 
00116 //......................................................................
00117 
00118 void LIRawNtAna::WriteOutHistos()
00119 {
00120   //write out the histos to the file, if it's open
00121   if (fOutFile){
00122     if (fOutFile->IsWritable()) {
00123       fOutFile->cd();
00124 
00125       MSG("LIRawNtAna",Msg::kInfo)
00126         <<"Writing histos to: "<<fOutFile->GetName()<<" ..."<<endl;
00127       fOutFile->Write();
00128       //fOutFile->Close();//this makes histos disappear from canvases
00129       //so do it in the destructor (need to make LIRawNtAna on heap)
00130     }
00131     else {
00132       MSG("LIRawNtAna",Msg::kWarning)
00133         <<"File not writable!"<<endl;
00134     }
00135   }
00136 }
00137 
00138 //......................................................................
00139 
00140 TFile* LIRawNtAna::OpenFile(Int_t runNumber,std::string prefix) const
00141 {
00142   //call this static function to ensure any histograms created
00143   //after the file is opened are added
00144   TH1::AddDirectory(true);
00145 
00146   //create the tfile pointer to be returned
00147   TFile* outputFile=0;
00148   
00149   //get the environmental variable
00150   char *anaDir=getenv("NUANA_DIR");
00151   
00152   //use a string to hold env instead 
00153   string sAnaDir="";
00154   
00155   if (anaDir!=NULL) {
00156     sAnaDir=anaDir;
00157   }
00158   else {
00159     MSG("LIRawNtAna",Msg::kInfo) 
00160       <<"Environmental variable $NUANA_DIR not set." 
00161       <<" Writing file(s) to current directory"<<endl;
00162     sAnaDir=".";
00163   }
00164   
00165   //convert variables to string
00166   string sRunNumber=Form("%d",runNumber);
00167   string sZeros="";
00168   if (runNumber>=0 && runNumber<10) sZeros="00000000";
00169   else if (runNumber>=10 && runNumber<100) sZeros="000000";
00170   else if (runNumber>=100 && runNumber<1000) sZeros="00000";
00171   else if (runNumber>=1000 && runNumber<10000) sZeros="0000";
00172   else if (runNumber>=10000 && runNumber<100000) sZeros="000";
00173   else if (runNumber>=100000 && runNumber<1000000) sZeros="00";
00174   else if (runNumber>=1000000 && runNumber<10000000) sZeros="0";
00175   else if (runNumber>=10000000 && runNumber<100000000) sZeros="";
00176   sRunNumber=sZeros+sRunNumber;
00177 
00178   //string sDetectorType="C";
00179   string sDetector="";
00180   //string sPrefix="h";//default
00181   string sPrefix="";//default
00182   if (prefix!="") sPrefix+=prefix;
00183   string sBase=sAnaDir+"/"+sPrefix+sDetector+sRunNumber;
00184   string sFileName=sBase+".root";
00185   
00186   //test if file already exists
00187   ifstream Test(sFileName.c_str());
00188   
00189   //open the appropriate file
00190   if(!Test){
00191     outputFile=new TFile(sFileName.c_str(),"RECREATE");
00192   }
00193   else {
00194     //Need new filename
00195     Int_t fred=1;
00196     while(Test) {
00197       Test.close();
00198       string sAppendage=Form("%d",fred);
00199       sFileName=sBase+"_"+sAppendage+".root";
00200       Test.open(sFileName.c_str());
00201       fred++;
00202     }
00203     outputFile=new TFile(sFileName.c_str(),"NEW");
00204     outputFile->SetCompressionLevel(9);
00205   }
00206   
00207   string sTmp="No File!";
00208   if (outputFile) sTmp=outputFile->GetName();
00209 
00210   MSG("LIRawNtAna",Msg::kInfo) 
00211     <<"Output file opened: "<<sTmp<<endl;
00212   return outputFile;
00213 }
00214 
00215 //......................................................................
00216 
00217 void LIRawNtAna::MakeChain()
00218 {
00219   MSG("LIRawNtAna", Msg::kDebug)<<"Running MakeChain method..."<<endl;
00220 
00221   //LI data files to read in
00222   char* envVariable=getenv("LIDATA");
00223   if (envVariable==NULL){
00224     MSG("LIRawNtAna",Msg::kFatal)
00225       <<endl<<endl
00226       <<"*************************************************************"
00227       <<endl<<"Environmental variable LIDATA not set!"<<endl
00228       <<"Please set LIDATA to the directory containing the"
00229       <<" liraw*.root files"<<endl
00230       <<"Note: If more than one file is found they will be"
00231       <<" concatenated and treated as one"<<endl
00232       <<"*************************************************************"
00233       <<endl<<endl<<"Program will exit here"<<endl;
00234     exit(0);
00235   }
00236   string sEnv=envVariable;
00237   MSG("LIRawNtAna",Msg::kInfo)
00238     <<"Looking for liraw*.root files using the env variable"<<endl
00239     <<"LIDATA="<<sEnv<<endl;
00240   
00241   string sFileName=sEnv+"/liraw*.root";
00242 
00243   // create a chain with li_tree
00244   //chain=new TChain("li_tree");  
00245   chain=new TChain("liraw");  
00246   //add the files to the chain
00247   Int_t nf=chain->Add(sFileName.c_str());
00248 
00249   if(nf==0){
00250     MSG("LIRawNtAna",Msg::kFatal)
00251       <<endl<<endl
00252       <<"*************************************************************"
00253       <<endl<<"No liraw*.root files found in "<<sEnv<<endl
00254       <<"Please set LIDATA to the directory containing the"
00255       <<" liraw*.root files"<<endl
00256       <<"Note: If more than one file is found they will be"
00257       <<" concatenated and treated as one"<<endl
00258       <<"*************************************************************"
00259       <<endl<<endl<<"Program will exit here"<<endl;
00260     exit(0);
00261   }
00262     
00263   MSG("LIRawNtAna",Msg::kInfo) 
00264     <<"Printing tree information:"<<endl;
00265   chain->Show(1);
00266   //chain->Print()
00267   
00268   if (nf==1){
00269     MSG("LIRawNtAna",Msg::kInfo) 
00270       <<endl<<"Analysing "<<nf<<" file of the form liraw*.root in"
00271       <<endl<<"LIDATA="<<sEnv<<endl<<endl;
00272   }
00273   else{
00274     MSG("LIRawNtAna",Msg::kInfo)
00275       <<endl<<"Analysing "<<nf
00276       <<" files of the form liraw*.root in directory"
00277       <<endl<<"LIDATA="<<sEnv<<endl<<endl;
00278     MSG("LIRawNtAna",Msg::kInfo)
00279       <<"Reading in files..."<<endl;
00280   }
00281 
00282   MSG("LIRawNtAna", Msg::kDebug)<<"Finished the MakeChain method"<<endl;
00283 }
00284 
00285 //......................................................................
00286 
00287 void LIRawNtAna::SetChainBranches()
00288 {
00289   //chain->SetBranchAddress("ashtray",&ashtray);
00290   chain -> SetBranchAddress("run", &nt_run);
00291   chain -> SetBranchAddress("snarl", &nt_snarl);
00292   chain -> SetBranchAddress("timesec", &nt_timesec);
00293   chain -> SetBranchAddress("timenanosec", &nt_timenanosec);
00294   chain -> SetBranchAddress("errcode", &nt_errcode);
00295   chain -> SetBranchAddress("crate", &nt_crate);
00296   chain -> SetBranchAddress("varc", &nt_varc);
00297   chain -> SetBranchAddress("vmm", &nt_vmm);
00298   chain -> SetBranchAddress("vaadc", &nt_vaadc);
00299   chain -> SetBranchAddress("vachip", &nt_vachip);
00300   chain -> SetBranchAddress("vachannel", &nt_vachannel);
00301   chain -> SetBranchAddress("pixel", &nt_pixel);
00302   chain -> SetBranchAddress("adc", &nt_adc);
00303   chain -> SetBranchAddress("tdc", &nt_tdc);
00304   chain -> SetBranchAddress("readouttype", &nt_readouttype);
00305   chain -> SetBranchAddress("pbox", &nt_pbox);
00306   chain -> SetBranchAddress("pboxopp", &nt_pboxopp);
00307   chain -> SetBranchAddress("led", nt_led);
00308   chain -> SetBranchAddress("ledopp", nt_ledopp);
00309 
00310 }
00311 
00312 //......................................................................
00313 
00314 void LIRawNtAna::InitialiseLoopVariables()
00315 {
00316   MSG("LIRawNtAna",Msg::kInfo)<<"Initialising loop variables..."<<endl;
00317   /*
00318   liEvent=-1;//want first event to be 0 not 1
00319   lastLed=-1;
00320   lastPulserBox=-1;
00321   lastCalibPoint=-1;
00322   liRunNum=0;
00323   run=0;
00324 
00325   //get first event
00326   chain->GetEvent(0);  
00327   previousRunNumber=runNumber;
00328   */
00329   MSG("LIRawNtAna",Msg::kInfo)<<"Initialisation complete"<<endl;
00330 }
00331 
00332 //......................................................................
00333 
00334 void LIRawNtAna::SetLoopVariables(Int_t entry)
00335 {
00336 
00337   //Float_t fract=ceil(numEvents/10.);
00338   Float_t fract=ceil(numEvents/10000.);
00339   
00340   if (ceil(((Float_t)entry)/fract)==((Float_t)entry)/fract){
00341     MSG("LIRawNtAna",Msg::kInfo) 
00342       <<"Fraction of loop complete: "<<entry 
00343       <<"/"<<numEvents<<"  ("
00344       <<(Int_t)(100.*entry/numEvents)<<"%)"<<endl;
00345   }
00346 
00347   chain->GetEvent(entry);
00348 }  
00349 
00350 //......................................................................
00351 
00352 void LIRawNtAna::Demo()
00353 {
00354   MSG("LIAnalysis",Msg::kInfo)
00355     <<endl<<" ** Running the Demo method... ** "<<endl;
00356   
00357   //open the file
00358   fOutFile=this->OpenFile(100,"AdcHistos");
00359 
00360   //declare variables
00361   Detector::Detector_t det=Detector::kFar;
00362   ElecType::Elec_t etype=ElecType::kVA;
00363   map<UInt_t,TH1F*> mHistos;
00364   
00365   Bool_t createHistos=true;
00366 
00367   //create histos
00368   if (createHistos) {
00369     for(Int_t crate=0;crate<16;++crate){
00370       for(Int_t varc=0;varc<3;++varc){
00371         for(Int_t vmm=0;vmm<5;++vmm){
00372           for(Int_t vaadc=0;vaadc<2;++vaadc){
00373             for(Int_t vachip=0;vachip<3;++vachip){
00374               for(Int_t vachannel=2;vachannel<18;++vachannel){
00375               
00376                 RawChannelId rcid(det,etype,
00377                                   crate,varc,vmm,vaadc,vachip,vachannel);
00378               
00379                 Int_t index=crate*10000+rcid.GetChAdd();
00380 
00381         /*      MAXMSG("LIRawNtAna",Msg::kInfo,100) 
00382                   <<"c-v-v-v-v-c="<<crate
00383                   <<"-"<<varc
00384                   <<"-"<<vmm
00385                   <<"-"<<vaadc
00386                   <<"-"<<vachip
00387                   <<"-"<<vachannel
00388                   <<", rcid="<<rcid.GetChAdd()
00389                   <<", index="<<index
00390                   <<endl;
00391           */    
00392                 //      string s="hAdc";
00393                 string sChAdd=Form("crate%i_varc%i_vmm%i_vaadc%i_vachip%i_vachannel%i",crate,varc,vmm,vaadc,vachip,vachannel);
00394         //      string sChAdd=Form("%d",rcid.GetChAdd());
00395         //      s=s+sCrate+"_"+sChAdd;
00396          string s=sChAdd;
00397                 mHistos[index]=new TH1F(s.c_str(),s.c_str(),
00398                                         1000,0,1000);
00399 
00400               }
00401             }
00402           }
00403         }
00404       }
00405     }
00406   }
00407   
00411   
00412   this->InitialiseLoopVariables();  
00413   
00414   for(Int_t entry=0;entry<numEvents;entry++){
00415   //for(Int_t entry=0;entry<600000;entry++){
00416   //for(Int_t entry=690000;entry<numEvents;entry++){
00417     this->SetLoopVariables(entry);
00418     
00419     //MAXMSG("LIAnalysis",Msg::kInfo,100)
00420     //<<"snarl="<<nt_snarl<<endl;
00421     
00422     RawChannelId rcid(det,etype,
00423                       nt_crate,nt_varc,nt_vmm,nt_vaadc,
00424                       nt_vachip,nt_vachannel);
00425     
00426     Int_t index=nt_crate*10000+rcid.GetChAdd();
00427 
00428 /*    MAXMSG("LIRawNtAna",Msg::kInfo,100) 
00429       <<"c-v-v-v-v-c="<<nt_crate
00430       <<"-"<<nt_varc
00431       <<"-"<<nt_vmm
00432       <<"-"<<nt_vaadc
00433       <<"-"<<nt_vachip
00434       <<"-"<<nt_vachannel
00435       <<", rcid="<<rcid.GetChAdd()
00436       <<", index="<<index
00437       <<endl;
00438 */
00439     map<UInt_t,TH1F*>::iterator histoItr=mHistos.find(index);
00440     if (histoItr!=mHistos.end()) {//found it
00441       (histoItr->second)->Fill(nt_adc);
00442     }
00443     
00444   }//end of for
00445   
00449   
00450   MSG("LIAnalysis",Msg::kInfo)<<"Finished main loop"<<endl;
00451   
00452   MSG("LIAnalysis",Msg::kInfo)
00453     <<" ** Finished the Demo method ** "<<endl;
00454 }
00455 
00456 //......................................................................
00457 
00458 void LIRawNtAna::DemoOpenFile(std::string fileName)
00459 {
00460   //TFile* file=TFile::Open(fileName.c_str());
00461   TFile::Open(fileName.c_str());
00462   
00463   //declare variables
00464   Detector::Detector_t det=Detector::kFar;
00465   ElecType::Elec_t etype=ElecType::kVA;
00466 
00467   //create histos
00468   for(Int_t crate=0;crate<16;++crate){
00469     for(Int_t varc=0;varc<3;++varc){
00470       for(Int_t vmm=0;vmm<6;++vmm){
00471         for(Int_t vaadc=0;vaadc<2;++vaadc){
00472           for(Int_t vachip=0;vachip<3;++vachip){
00473             for(Int_t vachannel=2;vachannel<18;++vachannel){
00474               
00475               RawChannelId rcid(det,etype,
00476                                 crate,varc,vmm,vaadc,vachip,vachannel);
00477               
00478               Int_t index=crate*10000+rcid.GetChAdd();
00479               
00480               MAXMSG("LIRawNtAna",Msg::kInfo,100) 
00481                 <<"c-v-v-v-v-c="<<crate
00482                 <<"-"<<varc
00483                 <<"-"<<vmm
00484                 <<"-"<<vaadc
00485                 <<"-"<<vachip
00486                 <<"-"<<vachannel
00487                 <<", rcid="<<rcid.GetChAdd()
00488                 <<", index="<<index
00489                 <<endl;
00490               
00491               string s="hAdc";
00492               string sCrate=Form("%d",crate);
00493               string sChAdd=Form("%d",rcid.GetChAdd());
00494               s=s+sCrate+"_"+sChAdd;
00495 
00496               TH1F* h=(TH1F*)gROOT->FindObject(s.c_str());
00497               
00498               if (h) {
00499                 MAXMSG("LIRawNtAna",Msg::kInfo,1000) 
00500                   <<"mean="<<h->GetMean()<<endl;
00501               }
00502               else cout<<"No histo with name="<<s<<endl;
00503             }
00504           }
00505         }
00506       }
00507     }
00508   }
00509 
00510 
00511 }
00512 
00513 //......................................................................

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