ROOT logo
#if !defined(__CINT__) || defined(__MAKECINT__)
#include <TROOT.h>
#include <TFile.h>
#include <TDirectory.h>
#include <TF1.h>
#include <TH1.h>
#include <TObjArray.h>
#include <TGraphErrors.h>
#include "KMCDetector.h"
#endif

// Process the output file of testDetKMC.C macro to prepare summary 
// for the "summary class" icl (definition of acceptable track).
// See ProcessSummary comment for details.
// The names of the graphs are prefixed by `pref`.
// If outF is provided, the graphs will be stored there


TObjArray* PrepSummaryKMC(const char* sumf, int cls, const char* pref=0, const char* outF=0);
TObjArray* ProcessSummary(TObjArray* sums, int icl, const char* pref=0);

enum {kSigAD,kSigAZ,kSigAPt,kSigD,kSigZ,kSigPt,kEff,kUpd};
TF1* gs = 0;

TObjArray* PrepSummaryKMC(const char* sumf, int cls, const char* pref, const char* outF)
{
  if (!gROOT->GetClass("KMCDetector")) gROOT->LoadMacro("KMCDetector.cxx+");
  TFile* fl = TFile::Open(sumf);
  if (!fl) {printf("No file %s\n",sumf); return 0;}
  TObjArray* arrs = (TObjArray*)gDirectory->Get("trSum");
  if (!arrs) {printf("No summary in file %s\n",sumf); return 0;}
  //
  TObjArray* sums =  ProcessSummary(arrs,cls,pref);
  if (!sums) return 0;
  //
  if (outF) {
    TFile* flOut = TFile::Open(outF,"update");
    if (!flOut) {printf("Failed to open output file %s\n",outF);}
    else {
      flOut->WriteObject(sums,sums->GetName(),"kSingleKey");
      flOut->Close();
      delete flOut;
      printf("Stored array %s in %s\n",sums->GetName(), outF);
    }
  }
  //
  return sums;
}


