Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:50

0001 // This macro can be used after the CheckAllIOVs.py to extract from the summaries
0002 // the number of modules with LV/HV on or off as a function of the IOV.
0003 
0004 
0005 #include <iostream>
0006 #include <fstream>
0007 #include <sstream>
0008 #include <string>
0009 #include <algorithm>
0010 #include <iterator>
0011 
0012 #include "TString.h"
0013 #include "TCanvas.h"
0014 #include "TH1F.h"
0015 #include "TLegend.h"
0016 #include "TGraph.h"
0017 #include "TVectorD.h"
0018 #include "TDatime.h"
0019 #include "TFile.h"
0020 #include "TStyle.h"
0021 #include "TROOT.h"
0022 
0023 using namespace std;
0024 
0025 void Tokenize(const string& str,
0026           vector<string>& tokens,
0027           const string& delimiters = " ")
0028 {
0029   // Skip delimiters at beginning.
0030   string::size_type lastPos = str.find_first_not_of(delimiters, 0);
0031   // Find first "non-delimiter".
0032   string::size_type pos     = str.find_first_of(delimiters, lastPos);
0033   
0034   while (string::npos != pos || string::npos != lastPos) {
0035     // Found a token, add it to the vector.
0036     tokens.push_back(str.substr(lastPos, pos - lastPos));
0037     // Skip delimiters.  Note the "not_of"
0038     lastPos = str.find_first_not_of(delimiters, pos);
0039     // Find next "non-delimiter"
0040     pos = str.find_first_of(delimiters, lastPos);
0041   }
0042 }
0043 
0044 // Function to tokenize a string
0045 vector<string> tokenize(const string & line)
0046 {
0047   stringstream ss(line);
0048   vector<string> tokenized;
0049   while(ss) {
0050     string tok;
0051     ss >> tok;
0052     tokenized.push_back(tok);
0053   }
0054   return tokenized;
0055 }
0056 
0057 double * duplicateForGraph(const unsigned int size, const Float_t * summedValues)
0058 {
0059   double * summedValuesArray = new double[size*2-1];
0060   for( unsigned int i=0; i<size; ++i ) {
0061     summedValuesArray[2*i] = summedValues[i];
0062     // cout << "summedValuesArray["<<2*i<<"] = " << summedValuesArray[2*i] << endl;
0063     if( i != size-1 ) {
0064       summedValuesArray[2*i+1] = summedValues[i];
0065     }
0066   }
0067   return summedValuesArray;
0068 }
0069 
0070 struct Holder
0071 {
0072   Holder() :
0073     layer( new vector<int>(20, 0) ),
0074     iov(0)
0075   {}
0076   // void add(const int layerNum, const int side, const int modulesOff, const double & inputIOV)
0077   void add(const int layerNum, const int side, const int modulesOff, const double & timeInSeconds)
0078   {
0079     (*layer)[layerNum+side*10] = modulesOff;
0080     iov = timeInSeconds;
0081   }
0082   int modules(const int layerNum, const int side)
0083   {
0084     if( layerNum+side*10 < int(layer->size()) ) {
0085       return (*layer)[layerNum+side*10];
0086     }
0087     else {
0088       cout << "ERROR: layerNum+side*10 = " << layerNum+side*10 << " bigger than number of layers = " << layer->size() << endl;
0089       cout << "Returning 0" << endl;
0090     }
0091     return 0;
0092   }
0093   vector<int> * layer;
0094   double iov;
0095 };
0096 
0097 
0098 struct HistoHolder
0099 {
0100   HistoHolder(const TString & subDet, const int IOVs)
0101   {
0102     layer = new vector<TH1F*>;
0103     fillLayers(subDet, IOVs);
0104     fillLayers(subDet, IOVs, "stereo");
0105   }
0106 
0107   void fillLayers(const TString & subDet, const int IOVs, const TString & addToName = "")
0108   {
0109     for( int i=0; i<10; ++i ) {
0110       stringstream ss;
0111       ss << i;
0112       layer->push_back(new TH1F(subDet+ss.str()+addToName, subDet+ss.str()+addToName, IOVs, 0, IOVs));
0113       timeVector.push_back(new vector<double>);
0114       valueVector.push_back(new vector<double>);
0115     }
0116   }
0117 
0118   void SetBinContent(const int IOV, const int layerNum, const int side, const int modulesOff, const double & time)
0119   {
0120     // Skip the zero time case
0121     // cout << "IOV = " << IOV << ", layerNum = " << layerNum << ", side = " << side << ", modulesOff = " << modulesOff << ", time = " << time << endl;
0122     // if( time == 0 ) cout << "TIME == 0" << endl;
0123 
0124     (*layer)[layerNum+side*10]->SetBinContent(IOV, modulesOff);
0125     timeVector[layerNum+side*10]->push_back( time );
0126     valueVector[layerNum+side*10]->push_back( modulesOff );
0127   }
0128 
0129   TH1F* histo(const int layerNum, const int side)
0130   {
0131     return (*layer)[layerNum+side*10];
0132   }
0133 
0134   void removeZeros(const int layerNum, const int side)
0135   {
0136     // Remove all the times == 0
0137     vector<double>::iterator it = find(timeVector[layerNum+10*side]->begin(), timeVector[layerNum+10*side]->end(), 0);
0138     while( it != timeVector[layerNum+10*side]->end() ) {
0139       timeVector[layerNum+10*side]->erase(it);
0140       valueVector[layerNum+10*side]->erase(valueVector[layerNum+10*side]->begin()+distance(timeVector[layerNum+10*side]->begin(), it));
0141       it = find(timeVector[layerNum+10*side]->begin(), timeVector[layerNum+10*side]->end(), 0);
0142     }
0143   }
0144 
0145   double * time(const int layerNum, const int side)
0146   {
0147     // Take twice the values. We propagate the previous point to the next time,
0148     // so that the graph will display in a way similar to a TH1F (but with the correct spacing
0149     // between times).
0150     unsigned int size = timeVector[layerNum+side*10]->size();
0151     double * timeV = new double[2*size-1];
0152     for( unsigned int i=0; i<size; ++i ) {
0153       timeV[2*i] = (*(timeVector[layerNum+side*10]))[i];
0154       // Put the next time, which will correspond to the value of the current time
0155       if( i != size-1 ) {
0156     timeV[2*i+1] = (*(timeVector[layerNum+side*10]))[i+1];
0157       }
0158     }
0159     return timeV;
0160   }
0161 
0162   double * value(const int layerNum, const int side)
0163   {
0164     unsigned int size = valueVector[layerNum+side*10]->size();
0165     double * valueV = new double[2*size-1];
0166     for( unsigned int i=0; i<size; ++i ) {
0167       valueV[2*i] = (*(valueVector[layerNum+side*10]))[i];
0168 
0169       // Put the same value, which will correspond to the next time
0170       if( i != size-1 ) {
0171     valueV[2*i+1] = (*(valueVector[layerNum+side*10]))[i];
0172       }
0173     }
0174     return valueV;
0175   }
0176 
0177   unsigned int getSize(const int layerNum, const int side)
0178   {
0179     return 2*(timeVector[layerNum+side*10]->size())-1;
0180   }
0181 
0182   vector<TH1F*> * layer;
0183   vector<vector<double> *> timeVector;
0184   vector<vector<double> *> valueVector;
0185 };
0186 
0187 void drawHistoTracker(TH1F* histo, const TString option, const unsigned int color, vector<vector<HistoHolder> > & histos)
0188 {
0189   // +1 because the array returned by the histogram starts from the underflow bin
0190   Float_t * summedValues = histo->GetArray()+1;
0191   unsigned int size = histo->GetNbinsX();
0192   double * summedValuesArray = duplicateForGraph(size, summedValues);
0193   TGraph * graph = new TGraph(histos[0][0].getSize(1, 0), histos[0][0].time(1, 0), summedValuesArray);
0194   graph->Draw(option);
0195   graph->SetLineColor(color);
0196   graph->GetXaxis()->SetTimeDisplay(1);
0197   graph->GetXaxis()->SetLabelOffset(0.02);
0198   graph->GetXaxis()->SetTimeFormat("#splitline{  %d}{%H:%M}");
0199   graph->GetXaxis()->SetTimeOffset(0,"gmt");
0200   graph->GetYaxis()->SetRangeUser(0,16000);
0201   graph->GetXaxis()->SetTitle("day/hour");
0202   graph->GetXaxis()->SetTitleSize(0.03);
0203   graph->GetXaxis()->SetTitleColor(kBlack);
0204   graph->GetXaxis()->SetTitleOffset(1.80);
0205   graph->GetYaxis()->SetTitle("number of modules off");
0206   graph->GetYaxis()->SetTitleSize(0.03);
0207   graph->GetYaxis()->SetTitleColor(kBlack);
0208   graph->GetYaxis()->SetTitleOffset(1.80);
0209   graph->SetTitle();
0210 }
0211 
0212 vector<vector<Holder> > extractFromFile( const string & fileName, const string & date )
0213 {
0214   ifstream inputFile(fileName.c_str());
0215 
0216   vector<string> tokens;
0217   Tokenize(date, tokens, "_");
0218 
0219   unsigned int day = 0;
0220   stringstream sDay(tokens[2]); // day
0221   sDay >> day;
0222   unsigned int hour = 0;
0223   stringstream sHour(tokens[3]); // hour
0224   sHour >> hour;
0225   unsigned int minute = 0;
0226   stringstream sMinute(tokens[4]); // minute
0227   sMinute >> minute;
0228   unsigned int second = 0;
0229   stringstream sSecond(tokens[5]); // second
0230   sSecond >> second;
0231   unsigned int year = 0;
0232   stringstream sYear(tokens[6]); // year
0233   sYear >> year;
0234 
0235   // Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
0236   std::map<string, unsigned int> monthsToNumbers;
0237   monthsToNumbers.insert(make_pair("Jan", 1));
0238   monthsToNumbers["Feb"] = 2;
0239   monthsToNumbers["Mar"] = 3;
0240   monthsToNumbers["Apr"] = 4;
0241   monthsToNumbers["May"] = 5;
0242   monthsToNumbers["Jun"] = 6;
0243   monthsToNumbers["Jul"] = 7;
0244   monthsToNumbers["Aug"] = 8;
0245   monthsToNumbers["Sep"] = 9;
0246   monthsToNumbers["Oct"] = 10;
0247   monthsToNumbers["Nov"] = 11;
0248   monthsToNumbers["Dec"] = 12;
0249 
0250   std::map<string, unsigned int>::iterator month = monthsToNumbers.find(tokens[1]);
0251   TDatime date1(year, month->second, day, hour, minute, second);
0252 
0253   double timeInSeconds = date1.Convert();
0254 
0255   vector<vector<Holder> > holder;
0256   // HV/LV = 2 cases
0257   for( int i=0; i<2; ++i ) {
0258     holder.push_back(vector<Holder>());
0259     // 4 possible subdetectors
0260     for( int j=0; j<4; ++j ) {
0261       holder[i].push_back(Holder());
0262     }
0263   }
0264 
0265   int HVLV = 0;
0266   string line;
0267 
0268   bool start = false;
0269 
0270   string subDet = "";
0271   while( getline(inputFile, line) ) {
0272 
0273     // Skip empty lines
0274     if( line == "" ) continue;
0275 
0276     if( line.find("subDet") != string::npos ) {
0277       start = true;
0278     }
0279     // Skip the rest until you find the starting line
0280     if( start == false ) continue;
0281 
0282     if( line.find("Summary") != string::npos ) {
0283       // Skip also the next two lines
0284       getline(inputFile, line);
0285       getline(inputFile, line);
0286       ++HVLV;
0287     }
0288     else if( line.find("%MSG") != string::npos || line.find("DummyCondObjPrinter") != string::npos ) continue;
0289 
0290     vector<string> tokenized(tokenize(line));
0291     if( !tokenized.empty() && tokenized[0] != "" ) {
0292       int index = 0;
0293       if( tokenized.size() == 5 ) {
0294     subDet = tokenized[0];
0295     ++index;
0296       }
0297 
0298       // Extract the relevant quantities
0299       stringstream ss1( tokenized[index] );
0300       int layerNum = 0;
0301       ss1 >> layerNum;
0302       stringstream ss2( tokenized[index+1] );
0303       int side = 0;
0304       ss2 >> side;
0305       stringstream ss3( tokenized[index+2] );
0306       int modulesOff = 0;
0307       ss3 >> modulesOff;
0308 
0309       if( subDet == "TIB" )      holder[HVLV][0].add( layerNum, side, modulesOff, timeInSeconds );
0310       else if( subDet == "TID" ) holder[HVLV][1].add( layerNum, side, modulesOff, timeInSeconds );
0311       else if( subDet == "TOB" ) holder[HVLV][2].add( layerNum, side, modulesOff, timeInSeconds );
0312       else if( subDet == "TEC" ) holder[HVLV][3].add( layerNum, side, modulesOff, timeInSeconds );
0313 
0314     }
0315   }
0316 
0317   return holder;
0318 }
0319 
0320 // Small function used to fill the histograms for the different layers of the different subDetectors
0321 void fillHistos( vector<vector<Holder> > & it, vector<vector<HistoHolder> > & histos, const int firstLayer, const int totLayers,
0322          const int doubleSidedLayers, const int HVLVid, const int subDetId, const int iov )
0323 {
0324   for( int layerNum = firstLayer; layerNum <= totLayers; ++layerNum ) {
0325     histos[HVLVid][subDetId].SetBinContent( iov, layerNum, 0, it[HVLVid][subDetId].modules(layerNum, 0), it[HVLVid][subDetId].iov );
0326     if( layerNum <= doubleSidedLayers ) {
0327       histos[HVLVid][subDetId].SetBinContent( iov, layerNum, 1, it[HVLVid][subDetId].modules(layerNum, 1), it[HVLVid][subDetId].iov );
0328     }
0329   }
0330 }
0331 
0332 // Small function used to draw the histograms for the different layers of the different subDetectors
0333 void drawHistos( TCanvas ** canvas, vector<vector<HistoHolder> > & histos, TH1F ** histoTracker, const int firstLayer, const int totLayers,
0334          const int doubleSidedLayers, const int HVLVid, const int subDetId )
0335 {
0336   TString option("AL");
0337   int lineColor = 2;
0338   if( HVLVid == 1 ) {
0339     // option = "SAME";
0340     // No SAME option for the TGraph (it contains the A, and it would mean a different thing)
0341     // They will always be drawn as if SAME is selected.
0342     option = "L";
0343     lineColor = 1;
0344   }
0345   for( int layerNum = firstLayer; layerNum <= totLayers; ++layerNum ) {
0346     // First of all remove all the times = 0
0347     histos[HVLVid][subDetId].removeZeros(layerNum, 0);
0348     canvas[subDetId]->cd(layerNum);
0349     TH1F * histo = histos[HVLVid][subDetId].histo( layerNum, 0 );
0350 
0351     // cout << "[0] = " << histo->GetArray()[0] << ", [1] = " << histo->GetArray()[1] << ",[2] = " << histo->GetArray()[2] << endl;
0352 
0353     TGraph * graph = new TGraph(histos[HVLVid][subDetId].getSize(layerNum, 0), histos[HVLVid][subDetId].time(layerNum, 0), histos[HVLVid][subDetId].value(layerNum, 0));
0354     graph->SetTitle(histo->GetTitle());
0355     graph->SetLineColor(lineColor);
0356     graph->Draw(option);
0357     graph->SetMarkerColor(lineColor);
0358     graph->GetXaxis()->SetTimeDisplay(1); 
0359     graph->GetXaxis()->SetTimeFormat("#splitline{  %d}{%H:%M}");
0360     graph->GetXaxis()->SetLabelOffset(0.02);
0361     graph->GetXaxis()->SetTimeOffset(0,"gmt");
0362     histoTracker[HVLVid]->Add( histo );
0363 
0364     if( layerNum <= doubleSidedLayers ) {
0365       histos[HVLVid][subDetId].removeZeros(layerNum, 1);
0366       canvas[subDetId]->cd(totLayers+layerNum);
0367       histo = histos[HVLVid][subDetId].histo( layerNum, 1 );
0368       TGraph * graphStereo = new TGraph(histos[HVLVid][subDetId].getSize(layerNum, 1), histos[HVLVid][subDetId].time(layerNum, 1), histos[HVLVid][subDetId].value(layerNum, 1));
0369       graphStereo->SetTitle(histo->GetTitle());
0370       graphStereo->SetLineColor(lineColor);
0371       graphStereo->SetMarkerColor(lineColor);
0372       graphStereo->Draw(option);
0373 
0374       histoTracker[HVLVid]->Add( histo );
0375     }
0376   }
0377 }
0378 
0379 void clearEmptyFiles(vector<vector<vector<Holder> > > & holderVsIOV)
0380 {
0381   for( vector<vector<vector<Holder> > >::iterator it1 = holderVsIOV.begin(); it1 != holderVsIOV.end(); ++it1 ) {
0382     if( (*it1)[0][0].iov == 0 ) {
0383       cout << "Removing iov = 0" << endl;
0384       it1 = holderVsIOV.erase(it1);
0385     }
0386   }
0387 }
0388 
0389 void ExtractTrends()
0390 {
0391   gROOT->SetStyle("Plain");
0392   gStyle->SetCanvasColor(kWhite);
0393   gStyle->SetCanvasBorderMode(0);
0394   gStyle->SetPadBorderMode(0);
0395   gStyle->SetTitleFillColor(kWhite);
0396   gStyle->SetTitleColor(kWhite);
0397 
0398   TFile * outputFile = new TFile("trends.root", "RECREATE");
0399 
0400   ifstream listFile("list.txt");
0401   string fileName;
0402 
0403   vector<vector<vector<Holder> > > holderVsIOV;
0404 
0405   while( getline(listFile, fileName) ) {
0406 
0407     size_t first = fileName.find("__FROM");
0408     size_t last = fileName.find("_TO");
0409     string subString(fileName.substr(first+7, last-(first+7)));
0410     holderVsIOV.push_back(extractFromFile(fileName, subString));
0411   }
0412 
0413   // Clear the residuals from empty files
0414   clearEmptyFiles(holderVsIOV);
0415 
0416   // Create histograms for each subDet and layer and fill them
0417   vector<vector<HistoHolder> > histos;
0418   for( int i=0; i<2; ++i ) {
0419     histos.push_back(vector<HistoHolder>());
0420     string HVLVstring;
0421     if( i == 0 ) HVLVstring = "HV";
0422     else HVLVstring = "LV";
0423     histos[i].push_back(HistoHolder("TIB_"+HVLVstring, holderVsIOV.size()));
0424     histos[i].push_back(HistoHolder("TID_"+HVLVstring, holderVsIOV.size()));
0425     histos[i].push_back(HistoHolder("TOB_"+HVLVstring, holderVsIOV.size()));
0426     histos[i].push_back(HistoHolder("TEC_"+HVLVstring, holderVsIOV.size()));
0427   }
0428 
0429   vector<vector<vector<Holder> > >::iterator it = holderVsIOV.begin();
0430   int iov = 1;
0431   for( ; it != holderVsIOV.end(); ++it, ++iov ) {
0432 
0433     // HV status
0434     // ---------
0435     for( int HVLVid = 0; HVLVid < 2; ++HVLVid ) {
0436 
0437       // par:     holder, histos, firstLayer, totLayers, doubleSidedLayers, HVLVid, subDetId, iov
0438       fillHistos( *it,    histos,          1,         4,                 2, HVLVid,        0, iov ); // TIB
0439       fillHistos( *it,    histos,          1,         3,                 3, HVLVid,        1, iov ); // TID
0440       fillHistos( *it,    histos,          1,         6,                 2, HVLVid,        2, iov ); // TOB
0441       fillHistos( *it,    histos,          1,         9,                 9, HVLVid,        3, iov ); // TEC
0442     }
0443   }
0444 
0445   TCanvas *allCanvas[2];
0446   allCanvas[0] = new TCanvas("Tracker HV status", "HVstatus", 1000, 800);
0447   allCanvas[1] = new TCanvas("Tracker LV status", "LVstatus", 1000, 800);
0448   TH1F *histoTracker[2];
0449   histoTracker[0] = new TH1F("Tracker status HV", "TrackerHVstatus", holderVsIOV.size(), 0, holderVsIOV.size());
0450   histoTracker[1] = new TH1F("Tracker status LV", "TrackerLVstatus", holderVsIOV.size(), 0, holderVsIOV.size());
0451 
0452   // Loop again on the HVLV and draw the histograms
0453 
0454   TCanvas *canvas[4];
0455   canvas[0] = new TCanvas("TIB HV status", "HVstatus", 1000, 800);
0456   canvas[1] = new TCanvas("TID HV status", "HVstatus", 1000, 800);
0457   canvas[2] = new TCanvas("TOB HV status", "HVstatus", 1000, 800);
0458   canvas[3] = new TCanvas("TEC HV status", "HVstatus", 1000, 800);
0459 
0460   canvas[0]->Divide(4,2);
0461   canvas[1]->Divide(3,2);
0462   canvas[2]->Divide(6,2);
0463   canvas[3]->Divide(9,2);
0464 
0465   for( int HVLVid = 0; HVLVid < 2; ++HVLVid ) {
0466 
0467     // par:     canvas, histos, histoTracker, firstLayer, totLayers, doubleSidedLayers, HVLVid, subDetId, iov
0468     drawHistos( canvas, histos, histoTracker,          1,         4,                 2, HVLVid,        0 ); // TIB
0469     drawHistos( canvas, histos, histoTracker,          1,         3,                 3, HVLVid,        1 ); // TID
0470     drawHistos( canvas, histos, histoTracker,          1,         6,                 2, HVLVid,        2 ); // TOB
0471     drawHistos( canvas, histos, histoTracker,          1,         9,                 9, HVLVid,        3 ); // TEC
0472 
0473     outputFile->cd();
0474     canvas[0]->Draw();
0475     canvas[1]->Draw();
0476     canvas[2]->Draw();
0477     canvas[3]->Draw();
0478     canvas[0]->Write();
0479     canvas[1]->Write();
0480     canvas[2]->Write();
0481     canvas[3]->Write();
0482   }
0483   allCanvas[0]->cd();
0484 
0485   histoTracker[0]->SetLineColor(2);
0486 
0487   drawHistoTracker( histoTracker[1], "AL", 1, histos);
0488   drawHistoTracker( histoTracker[0], "L", 2, histos);
0489 
0490   TLegend * legend2 = new TLegend(0.715,0.87,0.98,1,NULL,"brNDC");
0491   legend2->SetTextSize(0.035);
0492   legend2->SetFillColor(0); // Have a white background
0493   legend2->AddEntry(histoTracker[0], "High Voltage off");
0494   legend2->AddEntry(histoTracker[1], "Low Voltage off");
0495   legend2->Draw("SAME");
0496 
0497   allCanvas[0]->Draw();
0498   allCanvas[0]->Write();
0499 
0500   outputFile->Write();
0501   outputFile->Close();
0502 }