Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:14

0001 #include <iostream>
0002 #include <cstdlib>
0003 #include <string>
0004 #include <sstream>
0005 #include <vector>
0006 #include <time.h>
0007 
0008 #include "OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h"
0009 #include "OnlineDB/EcalCondDB/interface/all_lmf_types.h"
0010 #include "OnlineDB/EcalCondDB/interface/RunDat.h"
0011 
0012 #include "OnlineDB/EcalCondDB/interface/RunDat.h"
0013 #include "OnlineDB/EcalCondDB/interface/RunLaserRunDat.h"
0014 
0015 // fixme
0016 #include "OnlineDB/EcalCondDB/interface/LMFLaserPrimDat.h"
0017 #include "OnlineDB/EcalCondDB/interface/LMFLaserPNPrimDat.h"
0018 #include "OnlineDB/EcalCondDB/interface/LMFLaserPulseDat.h"
0019 
0020 #include "MELaserPrim.hh"
0021 
0022 
0023 #include "TROOT.h"
0024 #include "TFile.h"
0025 #include "TDirectory.h"
0026 #include "TH2F.h"
0027 #include "TF1.h"
0028 
0029 
0030 using namespace std;
0031 
0032 class CondDBApp {
0033 public:
0034 
0035   /**
0036    *   App constructor; Makes the database connection
0037    */
0038   CondDBApp(string host, string sid, string user, string pass, int port)
0039   {
0040     try {
0041       cout << "Making connection...to " << port << flush;
0042       econn = new EcalCondDBInterface( host, sid, user, pass, port );
0043       cout << "Done." << endl;
0044     } catch (runtime_error &e) {
0045       cerr << e.what() << endl;
0046       exit(-1);
0047     }
0048 
0049  
0050   }
0051   CondDBApp(string sid, string user, string pass)
0052   {
0053     try {
0054       cout << "Making connection...to " << sid << endl;
0055       econn = new EcalCondDBInterface(  sid, user, pass );
0056       cout << "Done." << endl;
0057     } catch (runtime_error &e) {
0058       cerr << e.what() << endl;
0059       exit(-1);
0060     }
0061 
0062  
0063   }
0064 
0065 
0066 
0067   /**
0068    *  App destructor;  Cleans up database connection
0069    */
0070   ~CondDBApp() 
0071   {
0072     delete econn;
0073   }
0074 
0075 inline std::string to_string( char value[])
0076                                                                                 
0077 {
0078                                                                                 
0079     std::ostringstream streamOut;
0080                                                                                 
0081     streamOut << value;
0082                                                                                 
0083     return streamOut.str();
0084                                                                                 
0085 }
0086                                                                                 
0087                                                                                 
0088 
0089   RunIOV makeRunIOV(int run_num, std::string& location)
0090   {
0091     cout << "entering makeRunIOV for run " << run_num<< " and location= "<< location << endl;
0092     // The objects necessary to identify a dataset
0093 
0094     LocationDef locdef;
0095     RunTypeDef rundef;
0096     RunTag runtag;
0097 
0098     locdef.setLocation(location);
0099  
0100     rundef.setRunType("LASER");
0101      
0102     runtag.setLocationDef(locdef);
0103     runtag.setRunTypeDef(rundef);
0104     runtag.setGeneralTag("LASER"); 
0105     cout << " set the run tag and location " << endl;
0106 
0107     // Our beginning time will be the current GMT time
0108     // This is the time zone that should be written to the DB!
0109     // (Actually UTC)
0110     Tm startTm;
0111     startTm.setToCurrentGMTime();
0112 
0113     uint64_t microseconds = startTm.microsTime();
0114     startmicros = microseconds;
0115 
0116 
0117     cout << "Starting Time:    " << startTm.str() << endl;
0118     cout << "Starting Micros:  " << startmicros << endl;
0119 
0120     // Set the properties of the iov
0121     RunIOV runiov;
0122 
0123     startTm.setToMicrosTime(microseconds);
0124     cout << "Setting run " << run_num << " run_start " << startTm.str() << endl;
0125     runiov.setRunNumber(run_num);
0126     runiov.setRunStart(startTm);
0127     runiov.setRunTag(runtag);
0128     
0129     return runiov;
0130 
0131   }
0132 
0133   
0134   LMFRunIOV makeLMFRunIOV(RunIOV* runiov, int subr)
0135   {
0136     // LMF Tag and IOV
0137     
0138     LMFRunTag lmftag;
0139     
0140     LMFRunIOV lmfiov;
0141     lmfiov.setLMFRunTag(lmftag);
0142     lmfiov.setRunIOV(*runiov);
0143     lmfiov.setSubRunNumber(subr);
0144     lmfiov.setSubRunStart(runiov->getRunStart());
0145     lmfiov.setSubRunType("Standard");
0146 
0147     return lmfiov;
0148   }
0149 
0150 
0151 
0152   /**
0153    *  Write LMFLaserBlueRawDat objects with associated RunTag and RunIOVs
0154    *  IOVs are written using automatic updating of the 'RunEnd', as if
0155    *  the time of the end of the run was not known at the time of writing.
0156    */
0157   void testWrite(int run_num)
0158   {
0159     std::string location="H4B"; // this is H4 beam 
0160     cout << "Writing LMFLaserBlueDat to database..." << endl;
0161     RunIOV runiov = this->makeRunIOV(run_num, location);
0162     RunTag runtag = runiov.getRunTag();
0163     run_t run = runiov.getRunNumber();
0164 
0165     // write to the DB
0166     cout << "Inserting run..." << flush;
0167     econn->insertRunIOV(&runiov);
0168     cout << "Done inserting run" << endl;
0169 
0170 
0171     cout << "Fetching run by tag just used..." << flush;
0172     RunIOV runiov_prime = econn->fetchRunIOV(&runtag, run);
0173     cout << "Done fetching run" << endl;
0174 
0175 
0176 
0177     EcalLogicID ecid_allEcal;
0178     ecid_allEcal = econn->getEcalLogicID("ECAL");
0179     map< EcalLogicID, RunLaserRunDat >    dataset;
0180     RunLaserRunDat rd;
0181     rd.setLaserSequenceType("STANDARD");
0182     rd.setLaserSequenceCond("STANDALONE");
0183     // or eventually this:
0184     // rd.setSequenceType("IN_THE_GAP");
0185     dataset[ecid_allEcal]=rd;
0186     econn->insertDataSet( &dataset, &runiov );
0187 
0188     cout << "Done inserting laser config" << endl;
0189 
0190     vector<EcalLogicID> ecid_vec;
0191     int sm_min=1;
0192     int sm_max=36;
0193     int ch_min=0;
0194     int ch_max=1;
0195     ecid_vec = econn->getEcalLogicIDSetOrdered("EB_LM_side", sm_min, sm_max, 
0196                            ch_min, ch_max, 
0197                            EcalLogicID::NULLID, EcalLogicID::NULLID, 
0198                            "EB_crystal_number", 1234 );
0199 
0200     vector<EcalLogicID> ecid_vec_pn;
0201     ch_min=0;
0202     ch_max=9;
0203     ecid_vec_pn = econn->getEcalLogicIDSet("EB_LM_PN", sm_min, sm_max,ch_min, ch_max);
0204 
0205     // fanout 
0206     vector<EcalLogicID> ecid_vec_fanout;
0207     ch_min=0;
0208     ch_max=1;
0209     ecid_vec_fanout = econn->getEcalLogicIDSet("EB_LM_side", sm_min, sm_max,ch_min, ch_max);
0210 
0211     cout << "Done retrieval of logicid " << endl;
0212 
0213 
0214     // LMF Tag and IOV
0215     int subrunnumber = 0;
0216 
0217 
0218     for( int color=0; color<1; color++ ) {
0219     for (int sm_num=1; sm_num<37; sm_num++){
0220       for( int side=0; side<2; side++ ) {
0221 
0222             subrunnumber ++;
0223 
0224         cout << "going to generate lmf run iov ..." << endl;
0225             LMFRunIOV lmfiov = this->makeLMFRunIOV( &runiov, subrunnumber );
0226         cout << "Done generating lmf run iov " << endl;
0227 
0228             // datasets
0229             //      typedef LMFPNBluePrimDat LMFLaserBluePNPrimDat;  // name of class should be fixed
0230             map< EcalLogicID, LMFRunDat             >    dataset_lmfrun;
0231             map< EcalLogicID, LMFLaserConfigDat     >    dataset_config;
0232             //      map< EcalLogicID, LMFLaserBluePrimDat   >      dataset_prim;
0233             map< EcalLogicID, LMFLaserPrimDat   >          dataset_prim;
0234             //      map< EcalLogicID, LMFLaserBluePNPrimDat >    dataset_pnprim;
0235             map< EcalLogicID, LMFLaserPNPrimDat >        dataset_pnprim;
0236             //      map< EcalLogicID, LMFLaserBluePulseDat  >     dataset_pulse;
0237             map< EcalLogicID, LMFLaserPulseDat  >     dataset_pulse;
0238 
0239             // Set the data
0240 
0241             LMFRunDat lmf_lmfrun;
0242             lmf_lmfrun.setNumEvents(  150  );
0243             lmf_lmfrun.setQualityFlag(   1 );
0244         int idchannel=(sm_num-1)*2+side; 
0245         std::cout << "channel "<< idchannel << endl;
0246 
0247         dataset_lmfrun[ ecid_vec_fanout[(sm_num-1)*2+side] ] = lmf_lmfrun;
0248             cout << "Inserting lmf run..." << flush;
0249             econn->insertDataSet( &dataset_lmfrun, &lmfiov );
0250             cout << "Done." << endl;
0251 
0252 
0253             LMFLaserConfigDat lmf_config;
0254             lmf_config.setWavelength( 492     );
0255             lmf_config.setVFEGain(    12       );
0256             lmf_config.setPNGain(     16        );
0257             lmf_config.setPower(      100     );
0258             lmf_config.setAttenuator( 1 );
0259             lmf_config.setCurrent(    100    );
0260             lmf_config.setDelay1(     0    );
0261             lmf_config.setDelay2(     0    );
0262 
0263         dataset_config[ecid_vec_fanout[(sm_num-1)*2+side]] = lmf_config;
0264             cout << "Inserting lmf config..." << flush;
0265             econn->insertDataSet( &dataset_config, &lmfiov );
0266             cout << "Done." << endl;
0267 
0268             //
0269             // Laser MATACQ Primitives
0270             //
0271         LMFLaserPulseDat::setColor( color ); // set the color
0272             LMFLaserPulseDat lmf_pulse;
0273             lmf_pulse.setFitMethod( 0 );  // fixme -- is it a string or an int ???
0274             lmf_pulse.setAmplitude( 100.4   );
0275             lmf_pulse.setTime(     34.5   );
0276             lmf_pulse.setRise(     3.2  );
0277             lmf_pulse.setFWHM(     33.6  );
0278             lmf_pulse.setFW80(    44.5    );
0279             lmf_pulse.setFW20(     45.3   );
0280             lmf_pulse.setSliding(  43.2 );
0281             // Fill the dataset
0282             dataset_pulse[ecid_vec_fanout[(sm_num-1)*2+side]] = lmf_pulse;
0283             cout << "Inserting lmf pulse ..." << flush;
0284             econn->insertDataSet( &dataset_pulse, &lmfiov );
0285             cout << "Done." << endl;
0286 
0287 
0288         // LASER BLUE PRIM Data 
0289         LMFLaserPrimDat::setColor( color ); // set the color
0290         int nchan=0;
0291         if(side==0) nchan=800;
0292         if(side==1) nchan=900;
0293 
0294             for( int ixt=1; ixt<=nchan;  ixt++ ) {
0295 
0296 
0297           float x= rand()/1e8;
0298           float apdpn = 1.5+x;
0299           float apd = 1500.+x;
0300           float apd_rms = 15.5+x;
0301           float apdpn_rms = 0.1+x;
0302         
0303 
0304           EcalLogicID ecid_prim = ecid_vec[(sm_num-1)*1700+side*800+ixt -1]; 
0305           // Set the data
0306           LMFLaserPrimDat bluelaser;
0307           bluelaser.setFlag(   1 );
0308           bluelaser.setMean(apd  );
0309           bluelaser.setRMS( apd_rms  );
0310           bluelaser.setPeak(apd  );
0311           bluelaser.setAPDOverPNAMean(apdpn );
0312           bluelaser.setAPDOverPNARMS(apdpn_rms  );
0313           bluelaser.setAPDOverPNAPeak( apdpn );  
0314           bluelaser.setAPDOverPNBMean(apdpn);
0315           bluelaser.setAPDOverPNBRMS( apdpn_rms  );
0316           bluelaser.setAPDOverPNBPeak(apdpn );
0317           
0318           bluelaser.setAPDOverPNMean( apdpn );
0319           bluelaser.setAPDOverPNRMS( apdpn_rms  );
0320           bluelaser.setAPDOverPNPeak(apdpn  );
0321           
0322           bluelaser.setAlpha(   1.2       );
0323           bluelaser.setBeta(    1.3       );
0324           bluelaser.setShapeCor( 100.2      );
0325           // Fill the dataset
0326           dataset_prim[ecid_prim] = bluelaser;
0327       
0328       }
0329 
0330 
0331             // Inserting the dataset, identified by iov
0332             cout << "Inserting  _PRIM_DAT  ..." << flush;
0333             econn->insertDataSet( &dataset_prim,   &lmfiov );
0334             cout << "Done." << endl;
0335 
0336         LMFLaserPNPrimDat::setColor( color ); // set the color
0337 
0338         nchan=10;
0339             for( int ipn=1; ipn<=nchan ; ipn++ ) {
0340 
0341           float x= rand()/1e8;
0342           float pn = 800.+x;
0343 
0344           EcalLogicID ecid_pn = ecid_vec_pn[(sm_num-1)*10+ipn -1];
0345           // Set the data
0346           LMFLaserPNPrimDat bluepn;
0347           bluepn.setFlag(1);
0348           bluepn.setMean( pn );
0349           bluepn.setRMS( pn/10. );
0350           bluepn.setPeak( pn);
0351           bluepn.setPNAOverPNBMean( 1.+x );
0352           bluepn.setPNAOverPNBRMS((1+x)*0.1 );
0353           bluepn.setPNAOverPNBPeak( 1.+x);
0354           // Fill the dataset
0355           dataset_pnprim[ecid_pn] = bluepn;
0356         }
0357 
0358             cout << "Inserting _PN_PRIM_DAT ..." << flush;
0359             econn->insertDataSet( &dataset_pnprim, &lmfiov );
0360             cout << "Done." << endl;
0361 
0362       }
0363     }
0364       }
0365   
0366     cout << "finished processing the APD and PN data"  << endl;
0367     
0368 
0369     
0370 
0371     Tm startTm;
0372     startTm.setToCurrentGMTime();
0373     cout << "program finished at:    " << startTm.str() << endl;
0374 
0375   }
0376   
0377  
0378 
0379 
0380   /**
0381    *  Write a data set
0382    */
0383   template<class DATT, class IOVT>
0384   void testTable(DATT* data, IOVT* iov, EcalLogicID* ecid)
0385   {
0386     tablesTried++;
0387     try {
0388       cout << "Table " << tablesTried << "..." << flush;
0389       map<EcalLogicID, DATT> dataset;
0390       dataset[*ecid] = *data;
0391       econn->insertDataSet(&dataset, iov);
0392       cout << "write OK..." << flush;
0393       dataset.clear();
0394       econn->fetchDataSet(&dataset, iov);
0395       if (!dataset.size()) {
0396     throw(runtime_error("Zero rows read back"));
0397       }
0398       cout << "read OK" << endl;
0399       tablesOK++;
0400     } catch (runtime_error &e) {
0401       cout << "testTable FAILED:  " << e.what() << endl;
0402     } catch (...) {
0403       cout << "testTable FAILED:  unknown exception" << endl;
0404     }
0405   }
0406 
0407 
0408 
0409 
0410 
0411 private:
0412   CondDBApp();  // hidden default constructor
0413   EcalCondDBInterface* econn;
0414   string locations[4];
0415   uint64_t startmicros;
0416   uint64_t endmicros;
0417   run_t startrun;
0418   run_t endrun;
0419 
0420   int tablesTried;
0421   int tablesOK;
0422 
0423   /**
0424    *   Iterate through the dataset and print some data
0425    */
0426   void printDataSet( const map<EcalLogicID, LMFLaserBlueRawDat>* dataset, int limit = 0 ) const
0427   {
0428     cout << "==========printDataSet()" << endl;
0429     if (dataset->size() == 0) {
0430       cout << "No data in map!" << endl;
0431     }
0432     EcalLogicID ecid;
0433     LMFLaserBlueRawDat bluelaser;
0434 
0435     int count = 0;
0436     typedef map< EcalLogicID, LMFLaserBlueRawDat >::const_iterator CI;
0437     for (CI p = dataset->begin(); p != dataset->end(); p++) {
0438       count++;
0439       if (limit && count > limit) { return; }
0440       ecid = p->first;
0441       bluelaser  = p->second;
0442      
0443       cout << "SM:                     " << ecid.getID1() << endl;
0444       cout << "Xtal:                   " << ecid.getID2() << endl;
0445       cout << "APD Peak:               " << bluelaser.getAPDPeak() << endl;
0446       cout << "========================" << endl;
0447     }
0448     cout << endl;
0449   }
0450 
0451 
0452   /**
0453    *   Print out a RunTag
0454    */
0455   void printTag( const RunTag* tag) const
0456   {
0457     cout << endl;
0458     cout << "=============RunTag:" << endl;
0459     cout << "GeneralTag:         " << tag->getGeneralTag() << endl;
0460     cout << "Location:           " << tag->getLocationDef().getLocation() << endl;
0461     cout << "Run Type:           " << tag->getRunTypeDef().getRunType() << endl;
0462     cout << "====================" << endl;
0463   }
0464 
0465   void printIOV( const RunIOV* iov) const
0466   {
0467     cout << endl;
0468     cout << "=============RunIOV:" << endl;
0469     RunTag tag = iov->getRunTag();
0470     printTag(&tag);
0471     cout << "Run Number:         " << iov->getRunNumber() << endl;
0472     cout << "Run Start:          " << iov->getRunStart().str() << endl;
0473     cout << "Run End:            " << iov->getRunEnd().str() << endl;
0474     cout << "====================" << endl;
0475   }
0476 };
0477 
0478 
0479 
0480 int main (int argc, char* argv[])
0481 {
0482   string host;
0483   string sid;
0484   string user;
0485   string pass;
0486   string smin_run;
0487   string sn_run;
0488 
0489   if (argc != 6) {
0490     cout << "Usage:" << endl;
0491     cout << "  " << argv[0] << " <SID> <user> <pass> <min_run> <n_run>" << endl;
0492     exit(-1);
0493   }
0494 
0495   sid = argv[1];
0496   user = argv[2];
0497   pass = argv[3];
0498   smin_run = argv[4];
0499   int min_run=atoi(smin_run.c_str());
0500   sn_run = argv[5];
0501   int n_run=atoi(sn_run.c_str())+min_run;
0502 
0503   try {
0504     //    CondDBApp app(host, sid, user, pass, port);
0505     CondDBApp app( sid, user, pass);
0506 
0507     for (int i=min_run; i<n_run; i++){
0508       int run_num=i;
0509       cout << "going to write run " <<run_num << endl;
0510       app.testWrite(run_num);
0511     }
0512 
0513 
0514   } catch (exception &e) {
0515     cout << "ERROR:  " << e.what() << endl;
0516   } catch (...) {
0517     cout << "Unknown error caught" << endl;
0518   }
0519 
0520   cout << "All Done." << endl;
0521 
0522   return 0;
0523 }