Coverity fix for uninitialized variables and check for returned null value
[u/mrichter/AliRoot.git] / MUON / MUONStatusMap.C
index 3797db6..0b19c04 100644 (file)
 
 /* $Id$ */
 
-/// Macro to check/test pad status and pad status map makers
-// Laurent Aphecetche
+/// \ingroup macros
+/// \file MUONStatusMap.C
+/// \brief Macro to check/test pad status and pad status map makers
+///
+/// \author Laurent Aphecetche
 
 #if !defined(__CINT__) || defined(__MAKECINT__)
 #include "AliCDBManager.h"
+#include "AliCDBEntry.h"
+#include "AliLog.h"
+#include "AliMpCDB.h"
+#include "AliMpDEManager.h"
+#include "AliMUONCDB.h"
 #include "AliMUONCalibrationData.h"
-#include "AliMUONObjectPair.h"
 #include "AliMUONPadStatusMaker.h"
 #include "AliMUONPadStatusMapMaker.h"
-#include "AliMUONV2DStore.h"
+#include "AliMUONRecoParam.h"
 #include "AliMUONVCalibParam.h"
-#include "AliMUONVDataIterator.h"
-#include "AliMpIntPair.h"
+#include "AliMUONVStore.h"
+#include "AliMpConstants.h"
+#include "AliMpDDLStore.h"
+#include "AliMpDetElement.h"
+#include "AliMpManuIterator.h"
 #include "Riostream.h"
+#include "TAxis.h"
+#include "TCanvas.h"
+#include "TLegend.h"
+#include "TFile.h"
+#include "TGraph.h"
+#include "TBox.h"
+#include "TH2F.h"
+#include "TStyle.h"
+#include "TText.h"
+#include <vector>
 #endif
 
-void findBad(const AliMUONV2DStore& status)
+namespace
 {
-  AliMUONVDataIterator* it = status.Iterator();
-  AliMUONObjectPair* pair;
+  Int_t NTOTALNUMBEROFPADS(1064008);
+}
+
+//______________________________________________________________________________
+void ReadIntegers(const char* filename, std::vector<int>& integers)
+{
+  /// Read integers from filename, where integers are either
+  /// separated by "," or by return carriage
+  ifstream in(gSystem->ExpandPathName(filename));
+  int i;
+  
+  char line[10000];
   
-  while ( ( pair = static_cast<AliMUONObjectPair*>(it->Next()) ) )
+  in.getline(line,10000,'\n');
+  
+  TString sline(line);
+  
+  if (sline.Contains(","))
   {
-    AliMpIntPair* p = static_cast<AliMpIntPair*>(pair->First());
-    Int_t detElemId = p->GetFirst();
-    Int_t manuId = p->GetSecond();
-    AliMUONVCalibParam* param = static_cast<AliMUONVCalibParam*>(pair->Second());
-    Bool_t bad(kFALSE);
-    for ( Int_t i = 0; i < param->Size(); ++i ) 
+    TObjArray* a = sline.Tokenize(",");
+    TIter next(a);
+    TObjString* s;
+    while ( ( s = static_cast<TObjString*>(next()) ) )
     {
-      if ( param->ValueAsInt(0) ) bad = kTRUE;
+      integers.push_back(s->String().Atoi());
     }
-    if (bad)
+  }
+  else
+  {
+    integers.push_back(sline.Atoi());
+    
+    while ( in >> i )
     {
-      cout << Form("DE %4d ManuId %4d",detElemId,manuId) << endl;
+      integers.push_back(i);
     }
-    if (it->IsOwner()) delete pair;
   }
+  
+  std::sort(integers.begin(),integers.end());
 }
 
