Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:57:55

0001 #define MERun_cxx
0002 #include <iostream>
0003 #include <fstream>
0004 #include <cstdlib>
0005 #include <cassert>
0006 using namespace std;
0007 
0008 #include "MERun.hh"
0009 #include "../../interface/MELaserPrim.h"
0010 #include "../../interface/MEGeom.h"
0011 
0012 ClassImp( MERun )
0013 
0014 MERun::MERun( ME::Header header, ME::Settings settings, TString fname )
0015 : _header( header ), _settings( settings ), _fname(fname), _file(0), pn_t(0), mtq_t(0)
0016 {
0017   _type = _settings.type;
0018   _color = _settings.wavelength;
0019 
0020   //  assert( _color>=0 && _color<ME::iSizeC );  
0021   //  TTree* tree_ = PNTable();
0022   //   int nentries = tree_->GetEntriesFast();
0023   //   for( int jj=0; jj<nentries; jj++ )
0024   //     {
0025   //       //      int jj=(ilm-1)*2+ipn;
0026   //       tree_->LoadTree( jj );
0027   //       tree_->GetEntry( jj );
0028   //       //      assert( var>=0 && var<ME::iSizePN );
0029   //       cout << jj << " " << pn_i["LOGIC_ID"] << endl;
0030   //     }
0031   // 
0032 }
0033 
0034 MERun::~MERun()
0035 {
0036   closeLaserPrimFile();
0037 }
0038 
0039 void
0040 MERun::closeLaserPrimFile()
0041 {
0042   if( _file ) _file->Close();
0043   delete _file;
0044   _file = 0;
0045   _h.clear();
0046 }
0047 
0048 TFile*
0049 MERun::laserPrimFile( bool refresh )
0050 {
0051   if( refresh )
0052     closeLaserPrimFile();
0053 
0054   if( _file==0 )
0055     {
0056       FILE *test;
0057       test= fopen( _fname, "r" );
0058       if(test)
0059     {
0060       _file = TFile::Open( _fname );
0061       fclose( test );
0062     }
0063       assert( _file!=0 );
0064     }
0065   return _file;
0066 }
0067 
0068 TH2*
0069 MERun::APDHist( int var )
0070 {
0071   //  assert( var>=0 && var<ME::iSizeAPD );
0072   // get an histogram in the LmfLaserPrim table
0073   int table(0);
0074   TString varName;
0075   if( _type==ME::iLaser )
0076     {
0077       table = ME::iLmfLaserPrim;
0078       varName = ME::APDPrimVar[var];
0079     }
0080   else if( _type==ME::iTestPulse  )
0081     {
0082       table = ME::iLmfTestPulsePrim;
0083       varName = ME::TPAPDPrimVar[var];
0084     }
0085 
0086   TH2* h(0);
0087 
0088   TFile* f = laserPrimFile();
0089   if( f==0 ) return h;
0090   
0091   TString histName = MELaserPrim::lmfLaserName( table, _type, _settings.wavelength );
0092   histName += MELaserPrim::separator;
0093   histName += varName;
0094   if( _h.count(varName)==0 )
0095     {
0096       h = (TH2*) f->Get( histName );
0097       if( h!=0 ) _h[varName] = h;
0098       if( varName == "LOGIC_ID" )
0099     // if( varName == "MEAN" )
0100     {
0101       TAxis* ax = h->GetXaxis();
0102       TAxis* ay = h->GetYaxis();
0103       
0104       cout << "X axis Nbins=" << ax->GetNbins() 
0105            << " first=" << ax->GetFirst()
0106            << " lowedge(first)=" << ax->GetBinLowEdge(ax->GetFirst())
0107            << " last="  << ax->GetLast()
0108            << " lowedge(last)=" << ax->GetBinLowEdge(ax->GetLast())
0109            << endl;
0110       cout << "Y axis Nbins=" << ay->GetNbins() 
0111            << " first=" << ay->GetFirst()
0112            << " lowedge(first)=" << ay->GetBinLowEdge(ay->GetFirst())
0113            << " last="  << ay->GetLast()
0114            << " lowedge(last)=" << ay->GetBinLowEdge(ay->GetLast())
0115            << endl;
0116     }
0117     }
0118   else
0119     {
0120       h = _h[varName];
0121     }
0122   return h;
0123 }
0124 
0125 TTree*
0126 MERun::PNTable()
0127 {
0128   TFile* f = laserPrimFile();
0129   if( f==0 ) return 0;
0130 
0131   unsigned int size_(0);
0132   int table(0);
0133   if( _type==ME::iLaser )
0134     {
0135       table = ME::iLmfLaserPnPrim;
0136       size_ = ME::iSizePN;
0137     }
0138   else if( _type==ME::iTestPulse )
0139     {
0140       table = ME::iLmfTestPulsePnPrim;
0141       size_ = ME::iSizeTPPN;
0142     }
0143 
0144   TString tableName = MELaserPrim::lmfLaserName( table, _type, _settings.wavelength );
0145   // don't cache the pn_t pointer, it's dangerous
0146   pn_i.clear();
0147   pn_d.clear();
0148   pn_t = (TTree*) f->Get(tableName);
0149   assert( pn_t!=0 );
0150   TString vname;
0151   vname = "LOGIC_ID"; pn_t->SetBranchAddress( vname, &pn_i[vname] );
0152   vname = "FLAG";     pn_t->SetBranchAddress( vname, &pn_i[vname] );
0153   for( unsigned int ii=1; ii<size_; ii++ )
0154     {
0155       vname = ME::PNPrimVar[ii]; pn_t->SetBranchAddress( vname, &pn_d[vname] );
0156     }
0157   return pn_t;
0158 }
0159 
0160 TTree*
0161 MERun::MTQTable()
0162 {
0163 
0164   TFile* f = laserPrimFile();
0165   if( f==0 ) return 0;
0166   int table = ME::iLmfLaserPulse;
0167   TString tableName = MELaserPrim::lmfLaserName( table, _type, _settings.wavelength );
0168   // don't cache the mtq_t pointer, it's dangerous
0169   mtq_i.clear();
0170   mtq_d.clear();
0171   mtq_t = (TTree*) f->Get(tableName);
0172   assert( mtq_t!=0 );
0173   TString vname;
0174   vname = "LOGIC_ID";   mtq_t->SetBranchAddress( vname, &mtq_i[vname] );
0175   vname = "FIT_METHOD"; mtq_t->SetBranchAddress( vname, &mtq_i[vname] );
0176   for( int ii=ME::iMTQ_FIT_METHOD+1; ii<ME::iSizeMTQ; ii++ )
0177     {
0178       vname = ME::MTQPrimVar[ii]; mtq_t->SetBranchAddress( vname, &mtq_d[vname] );
0179     }
0180   return mtq_t;
0181 }
0182 
0183 float
0184 MERun::getVal( int table, int var, int i1, int i2 )
0185 {
0186   if( table==ME::iLmfLaserPrim || table==ME::iLmfTestPulsePrim )
0187     {
0188       int ix = i1;
0189       int iy = i2;
0190       TH2* h_ = APDHist( var );
0191       if( h_==0 ) return 0;
0192       int binx = h_->GetXaxis()->FindBin( ix+0.5 );
0193       int biny = h_->GetYaxis()->FindBin( iy+0.5 );
0194       //int binx = h_->GetXaxis()->FindBin( ix );
0195       //      int biny = h_->GetYaxis()->FindBin( iy );
0196       float val =  (float) h_->GetCellContent( binx, biny );
0197       return val;  
0198     }
0199   else if( table==ME::iLmfLaserPnPrim || table==ME::iLmfTestPulsePnPrim )
0200     {
0201       //      assert( var>=0 && var<ME::iSizePN );
0202       int ilm = i1;
0203       //      assert( ilm>=1 && ilm<=9 );
0204       if( !( ilm>=1 && ilm<=9 ) )
0205     {
0206       //      cout << "wrong module number for barrel! ilm=" << ilm << endl;  
0207       return 0.; // SUPER FIXME !!!
0208     }
0209       int ipn = i2;
0210       assert( ipn==0 || ipn==1 );
0211       // get the PN identifier
0212       pair<int,int> p_ = MEEBGeom::pn(ilm);
0213       int pnid = (ipn==0)? p_.first : p_.second; 
0214       TTree* tree_ = PNTable();
0215       int nentries = tree_->GetEntriesFast();
0216       int jj;
0217       for( jj=0; jj<nentries; jj++ )
0218     {
0219       tree_->LoadTree( jj );
0220       tree_->GetEntry( jj );
0221       int pnid_ = pn_i["LOGIC_ID"]%10000;
0222 //        cout << "ilm/ipn/pnid/jj/pn_i/pnid_/pnid " 
0223 //         << ilm << "/" 
0224 //         << ipn << "/" 
0225 //         << pnid << "/" 
0226 //         << jj << "/" 
0227 //         << pn_i["LOGIC_ID"] << "/" << pnid_ << endl;
0228       if( pnid_==pnid ) break;
0229     }
0230       if( jj==nentries ) return 0;
0231       TString varName;
0232       if( table==ME::iLmfLaserPnPrim )
0233     varName=ME::PNPrimVar[var];
0234       else if( table==ME::iLmfTestPulsePnPrim )
0235     varName=ME::TPPNPrimVar[var];
0236       float val = (float) pn_d[varName];
0237       //      cout << pn_i["LOGIC_ID"] << " " << ilm << " " << ipn 
0238       //       << " " << varName << "=" << val << endl;
0239       return val;
0240     }
0241   else if( table==ME::iLmfLaserPulse )
0242     {
0243       TTree* tree_ = MTQTable();
0244       int jj=_header.side;
0245       assert( jj==0 || jj==1 );
0246       tree_->LoadTree( 0 );
0247       tree_->GetEntry( 0 );
0248       assert( var>=0 && var<ME::iSizeMTQ );
0249       TString varName = ME::MTQPrimVar[var];
0250       float val = (float) mtq_d[varName];
0251 
0252       return val;
0253     }
0254   return 0;
0255 }
0256 
0257 bool
0258 MERun::operator==( const MERun& o ) const
0259 {
0260   bool out;
0261   out = ( _header.run  == o._header.run  )
0262     &&  ( _header.lb   == o._header.lb   )
0263     &&  ( _header.side == o._header.side )
0264     &&  ( _settings.wavelength == o._settings.wavelength );
0265   return out;
0266 }
0267 
0268 void
0269 MERun::print( ostream& o ) const
0270 {
0271   o << "\tRun    \t=\t" << _header.run    << endl;
0272   o << "\tLB     \t=\t" << _header.lb     << endl;
0273   o << "\tDCC    \t=\t" << _header.dcc    << endl;
0274   o << "\tSide   \t=\t" << _header.side   << endl;
0275   o << "\tEvents \t=\t" << _header.events << endl;
0276   //  o << hex;
0277   o << "\tTSBegin\t=\t" << _header.ts_beg << endl;
0278   o << "\tTSEnd  \t=\t" << _header.ts_end << endl;
0279   //  o << "\tTSLow  \t=\t" << time_low()     << endl;  
0280   o << "\tTime   \t=\t" << time()    << endl;  
0281   //  o << dec;
0282   o << "\tDt(sec)\t=\t" << _header.ts_end-_header.ts_beg << endl;
0283   o << "\tColor  \t=\t" << _settings.wavelength << endl;
0284   o << "\tPower  \t=\t" << _settings.power      << endl;
0285   o << "\tFilter \t=\t" << _settings.filter     << endl;
0286   o << "\tDelay  \t=\t" << _settings.delay      << endl;
0287   o << "\tMGPA   \t=\t" << _settings.mgpagain   << endl;
0288   o << "\tMEM    \t=\t" << _settings.memgain    << endl;
0289   o << "---> ROOT file name " << _fname << endl;
0290 }
0291 
0292 //unsigned int
0293 //MERun::time_low() const
0294 //{
0295 //  return ME::time_low( _header.ts_beg );
0296 //}
0297 
0298 // time in seconds
0299 ME::Time
0300 MERun::time() const
0301 {
0302   return ME::time_high( _header.ts_beg );
0303 }