CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/DQMServices/XdaqCollector/src/XmasToDQMSource.cc

Go to the documentation of this file.
00001 /*
00002 */
00003 
00004 #include "DQMServices/XdaqCollector/interface/XmasToDQMSource.h"
00005 
00006 #include "FWCore/ServiceRegistry/interface/Service.h"
00007 
00008 #include "TRandom.h" // this is just the random number generator
00009 #include <math.h>
00010 
00011 #include <boost/tokenizer.hpp>
00012 //#include <set>
00013 #include <sstream>
00014 
00015 #include <time.h>
00016 
00017 
00018 
00019 using namespace std;
00020 using namespace edm;
00021 
00022 
00023 #define BXBIN 3564
00024 #define WCBIN 257
00025 
00026 //minimun event fragment size = header (8 bytes) + trailer (8 bytes) + zero payload = 16
00027 #define MIN_EVENT_FRAGMENT_SIZE 16
00028 
00029 
00030 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
00031 
00032 
00033 //
00034 // constructors and destructor
00035 //
00036 XmasToDQMSource::XmasToDQMSource( const edm::ParameterSet& ps ) :
00037 counterEvt_(0)
00038 {
00039     cout << "Constructor of XmasToDQMSource called...." << endl;
00040      
00041      dbe_ = Service<DQMStore>().operator->();
00042      parameters_ = ps;
00043      monitorName_ = parameters_.getUntrackedParameter<string>("monitorName","DAQ");
00044      
00045      cout << "Monitor name = " << monitorName_ << endl;
00046      if (monitorName_ != "" ) 
00047         monitorName_ = monitorName_+"/" ;
00048      
00049      prescaleEvt_ = parameters_.getUntrackedParameter<int>("prescaleEvt", -1);
00050      cout << "===> DQM event prescale = " << prescaleEvt_ << " events "<< endl;
00051  
00052  
00054   //const int NBINS = 50; XMIN = 0; XMAX = 20000;
00055   
00056     // create and cd into new folder
00057   //dbe_->setCurrentFolder(/*monitorName_+*/"wse");
00058   //h1 = dbe_->book1D("histo", "Example 1D histogram.", NBINS, XMIN, XMAX);
00059   //h1->setAxisTitle("x-axis title", 1);
00060   //h1->setAxisTitle("y-axis title", 2);
00061   
00062   // assign tag to MEs h1, h2 and h7
00063   //const unsigned int detector_id = 17;
00064   //dbe_->tag(h1, detector_id);
00065 }
00066 
00067 
00068 XmasToDQMSource::~XmasToDQMSource()
00069 {
00070    
00071    // do anything here that needs to be done at desctruction time
00072    // (e.g. close files, deallocate resources etc.)
00073   
00074 }
00075 
00076 
00077 //--------------------------------------------------------
00078 void XmasToDQMSource::beginJob(){
00079 
00080 }
00081 
00082 //--------------------------------------------------------
00083 void XmasToDQMSource::beginRun(const edm::Run& r, const EventSetup& context) {
00084 
00085 }
00086 
00087 //--------------------------------------------------------
00088 void XmasToDQMSource::beginLuminosityBlock(const LuminosityBlock& lumiSeg, 
00089      const EventSetup& context) {
00090   
00091 }
00092 
00093 // ----------------------------------------------------------
00094 void XmasToDQMSource::analyze(const Event& iEvent, 
00095                                const EventSetup& iSetup )
00096 {  
00097         /*time_t start,end;
00098         static int times_called=0;
00099         
00100         times_called++;*/
00101         
00102         
00103         //std::cout << "inside Analyze.... " << std::endl;
00104         
00105         std::map<std::string, std::string, std::less<std::string> >::iterator i;
00106         
00107         //cout << "DQMSourceExample::analyze before BSem_.takeO()" << endl;
00108         
00109         //xmas2dqm::wse::ToDqm::instance()->BSem_.take();
00110 
00111         std::cout << "inside DQMSource::Analyze...ready to lock the data mutex" << std::endl;
00112         //protect access to the queue
00113         pthread_mutex_lock(&xmas2dqm::wse::ToDqm::instance()->LASmutex_);
00114         
00115         
00116         std::cout << "inside DQMSource::Analyze...check (...and possible wait) if data queue is empty" << std::endl;
00117         
00118         //check if the queue is empty and wait (a signal that informs that an element has been pushed)
00119         while(xmas2dqm::wse::ToDqm::instance()->/*QTable_*/MemoryTable_.size() <= 0)
00120         {
00121                 pthread_cond_wait(&xmas2dqm::wse::ToDqm::instance()->more_, &xmas2dqm::wse::ToDqm::instance()->LASmutex_);
00122         }
00123         
00124         
00125         std::cout << "inside DQMSource::Analyze...data queue has elements...proceeding..." << std::endl;
00126         
00127         //start = time(NULL);
00128         
00129         //xdata::Table::Reference ref_table;
00130         xdata::Table *ref_table = NULL;
00131         
00132         
00133         if(xmas2dqm::wse::ToDqm::instance()->/*QTable_*/MemoryTable_.size() > 0)
00134         {
00135                 //cout << " DQMSourceExample::analyze : Queue size  > 0 " << xmas2dqm::wse::ToDqm::instance()->QTable_.size() << endl;
00136                 
00137                 //pop an element from the queue of the LAS data
00138                 ref_table = xmas2dqm::wse::ToDqm::instance()->/*QTable_*/ MemoryTable_.front();
00139                 //xmas2dqm::wse::ToDqm::instance()->QTable_.pop();              
00140                 
00141         }
00142         
00143         //Insert data to histograms transfered to DQM GUI servers (print the table)
00144         if(ref_table != NULL)
00145         {
00146 
00147                 for ( size_t r = 0; r < ref_table->numberOfRows_; r++ )
00148                 {
00149                         
00150                         //check if the  flashlist contains the element we want to monitor
00151                         if(!ref_table->columnData_[xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element])
00152                         {
00153                                 break;
00154                         }
00155                 
00156                         /* remove prints for benchmarking*/
00157                         /*
00158                         std::cout << "********* Printing table inside DQMSourceExample ***************" << std::endl;
00159                         std:: cout << ref_table->columnData_["context"]->elementAt(r)->toString() << std::endl;
00160                         std:: cout << ref_table->columnData_["slotNumber"]->elementAt(r)->toString() << std::endl;*/
00161                         
00162                         //if(ref_table->columnData_["wcHistogram"]->elementAt(r)->toString() == "[]")
00163                         if(ref_table->columnData_[xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString()]->elementAt(r)->toString() == "[]")
00164                         {
00165                                 /* remove prints for benchmarking*/
00166                                 /*std::cout << ref_table->columnData_["context"]->elementAt(r)->toString() << " has empty bxHistogram" << std::endl;*/
00167                                 continue;
00168                         }
00169                         
00170                         
00171                         //check if there is a column runNumber in the LAS table
00172                         if(ref_table->columnData_["runNumber"])
00173                         {
00174                                 /* remove prints for benchmarking*/
00175                                 
00176                                 
00177                                 //xmas2dqm::wse::ToDqm::instance()->BSem_.take();
00178                                 
00179                                 //if runNumber in LAS record different than currnet runNumber go to next LAS record
00180                                 if (xmas2dqm::wse::ToDqm::instance()->runNumber_.toString() != ref_table->columnData_["runNumber"]->elementAt(r)->toString())
00181                                 {
00182                                         continue;
00183                                 }
00184                                 
00185                                 
00186                                 //xmas2dqm::wse::ToDqm::instance()->BSem_.give();
00187                                 //std::cout << "runNumber ... = " << ref_table->columnData_["runNumber"]->elementAt(r)->toString() << std::endl;
00188                         }       
00189                         
00190                         //boost::tokenizer<> Context_tokens(ref_table->columnData_["Context"]->elementAt(r)->toString());
00191                         
00192                         boost::char_separator<char> sep(":/.");
00193                         tokenizer Context_tokens(ref_table->columnData_["Context"]->elementAt(r)->toString(), sep);
00194                 
00195                 
00196                         //check if the combination Host + slotNumber exists already in the set of hosts + slotNumbers
00197                         //if not book a new histogram with correspondent name and push data, else push data to existent histogram
00198                         //std::string host_slot = *(++Context_tokens.begin()) + "_" + ref_table->columnData_["slotNumber"]->elementAt(r)->toString();
00199                         
00200                         std::string host_slot;
00201                         host_slot = *(++ Context_tokens.begin());
00202 
00203                         //check if there is a column slotNumber in the LAS table in order to use as key for the histogram map the combination of host+slot
00204                         //useful mostly for bxHistogram, wcHistogram of frlHisto, where the histograms (flashlist elements) refer to host+slot
00205                         if(ref_table->columnData_["slotNumber"])
00206                         {
00207                                 //host_slot = *(++ Context_tokens.begin()) + "-" + *(++ ++ Context_tokens.begin()) + "-" + *(++ ++ ++Context_tokens.begin()) + "_" + ref_table->columnData_["slotNumber"]->elementAt(r)->toString();
00208                                 host_slot = host_slot + "_" + ref_table->columnData_["slotNumber"]->elementAt(r)->toString();
00209                         }
00210                 
00211                         //host_slot = host_slot + "_" + ref_table->columnData_["slotNumber"]->elementAt(r)->toString();
00212                         
00213                 
00214                         //check if there is no entry in the map for this host (+slot in case of wcHistogram, bxHistogram)
00215                         if( HostSlotMap.find(host_slot) == HostSlotMap.end())
00216                         {
00217                                 /* remove prints for benchmarking*/
00218                                 std::cout << "booking new histogram..." << host_slot << std::endl;
00219                         
00220                                 HostSlotMap[host_slot] = new /*struct*/ Data();
00221                                 
00222                         
00223                                 HostSlotMap[host_slot]->lastTimestamp = ref_table->columnData_["timestamp"]->elementAt(r)->toString();
00224                                 
00225                                 // create and cd into new folder 
00226                                 //dbe_->setCurrentFolder(monitorName_ + "wcHisto");
00227                                 dbe_->setCurrentFolder(monitorName_ + xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString());
00228                                 
00229                                 //the wcHistogramResolution equals the value of the register Histogram of the FRL, not the value of the bytes resolution for the bin
00230                                 // the value of the register multiplied by 16 gives the byte resolution - range of a wcHistogram bin
00231                                 // if(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() == "wcHistogram")
00232 //                              {
00233 //                                      HostSlotMap[host_slot]->Histogram1D = dbe_->book1D("wc_"+ host_slot, "FRL wcHisto", WCBIN, 
00234 //                                      MIN_EVENT_FRAGMENT_SIZE, MIN_EVENT_FRAGMENT_SIZE + WCBIN*16*atoi(ref_table->columnData_["wcHistogramResolution"]->elementAt(r)->toString().c_str()));
00235 //                                      HostSlotMap[host_slot]->Histogram1D->setAxisTitle("Event fragment size (Bytes)"/*"x-axis title"*/, 1);
00236 //                                      HostSlotMap[host_slot]->Histogram1D->setAxisTitle("Events"/*"y-axis title"*/, 2);
00237 //                              }
00238 //                              else if(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() == "bxHistogram")
00239 //                              {
00240 //                                      HostSlotMap[host_slot]->Histogram1D = dbe_->book1D("bx_"+ host_slot, "FRL bxHisto", BXBIN, 1, BXBIN);
00241 //                              
00242 //                                      HostSlotMap[host_slot]->Histogram1D->setAxisTitle("LHC orbit Bunch"/*"x-axis title"*/, 1);
00243 //                                      HostSlotMap[host_slot]->Histogram1D->setAxisTitle("Events"/*"y-axis title"*/, 2);
00244 //                              }
00245                                 
00246                                 std::istringstream str2num;
00247                                 int nbins;
00248                                 double xmin,xmax;
00249 
00250                                 str2num.str(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.bins.toString());
00251                                 str2num >> nbins; // now stream is in end of file state
00252                                 str2num.clear(); // clear end of file state
00253 
00254                                 str2num.str(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.xmin.toString());
00255                                 str2num >> xmin; // now stream is in end of file state
00256                                 str2num.clear(); // clear end of file state
00257 
00258                                 str2num.str(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.xmax.toString());
00259                                 str2num >> xmax; // now stream is in end of file state
00260                                 str2num.clear(); // clear end of file state
00261 
00262                                 HostSlotMap[host_slot]->Histogram1D = dbe_->book1D(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() + "_" + host_slot, "", nbins, xmin, xmax);
00263                                 HostSlotMap[host_slot]->Histogram1D->setAxisTitle(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.xtitle.toString()/*"x-axis title"*/, 1);
00264                                 HostSlotMap[host_slot]->Histogram1D->setAxisTitle(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.ytitle.toString()/*"y-axis title"*/, 2);
00265                                 
00266                         
00267                                 /* remove prints for benchmarking*/
00268                                 /*std::cout << "booked histogram = " << host_slot << std::endl;*/
00269                                 
00270                                 
00271                                 boost::char_separator<char> histo_sep("[,]");
00272                                 tokenizer Histogram_values(ref_table->columnData_[xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString()]->elementAt(r)->toString(), histo_sep);
00273                                 
00274                                 int iwc=0; //bin counter
00275                                 
00276                                 for(tokenizer::iterator itok=Histogram_values.begin(); itok!=Histogram_values.end();++itok)
00277                                 {
00278                                         iwc++;
00279                                         //remove for benchmarking
00280                                         //std::cout << "iwc = "<< iwc << " *itok = " << *itok << std::endl;
00281                                         string s = *itok;
00282                                         //std::cout << "iwc = "<< iwc << " s = " << s << std::endl;
00283                                         //HostSlotMap[host_slot]->Fill(atoi(s.c_str()));
00284                                         
00285                                         std::istringstream istrfloat(s);
00286                                         float bin_value;
00287                                         istrfloat >> bin_value;
00288                                         
00289                                         //std::cout << "iwc = "<< iwc << " bin_value = " << bin_value << std::endl;
00290                                         
00291                                         if(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() == "wcHistogram" || xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() == "bxHistogram")
00292                                         {
00293                                                 HostSlotMap[host_slot]->Histogram1D->setBinContent(iwc-1, bin_value/*atoi(s.c_str())*/);
00294                                         }
00295                                         else
00296                                         {
00297                                                 HostSlotMap[host_slot]->Histogram1D->Fill(bin_value);
00298                                         }                                       
00299                                         
00300                                         if(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() == "wcHistogram" && iwc >= nbins /*WCBIN*/)
00301                                                 break;
00302                                                 
00303                                         if(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() == "bxHistogram" && iwc >= nbins /*BXBIN*/)
00304                                                 break;
00305                                 }
00306                                 
00307                                                 
00308                         }
00309                         else
00310                         {
00311                         
00312                                 std::istringstream str2num;
00313                                 int nbins;
00314 
00315                                 str2num.str(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.bins.toString());
00316                                 str2num >> nbins; // now stream is in end of file state
00317                                 str2num.clear(); // clear end of file state
00318                                 
00319                                 
00320                                 //check if the timestamp has changed and proceed adding data only if timestamp has changed
00321                                 if(HostSlotMap[host_slot]->lastTimestamp == ref_table->columnData_["timestamp"]->elementAt(r)->toString())
00322                                 {
00323                                         //std::cout << host_slot << " same timestamp found..." << std::endl;
00324                                         continue;
00325                                 }
00326                                 else
00327                                 {
00328                                         //std::cout << host_slot << " different timestamp found..." << std::endl;
00329                                         HostSlotMap[host_slot]->lastTimestamp == ref_table->columnData_["timestamp"]->elementAt(r)->toString();
00330                                 }
00331                                 
00332                                 
00333                                 
00334                                 //insert wcHistogram values
00335                                 boost::char_separator<char> histo_sep("[,]");
00336                                 tokenizer Histogram_values(ref_table->columnData_[xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString()]->elementAt(r)->toString(), histo_sep);
00337                                 
00338                                 int iwc=0; //bin counter
00339                                 
00340                                 for(tokenizer::iterator itok=Histogram_values.begin(); itok!=Histogram_values.end();++itok)
00341                                 {
00342                                         iwc++;
00343                                         //remove for benchmarking
00344                                         //std::cout << "fill booked histogram iwc = "<< iwc << " *itok = " << *itok << std::endl;
00345                                         
00346                                         string s = *itok;
00347                                         //HostSlotMap[host_slot]->Fill(atoi(s.c_str()));
00348                                         
00349                                         std::istringstream istrfloat(s);
00350                                         float bin_value;
00351                                         istrfloat >> bin_value;
00352                                         
00353                                         if(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() == "wcHistogram" || xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() == "bxHistogram")
00354                                         {
00355                                                 HostSlotMap[host_slot]->Histogram1D->setBinContent(iwc-1, bin_value/*atoi(s.c_str())*/);
00356                                         }
00357                                         else
00358                                         {
00359                                                 HostSlotMap[host_slot]->Histogram1D->Fill(bin_value);
00360                                         }                                       
00361 
00362                                         if(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() == "wcHistogram" && iwc >= nbins/*WCBIN*/)
00363                                                 break;
00364                                                 
00365                                         if(xmas2dqm::wse::ToDqm::instance()->flashlistMonitor_.bag.element.toString() == "bxHistogram" && iwc >= nbins /*BXBIN*/)
00366                                                 break;
00367                                 }
00368                                 
00369                         }
00370                 }
00371         }       
00372         
00373         
00374         xmas2dqm::wse::ToDqm::instance()->MemoryTable_.pop();
00375         
00376         std::cout << "after poping from the data Queue...."<< std::endl;
00377         
00378         if(ref_table !=NULL)
00379         {
00380                 ref_table->~Table();
00381         }
00382         
00383         //std::cout << "after calling xdata::Table::Reference destructor...."<< std::endl;
00384         
00385         delete ref_table ;
00386         
00387         //std::cout << "after call of delete...."<< std::endl;
00388         
00389         
00390         /*end = time(NULL);
00391         
00392         std::cout << "time called = " << times_called << " time in seconds needed = " << (end - start) << std::endl;*/
00393         
00394         //cout << "DQMSourceExample::analyze before BSem_.give()" << endl;
00395         
00396         //signal that a new element has been inserted
00397         pthread_cond_signal(&xmas2dqm::wse::ToDqm::instance()->less_);
00398         
00399         //std::cout << "after signaligng less...." << std::endl;
00400         
00401         //allow access to the queue
00402         pthread_mutex_unlock(&xmas2dqm::wse::ToDqm::instance()->LASmutex_);
00403         
00404         std::cout << "after unlocking the mutex...." << std::endl;
00405         //xmas2dqm::wse::ToDqm::instance()->BSem_.give();
00406         
00407         
00408 
00409         counterEvt_++;
00410         if (prescaleEvt_ > 0 && counterEvt_%prescaleEvt_!=0) return;
00411         // cout << " processing conterEvt_: " << counterEvt_ <<endl;
00412   
00413         /*if(counterEvt_%100 == 0)
00414         {
00415                 cout << " # of events = " << counterEvt_ << endl;
00416                 dbe_->save("/tmp/thehisto.root","/wse");
00417         }*/
00418         
00419         //std::cout << "returning from XmasToDQMSource::analyze...." << std::endl;
00420         //usleep(100);
00421         //sleep(1000);
00422 
00423 }
00424 
00425 
00426 
00427 
00428 //--------------------------------------------------------
00429 void XmasToDQMSource::endLuminosityBlock(const LuminosityBlock& lumiSeg, 
00430                                           const EventSetup& context) {
00431 }
00432 //--------------------------------------------------------
00433 void XmasToDQMSource::endRun(const Run& r, const EventSetup& context){
00434 
00435   
00436   //dbe_->setCurrentFolder(/*monitorName_+*/"wse");
00437 
00438 }
00439 //--------------------------------------------------------
00440 void XmasToDQMSource::endJob(){
00441 }
00442 
00443