#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "../scripts/palettes.C" using namespace std; class PlotLooper{ public: struct Plot{ TH1* histogram; TH1* original_histogram; double value; Plot():histogram(NULL),original_histogram(NULL),value(1){} }; PlotLooper( const char* file_names, const char* histograms, const char* normalisation, const char* value_format, double max_field, double min_field, const char* method ) :fNormalisationVal(1),fNormalisation_hist(NULL),fInputFileNames(file_names), fHistogramNames(histograms), fNormalisations(normalisation), fValueFormat(value_format),fPlotMethod(method),fValueFunction(NULL){ fMaxField=1.01*(max_field-0.99*min_field); fMinField=0.99*min_field; } bool Initiailise(){ SetUserPalette(1,255,0.5); // Sort file names fInputFileNames_tkns=fInputFileNames.Tokenize(" "); fNHists=fInputFileNames_tkns->GetEntries(); fDifferentInputFiles=(fNHists!=1); fUseFullNameForValue=fDifferentInputFiles; // Sort histogram names fHistogramNames_tkns=TokenizeAndCheck(fHistogramNames,fNHists,fDifferentHists, "histogram name"); if (!fHistogramNames_tkns) return false; if(fNHists==1){ fNHists=fHistogramNames_tkns->GetEntries(); fUseFullNameForValue=fInputFileNames.Contains(":"); } // sort out the normalisation fNormalisations_tkns=TokenizeAndCheck(fNormalisations,fNHists,fNormPerPlot, "normalisation"); if (!fNormalisations_tkns) return false; if(!fNormPerPlot) { if(fNormalisations.Contains("%")){ fNormalisations_tkns=BuildNormalisationTokens(fNHists,fNormalisations); cout<<"Normalisation tokens:"<Print(); fNormPerPlot=true; }else{ SortOneNormalisationToken(0); } } cout<Add(new TObjString(Form(format,value))); } return array; } Plot GetPlot(int i_plot){ Plot return_plot=GetPlotWithoutNorm(i_plot); if(!return_plot.histogram) return return_plot; double norm_value=1; TH1* norm_hist=NULL; GetNormalisation(i_plot,norm_value,norm_hist); ApplyNormalisation(return_plot.histogram,norm_value,norm_hist); return return_plot; } Plot GetPlotWithoutNorm(int i_plot){ cout<<"Processing plot: "<At(i_plot))->GetString(); input_name+=":"; }else{ input_name=fInputFileNames; } cout<<"input_name: "<At(i_plot))->GetName(); }else{ input_name+=fHistogramNames; } cout<<"input_name: "<GetName():"NULL")<GetEntries() !=2){ cout<<"Error: Wrong number of tokens for input name which should be of the form: : (and contain a single colon, ':' )"<At(0))->GetName(); const TString hist_name=((TObjString*)tokens->At(1))->GetName(); if(file=="<>"){ file=fCurrentFile->GetName(); }else{ fCurrentFile=NULL; fCurrentFile=TFile::Open(file.Data()); if(!fCurrentFile) { cout<<"Error: Cannot open file: "<Get(hist_name); if(!profile) { cout<<"Error: Cannot find histogram ("<(hist); if(hist2d){ cout<<"2D histogram, make profile."<ProjectionX(); }else if (fPlotMethod=="projectionY"){ hist=hist2d->ProjectionY(); }else if (fPlotMethod=="profileY"){ hist=hist2d->ProfileY(); }else if (fPlotMethod=="profileX"){ hist=hist2d->ProfileX(); }else{ cout<<"Bad 2D --> 1D method: "<Scale(1/norm_value); if(norm_hist) hist->Divide(norm_hist); cout<<"applied normalisation"<At(i_plot))->GetString(); TObjArray* float_and_hist=token.Tokenize("*"); if(float_and_hist->GetEntries()>=2){ for(int i=0; iGetEntries();++i_plot){ SortOneNormalisationToken(i,float_and_hist); } return; } // Is it only a number? if(token.IsFloat()){ fNormalisationVal*=token.Atof(); return; } // Treat as a histogram TH1* hist=GetHistogramFromFile(token); if(!fNormalisation_hist){ fNormalisation_hist=(TH1*)hist->Clone(Form("norm_%d",i_plot)); }else{ fNormalisation_hist->Multiply(hist); } } double ExtractValue(TString input_name){ TString string_for_value; if( fUseFullNameForValue ){ if( fDifferentHists ){ string_for_value=input_name; }else{ string_for_value=input_name.Tokenize(":")->At(0)->GetName(); } } else if( fDifferentHists) { string_for_value=input_name.Tokenize(":")->At(1)->GetName(); } cout<<"Extracting field value from: "<Eval(value); } return value; } void SetPlotColour(double value,TH1* hist){ hist->SetTitle(Form("%g",value)); // Set the profile's line and marker colour double colorIndex=(value-fMinField)/fMaxField*gStyle->GetNumberOfColors(); int colour=gStyle->GetColorPalette(colorIndex); hist->SetLineColor(colour); hist->SetLineStyle(0); hist->SetMarkerColor(colour); cout<<"Colours: "<GetEntries()==1){ many_values=false; }else if(tokens->GetEntries()==0){ cout<<"Error: No "<GetEntries()!=n_expected_tokens){ cout<<"Error: Number of "<Rebin(rebin); cout<<"Adding profile: "<GetName()<<", "<GetTitle()<AddEntry(hist,hist->GetTitle(),"l"); } cout<<"Loaded all plots"<GetXaxis()->SetRangeUser(xmin,xmax); if(y_high!=y_low){ if(y_high < y_low) { double tmp=y_low; y_high=y_low; y_low=tmp; } stack.GetHistogram()->GetYaxis()->SetRangeUser(y_low,y_high); } legend->SetFillColor(kWhite); legend->SetNColumns(2); legend->SetMargin(0.75); //legend->SetHeader("Momentum At Target (MeV/c)"); legend->Draw(); c1->SetGridy(); c1->SetGridx(); c1->Print(Form("%s.png" ,outFileName)); c1->Print(Form("%s.svg" ,outFileName)); c1->Print(Form("%s.root",outFileName)); }