Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:46:57

0001 #define MERunManager_cxx
0002 
0003 #include <errno.h>
0004 #include <dirent.h>
0005 #include <iostream>
0006 #include <fstream>
0007 #include <cstdlib>
0008 #include <cassert>
0009 #include <string>
0010 using namespace std;
0011 #include "MERunManager.hh"
0012 #include "../../interface/MEGeom.h"
0013 #include "../../interface/MELaserPrim.h"
0014 #include "MERun.hh"
0015 #include "../../interface/MEChannel.h"
0016 #include "MEVarVector.hh"
0017 
0018 #include <TFile.h>
0019 #include <TString.h>
0020 #include <TObjString.h>
0021 
0022 ClassImp( MERunManager )
0023  
0024 MERunManager::MERunManager( unsigned int lmr, 
0025                 unsigned int type, 
0026                 unsigned int color )
0027 : _lmr(lmr), _type(type), _color(color), _tree(0)
0028 {  
0029   ME::regionAndSector( _lmr, _reg, _sm, _dcc, _side );  
0030   _lmdataPath = ME::lmdataPath( _lmr );
0031   _primPath   = ME::primPath( _lmr );
0032 
0033   updateRunList();
0034 }
0035 
0036 MERunManager::~MERunManager()
0037 {
0038   // the run manager owns the runs
0039   // here delete all the runs...
0040   while( !_runs.empty() ) 
0041   {    
0042     MERun*   aRun = _runs.begin()->second;
0043     //    cout << ".... Deleting run "  << endl;
0044     //    aRun->print( cout );
0045     delete aRun;
0046     _runs.erase( _runs.begin() );  
0047   }
0048 }
0049 
0050 void
0051 MERunManager::updateRunList()
0052 {
0053   TString runlistfile = ME::runListName( _lmr, _type, _color );
0054 
0055   std::ifstream fin;
0056   fin.open(runlistfile);
0057 
0058   MERun* aRun(0);
0059   ME::Time key(0);
0060   bool store(false);
0061   do
0062     {
0063       if( store )  
0064     {
0065       _runs[key] = aRun;
0066     }
0067       string rundir;
0068       long long tsb, tse;
0069       int rr, lb, mpga , mem, pp, ff, dd, evts;      
0070       fin >> rundir;
0071       fin >> rr >> lb >> evts >> tsb >> tse >> mpga >> mem >> pp >> ff >> dd;      
0072       if( rr<MusEcal::firstRun || rr>MusEcal::lastRun ) continue;
0073       
0074       key = ME::time_high(tsb);
0075 
0076       //      cout << "DEBUG MERunManager::updateRunList after key: "<< tsb<< " "<< key << endl;
0077 
0078       ME::Header header;
0079       header.rundir = rundir;
0080       header.dcc    = _dcc;
0081       header.side   = _side;
0082       header.run=rr;
0083       header.lb=lb;
0084       header.ts_beg=tsb;
0085       header.ts_end=tse;
0086       header.events=evts;
0087       
0088       ME::Settings settings;    
0089       settings.type = _type;
0090       settings.wavelength = _color;
0091       settings.mgpagain=mpga;
0092       settings.memgain=mem;
0093       settings.power=pp;
0094       settings.filter=ff;
0095       settings.delay=dd;
0096       
0097       aRun=0;
0098       store = ( _runs.count( key )==0 );
0099       //      cout << "DEBUG MERunManager::updateRunList after store: "<< store << endl;
0100       if( store ) 
0101     {
0102       TString fname = ME::rootFileName( header, settings );
0103       FILE *test;
0104       test = fopen( fname, "r" );
0105       if(test)
0106         {
0107           cout << "File " << fname << " found." << endl;
0108           fclose( test );
0109         }
0110       else
0111         {
0112           //           cout << "File " << fname << " not found." << endl;
0113           //           cout << "Warning: file " << fname << " does not exist" << endl;
0114           store = false;
0115           TString path_ = _lmdataPath;
0116           switch( _type )
0117         {
0118         case ME::iLaser:
0119           path_ += "Laser/";     break;
0120         case ME::iTestPulse:
0121           path_ += "TestPulse/"; break;
0122           //        case ME::iPedestal:
0123           //          path_ += "Pedestal/";  break;
0124         default:
0125           abort();
0126         };
0127           path_ += "Analyzed/"; // FIXME!...
0128           path_ += header.rundir;
0129           path_ += "/";
0130           //          cout << path_ << endl;
0131           //          cout << _primPath << endl;
0132           MELaserPrim prim( header, settings, path_, fname );
0133           if( prim.init_ok )
0134         {
0135           cout << "Primitives for DCC=" << header.dcc << " Side=" << header.side; 
0136           cout << " Type=" << settings.type;
0137           if( settings.type==ME::iLaser ) cout << " Color=" << settings.wavelength;
0138           cout << " Run=" << header.rundir  << " TS=" << header.ts_beg << endl;
0139                   cout << "Fill histograms " << endl;
0140           prim.fillHistograms();
0141                   cout << "Write histograms " << endl;
0142           prim.writeHistograms();
0143           // 
0144           // OK try again
0145           test = fopen( fname, "r" );
0146           if(test)
0147             {
0148               store = true;
0149               fclose( test );
0150             }
0151         }
0152         }     
0153       //      cout << "DEBUG MERunManager::updateRunList after store2: "<< store << endl;
0154       if( store ) aRun = new MERun( header, settings, fname );
0155     }
0156     }
0157   while( fin.peek() != EOF );
0158   fin.close();
0159   
0160   bool runlists_are_not_complete = false;
0161   if( runlists_are_not_complete )
0162     {
0163 
0164       vector< TString > files = vector< TString >();
0165       DIR *dp;
0166       struct dirent *dirp;
0167       if( ( dp  = opendir(_primPath.Data()) ) == 0 ) 
0168     {
0169       cout << "Error(" << errno << ") opening " << _primPath << endl;
0170       return;
0171     }
0172       else
0173     {
0174            cout << "Opening " << _primPath << endl;
0175     }
0176   
0177       while( ( dirp = readdir(dp) ) != 0 ) 
0178     {
0179       files.push_back( TString(dirp->d_name) );
0180             cout << "Taking " << TString(dirp->d_name) << endl;
0181     }
0182       closedir(dp);
0183 
0184       int count(0);
0185       int count_notfound(0);
0186       for( unsigned ii=0; ii<files.size(); ii++ ) 
0187     {      
0188       TString fname_ = files[ii];
0189       if( !fname_.Contains(".root") ) continue;
0190       if( _type==ME::iLaser )
0191         {
0192           if( !fname_.Contains("Laser") ) continue;
0193           if( _color==ME::iBlue && !fname_.Contains("Blue") ) continue; 
0194         }
0195       else if( _type==ME::iTestPulse && !fname_.Contains("TestPulse") ) continue; 
0196 
0197       TString file_ = fname_;
0198       TObjArray* array_ = file_.Tokenize(".");
0199       TObjString* token_ = (TObjString*)array_->operator[](0);
0200       TString str_ = token_->GetString();
0201       //      cout << str_ << endl;
0202       array_ = str_.Tokenize("_");
0203       int nTokens_= array_->GetEntries();
0204       if( nTokens_==0 ) continue;
0205       //       for( int iToken=0; iToken<nTokens_; iToken++ )
0206       //    {
0207       //      token_ = (TObjString*)array_->operator[](iToken);
0208       //      cout << iToken << "->" << token_->GetString() << endl;
0209       //    }       
0210       TString lmrName_ = ((TObjString*)array_->operator[](1))->GetString();
0211       if( lmrName_!=ME::smName( _lmr ) ) continue;
0212             cout << "Sector=" << lmrName_ << endl;
0213       
0214       int side_ = ((TObjString*)array_->operator[](2))->GetString().Atoi();
0215       if( side_!=_side ) continue;
0216             cout << "Side=" << side_ << endl;
0217       
0218       TString rundir_ = ((TObjString*)array_->operator[](4))->GetString();
0219       rundir_+="_";
0220       rundir_+= ((TObjString*)array_->operator[](5))->GetString();
0221             cout << "Rundir=" << rundir_ << endl;
0222       
0223       int run_= TString(rundir_(3,5)).Atoi();
0224             cout << "run=" << run_ << endl;
0225 
0226       int lb_= TString(rundir_(11,4)).Atoi();
0227             cout << "LB=" << lb_ << endl;
0228 
0229       long long ts_ = TString( ((TObjString*)array_->operator[](6))->GetString().operator()(2,32)).Atoll();
0230             cout << "TS=" << ts_ << endl;
0231 
0232       ME::Time key = ME::time_high(ts_);
0233 
0234       count++;
0235       
0236       if( _runs.count(key)!=0 ) continue;
0237 
0238       count_notfound++;
0239 
0240        cout << lmrName_ << " Adding run not found in the runlist file " << fname_ << endl;
0241 
0242       ME::Header header;
0243       header.rundir = rundir_;
0244       header.dcc    = _dcc;
0245       header.side   = _side;
0246       header.run=run_;
0247       header.lb=lb_;
0248       header.ts_beg=ts_;
0249       header.ts_end=ts_;
0250       header.events=600;
0251       
0252       ME::Settings settings;    
0253       settings.type = _type;
0254       settings.wavelength = _color;
0255       settings.mgpagain=0;
0256       settings.memgain=0;
0257       settings.power=0;
0258       settings.filter=0;
0259       settings.delay=0;
0260 
0261       TString fname = ME::rootFileName( header, settings );     
0262       MERun* aRun = new MERun( header, settings, fname );      
0263       _runs[key] = aRun;
0264     }
0265   //       << " (not found in runlist=" << count_notfound << ")" << endl;
0266     }
0267 
0268   cout << "LMR=" << _lmr << "---> Number of primitive files: " << _runs.size() << endl; 
0269   MusEcal::RunIterator p;
0270   _first = 0;
0271   for( p=_runs.begin(); p!=_runs.end(); ++p )
0272     {
0273       ME::Time key = p->first;
0274       if( _first==0 ) _first = key;
0275       _last = key;
0276     }
0277   _current   = _first;
0278   _normFirst = _first;
0279   _normLast  = _last;
0280 }
0281 
0282 MusEcal::RunIterator
0283 MERunManager::it( ME::Time key )
0284 {
0285   return _runs.find( key );
0286 }
0287 
0288 MusEcal::RunIterator
0289 MERunManager::it()
0290 {
0291   return _runs.begin();
0292 }
0293 
0294 MusEcal::RunIterator 
0295 MERunManager::cur()
0296 {
0297   return _runs.find( _current );
0298 }
0299 
0300 MusEcal::RunIterator 
0301 MERunManager::first()
0302 {
0303   return _runs.equal_range( _first ).first;
0304 }
0305 
0306 MusEcal::RunIterator 
0307 MERunManager::last()
0308 {
0309   return _runs.equal_range( _last ).second;
0310 }
0311 
0312 MusEcal::RunIterator 
0313 MERunManager::from( ME::Time key )
0314 {
0315   return _runs.equal_range( key ).first;
0316 }
0317 
0318 MusEcal::RunIterator 
0319 MERunManager::to( ME::Time key )
0320 {
0321   return _runs.equal_range( key ).second;
0322 }
0323 
0324 MusEcal::RunIterator 
0325 MERunManager::begin()
0326 {
0327   return _runs.begin();
0328 }
0329 
0330 MusEcal::RunIterator 
0331 MERunManager::end()
0332 {
0333   return _runs.end();
0334 }
0335 
0336 ME::Time
0337 MERunManager::beginKey() const
0338 {
0339   return _runs.begin()->first;
0340 }
0341 
0342 ME::Time
0343 MERunManager::endKey() const
0344 {
0345   return _runs.rbegin()->first;
0346 }
0347 
0348 MERun* 
0349 MERunManager::beginRun() 
0350 {
0351   return _runs.begin()->second;
0352 }
0353 
0354 MERun* 
0355 MERunManager::endRun() 
0356 {
0357   return _runs.rbegin()->second;
0358 }
0359 
0360 MERun*   
0361 MERunManager::curRun() 
0362 {
0363   return run( _current );
0364 }
0365 
0366 MERun* 
0367 MERunManager::firstRun()
0368 {
0369   return run( _first );
0370 }
0371 
0372 MERun* 
0373 MERunManager::lastRun() 
0374 {
0375   return run( _last );
0376 }
0377 
0378 MERun* 
0379 MERunManager::run( ME::Time key )
0380 {
0381   if( key==0 ) return 0;
0382   MusEcal::RunIterator p = _runs.find( key );
0383   if( p!=_runs.end() ) return p->second;
0384   return 0;
0385 }
0386 
0387 void 
0388 MERunManager::print()
0389 {
0390   
0391   cout << "Number of runs to analyze : " << _runs.size() << endl;
0392   if( _runs.size()>0 )
0393     {
0394       //      cout << "First: " << _first << endl;
0395       //      firstRun()-> print( cout );
0396       cout << "Current: " << _current << endl;
0397       curRun()  -> print( cout );
0398       //      cout << "Last: " << _last << endl;
0399       //      lastRun() -> print( cout );
0400 //       for( int ixb=0; ixb<=84; ixb++ )
0401 //  {
0402 //    for( int iyb=0; iyb<=19; iyb++ )
0403 //      {
0404 //        //          int ix= (int) ax->GetBinLowEdge( ixb );
0405 //        //          int iy= (int) ay->GetBinLowEdge( iyb );
0406 //        cout << "ix=" << ixb << "\tiy=" << iyb;
0407 //        cout << "\tID     " 
0408 //         << "\t" << 
0409 //      (int) curRun()->getVal( ME::iLmfLaserPrim,"LOGIC_ID",ixb, iyb )
0410 //         << "\tAPD     " 
0411 //         << "\t" << 
0412 //      curRun()->getVal( ME::iLmfLaserPrim,"MEAN",ixb, iyb )
0413 //         << "\t" << 
0414 //      curRun()->getVal( ME::iLmfLaserPrim,"RMS",ixb, iyb )
0415 //         << "\t" << 
0416 //      curRun()->getVal( ME::iLmfLaserPrim,"M3",ixb, iyb )
0417 //         << endl;
0418 //      }
0419 //  }
0420     } 
0421 }
0422 
0423 bool
0424 MERunManager::setCurrentRun( ME::Time key )
0425 {
0426   // first close the file of the current run
0427   MERun* aRun = curRun();
0428   if( aRun ) aRun->closeLaserPrimFile();
0429 
0430   key = closestKeyInFuture( key );
0431   _current = key;
0432   MusEcal::RunIterator p = _runs.find( key );
0433   if( p==_runs.end() ) return false;
0434   aRun = p->second;
0435   assert( aRun!=0 );
0436   return true;
0437 }
0438 
0439 void
0440 MERunManager::setNoCurrent()
0441 {
0442   _current=0;
0443 }
0444 
0445 ME::Time
0446 MERunManager::closestKey( ME::Time key )
0447 {
0448   if( _runs.size()==0 ) return 0;
0449   MusEcal::RunIterator p = _runs.find( key );
0450   if( p==_runs.end() )
0451     {
0452       cout << "**** Key is not found: find the closest key " << endl;
0453       p = _runs.lower_bound( key );
0454       if( p==_runs.end() )
0455     {
0456       cout << " ----> Closest is last entry " << endl;
0457       p--;
0458     }
0459       else if( p==_runs.begin() )
0460     {
0461       cout << " ----> Closest is first entry " << endl;
0462     }
0463       else
0464     {
0465       MusEcal::RunIterator p0 = p;
0466       p0--;
0467       cout << " ----> Key " << key << " is between " << p0->first << " and " << p->first << endl;
0468       if( key-(p0->first) < (p->first)-key ) p--; 
0469     }
0470       key = p->first;
0471       cout << "**** Closest key is " << key << endl;
0472     }
0473   return key;
0474 }
0475 
0476 ME::Time
0477 MERunManager::closestKeyInFuture( ME::Time key )
0478 {
0479   if( _runs.size()==0 ) return 0;
0480   MusEcal::RunIterator p = _runs.find( key );
0481   if( p==_runs.end() )
0482     {
0483       p = _runs.lower_bound( key );
0484       if( p==_runs.end() )
0485     {
0486       p--;
0487     }
0488       key = p->first;
0489     }
0490   return key;
0491 }
0492 
0493 bool
0494 MERunManager::setPlotRange( ME::Time key1, ME::Time key2, bool verbose )
0495 {
0496   if( key2<=key1 ) 
0497     {
0498       key2  = lastKey(); 
0499       key1  = firstKey(); 
0500     }
0501   if( key1>key2 ) return false;
0502   MusEcal::RunIterator p1 = _runs.find( key1 );
0503   MusEcal::RunIterator p2 = _runs.find( key2 );
0504   if( p1==_runs.end() || p2==_runs.end() ) return false;
0505   _first = key1;
0506   _last  = key2;
0507   if( _current<_first ) setCurrentRun( _first );
0508   if( _current>_last )  setCurrentRun( _last );
0509   _normFirst = _first;
0510   _normLast  = _last;
0511   return true;
0512 }
0513 
0514 bool
0515 MERunManager::setNormRange( ME::Time key1, ME::Time key2, bool verbose )
0516 {
0517   if( key1>key2 ) return false;
0518   MusEcal::RunIterator p1 = _runs.find( key1 );
0519   MusEcal::RunIterator p2 = _runs.find( key2 );
0520   if( p1==_runs.end() || p2==_runs.end() ) return false;
0521   _normFirst = key1;
0522   _normLast  = key2;
0523   return true;
0524 }
0525 
0526 void
0527 MERunManager::setBadRun()
0528 {
0529   _badRuns[ curKey() ] = false;
0530 }
0531 
0532 void
0533 MERunManager::setBadRange( ME::Time key1, ME::Time key2, bool verbose )
0534 {
0535   MusEcal::RunIterator it;
0536   for( it=from( key1 ); it!=to( key2 ); ++it )
0537     {
0538       ME::Time key = it->first;
0539       _badRuns[ key ]=false;
0540     }
0541 }
0542 
0543 void
0544 MERunManager::refreshBadRuns()
0545 {
0546   setPlotRange( beginKey(), endKey(), true );
0547   _badRuns.clear();
0548 }
0549 
0550 bool 
0551 MERunManager::isGood( ME::Time key )
0552 {
0553   if( _badRuns.count( key ) ) return _badRuns[ key ];
0554   if( _runs.count( key ) ) return true;
0555   return false;
0556 }
0557 
0558 MEChannel* 
0559 MERunManager::tree()
0560 {
0561   if( _tree!=0 ) return _tree;
0562   _tree = ME::regTree(_reg)->getDescendant( ME::iLMRegion, _lmr );
0563   assert( _tree!=0 );
0564   return _tree;
0565 }
0566 
0567 void
0568 MERunManager::fillMaps()
0569 {
0570   //
0571   // First, fill "APD" map
0572   //
0573 
0574   // cleanup
0575   while( !_apdMap.empty() ) 
0576   {    
0577     delete _apdMap.begin()->second;
0578     _apdMap.erase( _apdMap.begin() );  
0579   }
0580 
0581   for( int ipn=0; ipn<2; ipn++ )
0582     {
0583       while( !_pnMap[ipn].empty() ) 
0584     {    
0585       delete _pnMap[ipn].begin()->second;
0586       _pnMap[ipn].erase( _pnMap[ipn].begin() );  
0587     }
0588     }
0589 
0590   while( !_mtqMap.empty() ) 
0591   {    
0592     delete _mtqMap.begin()->second;
0593     _mtqMap.erase( _mtqMap.begin() );  
0594   }
0595 
0596   unsigned int size_(0);
0597   unsigned int table_(0);
0598   if( _type==ME::iLaser )
0599     {
0600       table_=ME::iLmfLaserPrim;
0601       size_=ME::iSizeAPD;
0602     }
0603   else if( _type==ME::iTestPulse )
0604     {
0605       table_=ME::iLmfTestPulsePrim;
0606       size_=ME::iSizeTPAPD;
0607     }
0608   
0609   vector< MEChannel* > listOfChan_;
0610   //
0611   // at the crystal level
0612   //
0613   listOfChan_.clear();
0614   tree()->getListOfDescendants( ME::iCrystal, listOfChan_ );
0615 
0616   int reg_ = tree()->getAncestor(0)->id(); // fixme
0617   cout << "id=" << reg_ << endl;
0618 
0619   cout << "Filling APD Maps (number of C=" << listOfChan_.size() << ")" << endl;
0620   for( unsigned int ichan=0; ichan<listOfChan_.size(); ichan++ )
0621     {
0622       //      if( ichan%25==0 ) cout << "." << flush;
0623       MEChannel* leaf_ = listOfChan_[ichan];
0624       int ix = leaf_->ix();
0625       int iy = leaf_->iy();
0626       if( reg_==ME::iEBM || reg_==ME::iEBP )
0627     {
0628       int ieta = leaf_->ix();
0629       int iphi = leaf_->iy();
0630       MEEBGeom::XYCoord ixy = MEEBGeom::localCoord( ieta, iphi );
0631       ix = ixy.first;
0632       iy = ixy.second;
0633     }
0634 
0635       MEVarVector* varVector_ = new MEVarVector( size_ );
0636       for( MusEcal::RunIterator p=_runs.begin(); p!=_runs.end(); ++p )
0637     {
0638       MERun* run_ = p->second;
0639       ME::Time time = run_->time();
0640       varVector_->addTime( time );
0641       for( unsigned ii=0; ii<size_; ii++ )
0642         {
0643           float val = run_->getVal( table_, ii, ix, iy );
0644           varVector_->setVal( time, ii, val );
0645         }
0646     }
0647       _apdMap[leaf_] = varVector_;
0648     }
0649   cout << "...done." << endl;
0650 
0651   //
0652   // At higher levels
0653   //
0654   for( int ig=ME::iSuperCrystal; ig>=ME::iLMRegion; ig-- )
0655     {
0656       listOfChan_.clear();
0657       if( ig==ME::iLMRegion ) listOfChan_.push_back( tree() );
0658       else tree()->getListOfDescendants( ig, listOfChan_ );
0659       cout << "Filling APD Maps (number of " << ME::granularity[ig] << "=" 
0660            << listOfChan_.size() << ")" << endl;
0661       for( unsigned int ichan=0; ichan<listOfChan_.size(); ichan++ )
0662     {
0663       //      if( ichan ) cout << "." << flush;
0664       MEChannel* leaf_ = listOfChan_[ichan];
0665       MEVarVector* varVector_ = new MEVarVector( size_ );
0666       for( MusEcal::RunIterator p=_runs.begin(); p!=_runs.end(); ++p )
0667         {
0668           MERun* run_ = p->second;
0669           ME::Time time = run_->time();
0670           varVector_->addTime( time );
0671           for( unsigned ii=0; ii<size_; ii++ )
0672         {
0673           float val=0;
0674           float n=0;
0675           // loop on daughters
0676           for( unsigned int idau=0; idau<leaf_->n(); idau++ )
0677             {
0678               float val_(0);
0679               bool flag_=true;
0680               assert( _apdMap[leaf_->d(idau)]
0681                   ->getValByTime( time, ii, val_, flag_ ) );
0682               if( val_>0 )
0683             {
0684               n++;
0685               val+=val_;
0686             }
0687             }
0688           if( n!=0 ) val/=n; 
0689           varVector_->setVal( time, ii, val );
0690         }
0691         }
0692       _apdMap[leaf_] = varVector_;
0693     }
0694       cout << "...done." << endl;
0695     }
0696   
0697   //
0698   // Second, fill "PN" map
0699   // 
0700   
0701 
0702   if( _type==ME::iLaser )
0703     {
0704       table_=ME::iLmfLaserPnPrim;
0705       size_=ME::iSizePN;
0706     }
0707   else if( _type==ME::iTestPulse )
0708     {
0709       table_=ME::iLmfTestPulsePnPrim;
0710       size_=ME::iSizeTPPN;
0711     }
0712 
0713   listOfChan_.clear();
0714   tree()->getListOfDescendants( ME::iLMModule, listOfChan_ );
0715 
0716 
0717   cout << "Filling PN Maps (number of LMM=" << listOfChan_.size() << ")" << endl;
0718 
0719 
0720   for( unsigned int ichan=0; ichan<listOfChan_.size(); ichan++ )
0721     {
0722       //      if( ichan ) cout << "." << flush;
0723       MEChannel* leaf_ = listOfChan_[ichan];
0724       int id_ = leaf_->id();
0725 
0726       for( int ipn=0; ipn<2; ipn++ )
0727     {
0728       MEVarVector* varVector_ = new MEVarVector( size_ );
0729       for( MusEcal::RunIterator p=_runs.begin(); p!=_runs.end(); ++p )
0730         {
0731           MERun* run_ = p->second;
0732           ME::Time time = run_->time();
0733           varVector_->addTime( time );
0734           for( unsigned jj=0; jj<size_; jj++ )
0735         {
0736           //float val=0;
0737           float val = run_->getVal( table_, jj, id_, ipn );
0738           varVector_->setVal( time, jj, val );
0739         }
0740         }
0741       _pnMap[ipn][leaf_] = varVector_;
0742     }
0743     }
0744   cout << "...done." << endl; 
0745 
0746 
0747   //
0748   // At higher levels
0749   //
0750   listOfChan_.clear();
0751   int ig=ME::iLMRegion;
0752   tree()->getListOfDescendants( ig, listOfChan_ );
0753   cout << "Filling PN Maps (number of " << ME::granularity[ig] << "=" 
0754        << listOfChan_.size() << ")" << endl;
0755   for( unsigned int ichan=0; ichan<listOfChan_.size(); ichan++ )
0756     {
0757       MEChannel* leaf_ = listOfChan_[ichan];
0758 
0759       for( int ipn=0; ipn<2; ipn++ ){
0760     
0761     MEVarVector* varVector_ = new MEVarVector( size_ );
0762     for( MusEcal::RunIterator p=_runs.begin(); p!=_runs.end(); ++p )
0763       {
0764         MERun* run_ = p->second;
0765         ME::Time time = run_->time();
0766         varVector_->addTime( time );
0767         for( unsigned ii=0; ii<size_; ii++ )
0768           {
0769         float val=0;
0770         float n=0;
0771         
0772         // loop on daughters
0773         for( unsigned int idau=0; idau<leaf_->n(); idau++ )
0774           {
0775             float val_(0);
0776             // bool flag_=true;
0777             // FIXME: add flag
0778             MEChannel* dau=leaf_->d(idau);
0779             int id_=dau->id();
0780             val_ = run_->getVal( table_, ii, id_, ipn );
0781             
0782             if( val_>0 )
0783               {
0784             n++;
0785             val+=val_;
0786               }
0787           }
0788         if( n!=0 ) val/=n; 
0789         varVector_->setVal( time, ii, val );
0790           }
0791       }
0792     _pnMap[ipn][leaf_] = varVector_;
0793       }
0794     }  
0795   cout << "...done." << endl;
0796   
0797   //
0798   // Third, fill "MTQ" map
0799   // 
0800 
0801   if( _type==ME::iLaser )
0802     {
0803       table_=ME::iLmfLaserPulse;
0804       size_=ME::iSizeMTQ;
0805     }
0806   else 
0807     {
0808       table_=ME::iLmfLaserPulse;
0809       size_=0;
0810     }
0811   listOfChan_.clear();
0812   listOfChan_.push_back( tree() );
0813   
0814   cout << "Filling MTQ Maps: (number of LMR=" << listOfChan_.size() << ") "<< endl;
0815   
0816   for( unsigned int ichan=0; ichan<listOfChan_.size(); ichan++ )
0817     {
0818       MEChannel* leaf_ = listOfChan_[ichan];
0819       int id_ = leaf_->id();
0820       
0821       MEVarVector* varVector_ = new MEVarVector( size_ );
0822       for( MusEcal::RunIterator p=_runs.begin(); p!=_runs.end(); ++p )
0823     {
0824       MERun* run_ = p->second;
0825       ME::Time time = run_->time();
0826       varVector_->addTime( time );
0827       for( unsigned jj=0; jj<size_; jj++ )
0828         {
0829           float val = run_->getVal( table_, jj, id_ );
0830           varVector_->setVal( time, jj, val );
0831         }
0832     }
0833       _mtqMap[leaf_] = varVector_;
0834       
0835     }
0836   cout << "...done." << endl; 
0837 
0838   setFlags();
0839 }
0840 
0841 void
0842 MERunManager::setFlags()
0843 {
0844   if( _type==ME::iLaser )
0845     {
0846       setLaserFlags();
0847     }
0848   else if( _type==ME::iTestPulse )
0849     {
0850       setTestPulseFlags();
0851     }
0852 }
0853 
0854 void
0855 MERunManager::setLaserFlags()
0856 {
0857   vector< MEChannel* > listOfChan_;
0858   //
0859   // at the crystal level
0860   //
0861   listOfChan_.clear();
0862   tree()->getListOfDescendants( ME::iCrystal, listOfChan_ );
0863 
0864   cout << "Setting APD Quality Flags " << endl;
0865 
0866   for( unsigned int ichan=0; ichan<listOfChan_.size(); ichan++ )
0867     {
0868       MEChannel* leaf_ = listOfChan_[ichan];
0869       MEVarVector* varVector_ = apdVector( leaf_ );
0870       vector< ME::Time > time;
0871       varVector_->getTime( time );
0872       for( unsigned int itime=0; itime<time.size(); itime++ )
0873     {
0874       ME::Time t_ = time[itime];
0875       float apd_;
0876       float apd_rms_;
0877       float apd_time_;
0878       float apd_time_nevt_;
0879       bool flag_;
0880       varVector_->getValByTime( t_, ME::iAPD_MEAN, apd_, flag_ );
0881       varVector_->getValByTime( t_, ME::iAPD_RMS, apd_rms_, flag_ );
0882       varVector_->getValByTime( t_, ME::iAPD_TIME_MEAN, apd_time_, flag_ );
0883       varVector_->getValByTime( t_, ME::iAPD_TIME_NEVT, apd_time_nevt_, flag_ );
0884      
0885       if( apd_<100  
0886           || (apd_rms_<10 || apd_rms_>500) 
0887           || (apd_time_<5 || apd_time_>8.5)
0888           || (apd_time_nevt_<400 || apd_time_nevt_>650) )
0889         {
0890           varVector_->setFlag( t_, ME::iAPD_MEAN, false );
0891           varVector_->setFlag( t_, ME::iAPD_RMS, false );
0892           varVector_->setFlag( t_, ME::iAPD_M3, false );
0893           varVector_->setFlag( t_, ME::iAPD_OVER_PNA_MEAN, false );
0894           varVector_->setFlag( t_, ME::iAPD_OVER_PNA_RMS, false );
0895           varVector_->setFlag( t_, ME::iAPD_OVER_PNA_M3, false );
0896           varVector_->setFlag( t_, ME::iAPD_OVER_PNB_MEAN, false );
0897           varVector_->setFlag( t_, ME::iAPD_OVER_PNB_RMS, false );
0898           varVector_->setFlag( t_, ME::iAPD_OVER_PNB_M3, false );
0899           varVector_->setFlag( t_, ME::iAPD_OVER_PN_MEAN, false );
0900           varVector_->setFlag( t_, ME::iAPD_OVER_PN_RMS, false );
0901           varVector_->setFlag( t_, ME::iAPD_OVER_PN_M3, false );
0902           varVector_->setFlag( t_, ME::iAPD_OVER_APDA_MEAN, false ); // JM
0903           varVector_->setFlag( t_, ME::iAPD_OVER_APDA_RMS, false ); // JM
0904           varVector_->setFlag( t_, ME::iAPD_OVER_APDA_M3, false ); // JM
0905           varVector_->setFlag( t_, ME::iAPD_OVER_APDB_MEAN, false ); // JM
0906           varVector_->setFlag( t_, ME::iAPD_OVER_APDB_RMS, false ); // JM
0907           varVector_->setFlag( t_, ME::iAPD_OVER_APDB_M3, false ); // JM
0908           varVector_->setFlag( t_, ME::iAPD_TIME_MEAN, false );
0909           varVector_->setFlag( t_, ME::iAPD_TIME_RMS, false );
0910           varVector_->setFlag( t_, ME::iAPD_TIME_M3, false );
0911           varVector_->setFlag( t_, ME::iAPD_TIME_M3, false ); 
0912         }
0913     }
0914     }
0915   
0916   listOfChan_.clear();
0917   tree()->getListOfDescendants( ME::iLMModule, listOfChan_ );
0918 
0919   cout << "Setting PN Quality Flags " << endl;
0920   for( unsigned int ichan=0; ichan<listOfChan_.size(); ichan++ )
0921     {
0922       MEChannel* leaf_ = listOfChan_[ichan];
0923       for( int ipn=0; ipn<2; ipn++ )
0924     {
0925       MEVarVector* varVector_ = pnVector( leaf_, ipn );
0926       vector< ME::Time > time;
0927       varVector_->getTime( time );
0928       for( unsigned int itime=0; itime<time.size(); itime++ )
0929         {
0930           ME::Time t_ = time[itime];
0931           float pn_;
0932           bool flag_;
0933           varVector_->getValByTime( t_, ME::iPN_MEAN, pn_, flag_ );
0934           if( pn_<100 )  
0935         {
0936           varVector_->setFlag( t_, ME::iPN_MEAN, false );
0937           varVector_->setFlag( t_, ME::iPN_RMS, false );
0938           varVector_->setFlag( t_, ME::iPN_M3, false );
0939           if( ipn==0 )
0940             {
0941               setFlag( leaf_, t_, ME::iAPD_OVER_PNA_MEAN, false );
0942               setFlag( leaf_, t_, ME::iAPD_OVER_PNA_RMS, false );
0943               setFlag( leaf_, t_, ME::iAPD_OVER_PNA_M3, false );
0944             }
0945           if( ipn==1 )
0946             {
0947               setFlag( leaf_, t_, ME::iAPD_OVER_PNB_MEAN, false );
0948               setFlag( leaf_, t_, ME::iAPD_OVER_PNB_RMS, false );
0949               setFlag( leaf_, t_, ME::iAPD_OVER_PNB_M3, false );
0950             }
0951           setFlag( leaf_, t_, ME::iAPD_OVER_PN_MEAN, false );
0952           setFlag( leaf_, t_, ME::iAPD_OVER_PN_RMS, false );
0953           setFlag( leaf_, t_, ME::iAPD_OVER_PN_M3, false );
0954         }
0955         }
0956     }
0957     }
0958 
0959   listOfChan_.clear();
0960   listOfChan_.push_back( tree() );
0961 
0962   cout << "Setting MTQ Quality Flags " << endl;
0963   
0964   for( unsigned int ichan=0; ichan<listOfChan_.size(); ichan++ )
0965     {
0966       MEChannel* leaf_ = listOfChan_[ichan];
0967       MEVarVector* varVectorMtq_ = mtqVector( leaf_ );
0968 
0969       vector< ME::Time > time;
0970       varVectorMtq_->getTime( time );
0971 
0972       for( unsigned int itime=0; itime<time.size(); itime++ )
0973     {
0974       ME::Time t_ = time[itime];
0975     
0976       float mtqampl_;
0977       float mtqfwhm_;
0978       bool flagmtq_; 
0979       varVectorMtq_->getValByTime( t_, ME::iMTQ_AMPL, mtqampl_, flagmtq_ );  
0980       varVectorMtq_->getValByTime( t_, ME::iMTQ_FWHM, mtqfwhm_, flagmtq_ );  
0981 
0982       if( mtqampl_< 100. ||  
0983           (mtqfwhm_ <10.0 ||  mtqfwhm_ >100.0) ){
0984         varVectorMtq_->setFlag( t_, ME::iMTQ_AMPL, false );
0985         varVectorMtq_->setFlag( t_, ME::iMTQ_RISE, false );
0986         varVectorMtq_->setFlag( t_, ME::iMTQ_FIT_METHOD, false );
0987         varVectorMtq_->setFlag( t_, ME::iMTQ_FWHM, false );
0988         varVectorMtq_->setFlag( t_, ME::iMTQ_FW20, false );
0989         varVectorMtq_->setFlag( t_, ME::iMTQ_FW80, false );
0990         varVectorMtq_->setFlag( t_, ME::iMTQ_SLIDING, false );
0991         varVectorMtq_->setFlag( t_, ME::iMTQ_TIME, false );
0992       } 
0993 
0994     }
0995     }
0996 }
0997 
0998 void
0999 MERunManager::setTestPulseFlags()
1000 {
1001   vector< MEChannel* > listOfChan_;
1002   //
1003   // at the crystal level
1004   //
1005   listOfChan_.clear();
1006   tree()->getListOfDescendants( ME::iCrystal, listOfChan_ );
1007   //  cout << "Setting TPAPD Quality Flags " << endl;
1008   for( unsigned int ichan=0; ichan<listOfChan_.size(); ichan++ )
1009     {
1010       MEChannel* leaf_ = listOfChan_[ichan];
1011       MEVarVector* varVector_ = apdVector( leaf_ );
1012       vector< ME::Time > time;
1013       varVector_->getTime( time );
1014       for( unsigned int itime=0; itime<time.size(); itime++ )
1015     {
1016       ME::Time t_ = time[itime];
1017       float apd_;
1018       float apd_rms_;
1019       float apd_nevt_;
1020       bool flag_;
1021       varVector_->getValByTime( t_, ME::iTPAPD_MEAN, apd_, flag_ );
1022       varVector_->getValByTime( t_, ME::iTPAPD_RMS,  apd_rms_, flag_ );
1023       varVector_->getValByTime( t_, ME::iTPAPD_NEVT, apd_nevt_, flag_ );
1024       if( apd_<100  
1025           || (apd_rms_==0 || apd_rms_>500) 
1026           || (apd_nevt_<400 || apd_nevt_>2000) )
1027         {
1028           varVector_->setFlag( t_, ME::iTPAPD_MEAN, false );
1029           varVector_->setFlag( t_, ME::iTPAPD_RMS, false );
1030           varVector_->setFlag( t_, ME::iTPAPD_M3, false );
1031           varVector_->setFlag( t_, ME::iTPAPD_NEVT, false );
1032         }
1033     }
1034     } 
1035 }
1036 
1037 void
1038 MERunManager::setFlag( MEChannel* leaf_, ME::Time t_, int ivar_, bool flag_ )
1039 {
1040   if( leaf_==0 ) return;
1041   MEVarVector* varVector_ = apdVector( leaf_ );
1042   varVector_->setFlag( t_, ivar_, flag_ );
1043   for( unsigned int idau=0; idau<leaf_->n(); idau++ )
1044     {
1045       setFlag( leaf_->d(idau), t_, ivar_, flag_ );
1046     }
1047 }
1048 
1049 MEVarVector*
1050 MERunManager::apdVector( MEChannel* leaf )
1051 {
1052   if( _apdMap.count( leaf ) !=0 ) return _apdMap[leaf];
1053   else
1054     return 0;
1055 } 
1056 
1057 MEVarVector*
1058 MERunManager::mtqVector( MEChannel* leaf )
1059 {
1060   if( _mtqMap.count( leaf ) !=0 ) return _mtqMap[leaf];
1061   else
1062     {
1063       cout<< "-- debug -- MERunManager::mtqVector empty"<< endl; 
1064       return 0;
1065     }
1066 } 
1067 
1068 MEVarVector*
1069 MERunManager::pnVector( MEChannel* leaf, int ipn )
1070 {
1071   assert( ipn>=0 && ipn<2 );
1072   if( _pnMap[ipn].count( leaf ) !=0 ) return _pnMap[ipn][leaf];
1073   else {
1074     cout<< "-- debug -- MERunManager::pnVector empty"<< endl; 
1075     return 0;
1076   }
1077 }
1078 
1079 
1080 void
1081 MERunManager::refresh()
1082 {
1083   for( MusEcal::RunIterator p=_runs.begin(); p!=_runs.end(); ++p )
1084     {
1085       p->second->closeLaserPrimFile();
1086     }
1087 }