Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-23 15:57:15

0001 #define MELaserPrim_cxx
0002 #include <cassert>
0003 #include "CalibCalorimetry/EcalLaserAnalyzer/interface/MELaserPrim.h"
0004 #include "CalibCalorimetry/EcalLaserAnalyzer/interface/MEGeom.h"
0005 #include <cassert>
0006 #include <cstdlib>
0007 
0008 const TString MELaserPrim::apdpn_arrayName[MELaserPrim::iSizeArray_apdpn] = {
0009     "APD", "APDoPN", "APDoPNA", "APDoPNB", "APDoAPD", "APDoAPDA", "APDoAPDB", "Time"};
0010 const TString MELaserPrim::apdpn_varName[MELaserPrim::iSize_apdpn] = {"Mean", "RMS", "M3", "Nevt", "Min", "Max"};
0011 const TString MELaserPrim::apdpn_varUnit[MELaserPrim::iSizeArray_apdpn][MELaserPrim::iSize_apdpn] =
0012 
0013     {{" (ADC Counts)", " (ADC Counts)", " (ADC Counts)", "", " (ADC Counts)", " (ADC Counts)"},
0014      {"", "", "", "", "", ""},
0015      {"", "", "", "", "", ""},
0016      {"", "", "", "", "", ""},
0017      {"", "", "", "", "", ""},
0018      {"", "", "", "", "", ""},
0019      {" (25 ns)", " (25 ns)", " (25 ns)", "", " (25 ns)", " (25 ns)"}};
0020 const TString MELaserPrim::apdpn_extraVarName[MELaserPrim::iSizeExtra_apdpn] = {"ShapeCor"};
0021 const TString MELaserPrim::apdpn_extraVarUnit[MELaserPrim::iSizeExtra_apdpn] = {""};
0022 const TString MELaserPrim::ab_varName[MELaserPrim::iSize_ab] = {"alpha", "beta", "width", "chi2"};
0023 const TString MELaserPrim::mtq_varName[MELaserPrim::iSize_mtq] = {
0024     "peak", "sigma", "fit", "ampl", "trise", "fwhm", "fw20", "fw80", "sliding"};
0025 const TString MELaserPrim::mtq_varUnit[MELaserPrim::iSize_mtq] = {"(nanoseconds)",
0026                                                                   "(nanoseconds)",
0027                                                                   "(nanoseconds)",
0028                                                                   "(ADC counts)",
0029                                                                   "(nanoseconds)",
0030                                                                   "(nanoseconds)",
0031                                                                   "(nanoseconds)",
0032                                                                   "(nanoseconds)",
0033                                                                   "(ADC counts)"};
0034 
0035 const TString MELaserPrim::separator = "__";
0036 
0037 //ClassImp( MELaserPrim )
0038 
0039 MELaserPrim::MELaserPrim(ME::Header header, ME::Settings settings, const char* inpath, const char* outfile)
0040     : init_ok(false), _isBarrel(true), _inpath(inpath), _outfile(outfile) {
0041   apdpn_file = nullptr;
0042   ab_file = nullptr;
0043   mtq_file = nullptr;
0044   tpapd_file = nullptr;
0045   apdpn_tree = nullptr;
0046   ab_tree = nullptr;
0047   pn_tree = nullptr;
0048   mtq_tree = nullptr;
0049   tpapd_tree = nullptr;
0050   tppn_tree = nullptr;
0051   ixmin = 0;
0052   ixmax = 0;
0053   iymin = 0;
0054   iymax = 0;
0055 
0056   _dcc = header.dcc;
0057   _side = header.side;
0058   _run = header.run;
0059   _lb = header.lb;
0060   _events = header.events;
0061   _ts = header.ts_beg;
0062   _ts_beg = header.ts_beg;
0063   _ts_end = header.ts_end;
0064 
0065   _type = settings.type;
0066   _color = settings.wavelength;
0067   _power = settings.power;
0068   _filter = settings.filter;
0069   _delay = settings.delay;
0070   _mgpagain = settings.mgpagain;
0071   _memgain = settings.memgain;
0072 
0073   if (_type == ME::iLaser) {
0074     _primStr = lmfLaserName(ME::iLmfLaserPrim, _type, _color) + separator;
0075     _pnPrimStr = lmfLaserName(ME::iLmfLaserPnPrim, _type, _color) + separator;
0076     _pulseStr = lmfLaserName(ME::iLmfLaserPulse, _type, _color) + separator;
0077   } else if (_type == ME::iTestPulse) {
0078     _tpPrimStr = lmfLaserName(ME::iLmfTestPulsePrim, _type) + separator;
0079     _tpPnPrimStr = lmfLaserName(ME::iLmfTestPulsePnPrim, _type) + separator;
0080   }
0081 
0082   _lmr = ME::lmr(_dcc, _side);
0083   ME::regionAndSector(_lmr, _reg, _sm, _dcc, _side);
0084   _isBarrel = (_reg == ME::iEBM || _reg == ME::iEBP);
0085   _sectorStr = ME::smName(_lmr);
0086   _regionStr = _sectorStr;
0087   _regionStr += "_";
0088   _regionStr += _side;
0089 
0090   init();
0091   bookHistograms();
0092   //fillHistograms();
0093   //writeHistograms();
0094 }
0095 
0096 TString MELaserPrim::channelViewName(int iname) {
0097   switch (iname) {
0098     case iECAL:
0099       return "ECAL";
0100     case iECAL_LMR:
0101       return "ECAL_LMR";
0102     case iEB_crystal_number:
0103       return "EB_crystal_number";
0104     case iEB_LM_LMM:
0105       return "EB_LM_LMM";
0106     case iEB_LM_PN:
0107       return "EB_LM_PN";
0108     case iEE_crystal_number:
0109       return "EE_crystal_number";
0110     case iEE_LM_LMM:
0111       return "EE_LM_LMM";
0112     case iEE_LM_PN:
0113       return "EE_LM_PN";
0114     default:
0115       abort();
0116   }
0117   return "";
0118 }
0119 
0120 int MELaserPrim::logicId(int channelView, int id1, int id2) {
0121   assert(channelView >= iECAL && channelView < iSize_cv);
0122   return 1000000 * channelView + 10000 * id1 + id2;
0123 }
0124 
0125 bool MELaserPrim::getViewIds(int logic_id, int& channelView, int& id1, int& id2) {
0126   bool out = true;
0127   int channelView_ = logic_id / 1000000;
0128   if (channelView != 0 && channelView_ != channelView)
0129     out = false;
0130   channelView = channelView_;
0131   id1 = (logic_id % 1000000) / 10000;
0132   id2 = logic_id % 10000;
0133   return out;
0134 }
0135 
0136 void MELaserPrim::init() {
0137   bool verbose_ = false;
0138 
0139   if (_inpath == "0") {
0140     if (verbose_)
0141       std::cout << "no input file" << std::endl;
0142     init_ok = true;
0143     return;  // GHM
0144   }
0145 
0146   TString cur(_inpath);
0147   if (!cur.EndsWith("/"))
0148     cur += "/";
0149 
0150   if (_type == ME::iLaser) {
0151     TString _APDPN_fname = cur;
0152     _APDPN_fname += "APDPN_LASER.root";
0153     TString _AB_fname = cur;
0154     _AB_fname += "AB.root";
0155     TString _MTQ_fname = cur;
0156     _MTQ_fname += "MATACQ.root";
0157 
0158     bool apdpn_ok, ab_ok, pn_ok, mtq_ok;
0159     apdpn_ok = false;
0160     ab_ok = false;
0161     pn_ok = false;
0162     mtq_ok = false;
0163 
0164     FILE* test;
0165     test = fopen(_APDPN_fname, "r");
0166     if (test) {
0167       apdpn_ok = true;
0168       pn_ok = true;
0169       fclose(test);
0170     }
0171     test = fopen(_AB_fname, "r");
0172     if (test) {
0173       ab_ok = true;
0174       fclose(test);
0175     }
0176     test = fopen(_MTQ_fname, "r");
0177     if (test) {
0178       mtq_ok = true;
0179       fclose(test);
0180     }
0181 
0182     if (apdpn_ok)
0183       apdpn_file = TFile::Open(_APDPN_fname);
0184     if (ab_ok)
0185       ab_file = TFile::Open(_AB_fname);
0186     if (mtq_ok)
0187       mtq_file = TFile::Open(_MTQ_fname);
0188 
0189     if (verbose_) {
0190       std::cout << _APDPN_fname << " ok=" << apdpn_ok << std::endl;
0191       std::cout << _AB_fname << " ok=" << ab_ok << std::endl;
0192       std::cout << _MTQ_fname << " ok=" << mtq_ok << std::endl;
0193     }
0194     if (!apdpn_ok || !pn_ok)
0195       return;  // FIXME !
0196 
0197     TString apdpn_tree_name;
0198     TString ab_tree_name;
0199     TString pn_tree_name;
0200     TString mtq_tree_name;
0201 
0202     apdpn_tree_name = "APDCol";
0203     ab_tree_name = "ABCol";
0204     pn_tree_name = "PNCol";
0205     mtq_tree_name = "MatacqCol";
0206 
0207     apdpn_tree_name += _color;
0208     ab_tree_name += _color;
0209     pn_tree_name += _color;
0210     mtq_tree_name += _color;
0211 
0212     if (mtq_ok) {
0213       TTree* ckeckMtq = (TTree*)mtq_file->Get(mtq_tree_name);
0214       if (ckeckMtq == nullptr)
0215         mtq_ok = false;
0216     }
0217 
0218     if (_color != ME::iIRed && _color != ME::iBlue) {
0219       std::cout << "MELaserPrim::init() -- Fatal Error -- Wrong Laser Color : " << _color << " ---- Abort "
0220                 << std::endl;
0221       return;
0222     }
0223 
0224     apdpn_tree = (TTree*)apdpn_file->Get(apdpn_tree_name);
0225     assert(apdpn_tree != nullptr);
0226     apdpn_tree->SetMakeClass(1);
0227     apdpn_tree->SetBranchAddress("dccID", &apdpn_dccID, &b_apdpn_dccID);
0228     apdpn_tree->SetBranchAddress("towerID", &apdpn_towerID, &b_apdpn_towerID);
0229     apdpn_tree->SetBranchAddress("channelID", &apdpn_channelID, &b_apdpn_channelID);
0230     apdpn_tree->SetBranchAddress("moduleID", &apdpn_moduleID, &b_apdpn_moduleID);
0231     apdpn_tree->SetBranchAddress("side", &apdpn_side, &b_apdpn_side);
0232     apdpn_tree->SetBranchAddress("ieta", &apdpn_ieta, &b_apdpn_ieta);
0233     apdpn_tree->SetBranchAddress("iphi", &apdpn_iphi, &b_apdpn_iphi);
0234     apdpn_tree->SetBranchAddress("flag", &apdpn_flag, &b_apdpn_flag);
0235     if (apdpn_tree->GetBranchStatus("ShapeCor"))
0236       apdpn_tree->SetBranchAddress("ShapeCor", &apdpn_ShapeCor, &b_apdpn_ShapeCor);
0237     else
0238       apdpn_ShapeCor = 0.0;
0239     for (int jj = 0; jj < iSizeArray_apdpn; jj++) {
0240       TString name_ = apdpn_arrayName[jj];
0241       apdpn_tree->SetBranchAddress(name_, apdpn_apdpn[jj], &b_apdpn_apdpn[jj]);
0242     }
0243 
0244     if (ab_ok) {
0245       ab_tree = (TTree*)ab_file->Get(ab_tree_name);
0246       assert(ab_tree != nullptr);
0247       ab_tree->SetMakeClass(1);
0248       ab_tree->SetBranchAddress("dccID", &ab_dccID, &b_ab_dccID);
0249       ab_tree->SetBranchAddress("towerID", &ab_towerID, &b_ab_towerID);
0250       ab_tree->SetBranchAddress("channelID", &ab_channelID, &b_ab_channelID);
0251       ab_tree->SetBranchAddress("ieta", &ab_ieta, &b_ab_ieta);
0252       ab_tree->SetBranchAddress("iphi", &ab_iphi, &b_ab_iphi);
0253       ab_tree->SetBranchAddress("flag", &ab_flag, &b_ab_flag);
0254       for (int ii = 0; ii < iSize_ab; ii++) {
0255         ab_tree->SetBranchAddress(ab_varName[ii], &ab_ab[ii], &b_ab_ab[ii]);
0256       }
0257     }
0258 
0259     pn_tree = (TTree*)apdpn_file->Get(pn_tree_name);
0260     assert(pn_tree != nullptr);
0261     pn_tree->SetMakeClass(1);
0262     pn_tree->SetBranchAddress("side", &pn_side, &b_pn_side);
0263     pn_tree->SetBranchAddress("pnID", &pn_pnID, &b_pn_pnID);
0264     pn_tree->SetBranchAddress("moduleID", &pn_moduleID, &b_pn_moduleID);
0265     pn_tree->SetBranchAddress("PN", pn_PN, &b_pn_PN);
0266     pn_tree->SetBranchAddress("PNoPN", pn_PNoPN, &b_pn_PNoPN);
0267     pn_tree->SetBranchAddress("PNoPNA", pn_PNoPNA, &b_pn_PNoPNA);
0268     pn_tree->SetBranchAddress("PNoPNB", pn_PNoPNB, &b_pn_PNoPNB);
0269 
0270     if (mtq_ok) {
0271       mtq_tree = (TTree*)mtq_file->Get(mtq_tree_name);
0272       assert(mtq_tree != nullptr);
0273       mtq_tree->SetMakeClass(1);
0274       mtq_tree->SetBranchAddress("side", &mtq_side, &b_mtq_side);
0275 
0276       for (int ii = 0; ii < iSize_mtq; ii++) {
0277         mtq_tree->SetBranchAddress(mtq_varName[ii], &mtq_mtq[ii], &b_mtq_mtq[ii]);
0278       }
0279     }
0280   } else if (_type == ME::iTestPulse) {
0281     TString _TPAPD_fname = cur;
0282     _TPAPD_fname += "APDPN_TESTPULSE.root";
0283 
0284     bool tpapd_ok;
0285     tpapd_ok = false;
0286 
0287     FILE* test;
0288     test = fopen(_TPAPD_fname, "r");
0289     if (test) {
0290       tpapd_ok = true;
0291       fclose(test);
0292     }
0293 
0294     if (tpapd_ok)
0295       tpapd_file = TFile::Open(_TPAPD_fname);
0296 
0297     if (verbose_) {
0298       std::cout << _TPAPD_fname << " ok=" << tpapd_ok << std::endl;
0299     }
0300     if (!tpapd_ok)
0301       return;
0302 
0303     TString tpapd_tree_name;
0304     TString tppn_tree_name;
0305 
0306     tpapd_tree_name = "TPAPD";
0307     tppn_tree_name = "TPPN";
0308 
0309     tpapd_tree = (TTree*)tpapd_file->Get(tpapd_tree_name);
0310     assert(tpapd_tree != nullptr);
0311     tpapd_tree->SetMakeClass(1);
0312     tpapd_tree->SetBranchAddress("ieta", &tpapd_ieta, &b_tpapd_ieta);
0313     tpapd_tree->SetBranchAddress("iphi", &tpapd_iphi, &b_tpapd_iphi);
0314     tpapd_tree->SetBranchAddress("dccID", &tpapd_dccID, &b_tpapd_dccID);
0315     tpapd_tree->SetBranchAddress("side", &tpapd_side, &b_tpapd_side);
0316     tpapd_tree->SetBranchAddress("towerID", &tpapd_towerID, &b_tpapd_towerID);
0317     tpapd_tree->SetBranchAddress("channelID", &tpapd_channelID, &b_tpapd_channelID);
0318     tpapd_tree->SetBranchAddress("moduleID", &tpapd_moduleID, &b_tpapd_moduleID);
0319     tpapd_tree->SetBranchAddress("flag", &tpapd_flag, &b_tpapd_flag);
0320     tpapd_tree->SetBranchAddress("gain", &tpapd_gain, &b_tpapd_gain);
0321     tpapd_tree->SetBranchAddress("APD", tpapd_APD, &b_tpapd_APD);
0322 
0323     tppn_tree = (TTree*)tpapd_file->Get(tppn_tree_name);
0324     assert(tppn_tree != nullptr);
0325     tppn_tree->SetMakeClass(1);
0326     tppn_tree->SetBranchAddress("side", &tppn_side, &b_tppn_side);
0327     tppn_tree->SetBranchAddress("pnID", &tppn_pnID, &b_tppn_pnID);
0328     tppn_tree->SetBranchAddress("moduleID", &tppn_moduleID, &b_tppn_moduleID);
0329     tppn_tree->SetBranchAddress("gain", &tppn_gain, &b_tppn_gain);
0330     tppn_tree->SetBranchAddress("PN", tppn_PN, &b_tppn_PN);
0331   }
0332   init_ok = true;
0333 }
0334 
0335 void MELaserPrim::bookHistograms() {
0336   if (!init_ok)
0337     return;
0338   refresh();
0339 
0340   TString i_name, d_name;
0341 
0342   if (_isBarrel) {
0343     ixmin = 0;
0344     ixmax = 85;
0345     nx = ixmax - ixmin;
0346     iymin = 0;
0347     iymax = 20;
0348     ny = iymax - iymin;
0349 
0350     //       for( int ilmod=1; ilmod<=9; ilmod++ )
0351     //  {
0352     //    _pn[ilmod] = MEEBGeom::pn( ilmod );
0353     //  }
0354   } else  // fixme --- to be implemented
0355   {
0356     ixmin = 1;
0357     ixmax = 101;
0358     nx = ixmax - ixmin;
0359     iymin = 1;
0360     iymax = 101;
0361     ny = iymax - iymin;
0362     //       for( int ilmod=1; ilmod<=21; ilmod++ )  // modules 20 and 21 are fake...
0363     //  {
0364     //    _pn[ilmod] = MEEEGeom::pn( ilmod );
0365     //  }
0366     //      abort();
0367   }
0368 
0369   TString t_name;
0370 
0371   //
0372   // Laser Run
0373   //
0374   t_name = "LMF_RUN_DAT";
0375   addBranchI(t_name, "LOGIC_ID");
0376   addBranchI(t_name, "NEVENTS");
0377   addBranchI(t_name, "QUALITY_FLAG");
0378 
0379   //
0380   // Laser Run IOV
0381   //
0382   t_name = "LMF_RUN_IOV";
0383   addBranchI(t_name, "TAG_ID");
0384   addBranchI(t_name, "SUB_RUN_NUM");
0385   addBranchI(t_name, "SUB_RUN_START_LOW");
0386   addBranchI(t_name, "SUB_RUN_START_HIGH");
0387   addBranchI(t_name, "SUB_RUN_END_LOW");
0388   addBranchI(t_name, "SUB_RUN_END_HIGH");
0389   addBranchI(t_name, "DB_TIMESTAMP_LOW");
0390   addBranchI(t_name, "DB_TIMESTAMP_HIGH");
0391   addBranchC(t_name, "SUB_RUN_TYPE");
0392 
0393   if (_type == ME::iLaser) {
0394     //
0395     // Laser ADC Primitives
0396     //
0397     bookHistoI(_primStr, "LOGIC_ID");
0398     bookHistoI(_primStr, "FLAG");
0399     bookHistoF(_primStr, "MEAN");
0400     bookHistoF(_primStr, "RMS");
0401     bookHistoF(_primStr, "M3");
0402     bookHistoF(_primStr, "APD_OVER_PNA_MEAN");
0403     bookHistoF(_primStr, "APD_OVER_PNA_RMS");
0404     bookHistoF(_primStr, "APD_OVER_PNA_M3");
0405     bookHistoF(_primStr, "APD_OVER_PNB_MEAN");
0406     bookHistoF(_primStr, "APD_OVER_PNB_RMS");
0407     bookHistoF(_primStr, "APD_OVER_PNB_M3");
0408     bookHistoF(_primStr, "APD_OVER_PN_MEAN");
0409     bookHistoF(_primStr, "APD_OVER_PN_RMS");
0410     bookHistoF(_primStr, "APD_OVER_PN_M3");
0411     bookHistoF(_primStr, "APD_OVER_APDA_MEAN");
0412     bookHistoF(_primStr, "APD_OVER_APDA_RMS");
0413     bookHistoF(_primStr, "APD_OVER_APDA_M3");
0414     bookHistoF(_primStr, "APD_OVER_APDB_MEAN");
0415     bookHistoF(_primStr, "APD_OVER_APDB_RMS");
0416     bookHistoF(_primStr, "APD_OVER_APDB_M3");
0417     bookHistoF(_primStr, "SHAPE_COR");
0418     bookHistoF(_primStr, "ALPHA");
0419     bookHistoF(_primStr, "BETA");
0420     // NEW GHM 08/06 --> SCHEMA MODIFIED?
0421     bookHistoF(_primStr, "TIME_MEAN");
0422     bookHistoF(_primStr, "TIME_RMS");
0423     bookHistoF(_primStr, "TIME_M3");
0424     bookHistoF(_primStr, "TIME_NEVT");
0425 
0426     //
0427     // Laser PN Primitives
0428     //
0429     t_name = lmfLaserName(ME::iLmfLaserPnPrim, _type, _color);
0430     addBranchI(t_name, "LOGIC_ID");
0431     addBranchI(t_name, "FLAG");
0432     addBranchF(t_name, "MEAN");
0433     addBranchF(t_name, "RMS");
0434     addBranchF(t_name, "M3");
0435     addBranchF(t_name, "PNA_OVER_PNB_MEAN");
0436     addBranchF(t_name, "PNA_OVER_PNB_RMS");
0437     addBranchF(t_name, "PNA_OVER_PNB_M3");
0438 
0439     //
0440     // Laser Pulse
0441     //
0442     t_name = lmfLaserName(ME::iLmfLaserPulse, _type, _color);
0443     addBranchI(t_name, "LOGIC_ID");
0444     addBranchI(t_name, "FIT_METHOD");
0445     addBranchF(t_name, "MTQ_AMPL");
0446     addBranchF(t_name, "MTQ_TIME");
0447     addBranchF(t_name, "MTQ_RISE");
0448     addBranchF(t_name, "MTQ_FWHM");
0449     addBranchF(t_name, "MTQ_FW20");
0450     addBranchF(t_name, "MTQ_FW80");
0451     addBranchF(t_name, "MTQ_SLIDING");
0452 
0453     //
0454     // Laser Config
0455     //
0456     t_name = lmfLaserName(ME::iLmfLaserConfig, _type);
0457     addBranchI(t_name, "LOGIC_ID");
0458     addBranchI(t_name, "WAVELENGTH");
0459     addBranchI(t_name, "VFE_GAIN");
0460     addBranchI(t_name, "PN_GAIN");
0461     addBranchI(t_name, "LSR_POWER");
0462     addBranchI(t_name, "LSR_ATTENUATOR");
0463     addBranchI(t_name, "LSR_CURRENT");
0464     addBranchI(t_name, "LSR_DELAY_1");
0465     addBranchI(t_name, "LSR_DELAY_2");
0466 
0467     //
0468     // Laser LaserRun config dat
0469     //
0470     t_name = "RUN_LASERRUN_CONFIG_DAT";
0471     addBranchI(t_name, "LOGIC_ID");
0472     addBranchC(t_name, "LASER_SEQUENCE_TYPE");
0473     addBranchC(t_name, "LASER_SEQUENCE_COND");
0474 
0475   } else if (_type == ME::iTestPulse) {
0476     //
0477     // Test Pulse ADC Primitives
0478     //
0479     bookHistoI(_tpPrimStr, "LOGIC_ID");
0480     bookHistoI(_tpPrimStr, "FLAG");
0481     bookHistoF(_tpPrimStr, "MEAN");
0482     bookHistoF(_tpPrimStr, "RMS");
0483     bookHistoF(_tpPrimStr, "M3");
0484     bookHistoF(_tpPrimStr, "NEVT");
0485 
0486     //
0487     // Test Pulse PN Primitives
0488     //
0489     t_name = lmfLaserName(ME::iLmfTestPulsePnPrim, _type);
0490     addBranchI(t_name, "LOGIC_ID");
0491     addBranchI(t_name, "FLAG");
0492     addBranchI(t_name, "GAIN");
0493     addBranchF(t_name, "MEAN");
0494     addBranchF(t_name, "RMS");
0495     addBranchF(t_name, "M3");
0496 
0497     //
0498     // Test Pulse Config
0499     //
0500     t_name = lmfLaserName(ME::iLmfTestPulseConfig, _type);
0501     addBranchI(t_name, "LOGIC_ID");
0502     addBranchI(t_name, "VFE_GAIN");
0503     addBranchI(t_name, "PN_GAIN");
0504   }
0505 }
0506 
0507 void MELaserPrim::fillHistograms() {
0508   TString t_name;
0509 
0510   if (!init_ok)
0511     return;
0512 
0513   Long64_t nentries = 0;
0514   Long64_t ientry = 0;
0515 
0516   int channelView_(0);
0517   int id1_(0), id2_(0);
0518   int logic_id_(0);
0519 
0520   if (_type == ME::iLaser) {
0521     nentries = apdpn_tree->GetEntriesFast();
0522     for (Long64_t jentry = 0; jentry < nentries; jentry++) {
0523       ientry = apdpn_tree->LoadTree(jentry);
0524       assert(ientry >= 0);
0525       apdpn_tree->GetEntry(jentry);
0526 
0527       if (ab_tree) {
0528         ientry = ab_tree->LoadTree(jentry);
0529         assert(ientry >= 0);
0530         ab_tree->GetEntry(jentry);
0531       }
0532 
0533       if (apdpn_iphi < 0)
0534         continue;
0535 
0536       // fixme remove until coordinated are fine
0537       //if(ab_tree) assert( apdpn_ieta==ab_ieta && apdpn_iphi==ab_iphi );
0538 
0539       int ix(0);
0540       int iy(0);
0541       if (_isBarrel) {
0542         // Barrel, global coordinates
0543         id1_ = _sm;
0544         if (apdpn_side != _side)
0545           continue;
0546         int ieta = apdpn_ieta;
0547         int iphi = apdpn_iphi;
0548         MEEBGeom::XYCoord xy_ = MEEBGeom::localCoord(ieta, iphi);
0549         ix = xy_.first;
0550         iy = xy_.second;
0551         id2_ = MEEBGeom::crystal_channel(ix, iy);
0552         channelView_ = iEB_crystal_number;
0553       } else {
0554         // EndCaps, global coordinates
0555         id1_ = apdpn_iphi;
0556         id2_ = apdpn_ieta;
0557         ix = id1_;
0558         iy = id2_;
0559         channelView_ = iEE_crystal_number;
0560       }
0561 
0562       logic_id_ = logicId(channelView_, id1_, id2_);
0563 
0564       int flag = apdpn_flag;
0565 
0566       setInt("LOGIC_ID", ix, iy, logic_id_);
0567       setInt("FLAG", ix, iy, flag);
0568       setVal("MEAN", ix, iy, apdpn_apdpn[iAPD][iMean]);
0569       setVal("RMS", ix, iy, apdpn_apdpn[iAPD][iRMS]);
0570       setVal("M3", ix, iy, apdpn_apdpn[iAPD][iM3]);  // fixme --- peak?
0571       setVal("APD_OVER_PNA_MEAN", ix, iy, apdpn_apdpn[iAPDoPNA][iMean]);
0572       setVal("APD_OVER_PNA_RMS", ix, iy, apdpn_apdpn[iAPDoPNA][iRMS]);
0573       setVal("APD_OVER_PNA_M3", ix, iy, apdpn_apdpn[iAPDoPNA][iM3]);  // fixme
0574       setVal("APD_OVER_PNB_MEAN", ix, iy, apdpn_apdpn[iAPDoPNB][iMean]);
0575       setVal("APD_OVER_PNB_RMS", ix, iy, apdpn_apdpn[iAPDoPNB][iRMS]);
0576       setVal("APD_OVER_PNB_M3", ix, iy, apdpn_apdpn[iAPDoPNB][iM3]);  // fixme
0577       setVal("APD_OVER_PN_MEAN", ix, iy, apdpn_apdpn[iAPDoPN][iMean]);
0578       setVal("APD_OVER_PN_RMS", ix, iy, apdpn_apdpn[iAPDoPN][iRMS]);
0579       setVal("APD_OVER_PN_M3", ix, iy, apdpn_apdpn[iAPDoPN][iM3]);  // fixme
0580       // JM
0581       setVal("APD_OVER_APD_MEAN", ix, iy, apdpn_apdpn[iAPDoAPDA][iMean]);
0582       setVal("APD_OVER_APD_RMS", ix, iy, apdpn_apdpn[iAPDoAPDA][iRMS]);
0583       setVal("APD_OVER_APD_M3", ix, iy, apdpn_apdpn[iAPDoAPDA][iM3]);  // fixme
0584       setVal("APD_OVER_APDA_MEAN", ix, iy, apdpn_apdpn[iAPDoAPDA][iMean]);
0585       setVal("APD_OVER_APDA_RMS", ix, iy, apdpn_apdpn[iAPDoAPDA][iRMS]);
0586       setVal("APD_OVER_APDA_M3", ix, iy, apdpn_apdpn[iAPDoAPDA][iM3]);  // fixme
0587       setVal("APD_OVER_APDB_MEAN", ix, iy, apdpn_apdpn[iAPDoAPDB][iMean]);
0588       setVal("APD_OVER_APDB_RMS", ix, iy, apdpn_apdpn[iAPDoAPDB][iRMS]);
0589       setVal("APD_OVER_APDB_M3", ix, iy, apdpn_apdpn[iAPDoAPDB][iM3]);  // fixme
0590       // JM
0591       setVal("SHAPE_COR", ix, iy, apdpn_ShapeCor);
0592       if (ab_tree) {
0593         setVal("ALPHA", ix, iy, ab_ab[iAlpha]);
0594         setVal("BETA", ix, iy, ab_ab[iBeta]);
0595       } else {
0596         setVal("ALPHA", ix, iy, 0.);
0597         setVal("BETA", ix, iy, 0.);
0598       }
0599       // NEW GHM 08/06
0600       setVal("TIME_MEAN", ix, iy, apdpn_apdpn[iTime][iMean]);
0601       setVal("TIME_RMS", ix, iy, apdpn_apdpn[iTime][iRMS]);
0602       setVal("TIME_M3", ix, iy, apdpn_apdpn[iTime][iM3]);
0603       setVal("TIME_NEVT", ix, iy, apdpn_apdpn[iTime][iNevt]);
0604     }
0605 
0606     //
0607     // PN primitives
0608     //
0609     t_name = lmfLaserName(ME::iLmfLaserPnPrim, _type, _color);
0610 
0611     nentries = pn_tree->GetEntriesFast();
0612     assert(nentries % 2 == 0);
0613     int module_(0);
0614 
0615     Long64_t jentry = 0;
0616 
0617     while (jentry < nentries) {
0618       for (int jj = 0; jj < 2; jj++) {
0619         // jj=0 --> PNA
0620         // jj=1 --> PNB
0621 
0622         int zentry = jentry + jj;
0623         assert(zentry < nentries);
0624 
0625         ientry = pn_tree->LoadTree(zentry);
0626         assert(ientry >= 0);
0627         pn_tree->GetEntry(zentry);
0628 
0629         if (_side != pn_side)
0630           break;
0631 
0632         if (jj == 1)
0633           assert(pn_moduleID == module_);
0634         module_ = pn_moduleID;
0635         assert(pn_pnID == jj);
0636 
0637         // get the PN number
0638         std::pair<int, int> memPn_ = ME::pn(_lmr, module_, (ME::PN)jj);
0639         if (_isBarrel) {
0640           //          assert( memPn_.first%600==_dcc%600 );
0641           id1_ = _sm;
0642           id2_ = memPn_.second;
0643         } else {
0644           int dee_ = MEEEGeom::dee(_lmr);
0645           //          int mem_ = memPn_.first%600;
0646           //          if(      dee_==1 )
0647           //            {
0648           //              if( jj==ME::iPNA ) assert( mem_==50 );
0649           //              else               assert( mem_==51 );
0650           //            }
0651           //          else if( dee_==2 )
0652           //            {
0653           //              if( jj==ME::iPNA ) assert( mem_==47 );  // warning !
0654           //              else               assert( mem_==46 );
0655           //              //              assert( mem_==46 || mem_==47 );
0656           //            }
0657           //          else if( dee_==3 )
0658           //            {
0659           //              if( jj==ME::iPNA ) assert( mem_==1 );
0660           //              else               assert( mem_==2 );
0661           //            }
0662           //          else if( dee_==4 )
0663           //            {
0664           //              if( jj==ME::iPNA ) assert( mem_==5 );
0665           //              else               assert( mem_==6 );
0666           //            }
0667           id1_ = dee_;
0668           id2_ = (jj + 1) * 100 + memPn_.second;
0669         }
0670 
0671         if (_isBarrel) {
0672           channelView_ = iEB_LM_PN;
0673         } else {
0674           channelView_ = iEE_LM_PN;
0675         }
0676         logic_id_ = logicId(channelView_, id1_, id2_);
0677 
0678         i_t[t_name + separator + "LOGIC_ID"] = logic_id_;
0679         f_t[t_name + separator + "MEAN"] = pn_PN[iMean];
0680         f_t[t_name + separator + "RMS"] = pn_PN[iRMS];
0681         f_t[t_name + separator + "M3"] = pn_PN[iM3];
0682         f_t[t_name + separator + "PNA_OVER_PNB_MEAN"] = (jj == 0) ? pn_PNoPNB[iMean] : pn_PNoPNA[iMean];
0683         f_t[t_name + separator + "PNA_OVER_PNB_RMS"] = (jj == 0) ? pn_PNoPNB[iRMS] : pn_PNoPNA[iRMS];
0684         f_t[t_name + separator + "PNA_OVER_PNB_M3"] = (jj == 0) ? pn_PNoPNB[iM3] : pn_PNoPNA[iM3];
0685 
0686         t_t[t_name]->Fill();
0687       }
0688       //      std::cout << "Module=" << module_ << "\tPNA=" << pn_[0] << "\tPNB=" << pn_[1] << std::endl;
0689 
0690       //      if( _isBarrel )
0691       //        jentry += 4;
0692       //      else
0693       //        jentry += 2;
0694       jentry += 2;
0695     }
0696 
0697     logic_id_ = logicId(iECAL_LMR, _lmr);
0698 
0699     //
0700     // MATACQ primitives
0701     //
0702 
0703     if (mtq_tree) {
0704       t_name = lmfLaserName(ME::iLmfLaserPulse, _type, _color);
0705 
0706       nentries = mtq_tree->GetEntriesFast();
0707       assert(nentries == 2);
0708       for (Long64_t jentry = 0; jentry < nentries; jentry++) {
0709         ientry = mtq_tree->LoadTree(jentry);
0710         assert(ientry >= 0);
0711         mtq_tree->GetEntry(jentry);
0712 
0713         if (mtq_side != _side)
0714           continue;
0715 
0716         i_t[t_name + separator + "LOGIC_ID"] = logic_id_;
0717         i_t[t_name + separator + "FIT_METHOD"] = 0;  // fixme  --- what's this? ? ?
0718         f_t[t_name + separator + "MTQ_AMPL"] = mtq_mtq[iAmpl];
0719         f_t[t_name + separator + "MTQ_TIME"] = mtq_mtq[iPeak];
0720         f_t[t_name + separator + "MTQ_RISE"] = mtq_mtq[iTrise];
0721         f_t[t_name + separator + "MTQ_FWHM"] = mtq_mtq[iFwhm];
0722         f_t[t_name + separator + "MTQ_FW20"] = mtq_mtq[iFw20];
0723         f_t[t_name + separator + "MTQ_FW80"] = mtq_mtq[iFw80];
0724         f_t[t_name + separator + "MTQ_SLIDING"] =
0725             mtq_mtq[iSlide];  // fixme  --- sliding: max of average in sliding window
0726 
0727         t_t[t_name]->Fill();
0728       }
0729     } else {
0730       t_name = lmfLaserName(ME::iLmfLaserPulse, _type, _color);
0731 
0732       i_t[t_name + separator + "LOGIC_ID"] = logic_id_;
0733       i_t[t_name + separator + "FIT_METHOD"] = 0;  // fixme
0734       f_t[t_name + separator + "MTQ_AMPL"] = 0.0;
0735       f_t[t_name + separator + "MTQ_TIME"] = 0.0;
0736       f_t[t_name + separator + "MTQ_RISE"] = 0.0;
0737       f_t[t_name + separator + "MTQ_FWHM"] = 0.0;
0738       f_t[t_name + separator + "MTQ_FW20"] = 0.0;
0739       f_t[t_name + separator + "MTQ_FW80"] = 0.0;
0740       f_t[t_name + separator + "MTQ_SLIDING"] = 0.0;
0741 
0742       t_t[t_name]->Fill();
0743     }
0744 
0745     //
0746     // Laser Run
0747     //
0748     t_name = lmfLaserName(ME::iLmfLaserRun, _type);
0749     //std::cout << "Fill "<< t_name << std::endl;
0750     i_t[t_name + separator + "LOGIC_ID"] = logic_id_;
0751     i_t[t_name + separator + "NEVENTS"] = _events;
0752     i_t[t_name + separator + "QUALITY_FLAG"] = 1;  // fixme
0753     t_t[t_name]->Fill();
0754 
0755     //
0756     // Laser Config
0757     //
0758     t_name = lmfLaserName(ME::iLmfLaserConfig, _type);
0759     //std::cout << "Fill "<< t_name << std::endl;
0760     i_t[t_name + separator + "LOGIC_ID"] = logic_id_;
0761     i_t[t_name + separator + "WAVELENGTH"] = _color;
0762     i_t[t_name + separator + "VFE_GAIN"] = _mgpagain;      // fixme
0763     i_t[t_name + separator + "PN_GAIN"] = _memgain;        // fixme
0764     i_t[t_name + separator + "LSR_POWER"] = _power;        // will be available from MATACQ data
0765     i_t[t_name + separator + "LSR_ATTENUATOR"] = _filter;  // idem
0766     i_t[t_name + separator + "LSR_CURRENT"] = 0;           // idem
0767     i_t[t_name + separator + "LSR_DELAY_1"] = _delay;      // idem
0768     i_t[t_name + separator + "LSR_DELAY_2"] = 0;           // idem
0769     t_t[t_name]->Fill();
0770 
0771   } else if (_type == ME::iTestPulse) {
0772     //       nentries = tpapd_tree->GetEntriesFast();
0773     //       for( Long64_t jentry=0; jentry<nentries; jentry++ )
0774     //  {
0775     //    ientry = tpapd_tree->LoadTree( jentry );
0776     //    assert( ientry>=0 );
0777     //    nb     = tpapd_tree->GetEntry( jentry );
0778 
0779     //    if( tpapd_iphi<0 ) continue;
0780 
0781     //    const bool new_= true;
0782 
0783     //    int ix;
0784     //    int iy;
0785     //    int id2;
0786     //    if( new_ )
0787     //      {
0788     //        // for Cruzet3 , global coordinates
0789     //        if ( tpapd_side != _side ) continue;
0790     //        int ieta=tpapd_ieta;
0791     //        int iphi=tpapd_iphi;
0792     //        MEEBGeom::XYCoord xy_ = MEEBGeom::localCoord( ieta, iphi );
0793     //        ix = xy_.first;
0794     //        iy = xy_.second;
0795     //        id2 = ix*20 + iy;  // !!! TO BE CHECKED !!!
0796     //      }
0797     //    else
0798     //      {
0799     //        // for Cruzet2 , local coordinates
0800     //        ix = tpapd_ieta;
0801     //        iy = 19-tpapd_iphi;
0802     //        id2 = ix*20 + (20 - iy);  // !!! TO BE CHECKED !!!
0803     //      }
0804     //    //
0805 
0806     //    int id1 = _sm;   // fixme --- this is for barrel
0807     //    int logic_id_ = 1011000000;    // fixme
0808     //    logic_id_ += 10000*id1 + id2;
0809 
0810     //    int flag = tpapd_flag;
0811 
0812     //    setInt( "LOGIC_ID",           ix, iy,  logic_id_ );
0813     //    setInt( "FLAG",               ix, iy,  flag );
0814     //    setVal( "MEAN",               ix, iy,  tpapd_APD[iMean] );
0815     //    setVal( "RMS",                ix, iy,  tpapd_APD[iRMS] );
0816     //    setVal( "M3",                 ix, iy,  tpapd_APD[iM3] );
0817     //    setVal( "NEVT",               ix, iy,  tpapd_APD[iNevt] );
0818     //  }
0819 
0820     //       //
0821     //       // PN primitives
0822     //       //
0823     //       t_name = lmfLaserName( ME::iLmfTestPulsePnPrim, _type );
0824 
0825     //       nentries = tppn_tree->GetEntriesFast();
0826     //       assert( nentries%2==0 );
0827     //       int module_, pn_[2];
0828     //       int id1_(_sm), id2_(0);
0829     //       int logic_id_(0);
0830 
0831     //       Long64_t jentry=0;
0832     //       if( _side==1 ) jentry+=2;  // fixme : true also for endcaps?
0833     //       while( jentry<nentries )
0834     //  {
0835     //    for( int jj=0; jj<2; jj++ )
0836     //      {
0837     //        // jj=0 --> PNA
0838     //        // jj=1 --> PNB
0839 
0840     //        int zentry = jentry+jj;
0841     //        assert( zentry<nentries );
0842 
0843     //        ientry = tppn_tree->LoadTree( zentry );
0844     //        assert( ientry>=0 );
0845     //        nb     = tppn_tree->GetEntry( zentry );
0846 
0847     //        if( jj==1 ) assert( tppn_moduleID==module_ );
0848     //        module_ = tppn_moduleID;
0849     //        assert( tppn_pnID==jj );
0850 
0851     //        pn_[jj] = ( jj==0 ) ? _pn[module_].first : _pn[module_].second;
0852     //        id2_ = pn_[jj];
0853     //        logic_id_ = 1131000000 ;
0854     //        //      logic_id_ = 0;    // fixme
0855     //        logic_id_ += 10000*id1_ + id2_;
0856 
0857     //        i_t[t_name+separator+"LOGIC_ID"] = logic_id_;
0858     //        i_t[t_name+separator+"GAIN"]  = tppn_gain;
0859     //        f_t[t_name+separator+"MEAN"]  = tppn_PN[iMean];
0860     //        f_t[t_name+separator+"RMS"]   = tppn_PN[iRMS];
0861     //        f_t[t_name+separator+"M3"]    = tppn_PN[iM3];     // fixme --- peak?
0862 
0863     //        t_t[t_name]->Fill();
0864 
0865     //      }
0866 
0867     //    //      std::cout << "Module=" << module_ << "\tPNA=" << pn_[0] << "\tPNB=" << pn_[1] << std::endl;
0868 
0869     //    jentry += 4;
0870     //  }
0871 
0872     //       logic_id_ = 1041000000;
0873     //       logic_id_ += 10000*id1_;
0874     //       logic_id_ += id1_;
0875 
0876     //       //
0877     //       // Test Pulse Run
0878     //       //
0879     //       t_name = lmfLaserName( ME::iLmfTestPulseRun, _type );
0880     //       //std::cout << "Fill "<< t_name << std::endl;
0881     //       i_t[t_name+separator+"LOGIC_ID"]       = logic_id_;  // fixme --- is there a channelview for this?
0882     //       i_t[t_name+separator+"NEVENTS"]        =  _events;
0883     //       i_t[t_name+separator+"QUALITY_FLAG"]   = 1;                // fixme
0884     //       t_t[t_name]->Fill();
0885 
0886     //       //
0887     //       // Test Pulse Config
0888     //       //
0889     //       t_name = lmfLaserName( ME::iLmfTestPulseConfig, _type );
0890     //       //std::cout << "Fill "<< t_name << std::endl;
0891     //       i_t[t_name+separator+"LOGIC_ID"]        = logic_id_;  // fixme
0892     //       i_t[t_name+separator+"VFE_GAIN"]        = _mgpagain; // fixme
0893     //       i_t[t_name+separator+"PN_GAIN"]         = _memgain;  // fixme
0894     //       t_t[t_name]->Fill();
0895   }
0896 
0897   //
0898   // Laser Run IOV
0899   //
0900   t_name = "LMF_RUN_IOV";
0901   //std::cout << "Fill "<< t_name << std::endl;
0902   i_t[t_name + separator + "TAG_ID"] = 0;          // fixme
0903   i_t[t_name + separator + "SUB_RUN_NUM"] = _run;  // fixme
0904   i_t[t_name + separator + "SUB_RUN_START_LOW"] = ME::time_low(_ts_beg);
0905   i_t[t_name + separator + "SUB_RUN_START_HIGH"] = ME::time_high(_ts_beg);
0906   i_t[t_name + separator + "SUB_RUN_END_LOW"] = ME::time_low(_ts_end);
0907   i_t[t_name + separator + "SUB_RUN_END_HIGH"] = ME::time_high(_ts_end);
0908   i_t[t_name + separator + "DB_TIMESTAMP_LOW"] = ME::time_low(_ts);
0909   i_t[t_name + separator + "DB_TIMESTAMP_HIGH"] = ME::time_high(_ts);
0910   c_t[t_name + separator + "SUB_RUN_TYPE"] = "LASER TEST CRUZET";  //fixme
0911   t_t[t_name]->Fill();
0912 }
0913 
0914 void MELaserPrim::writeHistograms() {
0915   if (!init_ok)
0916     return;
0917 
0918   out_file = new TFile(_outfile, "RECREATE");
0919   //  out_file->cd();
0920 
0921   std::map<TString, TH2*>::iterator it;
0922 
0923   for (it = i_h.begin(); it != i_h.end(); ++it) {
0924     it->second->Write();
0925     delete it->second;
0926   }
0927 
0928   for (it = f_h.begin(); it != f_h.end(); ++it) {
0929     it->second->Write();
0930     delete it->second;
0931   }
0932 
0933   std::map<TString, TTree*>::iterator it_t;
0934   for (it_t = t_t.begin(); it_t != t_t.end(); ++it_t) {
0935     it_t->second->Write();
0936     delete it_t->second;
0937   }
0938 
0939   //  std::cout << "Closing " << _outfile << std::endl;
0940   out_file->Close();
0941   delete out_file;
0942   out_file = nullptr;
0943 }
0944 
0945 MELaserPrim::~MELaserPrim() {
0946   delete apdpn_tree;
0947   delete ab_tree;
0948   delete pn_tree;
0949   delete mtq_tree;
0950   delete tpapd_tree;
0951   delete tppn_tree;
0952   if (apdpn_file != nullptr) {
0953     //      std::cout << "Closing apdpn_file " << std::endl;
0954     apdpn_file->Close();
0955     delete apdpn_file;
0956     apdpn_file = nullptr;
0957   }
0958   if (ab_file != nullptr) {
0959     //      std::cout << "Closing ab_file " << std::endl;
0960     ab_file->Close();
0961     delete ab_file;
0962     ab_file = nullptr;
0963   }
0964   if (mtq_file != nullptr) {
0965     //      std::cout << "Closing mtq_file " << std::endl;
0966     mtq_file->Close();
0967     delete mtq_file;
0968     mtq_file = nullptr;
0969   }
0970   if (tpapd_file != nullptr) {
0971     //      std::cout << "Closing tpapd_file " << std::endl;
0972     tpapd_file->Close();
0973     delete tpapd_file;
0974     tpapd_file = nullptr;
0975   }
0976 }
0977 
0978 void MELaserPrim::print(std::ostream& o) {
0979   o << "DCC/SM/side/type/color/run/ts " << _dcc << "/" << _sm << "/" << _side << "/" << _type << "/" << _color << "/"
0980     << _run << "/" << _ts << std::endl;
0981 
0982   //   for( int ix=ixmin; ix<ixmax; ix++ )
0983   //     {
0984   //       for( int iy=iymin; iy<iymax; iy++ )
0985   //    {
0986   //      int flag     = getInt( _primStr+"FLAG", ix, iy );
0987   //      if( flag==0 ) continue;
0988   //      int logic_id = getInt( _primStr+"LOGIC_ID", ix, iy );
0989   //      float   apd = getVal( _primStr+"MEAN", ix, iy );
0990   //      o << "Crystal ieta=" << ix << "\tiphi=" << iy << "\tlogic_id=" << logic_id << "\tAPD=" << apd <<  endl;
0991   //    }
0992   //     }
0993 }
0994 
0995 TString MELaserPrim::lmfLaserName(int table, int type, int color) {
0996   TString str("LMF_ERROR");
0997   if (table < 0 || table >= ME::iSizeLmf)
0998     return str;
0999   if (color < 0 || color >= ME::iSizeC)
1000     return str;
1001 
1002   if (type == ME::iLaser) {
1003     TString colstr;
1004     switch (color) {
1005       case ME::iBlue:
1006         colstr = "_BLUE";
1007         break;
1008       case ME::iGreen:
1009         colstr = "_GREEN";
1010         break;
1011       case ME::iRed:
1012         colstr = "_RED";
1013         break;
1014       case ME::iIRed:
1015         colstr = "_IRED";
1016         break;
1017       default:
1018         abort();
1019     }
1020     str = "LMF_LASER";
1021     switch (table) {
1022       case ME::iLmfLaserRun:
1023         str = "LMF_RUN";
1024         break;
1025       case ME::iLmfLaserConfig:
1026         str += "_CONFIG";
1027         break;
1028       case ME::iLmfLaserPulse:
1029         str += colstr;
1030         str += "_PULSE";
1031         break;
1032       case ME::iLmfLaserPrim:
1033         str += colstr;
1034         str += "_PRIM";
1035         break;
1036       case ME::iLmfLaserPnPrim:
1037         str += colstr;
1038         str += "_PN_PRIM";
1039         break;
1040       default:
1041         abort();
1042     }
1043   } else if (type == ME::iTestPulse) {
1044     str = "LMF_TEST_PULSE";
1045     switch (table) {
1046       case ME::iLmfTestPulseRun:
1047         str = "LMF_RUN";
1048         break;
1049       case ME::iLmfTestPulseConfig:
1050         str += "_CONFIG";
1051         break;
1052       case ME::iLmfTestPulsePrim:
1053         str += "_PRIM";
1054         break;
1055       case ME::iLmfTestPulsePnPrim:
1056         str += "_PN_PRIM";
1057         break;
1058       default:
1059         abort();
1060     }
1061   }
1062   str += "_DAT";
1063   return str;
1064 }
1065 
1066 void MELaserPrim::addBranchI(const char* t_name_, const char* v_name_) {
1067   TString slashI("/i");  // Warning: always unsigned
1068   TString t_name(t_name_);
1069   TString v_name(v_name_);
1070   if (t_t.count(t_name) == 0)
1071     t_t[t_name] = new TTree(t_name, t_name);
1072   t_t[t_name]->Branch(v_name, &i_t[t_name + separator + v_name], v_name + slashI);
1073 }
1074 
1075 void MELaserPrim::addBranchF(const char* t_name_, const char* v_name_) {
1076   TString slashF("/F");
1077   TString t_name(t_name_);
1078   TString v_name(v_name_);
1079   if (t_t.count(t_name) == 0)
1080     t_t[t_name] = new TTree(t_name, t_name);
1081   t_t[t_name]->Branch(v_name, &f_t[t_name + separator + v_name], v_name + slashF);
1082 }
1083 
1084 void MELaserPrim::addBranchC(const char* t_name_, const char* v_name_) {
1085   TString slashC("/C");
1086   TString t_name(t_name_);
1087   TString v_name(v_name_);
1088   if (t_t.count(t_name) == 0)
1089     t_t[t_name] = new TTree(t_name, t_name);
1090   t_t[t_name]->Branch(v_name, &c_t[t_name + separator + v_name], v_name + slashC);
1091 }
1092 
1093 void MELaserPrim::bookHistoI(const char* h_name_, const char* v_name_) {
1094   TString i_name = TString(h_name_) + TString(v_name_);
1095   TH2* h_ = new TH2I(i_name, i_name, nx, ixmin, ixmax, ny, iymin, iymax);
1096   setHistoStyle(h_);
1097   i_h[i_name] = h_;
1098 }
1099 
1100 void MELaserPrim::bookHistoF(const char* h_name_, const char* v_name_) {
1101   TString d_name = TString(h_name_) + TString(v_name_);
1102   TH2* h_ = new TH2F(d_name, d_name, nx, ixmin, ixmax, ny, iymin, iymax);
1103   setHistoStyle(h_);
1104   f_h[d_name] = h_;
1105 }
1106 
1107 bool MELaserPrim::setInt(const char* name, int ix, int iy, int ival) {
1108   TString name_;
1109   if (_type == ME::iLaser)
1110     name_ = _primStr + name;
1111   else if (_type == ME::iTestPulse)
1112     name_ = _tpPrimStr + name;
1113 
1114   int _ival = getInt(name_, ix, iy);
1115   assert(_ival != -99);
1116   if (_ival != 0)
1117     return false;
1118 
1119   TH2I* h_ = (TH2I*)i_h[name_];
1120   assert(h_ != nullptr);
1121   h_->Fill(ix + 0.5, iy + 0.5, ival);
1122 
1123   return true;
1124 }
1125 
1126 bool MELaserPrim::setVal(const char* name, int ix, int iy, float val) {
1127   TString name_;
1128   if (_type == ME::iLaser)
1129     name_ = _primStr + name;
1130   else if (_type == ME::iTestPulse)
1131     name_ = _tpPrimStr + name;
1132 
1133   float _val = getVal(name_, ix, iy);
1134   assert(_val != -99);
1135   if (_val != 0)
1136     return false;
1137 
1138   TH2F* h_ = (TH2F*)f_h[name_];
1139   assert(h_ != nullptr);
1140 
1141   h_->Fill(ix + 0.5, iy + 0.5, val);
1142 
1143   return true;
1144 }
1145 
1146 Int_t MELaserPrim::getInt(const char* name, int ix, int iy) {
1147   Int_t ival = -99;
1148   if (i_h.count(name) == 1) {
1149     TH2I* h_ = (TH2I*)i_h[name];
1150     assert(h_ != nullptr);
1151     int binx = h_->GetXaxis()->FindBin(ix + 0.5);
1152     int biny = h_->GetYaxis()->FindBin(iy + 0.5);
1153     ival = (Int_t)h_->GetCellContent(binx, biny);
1154   }
1155   return ival;
1156 }
1157 
1158 Float_t MELaserPrim::getVal(const char* name, int ix, int iy) {
1159   Float_t val = -99.;
1160   if (f_h.count(name) == 1) {
1161     TH2F* h_ = (TH2F*)f_h[name];
1162     assert(h_ != nullptr);
1163     int binx = h_->GetXaxis()->FindBin(ix + 0.5);
1164     int biny = h_->GetYaxis()->FindBin(iy + 0.5);
1165     val = h_->GetCellContent(binx, biny);
1166   }
1167   return val;
1168 }
1169 
1170 bool MELaserPrim::setInt(const char* tname, const char* vname, int ival) {
1171   TString key_(tname);
1172   key_ += separator;
1173   key_ += vname;
1174   assert(i_t.count(key_) == 1);
1175   i_t[key_] = ival;
1176   return true;
1177 }
1178 
1179 bool MELaserPrim::setVal(const char* tname, const char* vname, float val) {
1180   TString key_(tname);
1181   key_ += separator;
1182   key_ += vname;
1183 
1184   // ghm
1185   if (f_t.count(key_) != 1) {
1186     std::cout << key_ << std::endl;
1187   }
1188   assert(f_t.count(key_) == 1);
1189   f_t[key_] = val;
1190   return true;
1191 }
1192 
1193 bool MELaserPrim::fill(const char* tname) {
1194   TString key_(tname);
1195   assert(t_t.count(key_) == 1);
1196   t_t[key_]->Fill();
1197   return true;
1198 }
1199 
1200 void MELaserPrim::setHistoStyle(TH1* h) {
1201   if (h == nullptr)
1202     return;
1203 
1204   float _scale = 1;
1205 
1206   h->SetLineColor(4);
1207   h->SetLineWidth(1);
1208   h->SetFillColor(38);
1209   TAxis* axis[3];
1210   axis[0] = h->GetXaxis();
1211   axis[1] = h->GetYaxis();
1212   axis[2] = h->GetZaxis();
1213   for (int ii = 0; ii < 3; ii++) {
1214     TAxis* a = axis[ii];
1215     if (!a)
1216       continue;
1217     a->SetLabelFont(132);
1218     a->SetLabelOffset(_scale * 0.005);
1219     a->SetLabelSize(_scale * 0.04);
1220     a->SetTitleFont(132);
1221     a->SetTitleOffset(_scale * 1);
1222     a->SetTitleSize(_scale * 0.04);
1223   }
1224   h->SetStats(kTRUE);
1225 }
1226 
1227 void MELaserPrim::refresh() {
1228   std::map<TString, TH2*>::iterator it;
1229 
1230   for (it = i_h.begin(); it != i_h.end(); ++it) {
1231     delete it->second;
1232     it->second = nullptr;
1233   }
1234   i_h.clear();
1235 
1236   for (it = f_h.begin(); it != f_h.end(); ++it) {
1237     delete it->second;
1238     it->second = nullptr;
1239   }
1240   f_h.clear();
1241 
1242   std::map<TString, TTree*>::iterator it_t;
1243   for (it_t = t_t.begin(); it_t != t_t.end(); ++it_t) {
1244     delete it_t->second;
1245     it->second = nullptr;
1246   }
1247   t_t.clear();
1248 }