Utilizzo del codice GEANT4 in campo medico LNS (Mon 12-Oct Wed 14-Oct-2009) Gestione ed analisi dei dati prodotti Francesco Di Rosa
I.Overview of basic Analysis a)Generic and simple Output in Geant4 b)Analysis of ASCII files II.Overview of advanced Analysis a)Downloading the AIDA utility package b)Turning on the analysis package c)Loading the package d)Producing/Analyzing first histograms e)The packages: JAS, ROOT, PAW, OpenScientist III.Few examples of Analysis a)JAS b)ROOT c)PAW IV.Conclusion Outline
Geant4 does not attempt to provide its own data analysis tools, focusing instead on its central mission as a simulation toolkit The Geant4 user is expected to provide his own code to output results to an appropriate analysis format The Geant4 simple output: the simplest possible output format here G4Cout ASCII *. TXT (Row, Matrices, Line) *. CSV (Comma-Separated Values), *. PIPPO (What you want!!!!!!!!! Exe: mixed formats) The resulting files can be analyzed by tools such as: Excel, OpenOffice, Gnuplot, Matlab, OriginLab I. a) Generic and simple Output in Geant4
An Example of G4COUT (good for test and verify) I. a) Generic and simple Output in Geant4 Lung Interface!! G4cout " << " " << std::setw( 6) << edep << " " " << " " << std::setw( 2) << particleCharge << " " " << " " << std::setw( 2) << kineticEnergy << " " << G4endl;
std::ofstream myfile(“File_di_OUTPUT”, std::ios::app); myfile << edep << '\t' <<particleCharge<<'\t' << kineticEnergy << '\n' << G4endl; controlloTally=evtNb; An Example of ASCII File (Save As……) I. a) Generic and simple Output in Geant4
I. a) Generic and simple Output in Geant4 PDD
Excel Comes from a small company called MicroSoft Part of Microsoft Office (very common software) From the “Data” menu, select “Get External Data”…“Import Data…” Select the file using the extension (*.*) The Text Import Wizard will come up Check the “Delimited” box Hit “Next” Check the “Comma” box The bottom part of the Wizard should now show your data nicely separated into appropriate columns. Hit “Finish” You will see a dialog titled “Import Data” which will ask where to put the data. Accept the defaults: Existing worksheet: =$A$1 Hit “OK” The data should now show up in the spreadsheet (see the next slide) I. b) Analysis of ASCII files
I. b) Analysis of ASCII files Importing data in Excel
Plotting with Excel Select at least one item in the filled data area. From the “Insert” menu, select “Chart…” Select Chart type: “Histogram” Hit “Finish” You’re done I. b) Analysis of ASCII files Now that the data is in the spreadsheet, you can plot it as follows:
OpenOffice OpenOffice.org is a multiplatform and multilingual office suite and an open-source projectmultiplatformmultilingualoffice suiteopen-source Compatible with all other major office suites, the product is free to download, use, and distribute From the “File” menu, Select “Open…” In the “File type” box, select “Text CSV” Then select your file Typology ………………………………………. I. b) Analysis of ASCII files
Importing Data from a.csv File Into OpenOffice Once you select your file: The Text Import dialog will come up Check the “Comma” box The bottom part of the dialog should now show your data nicely separated into appropriate columns, then Hit “OK” I. b) Analysis of ASCII files The data should now show up in the spreadsheet (maybe)
Plotting with OpenOffice Now that the data is in the spreadsheet, you can plot it as follows: Select at least one item in the filled data area. From the “Insert” menu, select “Chart…” The defaults should be fine Hit “Next” I. b) Analysis of ASCII files Select the Icon for “Line” chart Hit “Create” You’re done!
“Gnuplot is a portable command-line driven interactive data and function plotting utility for UNIX, IBM OS/2, MS Windows, DOS, Macintosh, VMS, Atari and many other platforms.” “The software is copyrighted but freely distributed (i.e., you don't have to pay for it)” See the demo page ( I. b) Analysis of ASCII files Gnuplot
) Choose the data Folder 2) LOAD the output file 3) In the workspace appear your data I. b) Analysis of ASCII files 4. One-click with right mouse bottom on Workspace 5. Select Plot Catalog and prefer the Chart type 6. You’re done… :-/ Plotting with MatLab
I.Overview of basic Analysis a)Generic and simple Output in Geant4 b)Analysis of ASCII files II.Overview of advanced Analysis a)Downloading the AIDA utility package b)Turning on the analysis package c)Loading the package d)Producing/Analyzing first histograms e)The packages: JAS, ROOT, PAW, OpenScientist III.Few examples of Analysis a)JAS b)ROOT c)PAW IV.Conclusion Outline
While the Geant4 collaboration does not maintain any data analysis system, many data analysis tool developers have interfaced their systems to Geant4 and each of the following has an active Geant4 user community II. Overview of advanced Analysis (AIDA) ROOT is an Object Oriented Data Analysis Framework. It is heavily used in High Energy Physics. Freely available at AIDA (Analysis Interface for Data Analysis) is an interface standard used by several advanced data analysis tools. It provides a single standard way to create your data Commands to:instantiate/fill an nTuple instantiate/fill a Histogram You can switch from one AIDA-compliant tool to another without having to change how you book and fill your histograms (
AIDA Example AIDAAIDA User code (e.g. GEANT4) Analysis tool 1 Analysis tool 2 Analysis tool 3 Use same code with any AIDA-compliant analysis tool II. Overview of advanced Analysis (AIDA)
Now, you have just AIDA installed…… But….. If you DO NOT have AIDA installed, then (will take ~5-10 min): 1.Create AIDA directory under "geant4/work" directory mkdir geant4/work/AIDA 2.Download "osc_batch-v16r0-XX-i386-gcc_401.zip" into "AIDA" folder from –WindowsXX = CYGWIN –LinuxXX = Linux –MACOSXX = Darwin 3.Decompress the file 4.Go into “osc_batch/v16r2” directory cd osc_batch/v16r0 5.Setup the AIDA environment source aida-setup.csh or source aida-setup.sh 6.Done! II. a) Downloading & Setting the AIDA utility package
G4ANALYSIS_USE Set variable –setenv G4ANALYSIS_USE 1 (csh) –export G4ANALYSIS_USE=1 (bash o setup) Is it on? (linux commands) –1 (on) or 0 (off) –“echo”echo $G4ANALYSIS_USE –“env” env then look for G4ANALYSIS_USE II. b) Turning on the Analysis Package
Required files #include Define AnalysisManager.cc Create & Fill AnalysisManager.hh II. b) Loading the analysis package I
II. b) Loading the analysis package II: AnalysisManager.cc (create) From Hadrontherapy Example
…………. A01AnalysisManager::A01AnalysisManager() :analysisFactory(0), hFactory(0), tFactory(0) { // Hooking an AIDA compliant analysis system. analysisFactory = AIDA_createAnalysisFactory(); if(analysisFactory) { ITreeFactory* treeFactory = analysisFactory->createTreeFactory(); tree = treeFactory->create("A01.aida","xml",false,true,"compress=yes"); hFactory = analysisFactory->createHistogramFactory(*tree); tFactory = analysisFactory->createTupleFactory(*tree); delete treeFactory; // Will not delete the ITree. } …….. II. b) Loading the analysis package II: AnalysisManager.cc (create) From Extended Example A01
… std::string opts = "compress=no"; fTree = treeFactory->create("AnaEx01.aida","xml",false,true,opts); ….. // Create an histo factory that will create histo in the tree : AIDA::IHistogramFactory* histoFactory = fAIDA- >createHistogramFactory(*fTree); if (histoFactory) { fEAbs = histoFactory->createHistogram1D("EAbs",100,0,100); fLAbs = histoFactory->createHistogram1D("LAbs",100,0,100); … delete histoFactory; } … II. b) Loading the analysis package II: AnalysisManager.cc (create) From Extended Example AnaEx01: create histograms
… // Get a tuple factory : AIDA::ITupleFactory* tupleFactory = fAIDA->createTupleFactory(*fTree); if (tupleFactory) { // Create a tuple : fTuple = tupleFactory->create("AnaEx01","AnaEx01", "double EAbs,double LAbs,double EGap,double LGap"); delete tupleFactory; } … II. b) Loading the analysis package III: AnalysisManager.cc (create) From Extended Example AnaEx01: create ntuples
II. b) Loading the analysis package IV: AnalysisManager.cc (fill) From Hadrontherapy Example
… if (CHC) { G4int n_hit = CHC->entries(); for (G4int i=0;i<n_hit;i++) { G4double EAbs = (*CHC)[i]->GetEdepAbs(); G4double LAbs = (*CHC)[i]->GetTrakAbs(); … fEAbs->fill(EAbs); fLAbs->fill(LAbs); … fTuple->fill(0,EAbs); fTuple->fill(1,LAbs); … fTuple->addRow(); } }… II. b) Loading the analysis package IV: AnalysisManager.cc (fill) From Extened Example AnaEx01: fill histograms and ntuples
II. c) Producing/Analyzing first histograms Copy example into your work area (hadrontherapy) Go into example directory: cd hadrontherapy Compile: gmake or make Run: ~/geant4/work/bin/Linux-g++/…… A new file should appear: hadronterapy.root (.hbk,.aida) ANALYSIS Start JAS (…ROOT,AIDA,HBOOK) Open the file: File “Open File” then select XX.root Double click on “Blue Folder” Double click on histogram of interest Homework assignment
I.Overview of basic Analysis a)Generic and simple Output in Geant4 b)Analysis of ASCII files II.Overview of advanced Analysis a)Downloading the AIDA utility package b)Turning on the analysis package c)Loading the package d)Producing/Analyzing first histograms e)The packages: JAS, ROOT, PAW, OpenScientist III.Few examples of Analysis a)JAS b)ROOT c)PAW IV.Conclusion Outline
Java Analysis Studio: JAS (all files) The Root of Everything: ROOT (root files) Physics Analysis Workstation: PAW (hbook files) OpenScientist (all files) III. Few examples of advanced analysis
–Written entirely in Java (really portable) –Originally targeted at offline analysis (but also used extensively for online monitoring) –Rich, easy to use GUI Can view trees, plots, HTML pages with embedded active objects –Built-in editor/compiler for writing analysis code Based on open source JEdit editor (pure Java editor, syntax highlighting, auto indent, parenthesis matching, etc.) –Local and Client-Server Operation Access local or remote analysis objects Can send analysis code to the data and ship back only the results III a) Java Analysis Studio (JAS, Java Analysis Studio (JAS)
Start jas Open the file: File “Open File” then select OUTPUT.aida Double click on “Blue Folder” (OUTPUT.aida) Double click on histogram and then the ones of interest Double click on tuples then “Yellow Folder” III a) Java Analysis Studio (Example)
Built-in HTML viewer with embeddable “objects” (plots, buttons, etc.). Pages can display histograms. User can control layout, add, remove plots. Tree area provides access to analysis objects. Each item has popup menu and double- click action. Plot Widget, shows data in real time, optimized for fast refresh performance. III a) Java Analysis Studio (GUI)
III b) ROOT ( - ) In AnaEx01AnalysisManager.cc Verify or type the following lines std::string opts = “export=root"; fTree = treeFactory->create("AnaEx01.root",“ROOT",false,true,opts); Start root Launch browser: new TBrowser() Open the file: File “Open” then select nameFile.root Double click on “Yellow Folder” (ROOT Files) Double click on “Yellow Folder” (file.root) Double click on “Yellow Folder” (histograms) and then the ones of interest Double click on “Yellow Folder” (tuples) then right-mouse on “nameFile” and select Start Viewer Drag variables of interest into (X,Y,Z) (Ex: X=EAbs and Y=Labs) Select Draw Icon (bottom left corner) Create a cut: double click on E() then type: EGapcut (alias) and EGap<20 (expression) Drag cut into Scissor Select Draw Icon (bottom left corner) Can enable or disable the cut by double clicking on it
III b) ROOT (Hist Browser)
III b) ROOT ( TGhtml web browser plug-in) URL You can browse a root file You can execute a script
III b) ROOT (Macro Manager/Editor plug-in) Click on button to execute script
III b) ROOT (Math libraries)
III c) Physics Analysis Workstation (PAW, cernlib) PAW is no longer supported: you are on your own!!! In AnaEx01AnalysisManager.cc Verify or type the following lines std::string opts = “export=hbook"; fTree = treeFactory->create("AnaEx01.hbook",“HBOOK",false,true,opts); Should get: AnaEx01.hbook Start paw++ Open file: h/file 0 [filepath]/AnaEx01.hbook Double click on logical unit 1 (lun1) Double click on histograms or ntuple Select a variable and double lick in (X,Y,Z) (Ex: X=EAbs and Y=Labs) Click on Plot Click on Cut Editor … Left box = variable, middle box = expression, right box = value (Ex: EGap<20) Click on Plot
Conclusion The Geant4 user is expected to provide his own code to output results to an appropriate analysis format Geant4 does not attempt to provide its own data analysis tools, focusing instead on its central mission as a simulation toolkit Several analysis tools are available Real Conclusion Use Whatever Analysis Tool You Like Use as it suits your needs
MORE INFORMATIONS ABOUT AIDA
n -Tuple (The term originated as an abstraction of the sequence: single, double, triple, quadruple, quintuple, n-tuple) Let us consider the following example A Data Summary Tape contains events. Each event consists of many variables (200) for which we would like to make some distributions according to various selection criteria One possibility is to create and fill 200 histograms on an event-by-event basis while reading the DST An alternative solution is to create one Ntuple Instead of histograming the 200 variables directly, and therefore losing the exact values of the variables for each event and their correlations, the variables are first stored in an Ntuple. An Ntuple is like a table where the 200 variables mentioned above are the columns and each event is a row Let us use Example
Factory – “a building or set of buildings with facilities for manufacturing” (Webster Dictionary). O bjects in AIDA are created by Factories or internally by other objects. User should newer utilize “new”. static IAnalysisFactory create () IHistogramFactory createHistogramFactory (ITree tree) IFunctionFactory createFunctionFactory (ITree tree) ITupleFactory createTupleFactory (ITree tree) ITreeFactory createTreeFactory () IPlotterFactory createPlotterFactory () IDataPointSetFactory createDataPointSetFactory (ITree tree) IFitFactory createFitFactory () AIDA Factories – IAnalysisFactory
Creates 1D, 2D, 3D IHistograms, IClouds, IProfiles: –IHistogram1D createHistogram1D (String name, String title, int nBins, double lowerEdge, double upperEdge, String options ) –IHistogram1D createHistogram1D (String name, String title, int nBins, double[] binEdges, String options ) –Options: “type=efficiency” – create efficiency histogram. –Options: “autoconvert=true” – to enable auto conversion for Clouds. Does operations on those objects: –Copy and Arithmetic operations add/subtract/divide/multiply IHistogram1D add (String name, IHistogram1D h1, IHistogram1D h2) –Projections and Slices of 2D and 3D Objects: 2D -> 1D, 3D -> 2D IHistogram1D projectionX (String name, IHistogram2D h2D) IHistogram1D sliceX (String name, IHistogram2D h2D, int index) AIDA Factories – IHistogramFactory
IHistogram (1D-3D) Binned histogram: IHistogram1D, 2D, 3DIHistogram1D –“fill” methods (with/without weight) –Histogram info: entries, mean, rms, axis –Bin info: centre, entries, height, error –Histogram arithmetic: add, multiply, divide –Convenience methods, like coordinate-to- index conversion
ITuple ITuple - interface to the DataITuple –“get/set” methods for double, float, int, … –Information about columns: min, max, mean, rms –Navigating: start(), next(), skip(int nRows) –Project ITuple into 1D, 2D, 3D histogram –New features for AIDA 3.0: Support for complex internal structures (subfolders) Chaining of ITuples
ICloud Unbinned collection of points: ICloud1D, 2D, 3DICloud1D –Can represent scatter plot, dynamically rebinnable histogram –Can be converted to a binned histogram –Standard “get/set” methods for entries –Collection info: lower, upper, mean, rms
IFunction and Fitting Fitting: IFunction, IFitFunction –IFunction – simple interface, allows to set parameters and get function value –IFitFunction – fit function to a histogram Extends IFunction Various fit control methods: step size, bounds, etc. Allows to perform fit and get results AIDA 2.2 fitting functionality fairly limited –AIDA 3.0 (Under discussion) extended functionality
ITree –directory-like structure (Unix directory convention) Methods like: cd, ls, mkdir, etc. –AIDA analysis objects (tuples, histograms, clouds, ets.) exist within ITree directories –“save/restore” functionality, hides storage details from the user Compatible with database or file storage Can support multiple file formats Mount/Unmount functionality (like unix) allows multiple stores to be seamlessly merged AIDA XML format is defined for data interchange