-AliMUONV2DStore* MUONStatusMap(Int_t runNumber=0, Bool_t statusOnly=kFALSE, Int_t mask=0)
-{
-  AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT/SHUTTLE/TestShuttle/TestCDB");
+
+//______________________________________________________________________________
+void MUONStatusMap(AliMUONVStore*& vstatus,
+                   AliMUONVStore*& vstatusMap,
+                   const char* cdbStorage = "alien://folder=/alice/data/2011/OCDB",
+                   Int_t runNumber=145292)
+{  
+
+  AliCDBManager::Instance()->SetDefaultStorage(cdbStorage);
+  AliCDBManager::Instance()->SetRun(runNumber);
+  
+  AliMUONCDB::LoadMapping();
+  
+  AliMUONRecoParam* recoParam = AliMUONCDB::LoadRecoParam();
   
   AliMUONCalibrationData cd(runNumber);
   
   AliMUONPadStatusMaker statusMaker(cd);
   
-//  statusMaker.SetPedMeanLimits(50,200);
-  statusMaker.SetPedSigmaLimits(0.5,2);
+  statusMaker.SetLimits(*recoParam);
   
-  AliMUONV2DStore* status = statusMaker.MakeStatus();
-  if ( status )
+  UInt_t mask = recoParam->PadGoodnessMask();
+
+  statusMaker.Report(mask);
+  
+  vstatus = static_cast<AliMUONVStore*>(statusMaker.StatusStore()->Clone());
+  
+  const Bool_t deferredInitialization = kFALSE;
+  
+  AliMUONPadStatusMapMaker statusMapMaker(cd,mask,deferredInitialization);
+    
+  vstatusMap = static_cast<AliMUONVStore*>(statusMapMaker.StatusMap()->Clone());
+}
+
+//______________________________________________________________________________
+Int_t GetBadChannels(Int_t runNumber,
+                     Int_t& nbadped,
+                     Int_t& nbadhv,
+                     Int_t& nbadgain,
+                     Int_t& nbadocc,
+                     Int_t& nmissing,
+                     Int_t& nreco,
+                     Int_t chamber=-1)
+{
+  if (!AliCDBManager::Instance()->IsDefaultStorageSet())
   {
-    findBad(*status);
+//    AliCDBManager::Instance()->SetDefaultStorage("alien://folder=/alice/data/2011/OCDB?cacheFold=/local/cdb");
+    AliCDBManager::Instance()->SetDefaultStorage("raw://");
+  }
+  
+  AliCDBManager::Instance()->SetRun(runNumber);
+  
+  AliMpCDB::LoadDDLStore();
+  
+  AliMUONCalibrationData cd(runNumber,true);
+  
+  AliMUONPadStatusMaker statusMaker(cd);
+  
+  AliMUONRecoParam* recoParam = AliMUONCDB::LoadRecoParam();
+
+  statusMaker.SetLimits(*recoParam);
+  
+  AliMpManuIterator it;
+  Int_t detElemId, manuId;
+  
+  Int_t pedCheck = ( 
+                     AliMUONPadStatusMaker::kPedMeanZero |
+                     AliMUONPadStatusMaker::kPedMeanTooLow |
+                     AliMUONPadStatusMaker::kPedMeanTooHigh |
+                     AliMUONPadStatusMaker::kPedSigmaTooLow |
+                     AliMUONPadStatusMaker::kPedSigmaTooHigh );
+     
+  Int_t hvCheck = ( 
+                   AliMUONPadStatusMaker::kHVError |
+                   AliMUONPadStatusMaker::kHVTooLow |
+                   AliMUONPadStatusMaker::kHVTooHigh |
+                   AliMUONPadStatusMaker::kHVChannelOFF |
+                   AliMUONPadStatusMaker::kHVSwitchOFF );
+
+  
+  Int_t occCheck = (
+                    AliMUONPadStatusMaker::kManuOccupancyTooHigh 
+                   );
+                    
+  Int_t ntotal(0);
+  Int_t nbad(0);
+  nbadped=0;
+  nbadocc=0;
+  nbadgain=0;
+  nbadhv=0;
+  nmissing=0;
+  nreco=0;
+  
+  while ( it.Next(detElemId,manuId) )
+  {
+    AliMpDetElement* de = AliMpDDLStore::Instance()->GetDetElement(detElemId);
+    
+    if ( chamber >= 0 && AliMpDEManager::GetChamberId(detElemId) != chamber ) continue;
+    
+    for ( Int_t manuChannel = 0; manuChannel < AliMpConstants::ManuNofChannels(); ++manuChannel )
+    {
+      if ( de->IsConnectedChannel(manuId,manuChannel) )
+      {
+        ++ntotal;
+        
+        UInt_t status = statusMaker.PadStatus(detElemId, manuId, manuChannel);
+        
+        if (!status) continue;
+        
+        bool bad(false);
+        
+        if ( status & AliMUONPadStatusMaker::BuildStatus(pedCheck,0,0,0) ) 
+        {
+          ++nbadped;
+          bad=true;
+        }
+        
+        if ( status & AliMUONPadStatusMaker::BuildStatus(0,hvCheck,0,0) ) 
+        {
+          ++nbadhv;
+          bad=true;
+        }
+        
+        if ( status & AliMUONPadStatusMaker::BuildStatus(0,0,0,occCheck) ) 
+        {
+          ++nbadocc;
+          bad=true;
+        }
+        
+        if ( status & recoParam->PadGoodnessMask() ) 
+        {
+          ++nreco;
+        }
+        
+        if ( status & AliMUONPadStatusMaker::BuildStatus(AliMUONPadStatusMaker::kMissing,0,0,AliMUONPadStatusMaker::kMissing) )
+        {
+          bad=true;
+          ++nmissing;
+        }
+        
+        if (bad) ++nbad;
+      }
+    }
+  }
+  
+  if ( chamber<0 && ntotal!=NTOTALNUMBEROFPADS)
+  {
+    cerr << Form("ERROR ! NOT THE EXPECTED NUMBER OF CHANNELS (%d vs 1064008) FOR RUN %09d",
+                 ntotal,runNumber) << endl;
   }
   else
   {
-    cout << "ERROR. Could not get status from CDB" << endl;
-    return 0;
+    cout << Form("Chamber %d - %d channels",chamber,ntotal) << endl;
+    cout << Form("nbadped %5d nbadhv %5d nbadgain %5d nbadocc %5d nmissing %5d nreco %5d",
+                 nbadped,nbadhv,nbadgain,nbadocc,nmissing,nreco) << endl;
+  }
+  
+  AliCDBManager::Instance()->ClearCache(); 
+  return nbad;
+}
+
+//______________________________________________________________________________
+void Draw(TFile* f, const char* gname, TLegend* l, Bool_t normalized)
+{
+  if (!f) return;
+  
+  TGraph* g = static_cast<TGraph*>(f->Get(gname));
+  
+  if (!g) return;
+  
+  if ( normalized ) 
+  {
+    g = static_cast<TGraph*>(g->Clone());
+    for ( Int_t i = 0; i < g->GetN(); ++i ) 
+    {
+      Double_t y = g->GetY()[i];
+      g->SetPoint(i,g->GetX()[i],y/NTOTALNUMBEROFPADS);
+    }
   }
   
-  if ( statusOnly ) return status;
+  g->Draw("lp");
+  g->GetXaxis()->SetNdivisions(505);
+  g->GetXaxis()->SetNoExponent();
+  
+  if (l) l->AddEntry(g,gname,"LP");
+}
+
+//______________________________________________________________________________
+void DrawPeriod(double run1, double run2, double ymin, double ymax, const char* label)
+{
+  TBox* b = new TBox(run1,ymin,run2,ymax);
+  b->SetFillColor(5);
+  b->Draw();
+  TText* text = new TText((run1+run2)/2.0,ymax*0.6,label);
+  text->SetTextSize(0.05);
+  text->Draw();
+}
+
+//______________________________________________________________________________
+void DrawEvolution(const char* file, bool normalized=true)
+{
+
+  TFile* f = TFile::Open(gSystem->ExpandPathName(file));
+  
+  if (!f) return;
+  
+  TCanvas* c = new TCanvas("mch-status-evolution","mch-status-evolution");
+  
+  c->SetGridy();
+  c->SetTicky();
+  
+  c->Draw();
+  
+  TLegend* l = new TLegend(0.1,0.7,0.3,0.95,"ch evolution");
+
+  TGraph* g = static_cast<TGraph*>(f->Get("nbad"));
+  if (!g) return;
+  
+  int runmin = TMath::Nint(g->GetX()[0]);
+  int runmax = TMath::Nint(g->GetX()[g->GetN()-1]);
+  
+  cout << Form("Run range found in file %s = %d - %d",file,runmin,runmax) << endl;
+  
+  double ymax(0.4);
+  
+  TH2* h = new TH2F("hframe","hframe;Run number;Fraction of dead channels",100,runmin-200,runmax+200,100,0,ymax);
+
+  gStyle->SetOptStat(kFALSE);
+  h->Draw();
+  h->GetXaxis()->SetNoExponent();
+  h->GetXaxis()->SetNdivisions(505);
+
+  gStyle->SetOptTitle(kFALSE);
+
+  DrawPeriod(115881,117222,0,ymax,"10b"); 
+
+  DrawPeriod(119159,120824,0,ymax,"10c");
+
+  DrawPeriod(122374,126424,0,ymax,"10d");
+
+  DrawPeriod(127724,130850,0,ymax,"10e");
+
+  DrawPeriod(133005,134929,0,ymax,"10f");
+
+  DrawPeriod(135658,136376,0,ymax,"10g");
+
+  DrawPeriod(137133,139513,0,ymax,"10h");
+  
+  DrawPeriod(143856,146860,0,ymax,"11a");
+
+  DrawPeriod(148370,150702,0,ymax,"11b");
+
+  DrawPeriod(151566,154583,0,ymax,"11c");
+
+  DrawPeriod(158084,159606,0,ymax,"11d");
+
+  DrawPeriod(160677,162717,0,ymax,"11e");
+
+  DrawPeriod(162933,165744,0,ymax,"11f");
+
+  DrawPeriod(167703,170593,0,ymax,"11h");
+
+  DrawPeriod(176661,177295,0,ymax,"12a");
+
+  DrawPeriod(177384,178053,0,ymax,"12b");
+
+  Draw(f,"nbad",l,normalized);
+  Draw(f,"nbadped",l,normalized);
+  Draw(f,"nbadocc",l,normalized);
+  Draw(f,"nbadhv",l,normalized);
+  Draw(f,"nmissing",l,normalized);
+  Draw(f,"nreco",l,normalized);
+  
+  h->Draw("same");
+
+  c->RedrawAxis("g");
+  
+  l->Draw();
+}
+
+//______________________________________________________________________________
+void MUONStatusMapEvolution(const char* runlist, const char* outfile)
+{
+  // Compute the number of bad pads (because of bad ped, bad hv, bad occupancy
+  // or missing in configuration)
+  //
+  // output a root file with the different graphs.
+  //
+  // output can be then plotted using the DrawEvolution function
+  //
+  // Note that the output of different runlists can then be merged simply using
+  // the hadd program, and so then the DrawEvolution can be used over
+  // a huge period, e.g. a full year, while this method is better restricted
+  // to a period or even less (depending on your success of accessing the OCDB)
+  //
+  
+  std::vector<int> runs;
+
+  ReadIntegers(runlist,runs);
+  
+  if ( runs.empty() ) 
+  {
+    cout << "No runs to process..." << endl;
+    return;    
+  }
+  
+  int year(2012);
+  
+  if ( runs[0] <= 139699 ) year=2010;
+  
+  if ( runs[0] <= 176000 ) year=2011;
+      
+  AliCDBManager::Instance()->SetDefaultStorage(Form("alien://folder=/alice/data/%d/OCDB?cacheFold=/local/cdb",year));
+  
+  TList glist;
+  
+  glist.SetOwner(kTRUE);
+  
+  TGraph* gnbad = new TGraph(runs.size());
+  gnbad->SetName("nbad");
+  glist.Add(gnbad);
+  
+  TGraph* gnbadped = new TGraph(runs.size());
+  gnbadped->SetName("nbadped");
+  glist.Add(gnbadped);
+  
+  TGraph* gnbadocc = new TGraph(runs.size());
+  gnbadocc->SetName("nbadocc");
+  glist.Add(gnbadocc);
+  
+  TGraph* gnbadhv = new TGraph(runs.size());
+  gnbadhv->SetName("nbadhv");
+  glist.Add(gnbadhv);
+  
+  TGraph* gnmissing = new TGraph(runs.size());
+  gnmissing->SetName("nmissing");
+  glist.Add(gnmissing);
+
+  TGraph* gnreco = new TGraph(runs.size());
+  gnreco->SetName("nreco");
+  glist.Add(gnreco);
+  
+  for ( std::vector<int>::size_type i = 0; i < runs.size(); ++i ) 
+  {
+    Int_t runNumber = runs[i];
+    Int_t nbadped;
+    Int_t nbadhv;
+    Int_t nbadgain;
+    Int_t nbadocc;
+    Int_t nmissing;
+    Int_t nreco;
+    
+    Int_t nbad = GetBadChannels(runNumber,nbadped,nbadhv,nbadgain,nbadocc,nmissing,nreco);
+    
+    gnbad->SetPoint(i,runNumber,nbad);
+    gnbadped->SetPoint(i,runNumber,nbadped);
+    gnbadhv->SetPoint(i,runNumber,nbadhv);
+    gnbadocc->SetPoint(i,runNumber,nbadocc);
+    gnmissing->SetPoint(i,runNumber,nmissing);
+    gnreco->SetPoint(i,runNumber,nreco);
+  }
+  
+  TIter next(&glist);
+  TGraph* g;
+  Int_t index(0);
+
+  TFile f(outfile,"recreate");
+  Int_t  color[] = {  1 ,  2 ,  3 ,  4,  6, 8 };
+  Int_t marker[] = { 28 , 24 , 23 , 26, 30, 5 };
+
+  while ( ( g = static_cast<TGraph*>(next() ) ) )
+  {
+    g->GetXaxis()->SetNdivisions(505);
+    g->GetXaxis()->SetNoExponent();
+    g->SetMinimum(0);
+    g->GetYaxis()->SetNoExponent();
+    g->SetLineColor(color[index]);
+    g->SetMarkerStyle(marker[index]);
+    g->SetMarkerColor(color[index]);
+    g->SetMarkerSize(1.0);
+    ++index;
+    g->Write();
+  }
+    
+  f.Close();
   
-  AliMUONPadStatusMapMaker statusMapMaker;
   
-  return statusMapMaker.MakePadStatusMap(*status,mask);
 }