3 /* Revision 1.2 2007/12/13 09:45:45 gustavo
4 /* Scaling option and more comentaries added
6 /* Revision 1.1 2007/12/07 14:13:02 gustavo
7 /* Example macros for execution and configuration of the analysis
10 //---------------------------------------------------
11 // Example macro to do analysis with the
12 // analysis classes in PWG4Gamma
13 // Can be executed with Root and AliRoot
15 // Pay attention to the options and definitions
16 // set in the lines below
18 // Author : Gustavo Conesa Balbastre (INFN-LNF)
20 //-------------------------------------------------
21 enum anaModes {mLocal, mLocalCAF,mPROOF,mGRID};
22 //mLocal: Analyze locally files in your computer
23 //mLocalCAF: Analyze locally CAF files
24 //mPROOF: Analyze CAF files with PROOF
26 //---------------------------------------------------------------------------
27 //Settings to read locally several files, only for "mLocal" mode
28 //The different values are default, they can be set with environmental
29 //variables: INDIR, PATTERN, NEVENT, respectivelly
30 char * kInDir = "/home/group/alice/schutz/analysis/PWG4/data";
31 char * kPattern = ""; // Data are in diles /data/Run0,
33 Int_t kEvent = 1; // Number of files
34 //---------------------------------------------------------------------------
35 //Collection file for grid analysis
36 char * kXML = "collection.xml";
37 //---------------------------------------------------------------------------
38 //Scale histograms from file. Change to kTRUE when xsection file exists
39 //Put name of file containing xsection
40 //Put number of events per ESD file
41 //This is an specific case for normalization of Pythia files.
42 const Bool_t kGetXSectionFromFileAndScale = kTRUE ;
43 const char * kXSFileName = "pyxsec.root";
44 const Int_t kNumberOfEventsPerFile = 100;
45 //---------------------------------------------------------------------------
47 const Bool_t kMC = kTRUE; //With real data kMC = kFALSE
49 void anaGammaAnalysis(Int_t mode=mLocal, TString configName = "ConfigKineGammaDirect")
53 //--------------------------------------------------------------------
54 // Load analysis libraries
55 // Look at the method below,
56 // change whatever you need for your analysis case
57 // ------------------------------------------------------------------
60 //-------------------------------------------------------------------------------------------------
61 //Create chain from ESD and from cross sections files, look below for options.
62 //-------------------------------------------------------------------------------------------------
63 TChain *chain = new TChain("esdTree") ;
64 TChain * chainxs = new TChain("Xsection") ;
65 CreateChain(mode, chain, chainxs);
68 AliLog::SetGlobalLogLevel(AliLog::kError);//Minimum prints on screen
70 //--------------------------------------
71 // Make the analysis manager
72 //-------------------------------------
73 AliAnalysisManager *mgr = new AliAnalysisManager("Manager", "Manager");
76 AliMCEventHandler* mcHandler = new AliMCEventHandler();
77 mgr->SetMCtruthEventHandler(mcHandler);
80 AliAODHandler* aodHandler = new AliAODHandler();
81 aodHandler->SetOutputFileName("aod.root");
82 mgr->SetOutputEventHandler(aodHandler);
84 AliESDInputHandler *esdHandler = new AliESDInputHandler();
85 mgr->SetInputEventHandler(esdHandler);
87 //mgr->SetDebugLevel(10); // For debugging
89 //-------------------------------------------------------------------------
90 //Define task, put here any other task that you want to use.
91 //-------------------------------------------------------------------------
92 AliAnalysisTaskGamma * taskgamma = new AliAnalysisTaskGamma ("Gamma");
93 taskgamma->SetConfigFileName(configName); //Default name is ConfigGammaAnalysis
94 mgr->AddTask(taskgamma);
96 // Create containers for input/output
97 AliAnalysisDataContainer *cinput1 = mgr->CreateContainer("cchain",TChain::Class(),
98 AliAnalysisManager::kInputContainer);
99 AliAnalysisDataContainer *coutput1 = mgr->CreateContainer("tree", TTree::Class(),
100 AliAnalysisManager::kOutputContainer, "default");
101 AliAnalysisDataContainer *coutput2 = mgr->CreateContainer("gammahistos", TList::Class(),
102 AliAnalysisManager::kOutputContainer, "gammahistos.root");
104 mgr->ConnectInput (taskgamma, 0, cinput1);
105 mgr->ConnectOutput (taskgamma, 0, coutput1 );
106 mgr->ConnectOutput (taskgamma, 1, coutput2 );
108 //------------------------
110 //-----------------------
111 Int_t nfiles = chainxs->GetEntries();
112 if(kGetXSectionFromFileAndScale && nfiles > 0){
113 //Get the cross section
116 GetAverageXsection(chainxs, xsection, ntrials);
118 AliAnaScale * scale = new AliAnaScale("scale") ;
119 scale->Set(xsection/ntrials/kNumberOfEventsPerFile/nfiles) ;
122 AliAnalysisDataContainer *coutput3 = mgr->CreateContainer("gammahistosscaled", TList::Class(),
123 AliAnalysisManager::kOutputContainer, "gammahistosscaled.root");
124 mgr->ConnectInput (scale, 0, coutput2);
125 mgr->ConnectOutput (scale, 0, coutput3 );
128 //-----------------------
130 //-----------------------
132 if (mode==mLocal || mode == mLocalCAF)
134 else if (mode==mPROOF)
136 else if (mode==mGRID)
141 mgr->StartAnalysis(smode.Data(),chain);
143 else cout << "Chain was not produced ! "<<endl;
147 void LoadLibraries(const anaModes mode) {
149 //--------------------------------------
150 // Load the needed libraries most of them already loaded by aliroot
151 //--------------------------------------
152 gSystem->Load("libTree.so");
153 gSystem->Load("libGeom.so");
154 gSystem->Load("libVMC.so");
155 gSystem->Load("libXMLIO.so");
157 //----------------------------------------------------------
158 // >>>>>>>>>>> Local mode <<<<<<<<<<<<<<
159 //----------------------------------------------------------
160 if (mode==mLocal || mode == mLocalCAF || mode == mGRID) {
161 //--------------------------------------------------------
162 // If you want to use already compiled libraries
163 // in the aliroot distribution
164 //--------------------------------------------------------
165 //gSystem->Load("libSTEERBase");
166 //gSystem->Load("libESD");
167 //gSystem->Load("libAOD");
168 //gSystem->Load("libANALYSIS");
169 //gSystem->Load("libANALYSISalice");
170 //gSystem->Load("libPWG4Gamma");
172 //--------------------------------------------------------
173 //If you want to use root and par files from aliroot
174 //--------------------------------------------------------
175 SetupPar("STEERBase");
178 SetupPar("ANALYSIS");
179 SetupPar("ANALYSISalice");
180 SetupPar("PWG4Gamma");
184 //---------------------------------------------------------
185 // <<<<<<<<<< PROOF mode >>>>>>>>>>>>
186 //---------------------------------------------------------
187 else if (mode==mPROOF) {
190 // Put appropriate username here
191 // TProof::Reset("proof://mgheata@lxb6046.cern.ch");
192 TProof::Open("proof://mgheata@lxb6046.cern.ch");
194 // gProof->ClearPackages();
195 // gProof->ClearPackage("ESD");
196 // gProof->ClearPackage("AOD");
197 // gProof->ClearPackage("ANALYSIS");
198 // gProof->ClearPackage("PWG4Gamma");
200 // Enable the STEERBase Package
201 gProof->UploadPackage("STEERBase.par");
202 gProof->EnablePackage("STEERBase");
203 // Enable the ESD Package
204 gProof->UploadPackage("ESD.par");
205 gProof->EnablePackage("ESD");
206 // Enable the AOD Package
207 gProof->UploadPackage("AOD.par");
208 gProof->EnablePackage("AOD");
209 // Enable the Analysis Package
210 gProof->UploadPackage("ANALYSIS.par");
211 gProof->EnablePackage("ANALYSIS");
212 // Enable gamma jet analysis
213 gProof->UploadPackage("PWG4Gamma.par");
214 gProof->EnablePackage("PWG4Gamma");
216 gProof->ShowEnabledPackages();
221 void SetupPar(char* pararchivename)
223 //Load par files, create analysis libraries
224 //For testing, if par file already decompressed and modified
225 //classes then do not decompress.
227 TString cdir(Form("%s", gSystem->WorkingDirectory() )) ;
228 TString parpar(Form("%s.par", pararchivename)) ;
229 if ( gSystem->AccessPathName(parpar.Data()) ) {
230 gSystem->ChangeDirectory(gSystem->Getenv("ALICE_ROOT")) ;
231 TString processline(Form(".! make %s", parpar.Data())) ;
232 gROOT->ProcessLine(processline.Data()) ;
233 gSystem->ChangeDirectory(cdir) ;
234 processline = Form(".! mv /tmp/%s .", parpar.Data()) ;
235 gROOT->ProcessLine(processline.Data()) ;
237 if ( gSystem->AccessPathName(pararchivename) ) {
238 TString processline = Form(".! tar xvzf %s",parpar.Data()) ;
239 gROOT->ProcessLine(processline.Data());
242 TString ocwd = gSystem->WorkingDirectory();
243 gSystem->ChangeDirectory(pararchivename);
245 // check for BUILD.sh and execute
246 if (!gSystem->AccessPathName("PROOF-INF/BUILD.sh")) {
247 printf("*******************************\n");
248 printf("*** Building PAR archive ***\n");
249 cout<<pararchivename<<endl;
250 printf("*******************************\n");
252 if (gSystem->Exec("PROOF-INF/BUILD.sh")) {
253 Error("runProcess","Cannot Build the PAR Archive! - Abort!");
257 // check for SETUP.C and execute
258 if (!gSystem->AccessPathName("PROOF-INF/SETUP.C")) {
259 printf("*******************************\n");
260 printf("*** Setup PAR archive ***\n");
261 cout<<pararchivename<<endl;
262 printf("*******************************\n");
263 gROOT->Macro("PROOF-INF/SETUP.C");
266 gSystem->ChangeDirectory(ocwd.Data());
267 printf("Current dir: %s\n", ocwd.Data());
272 void CreateChain(const anaModes mode, TChain * chain, TChain * chainxs){
273 //Fills chain with data
274 TString ocwd = gSystem->WorkingDirectory();
276 //-----------------------------------------------------------
277 //Analysis of CAF data locally and with PROOF
278 //-----------------------------------------------------------
279 if(mode ==mPROOF || mode ==mLocalCAF){
281 gROOT->LoadMacro("$ALICE_ROOT/PWG0/CreateESDChain.C");
282 // The second parameter is the number of input files in the chain
283 chain = CreateESDChain("ESD12001.txt", 5);
286 //---------------------------------------
287 //Local files analysis
288 //---------------------------------------
289 else if(mode == mLocal){
290 //If you want to add several ESD files sitting in a common directory INDIR
291 //Specify as environmental variables the directory (INDIR), the number of files
292 //to analyze (NEVENT) and the pattern name of the directories with files (PATTERN)
294 if(gSystem->Getenv("INDIR"))
295 kInDir = gSystem->Getenv("INDIR") ;
296 else cout<<"INDIR not set, use default: "<<kInDir<<endl;
298 if(gSystem->Getenv("PATTERN"))
299 kPattern = gSystem->Getenv("PATTERN") ;
300 else cout<<"PATTERN not set, use default: "<<kPattern<<endl;
302 if(gSystem->Getenv("NEVENT"))
303 kEvent = atoi(gSystem->Getenv("NEVENT")) ;
304 else cout<<"NEVENT not set, use default: "<<kEvent<<endl;
306 //Check if env variables are set and are correct
307 if ( kInDir && kEvent) {
308 printf("Get %d files from directory %s\n",kEvent,kInDir);
309 if ( ! gSystem->cd(kInDir) ) {//check if ESDs directory exist
310 printf("%s does not exist\n", kInDir) ;
313 cout<<"INDIR : "<<kInDir<<endl;
314 cout<<"NEVENT : "<<kEvent<<endl;
315 cout<<"PATTERN: " <<kPattern<<endl;
317 //Loop on ESD files, add them to chain
323 for (event = 0 ; event < kEvent ; event++) {
324 sprintf(file, "%s/%s%d/AliESDs.root", kInDir,kPattern,event) ;
325 sprintf(filexs, "%s/%s%d/%s", kInDir,kPattern,event,kXSFileName) ;
327 //Check if file exists and add it, if not skip it
328 if ( fESD = TFile::Open(file)) {
329 if ( fESD->Get("esdTree") ) {
330 printf("++++ Adding %s\n", file) ;
331 chain->AddFile(file);
332 chainxs->Add(filexs) ;
336 printf("---- Skipping %s\n", file) ;
340 printf("number of entries # %lld, skipped %d\n", chain->GetEntries(), skipped*100) ;
343 TString input = "AliESDs.root" ;
344 cout<<">>>>>> No list added, take a single file <<<<<<<<< "<<input<<endl;
345 chain->AddFile(input);
348 }// local files analysis
350 //------------------------------
352 //-----------------------------
353 else if(mode == mGRID){
354 //Get colection file. It is specified by the environmental
357 if(gSystem->Getenv("XML") )
358 kXML = gSystem->Getenv("XML");
360 sprintf(kXML, "collection.xml") ;
362 if (!TFile::Open(kXML)) {
363 printf("No collection file with name -- %s -- was found\n",kXML);
366 else cout<<"XML file "<<kXML<<endl;
368 //Load necessary libraries and connect to the GRID
369 gSystem->Load("libNetx.so") ;
370 gSystem->Load("libRAliEn.so");
371 TGrid::Connect("alien://") ;
373 //Feed Grid with collection file
374 //TGridCollection * collection = (TGridCollection*)gROOT->ProcessLine(Form("TAlienCollection::Open(\"%s\", 0)", kXML));
375 TGridCollection * collection = (TGridCollection*) TAlienCollection::Open(kXML);
377 AliError(Form("%s not found", kXML)) ;
380 TGridResult* result = collection->GetGridResult("",0 ,0);
382 // Makes the ESD chain
383 printf("*** Getting the Chain ***\n");
384 for (Int_t index = 0; index < result->GetEntries(); index++) {
385 TString alienURL = result->GetKey(index, "turl") ;
386 cout << "================== " << alienURL << endl ;
387 chain->Add(alienURL) ;
388 alienURL.ReplaceAll("AliESDs.root",kXSFileName);
389 chainxs->Add(alienURL) ;
393 gSystem->ChangeDirectory(ocwd.Data());
396 //________________________________________________
397 void GetAverageXsection(TTree * tree, Double_t & xs, Float_t & ntr)
399 // Read the PYTHIA statistics from the file pyxsec.root created by
400 // the function WriteXsection():
401 // integrated cross section (xsection) and
402 // the number of Pyevent() calls (ntrials)
403 // and calculate the weight per one event xsection/ntrials
404 // The spectrum calculated by a user should be
405 // multiplied by this weight, something like this:
406 // TH1F *userSpectrum ... // book and fill the spectrum
407 // userSpectrum->Scale(weight)
409 // Yuri Kharlov 19 June 2007
410 // Gustavo Conesa 15 April 2008
411 Double_t xsection = 0;
416 Int_t nfiles = tree->GetEntries() ;
417 if (tree && nfiles > 0) {
418 tree->SetBranchAddress("xsection",&xsection);
419 tree->SetBranchAddress("ntrials",&ntrials);
420 for(Int_t i = 0; i < nfiles; i++){
424 cout << "xsection " <<xsection<<" ntrials "<<ntrials<<endl;
429 cout << "-----------------------------------------------------------------"<<endl;
430 cout << "Average of "<< nfiles<<" files: xsection " <<xs<<" ntrials "<<ntr<<endl;
431 cout << "-----------------------------------------------------------------"<<endl;
433 else cout << " >>>> Empty tree !!!! <<<<< "<<endl;