Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:13

0001 #include <iostream>
0002 #include <string>
0003 #include <sstream>
0004 #include <vector>
0005 #include <time.h>
0006 
0007 #include "OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h"
0008 #include "OnlineDB/EcalCondDB/interface/all_lmf_types.h"
0009 #include "OnlineDB/EcalCondDB/interface/RunDat.h"
0010 #include "TROOT.h"
0011 #include "TFile.h"
0012 #include "TDirectory.h"
0013 #include "TH2F.h"
0014 #include "TF1.h"
0015 
0016 
0017 using namespace std;
0018 
0019 class CondDBApp {
0020 public:
0021 
0022   /**
0023    *   App constructor; Makes the database connection
0024    */
0025   CondDBApp(string host, string sid, string user, string pass)
0026   {
0027     try {
0028       cout << "Making connection..." << flush;
0029       econn = new EcalCondDBInterface( host, sid, user, pass );
0030       cout << "Done." << endl;
0031     } catch (runtime_error &e) {
0032       cerr << e.what() << endl;
0033       exit(-1);
0034     }
0035 
0036  
0037   }
0038 
0039 
0040 
0041   /**
0042    *  App destructor;  Cleans up database connection
0043    */
0044   ~CondDBApp() 
0045   {
0046     delete econn;
0047   }
0048 
0049 inline std::string to_string( char value[])
0050                                                                                 
0051 {
0052                                                                                 
0053     std::ostringstream streamOut;
0054                                                                                 
0055     streamOut << value;
0056                                                                                 
0057     return streamOut.str();
0058                                                                                 
0059 }
0060                                                                                 
0061                                                                                 
0062 
0063   RunIOV makeRunIOV(std::string run_num, std::string& location)
0064   {
0065     cout << "entering retrieveRunIOV for run " << run_num<< " and location= "<< location << endl;
0066     // The objects necessary to identify a dataset
0067     LocationDef locdef;
0068     RunTypeDef rundef;
0069     RunTag runtag;
0070  
0071     locdef.setLocation(location);
0072  
0073     rundef.setRunType("LASER");
0074      
0075     runtag.setLocationDef(locdef);
0076     runtag.setRunTypeDef(rundef);
0077     runtag.setGeneralTag("LASER"); 
0078     cout << " set the run tag and location " << endl;
0079 
0080     // Set the properties of the iov
0081     RunIOV runiov;
0082 
0083 
0084     int run=atoi(run_num.c_str());
0085 
0086     //    string run_string=to_string(run_num);
0087     // sscanf(run_num, "%d", &run );
0088 
0089      runiov.setRunTag(runtag); 
0090      runiov.setRunNumber(run);
0091     cout << " before " << endl;
0092     //  int runiov_id=runiov.fetchIDNoTimeInfo();
0093     cout << " after " << endl;
0094     // runiov.setByID(runiov_id);
0095      
0096     cout << "Setting run " << run << endl;
0097     
0098     return runiov;
0099 
0100   }
0101 
0102   
0103   LMFRunIOV makeLMFRunIOV(RunIOV* runiov)
0104   {
0105     // LMF Tag and IOV
0106     
0107     LMFRunTag lmftag;
0108     
0109     LMFRunIOV lmfiov;
0110     lmfiov.setLMFRunTag(lmftag);
0111     lmfiov.setRunIOV(*runiov);
0112     lmfiov.setSubRunNumber(0);
0113     lmfiov.setSubRunStart(runiov->getRunStart());
0114 
0115     return lmfiov;
0116   }
0117 
0118 
0119 
0120   /**
0121    *  Write LMFLaserBlueRawDat objects with associated RunTag and RunIOVs
0122    *  IOVs are written using automatic updating of the 'RunEnd', as if
0123    *  the time of the end of the run was not known at the time of writing.
0124    */
0125   void testWrite(std::string& run_number)
0126   {
0127     std::string location="H4B"; // this is H4 beam 
0128     cout << "Writing LMFLaserBlueDat to database..." << endl;
0129     RunIOV runiov = this->makeRunIOV(run_number, location);
0130     RunTag runtag = runiov.getRunTag();
0131     run_t run = runiov.getRunNumber();
0132     cout << "Fetching run by tag just used..." << flush;
0133     RunIOV runiov_prime = econn->fetchRunIOV(&runtag, run);
0134 
0135     // LMF Tag and IOV
0136     LMFRunIOV lmfiov = this->makeLMFRunIOV(&runiov_prime);
0137 
0138     // retrieve run dat => get the sm in this run
0139     EcalLogicID ecid_sm;
0140     RunDat rd_sm;
0141 
0142   
0143     RunDat rd;
0144     map<EcalLogicID, RunDat> dataset;
0145     econn->fetchDataSet(&dataset, &runiov_prime);
0146 
0147 
0148     int sm_num=ecid_sm.getID1();  
0149     int count = 0;
0150     typedef map< EcalLogicID, RunDat >::const_iterator CI;
0151     for (CI p = dataset.begin(); p != dataset.end(); p++) {
0152       count++;
0153       ecid_sm = p->first;
0154       rd_sm  = p->second;
0155       sm_num=ecid_sm.getID1();
0156       cout << "SM:                     " << ecid_sm.getID1() << endl;
0157       cout << "========================" << endl;
0158     }
0159     cout << endl;
0160 
0161 
0162      
0163 
0164 
0165 
0166 
0167     // Get channel ID for SM sm_num, crystal c      
0168     map<EcalLogicID, LMFLaserBlueNormDat> dataset_lmf;
0169     map<EcalLogicID, LMFLaserBlueRawDat> dataset_lraw;
0170     map<EcalLogicID, LMFPNBlueDat> dataset_lpn;
0171     map<EcalLogicID, LMFMatacqBlueDat> dataset_mq;
0172 
0173     Int_t cx, cy;
0174 
0175     TFile *f;
0176     TFile *f_mq;
0177     
0178     ostringstream Run;
0179     ostringstream sROOT; 
0180     ostringstream sROOT_mq; 
0181 
0182     Run.str("");
0183     Run << "0000" << run;
0184 
0185     sROOT.str("");
0186     sROOT_mq.str("");
0187     // this is for when they will store the file with the SM number 
0188     // for now all are stored fas if Sm22
0189     //    sROOT << "/data1/daq/crogan/ROOT_files/SM" << sm_num << "/SM" << sm_num  << "-0000";
0190 
0191     sROOT << "/data1/daq/crogan/ROOT_files/SM22/SM22-0000";
0192     sROOT_mq << "/data1/daq/crogan/ROOT_files/SM22/MATACQ-SM22-0000";
0193     sROOT << run << ".root";
0194     sROOT_mq << run << ".root";
0195 
0196     cout << "opening file "<< sROOT.str().c_str() << endl;
0197     f = (TFile*) new TFile(sROOT.str().c_str());
0198     TDirectory *DIR = (TDirectory*) f->GetDirectory(Run.str().c_str());
0199 
0200     //    f = (TFile*) new TFile("/data1/daq/crogan/ROOT_files/SM22/SM22-000017400.root");
0201     //  TDirectory *DIR = (TDirectory*) f->GetDirectory("000017400");
0202 
0203     if(DIR != NULL){
0204       TH2F *APDPN = (TH2F*) DIR->Get("APDPN");
0205       TH2F *APD = (TH2F*) DIR->Get("APD");
0206       TH2F *APD_RMS = (TH2F*) DIR->Get("APD_RMS");
0207       TH2F *APDPN_RMS = (TH2F*) DIR->Get("APDPN_RMS");
0208       TH2F *PN = (TH2F*) DIR->Get("PN");
0209       if(APDPN !=NULL && APD !=NULL && APD_RMS !=NULL && APDPN_RMS!=NULL && PN !=NULL ){
0210 
0211     int pn_chan[]={1,101, 501, 901, 1301, 11, 111, 511, 911, 1311 };
0212 
0213     for (int c=1; c<1701; c++){
0214       // the channels are turned in phi and eta 
0215       // with respect to the standard numbering scheme
0216       cx = 85-(c-1)/20;
0217       cy = 20-(c-1)%20;
0218       float apdpn = (float) APDPN->GetBinContent(cx,cy);
0219       float apd = (float) APD->GetBinContent(cx,cy);
0220       float apd_rms = (float) APD_RMS->GetBinContent(cx,cy);
0221       float apdpn_rms = (float) APDPN_RMS->GetBinContent(cx,cy);
0222       float pn = (float) PN->GetBinContent(cx,cy);
0223       
0224       if(c%20==0) cout << "channel "<< c<< " value "<< apdpn << endl; 
0225       EcalLogicID ecid;
0226       ecid = econn->getEcalLogicID("EB_crystal_number", sm_num, c);
0227 
0228       // Set the data
0229       LMFLaserBlueNormDat bluelaser;
0230       LMFLaserBlueRawDat bluelaserraw;
0231       
0232       int ispn=-1;
0233       for(int j=0; j<10; j++){
0234         if(pn_chan[j]==c) ispn=j;
0235       }
0236 
0237       if(ispn!=-1){
0238         LMFPNBlueDat bluepn;
0239 
0240         bluepn.setPNPeak(pn);
0241         bluepn.setPNErr(pn/100.);
0242         EcalLogicID ecid_pn;
0243         ecid_pn = econn->getEcalLogicID("EB_LM_PN", sm_num, ispn);
0244         dataset_lpn[ecid_pn] = bluepn;
0245       }
0246 
0247       bluelaser.setAPDOverPNMean(apdpn);
0248       bluelaser.setAPDOverPNAMean(apdpn);
0249       bluelaser.setAPDOverPNBMean(apdpn);
0250       bluelaser.setAPDOverPNARMS(apdpn_rms);
0251       bluelaser.setAPDOverPNBRMS(apdpn_rms);
0252       bluelaser.setAPDOverPNRMS(apdpn_rms);
0253     
0254       bluelaserraw.setAPDPeak(apd);
0255       bluelaserraw.setAPDErr(apd_rms);
0256       
0257       // Fill the dataset
0258       dataset_lmf[ecid] = bluelaser;
0259       dataset_lraw[ecid] = bluelaserraw;
0260       
0261 
0262     }
0263       }
0264 
0265     }
0266     f->Close();
0267     cout << "finished processing the APD and PN data"  << endl;
0268 
0269     cout << "opening MATACQ file "<< sROOT_mq.str().c_str() << endl;
0270 
0271     try {
0272  
0273 
0274     f_mq = (TFile*) new TFile(sROOT_mq.str().c_str());
0275 
0276     TH1F *mq_height = (TH1F*) f_mq->Get("Height_Channel_0_blue");
0277     TH1F *mq_width = (TH1F*) f_mq->Get("Width_Channel_0_blue");
0278     TH1F *mq_timing = (TH1F*) f_mq->Get("Timing_Channel_0_blue");
0279     
0280     mq_height->Fit("gaus");   
0281     TF1 *fit_height= mq_height->GetFunction("gaus");
0282     float par_height= (float) fit_height ->GetParameter(1);
0283 
0284     mq_width->Fit("gaus");   
0285     TF1 *fit_width= mq_width->GetFunction("gaus");
0286     float par_width=(float) fit_width ->GetParameter(1);
0287 
0288     mq_timing->Fit("gaus");   
0289     TF1 *fit_timing= mq_timing->GetFunction("gaus");
0290     float par_timing=(float) fit_timing ->GetParameter(1);
0291 
0292     // siamo arrivati qui ... 
0293     // aggiustare il ecid del matacq e sistemare i dati del mq. 
0294 
0295     LMFMatacqBlueDat lmf_mq;
0296     EcalLogicID ecid_mq;
0297     ecid_mq = econn->getEcalLogicID("EB");
0298     lmf_mq.setAmplitude(par_height);
0299     lmf_mq.setWidth(par_width);
0300     lmf_mq.setTimeOffset(par_timing);
0301     dataset_mq[ecid_mq] = lmf_mq;
0302     econn->insertDataSet(&dataset_mq, &lmfiov);
0303 
0304     f_mq->Close();
0305 
0306 
0307     cout << "Done Matacq "  << endl;
0308 
0309     } catch (exception &e) { 
0310       cout << "TestLMF>> error with MATACQ " << e.what() ;
0311     } 
0312 
0313     // Insert the dataset, identifying by iov
0314     cout << "Inserting dataset..." << flush;
0315     econn->insertDataSet(&dataset_lmf, &lmfiov);
0316     econn->insertDataSet(&dataset_lraw, &lmfiov);
0317     econn->insertDataSet(&dataset_lpn, &lmfiov);
0318     cout << "Done." << endl;
0319 
0320  
0321   };
0322 
0323 
0324 
0325   /**
0326    *  Write a data set
0327    */
0328   template<class DATT, class IOVT>
0329   void testTable(DATT* data, IOVT* iov, EcalLogicID* ecid)
0330   {
0331     tablesTried++;
0332     try {
0333       cout << "Table " << tablesTried << "..." << flush;
0334       map<EcalLogicID, DATT> dataset;
0335       dataset[*ecid] = *data;
0336       econn->insertDataSet(&dataset, iov);
0337       cout << "write OK..." << flush;
0338       dataset.clear();
0339       econn->fetchDataSet(&dataset, iov);
0340       if (!dataset.size()) {
0341     throw(runtime_error("Zero rows read back"));
0342       }
0343       cout << "read OK" << endl;
0344       tablesOK++;
0345     } catch (runtime_error &e) {
0346       cout << "testTable FAILED:  " << e.what() << endl;
0347     } catch (...) {
0348       cout << "testTable FAILED:  unknown exception" << endl;
0349     }
0350   }
0351 
0352 
0353 
0354 
0355 
0356 private:
0357   CondDBApp();  // hidden default constructor
0358   EcalCondDBInterface* econn;
0359   string locations[4];
0360   uint64_t startmicros;
0361   uint64_t endmicros;
0362   run_t startrun;
0363   run_t endrun;
0364 
0365   int tablesTried;
0366   int tablesOK;
0367 
0368   /**
0369    *   Iterate through the dataset and print some data
0370    */
0371   void printDataSet( const map<EcalLogicID, LMFLaserBlueRawDat>* dataset, int limit = 0 ) const
0372   {
0373     cout << "==========printDataSet()" << endl;
0374     if (dataset->size() == 0) {
0375       cout << "No data in map!" << endl;
0376     }
0377     EcalLogicID ecid;
0378     LMFLaserBlueRawDat bluelaser;
0379 
0380     int count = 0;
0381     typedef map< EcalLogicID, LMFLaserBlueRawDat >::const_iterator CI;
0382     for (CI p = dataset->begin(); p != dataset->end(); p++) {
0383       count++;
0384       if (limit && count > limit) { return; }
0385       ecid = p->first;
0386       bluelaser  = p->second;
0387      
0388       cout << "SM:                     " << ecid.getID1() << endl;
0389       cout << "Xtal:                   " << ecid.getID2() << endl;
0390       cout << "APD Peak:               " << bluelaser.getAPDPeak() << endl;
0391       cout << "========================" << endl;
0392     }
0393     cout << endl;
0394   }
0395 
0396 
0397   /**
0398    *   Print out a RunTag
0399    */
0400   void printTag( const RunTag* tag) const
0401   {
0402     cout << endl;
0403     cout << "=============RunTag:" << endl;
0404     cout << "GeneralTag:         " << tag->getGeneralTag() << endl;
0405     cout << "Location:           " << tag->getLocationDef().getLocation() << endl;
0406     cout << "Run Type:           " << tag->getRunTypeDef().getRunType() << endl;
0407     cout << "====================" << endl;
0408   }
0409 
0410   void printIOV( const RunIOV* iov) const
0411   {
0412     cout << endl;
0413     cout << "=============RunIOV:" << endl;
0414     RunTag tag = iov->getRunTag();
0415     printTag(&tag);
0416     cout << "Run Number:         " << iov->getRunNumber() << endl;
0417     cout << "Run Start:          " << iov->getRunStart().str() << endl;
0418     cout << "Run End:            " << iov->getRunEnd().str() << endl;
0419     cout << "====================" << endl;
0420   }
0421 };
0422 
0423 
0424 
0425 int main (int argc, char* argv[])
0426 {
0427   string host;
0428   string sid;
0429   string user;
0430   string pass;
0431   string run_num;
0432 
0433   if (argc != 6) {
0434     cout << "Usage:" << endl;
0435     cout << "  " << argv[0] << " <host> <SID> <user> <pass> <run_num>" << endl;
0436     exit(-1);
0437   }
0438 
0439   host = argv[1];
0440   sid = argv[2];
0441   user = argv[3];
0442   pass = argv[4];
0443   run_num= argv[5];
0444 
0445   try {
0446     CondDBApp app(host, sid, user, pass);
0447 
0448     app.testWrite(run_num);
0449   } catch (exception &e) {
0450     cout << "ERROR:  " << e.what() << endl;
0451   } catch (...) {
0452     cout << "Unknown error caught" << endl;
0453   }
0454 
0455   cout << "All Done." << endl;
0456 
0457   return 0;
0458 }