TObjArray* ProcessSummary(TObjArray* arrs, int icl, const char* pref)
{
  // Process TObjArray (e.g. for set of pt bins) of TObjArray of KMCTrackSummary objects:
  // pick the KMCTrackSummary for "summary class" icl (definition of acceptable track) and create
  // graphs vs bin.
  // These graphs are returned in new TObjArray
  //
  TString prefs = pref;
  if (!gs) gs = new TF1("gs","gaus",-1,1);
  //
  int nb = arrs->GetEntriesFast();
  TObjArray* sums = (TObjArray*) arrs->At(0);
  int nclass = sums->GetEntriesFast();
  if (icl>=nclass) {printf("summary set has %d classes only, %d requested\n",nclass,icl);return 0;}
  //
  KMCTrackSummary* sm = (KMCTrackSummary*)sums->At(icl);
  //
  TH1* h;
  //
  h = sm->GetHMCSigDCARPhi();  // MC resolution in transverse DCA 
  TGraphErrors * grSigD = 0;
  if (h) {
    grSigD = new TGraphErrors(nb);
    grSigD->SetName(Form("%s%s",prefs.Data(),h->GetName()));
    grSigD->SetTitle(Form("%s%s",prefs.Data(),h->GetTitle()));
  }
  //
  TGraphErrors * grSigZ = 0;
  h = sm->GetHMCSigDCAZ();    // MC resolution in Z DCA
  if (h) {
    grSigZ = new TGraphErrors(nb);
    grSigZ->SetName(Form("%s%s",prefs.Data(),h->GetName()));
    grSigZ->SetTitle(Form("%s%s",prefs.Data(),h->GetTitle()));
  }
  //
  TGraphErrors * grSigAD = 0; // anaitical estimate for resolution in transverse DCA 
  {
    grSigAD = new TGraphErrors(nb);
    grSigAD->SetName(Form("%s%s",prefs.Data(),"sigmaDan"));
    grSigAD->SetTitle(Form("%s%s",prefs.Data(),"#sigmaD an"));
  }
  //
  TGraphErrors * grSigAZ = 0; // anaitical estimate for resolution in Z DCA 
  {
    grSigAZ = new TGraphErrors(nb);
    grSigAZ->SetName(Form("%s%s",prefs.Data(),"sigmaZan"));
    grSigAZ->SetTitle(Form("%s%s",prefs.Data(),"#sigmaZ an"));
  }
  //
  //
  TGraphErrors * grSigPt = 0; // MC res. in pt
  {
    grSigPt = new TGraphErrors(nb);
    grSigPt->SetName(Form("%s%s",prefs.Data(),"sigmaPt"));
    grSigPt->SetTitle(Form("%s%s",prefs.Data(),"#sigmaPt"));
  }
  //
  TGraphErrors * grSigAPt = 0; // analitycal res. in pt
  {
    grSigAPt = new TGraphErrors(nb);
    grSigAPt->SetName(Form("%s%s",prefs.Data(),"sigmaPtan"));
    grSigAPt->SetTitle(Form("%s%s",prefs.Data(),"#sigmaPt an"));
  }

  //
  TGraphErrors * grEff = 0; // MC efficiency
  {
    grEff = new TGraphErrors(nb);
    grEff->SetName(Form("%s_rate",prefs.Data()));
    grEff->SetTitle(Form("%s Rate",prefs.Data()));
  }
  //
  TGraphErrors * grUpd = 0; // number of Kalman track updates
  {
    grUpd = new TGraphErrors(nb);
    grUpd->SetName(Form("%s_updCalls",prefs.Data()));
    grUpd->SetTitle(Form("%s Updates",prefs.Data()));
  }
  //
  for (int ib=0;ib<nb;ib++) {
    sums = (TObjArray*) arrs->At(ib);
    sm = (KMCTrackSummary*)sums->At(icl);
    KMCProbe& prbRef = sm->GetRefProbe();
    KMCProbe& prbAn  = sm->GetAnProbe();
  
    double pt = prbRef.Pt();
    //
    if (grSigAD) {
      grSigAD->SetPoint(ib, pt,prbAn.GetSigmaY2()>0 ? TMath::Sqrt(prbAn.GetSigmaY2()) : 0.);
    }
    //
    if (grSigAZ) {
      grSigAZ->SetPoint(ib, pt,prbAn.GetSigmaZ2()>0 ? TMath::Sqrt(prbAn.GetSigmaZ2()) : 0.);
    }
    //
    if (grSigAPt) {
      double pts = TMath::Sqrt(prbAn.GetSigma1Pt2());
      grSigAPt->SetPoint(ib, pt,pts>0 ? pts*pt : 0.);
    }
    //
    if (grSigPt) {
      h = sm->GetHMCSigPt();
      h->Fit(gs,"0q");
      grSigPt->SetPoint(ib, pt, gs->GetParameter(2));
      grSigPt->SetPointError(ib, 0, gs->GetParError(2));
    }
    //
     if (grSigD) {
      h = sm->GetHMCSigDCARPhi();
      h->Fit(gs,"0q");
      grSigD->SetPoint(ib, pt,gs->GetParameter(2));
      grSigD->SetPointError(ib, 0,gs->GetParError(2));      
    }
    //
    if (grSigZ) {
      h = sm->GetHMCSigDCAZ();
      h->Fit(gs,"0q");
      grSigZ->SetPoint(ib, pt,gs->GetParameter(2));
      grSigZ->SetPointError(ib, 0,gs->GetParError(2));      
    }
    //
    if (grEff) {
      grEff->SetPoint(ib, pt,sm->GetEff());
      grEff->SetPointError(ib, 0,sm->GetEffErr());
    }
    //
    if (grUpd) {
      grUpd->SetPoint(ib, pt,sm->GetUpdCalls());
      grUpd->SetPointError(ib, 0, 0);
    }
  }
  //
  TObjArray* dest = new TObjArray();
  dest->AddAtAndExpand(grSigAD,kSigAD);
  dest->AddAtAndExpand(grSigAZ,kSigAZ);  
  dest->AddAtAndExpand(grSigAPt,kSigAPt);  
  dest->AddAtAndExpand(grSigD,kSigD);
  dest->AddAtAndExpand(grSigZ,kSigZ);  
  dest->AddAtAndExpand(grSigPt,kSigPt);  
  dest->AddAtAndExpand(grEff,kEff);
  dest->AddAtAndExpand(grUpd,kUpd);
  //
  if (!prefs.IsNull()) dest->SetName(pref);
  return dest;
}
 PrepSummaryKMC.C:1
 PrepSummaryKMC.C:2
 PrepSummaryKMC.C:3
 PrepSummaryKMC.C:4
 PrepSummaryKMC.C:5
 PrepSummaryKMC.C:6
 PrepSummaryKMC.C:7
 PrepSummaryKMC.C:8
 PrepSummaryKMC.C:9
 PrepSummaryKMC.C:10
 PrepSummaryKMC.C:11
 PrepSummaryKMC.C:12
 PrepSummaryKMC.C:13
 PrepSummaryKMC.C:14
 PrepSummaryKMC.C:15
 PrepSummaryKMC.C:16
 PrepSummaryKMC.C:17
 PrepSummaryKMC.C:18
 PrepSummaryKMC.C:19
 PrepSummaryKMC.C:20
 PrepSummaryKMC.C:21
 PrepSummaryKMC.C:22
 PrepSummaryKMC.C:23
 PrepSummaryKMC.C:24
 PrepSummaryKMC.C:25
 PrepSummaryKMC.C:26
 PrepSummaryKMC.C:27
 PrepSummaryKMC.C:28
 PrepSummaryKMC.C:29
 PrepSummaryKMC.C:30
 PrepSummaryKMC.C:31
 PrepSummaryKMC.C:32
 PrepSummaryKMC.C:33
 PrepSummaryKMC.C:34
 PrepSummaryKMC.C:35
 PrepSummaryKMC.C:36
 PrepSummaryKMC.C:37
 PrepSummaryKMC.C:38
 PrepSummaryKMC.C:39
 PrepSummaryKMC.C:40
 PrepSummaryKMC.C:41
 PrepSummaryKMC.C:42
 PrepSummaryKMC.C:43
 PrepSummaryKMC.C:44
 PrepSummaryKMC.C:45
 PrepSummaryKMC.C:46
 PrepSummaryKMC.C:47
 PrepSummaryKMC.C:48
 PrepSummaryKMC.C:49
 PrepSummaryKMC.C:50
 PrepSummaryKMC.C:51
 PrepSummaryKMC.C:52
 PrepSummaryKMC.C:53
 PrepSummaryKMC.C:54
 PrepSummaryKMC.C:55
 PrepSummaryKMC.C:56
 PrepSummaryKMC.C:57
 PrepSummaryKMC.C:58
 PrepSummaryKMC.C:59
 PrepSummaryKMC.C:60
 PrepSummaryKMC.C:61
 PrepSummaryKMC.C:62
 PrepSummaryKMC.C:63
 PrepSummaryKMC.C:64
 PrepSummaryKMC.C:65
 PrepSummaryKMC.C:66
 PrepSummaryKMC.C:67
 PrepSummaryKMC.C:68
 PrepSummaryKMC.C:69
 PrepSummaryKMC.C:70
 PrepSummaryKMC.C:71
 PrepSummaryKMC.C:72
 PrepSummaryKMC.C:73
 PrepSummaryKMC.C:74
 PrepSummaryKMC.C:75
 PrepSummaryKMC.C:76
 PrepSummaryKMC.C:77
 PrepSummaryKMC.C:78
 PrepSummaryKMC.C:79
 PrepSummaryKMC.C:80
 PrepSummaryKMC.C:81
 PrepSummaryKMC.C:82
 PrepSummaryKMC.C:83
 PrepSummaryKMC.C:84
 PrepSummaryKMC.C:85
 PrepSummaryKMC.C:86
 PrepSummaryKMC.C:87
 PrepSummaryKMC.C:88
 PrepSummaryKMC.C:89
 PrepSummaryKMC.C:90
 PrepSummaryKMC.C:91
 PrepSummaryKMC.C:92
 PrepSummaryKMC.C:93
 PrepSummaryKMC.C:94
 PrepSummaryKMC.C:95
 PrepSummaryKMC.C:96
 PrepSummaryKMC.C:97
 PrepSummaryKMC.C:98
 PrepSummaryKMC.C:99
 PrepSummaryKMC.C:100
 PrepSummaryKMC.C:101
 PrepSummaryKMC.C:102
 PrepSummaryKMC.C:103
 PrepSummaryKMC.C:104
 PrepSummaryKMC.C:105
 PrepSummaryKMC.C:106
 PrepSummaryKMC.C:107
 PrepSummaryKMC.C:108
 PrepSummaryKMC.C:109
 PrepSummaryKMC.C:110
 PrepSummaryKMC.C:111
 PrepSummaryKMC.C:112
 PrepSummaryKMC.C:113
 PrepSummaryKMC.C:114
 PrepSummaryKMC.C:115
 PrepSummaryKMC.C:116
 PrepSummaryKMC.C:117
 PrepSummaryKMC.C:118
 PrepSummaryKMC.C:119
 PrepSummaryKMC.C:120
 PrepSummaryKMC.C:121
 PrepSummaryKMC.C:122
 PrepSummaryKMC.C:123
 PrepSummaryKMC.C:124
 PrepSummaryKMC.C:125
 PrepSummaryKMC.C:126
 PrepSummaryKMC.C:127
 PrepSummaryKMC.C:128
 PrepSummaryKMC.C:129
 PrepSummaryKMC.C:130
 PrepSummaryKMC.C:131
 PrepSummaryKMC.C:132
 PrepSummaryKMC.C:133
 PrepSummaryKMC.C:134
 PrepSummaryKMC.C:135
 PrepSummaryKMC.C:136
 PrepSummaryKMC.C:137
 PrepSummaryKMC.C:138
 PrepSummaryKMC.C:139
 PrepSummaryKMC.C:140
 PrepSummaryKMC.C:141
 PrepSummaryKMC.C:142
 PrepSummaryKMC.C:143
 PrepSummaryKMC.C:144
 PrepSummaryKMC.C:145
 PrepSummaryKMC.C:146
 PrepSummaryKMC.C:147
 PrepSummaryKMC.C:148
 PrepSummaryKMC.C:149
 PrepSummaryKMC.C:150
 PrepSummaryKMC.C:151
 PrepSummaryKMC.C:152
 PrepSummaryKMC.C:153
 PrepSummaryKMC.C:154
 PrepSummaryKMC.C:155
 PrepSummaryKMC.C:156
 PrepSummaryKMC.C:157
 PrepSummaryKMC.C:158
 PrepSummaryKMC.C:159
 PrepSummaryKMC.C:160
 PrepSummaryKMC.C:161
 PrepSummaryKMC.C:162
 PrepSummaryKMC.C:163
 PrepSummaryKMC.C:164
 PrepSummaryKMC.C:165
 PrepSummaryKMC.C:166
 PrepSummaryKMC.C:167
 PrepSummaryKMC.C:168
 PrepSummaryKMC.C:169
 PrepSummaryKMC.C:170
 PrepSummaryKMC.C:171
 PrepSummaryKMC.C:172
 PrepSummaryKMC.C:173
 PrepSummaryKMC.C:174
 PrepSummaryKMC.C:175
 PrepSummaryKMC.C:176
 PrepSummaryKMC.C:177
 PrepSummaryKMC.C:178
 PrepSummaryKMC.C:179
 PrepSummaryKMC.C:180
 PrepSummaryKMC.C:181
 PrepSummaryKMC.C:182
 PrepSummaryKMC.C:183
 PrepSummaryKMC.C:184
 PrepSummaryKMC.C:185
 PrepSummaryKMC.C:186
 PrepSummaryKMC.C:187
 PrepSummaryKMC.C:188
 PrepSummaryKMC.C:189
 PrepSummaryKMC.C:190
 PrepSummaryKMC.C:191
 PrepSummaryKMC.C:192
 PrepSummaryKMC.C:193
 PrepSummaryKMC.C:194
 PrepSummaryKMC.C:195
 PrepSummaryKMC.C:196